]> git.tdb.fi Git - libs/game.git/blob - source/game/handle.h
Implement a basic ECS
[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
27         static Handle from_object(T *o) { Handle h; h.ptr = o; return h; }
28
29         template<typename U>
30                 requires std::is_base_of_v<T, U>
31         Handle(const Handle<U> &other): ptr(other.ptr) { }
32
33         T *get() const { return ptr; }
34         T &operator*() const { return *ptr; }
35         T *operator->() const { return ptr; }
36         explicit operator bool() const { return ptr; }
37
38         bool operator==(const Handle &other) const = default;
39 };
40
41 } // namespace Msp::Game
42
43 #endif