1 #ifndef MSP_GAME_OWNED_H_
2 #define MSP_GAME_OWNED_H_
14 class Owned: public Handle<T>
19 template<typename... Args>
20 Owned(Handle<Entity>, Args &&...);
22 template<typename... Args>
23 Owned(Entity &parent, Args &&... args): Owned(Handle<Entity>::from_object(&parent), std::forward<Args>(args)...) { }
25 Owned(Owned &&other): Handle<T>(other) { other.ptr = nullptr; }
26 Owned &operator=(Owned &&other);
27 ~Owned() { destroy(); }
31 static Stage &get_stage(O &);
38 template<typename... Args>
39 Owned<T>::Owned(Handle<Entity> parent, Args &&... args)
42 throw std::invalid_argument("Owned::Owned");
44 using DependentEntity = std::conditional_t<sizeof(T), Entity, Entity>;
45 Handle<DependentEntity> dparent = parent;
47 Pool<T> &pool = get_stage(*dparent).get_pools().template get_pool<T>();
48 this->ptr = pool.create(parent, std::forward<Args>(args)...);
49 if constexpr(std::is_base_of_v<Component, T>)
50 dparent->add_component(*this);
52 dparent->add_child(*this);
56 Owned<T> &Owned<T>::operator=(Owned &&other)
60 this->ptr = other.ptr;
68 Stage &Owned<T>::get_stage(O &obj)
70 if constexpr(std::is_base_of_v<Component, O>)
71 return get_stage(*obj.get_entity());
73 return obj.get_stage();
77 void Owned<T>::destroy()
83 Pool<T> &pool = get_stage(*obj).get_pools().template get_pool<T>();
85 if constexpr(std::is_base_of_v<Component, T>)
86 obj->get_entity()->remove_component(*this);
87 else if(auto parent = obj->get_parent().get())
88 parent->remove_child(*this);
90 pool.destroy(this->ptr);
93 } // namespace Msp::Game