]> git.tdb.fi Git - libs/game.git/blobdiff - source/game/owned.h
Add move constructor and assignment from Owned of derived type
[libs/game.git] / source / game / owned.h
index 620c6aab54e2114a7e3c718256fb794a92593d47..a30f040d58269019cd3634159100e7d2f8a976c3 100644 (file)
@@ -2,6 +2,7 @@
 #define MSP_GAME_OWNED_H_
 
 #include <stdexcept>
+#include "accessguard.h"
 #include "events.h"
 #include "handle.h"
 #include "stage.h"
@@ -21,13 +22,26 @@ public:
        Owned(Handle<P>, Args &&...);
 
        template<typename P, typename... Args>
-       Owned(P &parent, Args &&... args): Owned(Handle<P>::from_object(&parent), std::forward<Args>(args)...) { }
+       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 &);
 
@@ -39,33 +53,41 @@ template<typename T>
 template<typename P, typename... Args>
 Owned<T>::Owned(Handle<P> parent, Args &&... args)
 {
+#ifdef DEBUG
+       AccessGuard::get_instance().check<AccessGuard::Create>();
+#endif
+
        if(!parent)
                throw std::invalid_argument("Owned::Owned");
 
        Stage &stage = get_stage(*parent);
-       Pool<T> &pool = stage.get_pools().template get_pool<T>();
+       Pool<T> &pool = stage.get_pools().get_pool<T>();
+       bool first_created = !pool.get_capacity();
        this->ptr = pool.create(parent, std::forward<Args>(args)...);
        if constexpr(std::is_base_of_v<Component, T>)
        {
+               if(first_created)
+                       stage.get_reflector().get_or_create_class<T>().template set_polymorphic_base<Component>(**this);
                parent->add_component(*this);
                stage.get_event_source().emit<Events::ComponentCreated>(*this);
        }
        else
        {
+               if(first_created)
+                       stage.get_reflector().get_or_create_class<T>().template set_polymorphic_base<Entity>(**this);
                parent->add_child(*this);
                stage.get_event_source().emit<Events::EntityCreated>(*this);
        }
 }
 
 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>
@@ -85,6 +107,10 @@ void Owned<T>::destroy()
        if(!obj)
                return;
 
+#ifdef DEBUG
+       AccessGuard::get_instance().check<AccessGuard::Destroy>();
+#endif
+
        Stage &stage = get_stage(*obj);
 
        if constexpr(std::is_base_of_v<Component, T>)
@@ -98,7 +124,7 @@ void Owned<T>::destroy()
                parent->remove_child(*this);
        }
 
-       Pool<T> &pool = stage.get_pools().template get_pool<T>();
+       Pool<T> &pool = stage.get_pools().get_pool<T>();
        pool.destroy(this->ptr);
 }