]> git.tdb.fi Git - libs/game.git/blob - source/game/handle.h
2e544c7238e75ca7d5418c9f5c7a05c65e61081e
[libs/game.git] / source / game / handle.h
1 #ifndef MSP_GAME_HANDLE_H_
2 #define MSP_GAME_HANDLE_H_
3
4 #include <stdexcept>
5 #include "pool.h"
6
7 namespace Msp::Game {
8
9 class invalid_handle: public std::logic_error
10 {
11 public:
12         invalid_handle(const std::type_info &);
13 };
14
15 template<typename T>
16 class Handle
17 {
18         template<typename U>
19         friend class Handle;
20
21 protected:
22         T *ptr = nullptr;
23
24 public:
25         Handle() = default;
26         Handle(nullptr_t) { }
27
28         template<typename U>
29                 requires std::is_base_of_v<T, U>
30         Handle(const Handle<U> &other): ptr(other.ptr) { }
31
32         static Handle from_object(T *o) { Handle h; h.ptr = o; return h; }
33
34         T *get() const { return ptr; }
35         T &operator*() const { return *ptr; }
36         T *operator->() const { return ptr; }
37         explicit operator bool() const { return ptr; }
38
39         bool operator==(const Handle &other) const = default;
40 };
41
42 } // namespace Msp::Game
43
44 #endif