fennec
Loading...
Searching...
No Matches
type_registry.h
Go to the documentation of this file.
1// =====================================================================================================================
2// fennec, a free and open source game engine
3// Copyright © 2025 Medusa Slockbower
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <https://www.gnu.org/licenses/>.
17// =====================================================================================================================
18
30
31#ifndef FENNEC_RTTI_TYPE_REGISTRY_H
32#define FENNEC_RTTI_TYPE_REGISTRY_H
33
35
37
38#include <fennec/rtti/type.h>
39
40namespace fennec
41{
42
43template<typename BaseT, typename...ArgsT>
44class type_registry {
45public:
46 using ctor_t = BaseT* (*)(ArgsT...);
47
48 struct entry {
49 size_t priority;
50 type type;
51 ctor_t ctor;
52
53 entry()
54 : priority(0)
55 , type(nullptr)
56 , ctor(nullptr) {
57 }
58
59 entry(const entry& e)
60 : priority(e.priority)
61 , type(e.type)
62 , ctor(e.ctor) {
63 }
64
65 entry(entry&& e) noexcept
66 : priority(e.priority)
67 , type(e.type)
68 , ctor(e.ctor) {
69 }
70
71 ~entry() {
72 }
73
74 entry& operator=(const entry& e) {
75 priority = e.priority;
76 type = e.type;
77 ctor = e.ctor;
78 return *this;
79 }
80
81 entry& operator=(entry&&) noexcept = default;
82
83 entry(size_t p, fennec::type type, ctor_t ctor)
84 : priority(p), type(type), ctor(ctor) {
85 }
86 };
87
88 struct compare {
89 bool operator()(const entry& a, const entry& b) const {
90 return a.priority > b.priority and a.ctor < b.ctor;
91 }
92 };
93
94 using entrylist_t = priority_queue<entry, compare>;
95
96 template<typename DerivedT> requires(is_base_of_v<BaseT, DerivedT>)
97 static void register_type(size_t priority = 0) {
98 _global_list().emplace(
99 priority,
100 type::get<DerivedT>(),
101 _constructor_helper<DerivedT>
102 );
103 }
104
105 static const entrylist_t& get_type_list() {
106 return _global_list();
107 }
108
109private:
110 static entrylist_t& _global_list() {
111 static entrylist_t list;
112 return list;
113 }
114
115 template<typename T>
116 static BaseT* _constructor_helper(ArgsT...args) {
117 return new T(args...);
118 }
119};
120
121}
122
123#endif // FENNEC_RTTI_TYPE_REGISTRY_H
constexpr genType e()
Definition constants.h:635
Type Traits