]> git.tdb.fi Git - libs/game.git/blobdiff - source/game/owned.h
Change Owned's constructor to take a pointer to the parent
[libs/game.git] / source / game / owned.h
index 7159c7d9622a06fd83301b7565dc1d55c0beedcc..76a75521a47a0d04b1d02d0531e065144f026796 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,11 +22,7 @@ public:
        Owned(Handle<P>, Args &&...);
 
        template<typename P, typename... Args>
-       Owned(Owned<P> &p, Args &&... a): Owned(static_cast<Handle<P> &>(p), std::forward<Args>(a)...) { }
-
-       template<typename P, typename... Args>
-               requires(!std::is_const_v<P>)
-       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);
@@ -43,19 +40,28 @@ 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().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);
        }
@@ -89,6 +95,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>)