Flecs v3.2
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
enum.hpp
Go to the documentation of this file.
1
9#include <string.h>
10
11#define FLECS_ENUM_MAX(T) _::to_constant<T, 128>::value
12#define FLECS_ENUM_MAX_COUNT (FLECS_ENUM_MAX(int) + 1)
13
14#ifndef FLECS_CPP_ENUM_REFLECTION_SUPPORT
15#if !defined(__clang__) && defined(__GNUC__)
16#if __GNUC__ > 7 || (__GNUC__ == 7 && __GNUC_MINOR__ >= 5)
17#define FLECS_CPP_ENUM_REFLECTION_SUPPORT 1
18#else
19#define FLECS_CPP_ENUM_REFLECTION_SUPPORT 0
20#endif
21#else
22#define FLECS_CPP_ENUM_REFLECTION_SUPPORT 1
23#endif
24#endif
25
26namespace flecs {
27
29namespace _ {
30template <typename E, int Value>
32#if defined(__clang__) && __clang_major__ >= 16
33 // https://reviews.llvm.org/D130058, https://reviews.llvm.org/D131307
34 static constexpr E value = __builtin_bit_cast(E, Value);
35#else
36 static constexpr E value = static_cast<E>(Value);
37#endif
38};
39
40template <typename E, int Value>
42}
43
45template <typename E>
46struct enum_data;
47
48template <typename E>
49static enum_data<E> enum_type(flecs::world_t *world);
50
51template <typename E>
52struct enum_last {
53 static constexpr E value = FLECS_ENUM_MAX(E);
54};
55
56/* Utility macro to override enum_last trait */
57#define FLECS_ENUM_LAST(T, Last)\
58 namespace flecs {\
59 template<>\
60 struct enum_last<T> {\
61 static constexpr T value = Last;\
62 };\
63 }
64
65namespace _ {
66
67#if INTPTR_MAX == INT64_MAX
68 #ifdef ECS_TARGET_MSVC
69 #define ECS_SIZE_T_STR "unsigned __int64"
70 #elif defined(__clang__)
71 #define ECS_SIZE_T_STR "size_t"
72 #else
73 #ifdef ECS_TARGET_WINDOWS
74 #define ECS_SIZE_T_STR "constexpr size_t; size_t = long long unsigned int"
75 #else
76 #define ECS_SIZE_T_STR "constexpr size_t; size_t = long unsigned int"
77 #endif
78 #endif
79#else
80 #ifdef ECS_TARGET_MSVC
81 #define ECS_SIZE_T_STR "unsigned __int32"
82 #elif defined(__clang__)
83 #define ECS_SIZE_T_STR "size_t"
84 #else
85 #ifdef ECS_TARGET_WINDOWS
86 #define ECS_SIZE_T_STR "constexpr size_t; size_t = unsigned int"
87 #else
88 #define ECS_SIZE_T_STR "constexpr size_t; size_t = unsigned int"
89 #endif
90 #endif
91#endif
92
93template <typename E>
94constexpr size_t enum_type_len() {
95 return ECS_FUNC_TYPE_LEN(, enum_type_len, ECS_FUNC_NAME)
96 - (sizeof(ECS_SIZE_T_STR) - 1u);
97}
98
103#if defined(ECS_TARGET_CLANG)
104#if ECS_CLANG_VERSION < 13
105template <typename E, E C>
106constexpr bool enum_constant_is_valid() {
107 return !(
108 (ECS_FUNC_NAME[ECS_FUNC_NAME_FRONT(bool, enum_constant_is_valid) +
109 enum_type_len<E>() + 6 /* ', C = ' */] >= '0') &&
110 (ECS_FUNC_NAME[ECS_FUNC_NAME_FRONT(bool, enum_constant_is_valid) +
111 enum_type_len<E>() + 6 /* ', C = ' */] <= '9'));
112}
113#else
114template <typename E, E C>
115constexpr bool enum_constant_is_valid() {
116 return (ECS_FUNC_NAME[ECS_FUNC_NAME_FRONT(bool, enum_constant_is_valid) +
117 enum_type_len<E>() + 6 /* ', E C = ' */] != '(');
118}
119#endif
120#elif defined(ECS_TARGET_GNU)
121template <typename E, E C>
122constexpr bool enum_constant_is_valid() {
123 return (ECS_FUNC_NAME[ECS_FUNC_NAME_FRONT(constepxr bool, enum_constant_is_valid) +
124 enum_type_len<E>() + 8 /* ', E C = ' */] != '(');
125}
126#else
127/* Use different trick on MSVC, since it uses hexadecimal representation for
128 * invalid enum constants. We can leverage that msvc inserts a C-style cast
129 * into the name, and the location of its first character ('(') is known. */
130template <typename E, E C>
131constexpr bool enum_constant_is_valid() {
132 return ECS_FUNC_NAME[ECS_FUNC_NAME_FRONT(bool, enum_constant_is_valid) +
133 enum_type_len<E>() + 1] != '(';
134}
135#endif
136
137template <typename E, E C>
139 static constexpr bool value = enum_constant_is_valid<E, C>();
140};
141
143template <typename E, E C>
144static const char* enum_constant_to_name() {
145 static const size_t len = ECS_FUNC_TYPE_LEN(const char*, enum_constant_to_name, ECS_FUNC_NAME);
146 static char result[len + 1] = {};
147 return ecs_cpp_get_constant_name(
148 result, ECS_FUNC_NAME, string::length(ECS_FUNC_NAME),
149 ECS_FUNC_NAME_BACK);
150}
151
154 flecs::entity_t id;
155 int next;
156};
157
160 flecs::entity_t id;
161 int min;
162 int max;
163 enum_constant_data constants[FLECS_ENUM_MAX_COUNT];
164};
165
167template <typename E>
168struct enum_type {
169 static enum_data_impl data;
170
171 static enum_type<E>& get() {
172 static _::enum_type<E> instance;
173 return instance;
174 }
175
176 flecs::entity_t entity(E value) const {
177 return data.constants[static_cast<int>(value)].id;
178 }
179
180 void init(flecs::world_t *world, flecs::entity_t id) {
181#if !FLECS_CPP_ENUM_REFLECTION_SUPPORT
182 ecs_abort(ECS_UNSUPPORTED, "enum reflection requires gcc 7.5 or higher")
183#endif
184
185 ecs_log_push();
186 ecs_cpp_enum_init(world, id);
187 data.id = id;
188 data.min = FLECS_ENUM_MAX(int);
189 init< enum_last<E>::value >(world);
190 ecs_log_pop();
191 }
192
193private:
194 template <E Value>
195 static constexpr int to_int() {
196 return static_cast<int>(Value);
197 }
198
199 template <int Value>
200 static constexpr E from_int() {
202 }
203
204 template <E Value>
205 static constexpr int is_not_0() {
206 return static_cast<int>(Value != from_int<0>());
207 }
208
209 template <E Value, flecs::if_not_t< enum_constant_is_valid<E, Value>() > = 0>
210 static void init_constant(flecs::world_t*) { }
211
212 template <E Value, flecs::if_t< enum_constant_is_valid<E, Value>() > = 0>
213 static void init_constant(flecs::world_t *world) {
214 int v = to_int<Value>();
215 const char *name = enum_constant_to_name<E, Value>();
216 data.constants[v].next = data.min;
217 data.min = v;
218 if (!data.max) {
219 data.max = v;
220 }
221
222 data.constants[v].id = ecs_cpp_enum_constant_register(
223 world, data.id, data.constants[v].id, name, v);
224 }
225
226 template <E Value = FLECS_ENUM_MAX(E) >
227 static void init(flecs::world_t *world) {
228 init_constant<Value>(world);
229 if (is_not_0<Value>()) {
230 init<from_int<to_int<Value>() - is_not_0<Value>()>()>(world);
231 }
232 }
233};
234
235template <typename E>
237
238template <typename E, if_t< is_enum<E>::value > = 0>
239inline static void init_enum(flecs::world_t *world, flecs::entity_t id) {
240 _::enum_type<E>::get().init(world, id);
241}
242
243template <typename E, if_not_t< is_enum<E>::value > = 0>
244inline static void init_enum(flecs::world_t*, flecs::entity_t) { }
245
246} // namespace _
247
249template <typename E>
250struct enum_data {
251 enum_data(flecs::world_t *world, _::enum_data_impl& impl)
252 : world_(world)
253 , impl_(impl) { }
254
255 bool is_valid(int value) {
256 return impl_.constants[value].id != 0;
257 }
258
259 int first() const {
260 return impl_.min;
261 }
262
263 int last() const {
264 return impl_.max;
265 }
266
267 int next(int cur) const {
268 return impl_.constants[cur].next;
269 }
270
271 flecs::entity entity() const;
272 flecs::entity entity(int value) const;
273 flecs::entity entity(E value) const;
274
275 flecs::world_t *world_;
276 _::enum_data_impl& impl_;
277};
278
280template <typename E>
281enum_data<E> enum_type(flecs::world_t *world) {
282 _::cpp_type<E>::id(world); // Ensure enum is registered
283 auto& ref = _::enum_type<E>::get();
284 return enum_data<E>(world, ref.data);
285}
286
287} // namespace flecs
#define ecs_abort(error_code,...)
Abort.
Definition: log.h:343
constexpr bool enum_constant_is_valid()
Test if value is valid for enumeration.
Definition: enum.hpp:131
Enumeration constant data.
Definition: enum.hpp:153
Enumeration type data.
Definition: enum.hpp:159
Class that scans an enum for constants, extracts names & creates entities.
Definition: enum.hpp:168
Entity.
Definition: entity.hpp:30
Convenience type with enum reflection data.
Definition: enum.hpp:250
Class that wraps around a flecs::id_t.
Definition: decl.hpp:27
Component reference.
Definition: ref.hpp:23
The world.
Definition: world.hpp:132