]> git.tdb.fi Git - libs/game.git/blobdiff - source/game/handle.h
Make it possible to retrieve components and systems of a particular type
[libs/game.git] / source / game / handle.h
index d384f35c3c1fae6f29b1871a4c955079078d3e75..b08bcf8e4f903028894702e8085df47174ce640f 100644 (file)
@@ -23,13 +23,14 @@ protected:
 
 public:
        Handle() = default;
-
-       static Handle from_object(T *o) { Handle h; h.ptr = o; return h; }
+       Handle(nullptr_t) { }
 
        template<typename U>
                requires std::is_base_of_v<T, U>
        Handle(const Handle<U> &other): ptr(other.ptr) { }
 
+       static Handle from_object(T *o) { Handle h; h.ptr = o; return h; }
+
        T *get() const { return ptr; }
        T &operator*() const { return *ptr; }
        T *operator->() const { return ptr; }
@@ -38,6 +39,13 @@ public:
        bool operator==(const Handle &other) const = default;
 };
 
+template<typename T, typename U>
+       requires std::is_base_of_v<U, T>
+Handle<T> dynamic_handle_cast(Handle<U> h)
+{
+       return Handle<T>::from_object(dynamic_cast<T *>(h.get()));
+}
+
 } // namespace Msp::Game
 
 #endif