1 #ifndef MSP_GAME_OWNED_H_
2 #define MSP_GAME_OWNED_H_
14 class Owned: public Handle<T>
19 template<typename P, typename... Args>
20 Owned(Handle<P>, Args &&...);
22 template<typename P, typename... Args>
23 Owned(P &parent, Args &&... args): Owned(Handle<P>::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 P, typename... Args>
39 Owned<T>::Owned(Handle<P> parent, Args &&... args)
42 throw std::invalid_argument("Owned::Owned");
44 Pool<T> &pool = get_stage(*parent).get_pools().template get_pool<T>();
45 this->ptr = pool.create(parent, std::forward<Args>(args)...);
46 if constexpr(std::is_base_of_v<Component, T>)
47 parent->add_component(*this);
49 parent->add_child(*this);
53 Owned<T> &Owned<T>::operator=(Owned &&other)
57 this->ptr = other.ptr;
65 Stage &Owned<T>::get_stage(O &obj)
67 if constexpr(std::is_base_of_v<Component, O>)
68 return get_stage(*obj.get_entity());
70 return obj.get_stage();
74 void Owned<T>::destroy()
80 Pool<T> &pool = get_stage(*obj).get_pools().template get_pool<T>();
82 if constexpr(std::is_base_of_v<Component, T>)
83 obj->get_entity()->remove_component(*this);
84 else if(auto parent = obj->get_parent().get())
85 parent->remove_child(*this);
87 pool.destroy(this->ptr);
90 } // namespace Msp::Game