]> git.tdb.fi Git - libs/game.git/commitdiff
Add move constructor and assignment from Owned of derived type
authorMikko Rasa <tdb@tdb.fi>
Sun, 4 Dec 2022 21:32:05 +0000 (23:32 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 4 Dec 2022 21:32:05 +0000 (23:32 +0200)
source/game/owned.h

index 76a75521a47a0d04b1d02d0531e065144f026796..a30f040d58269019cd3634159100e7d2f8a976c3 100644 (file)
@@ -25,10 +25,23 @@ public:
        Owned(P *parent, Args &&... args): Owned(Handle<P>::from_object(parent), std::forward<Args>(args)...) { }
 
        Owned(Owned &&other): Handle<T>(other) { other.ptr = nullptr; }
-       Owned &operator=(Owned &&other);
+
+       template<typename U>
+               requires std::is_base_of_v<T, U>
+       Owned(Owned<U> &&other): Handle<T>(other) { other.ptr = nullptr; }
+
+       Owned &operator=(Owned &&other) { assign(std::move(other)); return *this; }
+
+       template<typename U>
+               requires std::is_base_of_v<T, U>
+       Owned &operator=(Owned<U> &&other) { assign(std::move(other)); return *this; }
+
        ~Owned() { destroy(); }
 
 private:
+       template<typename U>
+       void assign(Owned<U> &&);
+
        template<typename O>
        static Stage &get_stage(O &);
 
@@ -68,14 +81,13 @@ Owned<T>::Owned(Handle<P> parent, Args &&... args)
 }
 
 template<typename T>
-Owned<T> &Owned<T>::operator=(Owned &&other)
+template<typename U>
+void Owned<T>::assign(Owned<U> &&other)
 {
        destroy();
 
        this->ptr = other.ptr;
        other.ptr = nullptr;
-
-       return *this;
 }
 
 template<typename T>