]> git.tdb.fi Git - libs/game.git/blobdiff - source/game/owned.h
Enforce no creation or destruction of objects during tick
[libs/game.git] / source / game / owned.h
index 620c6aab54e2114a7e3c718256fb794a92593d47..fc2acab6ecb02ab0d4079630dae5f5fe5b8fc939 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,6 +22,10 @@ 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(Owned &&other): Handle<T>(other) { other.ptr = nullptr; }
@@ -39,11 +44,15 @@ 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>();
        this->ptr = pool.create(parent, std::forward<Args>(args)...);
        if constexpr(std::is_base_of_v<Component, T>)
        {
@@ -85,6 +94,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 +111,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);
 }