1 #ifndef MSP_GAME_HANDLE_H_
2 #define MSP_GAME_HANDLE_H_
10 class invalid_handle: public std::logic_error
13 invalid_handle(const std::type_info &);
30 requires std::is_base_of_v<T, U>
31 Handle(const Handle<U> &other): ptr(other.ptr) { }
33 static Handle from_object(T *o) { Handle h; h.ptr = o; return h; }
35 T *get() const { return ptr; }
36 T &operator*() const { return *ptr; }
37 T *operator->() const { return ptr; }
38 explicit operator bool() const { return ptr; }
40 bool operator==(const Handle &other) const = default;
41 bool operator<(const Handle &other) const { return std::less<T *const>()(ptr, other.ptr); }
44 template<typename T, typename U>
45 requires std::is_base_of_v<U, T>
46 Handle<T> dynamic_handle_cast(Handle<U> h)
48 return Handle<T>::from_object(dynamic_cast<T *>(h.get()));
51 } // namespace Msp::Game