]> git.tdb.fi Git - libs/game.git/commitdiff
Make the parent handle in Owned templated
authorMikko Rasa <tdb@tdb.fi>
Wed, 19 Oct 2022 08:07:44 +0000 (11:07 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 19 Oct 2022 08:07:44 +0000 (11:07 +0300)
It may be useful for some components designed to be used with certain
entity types.

source/game/owned.h

index 19daa7efe12afbbffeb3676d635088daca4889ff..e20ff775b61ed557201f4af40bb9244912d8452e 100644 (file)
@@ -16,11 +16,11 @@ class Owned: public Handle<T>
 public:
        Owned() = default;
 
-       template<typename... Args>
-       Owned(Handle<Entity>, Args &&...);
+       template<typename P, typename... Args>
+       Owned(Handle<P>, Args &&...);
 
-       template<typename... Args>
-       Owned(Entity &parent, Args &&... args): Owned(Handle<Entity>::from_object(&parent), std::forward<Args>(args)...) { }
+       template<typename P, typename... 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);
@@ -35,21 +35,18 @@ private:
 
 
 template<typename T>
-template<typename... Args>
-Owned<T>::Owned(Handle<Entity> parent, Args &&... args)
+template<typename P, typename... Args>
+Owned<T>::Owned(Handle<P> parent, Args &&... args)
 {
        if(!parent)
                throw std::invalid_argument("Owned::Owned");
 
-       using DependentEntity = std::conditional_t<sizeof(T), Entity, Entity>;
-       Handle<DependentEntity> dparent = parent;
-
-       Pool<T> &pool = get_stage(*dparent).get_pools().template get_pool<T>();
+       Pool<T> &pool = get_stage(*parent).get_pools().template get_pool<T>();
        this->ptr = pool.create(parent, std::forward<Args>(args)...);
        if constexpr(std::is_base_of_v<Component, T>)
-               dparent->add_component(*this);
+               parent->add_component(*this);
        else
-               dparent->add_child(*this);
+               parent->add_child(*this);
 }
 
 template<typename T>