Flecs v4.0
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#include <limits>
11
12// 126, so that FLECS_ENUM_MAX_COUNT is 127 which is the largest value
13// representable by an int8_t.
14#define FLECS_ENUM_MAX(T) _::to_constant<T, 126>::value
15#define FLECS_ENUM_MAX_COUNT (FLECS_ENUM_MAX(int) + 1)
16
17// Flag to turn off enum reflection
18#ifdef FLECS_CPP_NO_ENUM_REFLECTION
19#define FLECS_CPP_ENUM_REFLECTION_SUPPORT 0
20#endif
21
22// Test if we're using a compiler that supports the required features
23#ifndef FLECS_CPP_ENUM_REFLECTION_SUPPORT
24#if !defined(__clang__) && defined(__GNUC__)
25#if __GNUC__ > 7 || (__GNUC__ == 7 && __GNUC_MINOR__ >= 5)
26#define FLECS_CPP_ENUM_REFLECTION_SUPPORT 1
27#else
28#define FLECS_CPP_ENUM_REFLECTION_SUPPORT 0
29#endif
30#else
31#define FLECS_CPP_ENUM_REFLECTION_SUPPORT 1
32#endif
33#endif
34
35#if defined(__clang__) && __clang_major__ >= 16
36// https://reviews.llvm.org/D130058, https://reviews.llvm.org/D131307
37#define flecs_enum_cast(T, v) __builtin_bit_cast(T, v)
38#elif defined(__GNUC__) && __GNUC__ > 10
39#define flecs_enum_cast(T, v) __builtin_bit_cast(T, v)
40#else
41#define flecs_enum_cast(T, v) static_cast<T>(v)
42#endif
43
44namespace flecs {
45
47namespace _ {
48template <typename E, underlying_type_t<E> Value>
50 static constexpr E value = flecs_enum_cast(E, Value);
51};
52
53template <typename E, underlying_type_t<E> Value>
55}
56
58template <typename E>
59struct enum_data;
60
61template <typename E>
62static enum_data<E> enum_type(flecs::world_t *world);
63
64template <typename E>
65struct enum_last {
66 static constexpr E value = FLECS_ENUM_MAX(E);
67};
68
69/* Utility macro to override enum_last trait */
70#define FLECS_ENUM_LAST(T, Last)\
71 namespace flecs {\
72 template<>\
73 struct enum_last<T> {\
74 static constexpr T value = Last;\
75 };\
76 }
77
78namespace _ {
79
80#if INTPTR_MAX == INT64_MAX
81 #ifdef ECS_TARGET_MSVC
82 #if _MSC_VER >= 1929
83 #define ECS_SIZE_T_STR "unsigned __int64"
84 #else
85 #define ECS_SIZE_T_STR "unsigned int"
86 #endif
87 #elif defined(__clang__)
88 #define ECS_SIZE_T_STR "size_t"
89 #else
90 #ifdef ECS_TARGET_WINDOWS
91 #define ECS_SIZE_T_STR "constexpr size_t; size_t = long long unsigned int"
92 #else
93 #define ECS_SIZE_T_STR "constexpr size_t; size_t = long unsigned int"
94 #endif
95 #endif
96#else
97 #ifdef ECS_TARGET_MSVC
98 #if _MSC_VER >= 1929
99 #define ECS_SIZE_T_STR "unsigned __int32"
100 #else
101 #define ECS_SIZE_T_STR "unsigned int"
102 #endif
103 #elif defined(__clang__)
104 #define ECS_SIZE_T_STR "size_t"
105 #else
106 #ifdef ECS_TARGET_WINDOWS
107 #define ECS_SIZE_T_STR "constexpr size_t; size_t = unsigned int"
108 #else
109 #define ECS_SIZE_T_STR "constexpr size_t; size_t = unsigned int"
110 #endif
111 #endif
112#endif
113
114template <typename E>
115constexpr size_t enum_type_len() {
116 return ECS_FUNC_TYPE_LEN(, enum_type_len, ECS_FUNC_NAME)
117 - (sizeof(ECS_SIZE_T_STR) - 1u);
118}
119
124#if defined(ECS_TARGET_CLANG)
125#if ECS_CLANG_VERSION < 13
126template <typename E, E C>
127constexpr bool enum_constant_is_valid() {
128 return !((
129 (ECS_FUNC_NAME[ECS_FUNC_NAME_FRONT(bool, enum_constant_is_valid) +
130 enum_type_len<E>() + 6 /* ', C = ' */] >= '0') &&
131 (ECS_FUNC_NAME[ECS_FUNC_NAME_FRONT(bool, enum_constant_is_valid) +
132 enum_type_len<E>() + 6 /* ', C = ' */] <= '9')) ||
133 (ECS_FUNC_NAME[ECS_FUNC_NAME_FRONT(bool, enum_constant_is_valid) +
134 enum_type_len<E>() + 6 /* ', C = ' */] == '-'));
135}
136#else
137template <typename E, E C>
138constexpr bool enum_constant_is_valid() {
139 return (ECS_FUNC_NAME[ECS_FUNC_NAME_FRONT(bool, enum_constant_is_valid) +
140 enum_type_len<E>() + 6 /* ', E C = ' */] != '(');
141}
142#endif
143#elif defined(ECS_TARGET_GNU)
144template <typename E, E C>
145constexpr bool enum_constant_is_valid() {
146 return (ECS_FUNC_NAME[ECS_FUNC_NAME_FRONT(constexpr bool, enum_constant_is_valid) +
147 enum_type_len<E>() + 8 /* ', E C = ' */] != '(');
148}
149#else
150/* Use different trick on MSVC, since it uses hexadecimal representation for
151 * invalid enum constants. We can leverage that msvc inserts a C-style cast
152 * into the name, and the location of its first character ('(') is known. */
153template <typename E, E C>
154constexpr bool enum_constant_is_valid() {
155 return ECS_FUNC_NAME[ECS_FUNC_NAME_FRONT(bool, enum_constant_is_valid) +
156 enum_type_len<E>() + 1] != '(';
157}
158#endif
159
160/* Without this wrapper __builtin_bit_cast doesn't work */
161template <typename E, underlying_type_t<E> C>
162constexpr bool enum_constant_is_valid_wrap() {
163 return enum_constant_is_valid<E, flecs_enum_cast(E, C)>();
164}
165
166template <typename E, E C>
168 static constexpr bool value = enum_constant_is_valid<E, C>();
169};
170
172template <typename E, E C>
173static const char* enum_constant_to_name() {
174 static const size_t len = ECS_FUNC_TYPE_LEN(const char*, enum_constant_to_name, ECS_FUNC_NAME);
175 static char result[len + 1] = {};
176 return ecs_cpp_get_constant_name(
177 result, ECS_FUNC_NAME, string::length(ECS_FUNC_NAME),
178 ECS_FUNC_NAME_BACK);
179}
180
182template<typename T>
184 int32_t index; // Global index used to obtain world local entity id
185 T offset;
186};
187
198template <typename E, typename Handler>
200 using U = underlying_type_t<E>;
201
216 template <U Low, U High, typename... Args>
217 static constexpr U each_enum_range(U last_value, Args... args) {
218 return High - Low <= 1
219 ? High == Low
220 ? Handler::template handle_constant<Low>(last_value, args...)
221 : Handler::template handle_constant<High>(Handler::template handle_constant<Low>(last_value, args...), args...)
222 : each_enum_range<(Low + High) / 2 + 1, High>(
223 each_enum_range<Low, (Low + High) / 2>(last_value, args...),
224 args...
225 );
226 }
227
242 template <U Low, U High, typename... Args>
243 static constexpr U each_mask_range(U last_value, Args... args) {
244 // If Low shares any bits with Current Flag, or if High is less than/equal to Low (and High isn't negative because max-flag signed)
245 return (Low & High) || (High <= Low && High != high_bit)
246 ? last_value
247 : Handler::template handle_constant<High>(
248 each_mask_range<Low, ((High >> 1) & ~high_bit)>(last_value, args...),
249 args...
250 );
251 }
252
265 template <U Value = static_cast<U>(FLECS_ENUM_MAX(E)), typename... Args>
266 static constexpr U each_enum(Args... args) {
267 return each_mask_range<Value, high_bit>(each_enum_range<0, Value>(0, args...), args...);
268 }
269 /* to avoid warnings with bit manipulation, calculate the high bit with an
270 unsigned type of the same size: */
271 using UU = typename std::make_unsigned<U>::type;
272 static const U high_bit = static_cast<U>(static_cast<UU>(1) << (sizeof(UU) * 8 - 1));
273};
274
276template<typename E>
278private:
279 using U = underlying_type_t<E>;
280
284 struct reflection_count {
285 template <U Value, flecs::if_not_t< enum_constant_is_valid_wrap<E, Value>() > = 0>
286 static constexpr U handle_constant(U last_value) {
287 return last_value;
288 }
289
290 template <U Value, flecs::if_t< enum_constant_is_valid_wrap<E, Value>() > = 0>
291 static constexpr U handle_constant(U last_value) {
292 return 1 + last_value;
293 }
294 };
295
296public:
297 int min;
298 int max;
299 bool has_contiguous;
300 // If enum constants start not-sparse, contiguous_until will be the index of the first sparse value, or end of the constants array
301 U contiguous_until;
302 // Compile-time generated count of enum constants.
303 static constexpr unsigned int constants_size = enum_reflection<E, reflection_count>::template each_enum< static_cast<U>(enum_last<E>::value) >();
304 // Constants array is sized to the number of found-constants, or 1 (to avoid 0-sized array)
305 enum_constant_data<U> constants[constants_size? constants_size: 1];
306};
307
309template <typename E>
310struct enum_type {
311private:
312 using U = underlying_type_t<E>;
313
320 struct reflection_init {
321 template <U Value, flecs::if_not_t< enum_constant_is_valid_wrap<E, Value>() > = 0>
322 static U handle_constant(U last_value, flecs::world_t*) {
323 // Search for constant failed. Pass last valid value through.
324 return last_value;
325 }
326
327 template <U Value, flecs::if_t< enum_constant_is_valid_wrap<E, Value>() > = 0>
328 static U handle_constant(U last_value, flecs::world_t *world) {
329 // Constant is valid, so fill reflection data.
330 auto v = Value;
331 const char *name = enum_constant_to_name<E, flecs_enum_cast(E, Value)>();
332
333 ++enum_type<E>::data.max; // Increment cursor as we build constants array.
334
335 // If the enum was previously contiguous, and continues to be through the current value...
336 if (enum_type<E>::data.has_contiguous && static_cast<U>(enum_type<E>::data.max) == v && enum_type<E>::data.contiguous_until == v) {
337 ++enum_type<E>::data.contiguous_until;
338 }
339 // else, if the enum was never contiguous and hasn't been set as not contiguous...
340 else if (!enum_type<E>::data.contiguous_until && enum_type<E>::data.has_contiguous) {
341 enum_type<E>::data.has_contiguous = false;
342 }
343
344 ecs_assert(!(last_value > 0 && v < std::numeric_limits<U>::min() + last_value), ECS_UNSUPPORTED,
345 "Signed integer enums causes integer overflow when recording offset from high positive to"
346 " low negative. Consider using unsigned integers as underlying type.");
347 enum_type<E>::data.constants[enum_type<E>::data.max].offset = v - last_value;
348 if (!enum_type<E>::data.constants[enum_type<E>::data.max].index) {
349 enum_type<E>::data.constants[enum_type<E>::data.max].index =
350 flecs_component_ids_index_get();
351 }
352
353 flecs::entity_t constant = ecs_cpp_enum_constant_register(
354 world, type<E>::id(world), 0, name, &v, type<U>::id(world), sizeof(U));
355 flecs_component_ids_set(world,
356 enum_type<E>::data.constants[enum_type<E>::data.max].index,
357 constant);
358
359 return v;
360 }
361 };
362public:
363
364 static enum_data_impl<E> data;
365
366 static enum_type<E>& get() {
367 static _::enum_type<E> instance;
368 return instance;
369 }
370
371 flecs::entity_t entity(E value) const {
372 int index = index_by_value(value);
373 if (index >= 0) {
374 return data.constants[index].id;
375 }
376 return 0;
377 }
378
379 void init(flecs::world_t *world, flecs::entity_t id) {
380#if !FLECS_CPP_ENUM_REFLECTION_SUPPORT
381 ecs_abort(ECS_UNSUPPORTED, "enum reflection requires gcc 7.5 or higher")
382#endif
383 // Initialize/reset reflection data values to default state.
384 data.min = 0;
385 data.max = -1;
386 data.has_contiguous = true;
387 data.contiguous_until = 0;
388
389 ecs_log_push();
390 ecs_cpp_enum_init(world, id, type<U>::id(world));
391 // data.id = id;
392
393 // Generate reflection data
395 ecs_log_pop();
396 }
397};
398
399template <typename E>
401
402template <typename E, if_t< is_enum<E>::value > = 0>
403inline static void init_enum(flecs::world_t *world, flecs::entity_t id) {
404 _::enum_type<E>::get().init(world, id);
405}
406
407template <typename E, if_not_t< is_enum<E>::value > = 0>
408inline static void init_enum(flecs::world_t*, flecs::entity_t) { }
409
410} // namespace _
411
413template <typename E>
414struct enum_data {
415 using U = underlying_type_t<E>;
416
417 enum_data(flecs::world_t *world, _::enum_data_impl<E>& impl)
418 : world_(world)
419 , impl_(impl) { }
420
428 bool is_valid(U value) {
429 int index = index_by_value(value);
430 if (index < 0) {
431 return false;
432 }
433 return impl_.constants[index].index != 0;
434 }
435
443 bool is_valid(E value) {
444 return is_valid(static_cast<U>(value));
445 }
446
453 int index_by_value(U value) const {
454 if (impl_.max < 0) {
455 return -1;
456 }
457 // Check if value is in contiguous lookup section
458 if (impl_.has_contiguous && value < impl_.contiguous_until && value >= 0) {
459 return static_cast<int>(value);
460 }
461 U accumulator = impl_.contiguous_until? impl_.contiguous_until - 1: 0;
462 for (int i = static_cast<int>(impl_.contiguous_until); i <= impl_.max; ++i) {
463 accumulator += impl_.constants[i].offset;
464 if (accumulator == value) {
465 return i;
466 }
467 }
468 return -1;
469 }
470
477 int index_by_value(E value) const {
478 return index_by_value(static_cast<U>(value));
479 }
480
481 int first() const {
482 return impl_.min;
483 }
484
485 int last() const {
486 return impl_.max;
487 }
488
489 int next(int cur) const {
490 return cur + 1;
491 }
492
493 flecs::entity entity() const;
494 flecs::entity entity(U value) const;
495 flecs::entity entity(E value) const;
496
497 flecs::world_t *world_;
498 _::enum_data_impl<E>& impl_;
499};
500
502template <typename E>
503enum_data<E> enum_type(flecs::world_t *world) {
504 _::type<E>::id(world); // Ensure enum is registered
505 auto& ref = _::enum_type<E>::get();
506 return enum_data<E>(world, ref.data);
507}
508
509} // namespace flecs
component< T > & constant(const char *name, T value)
Add constant.
Definition component.inl:31
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:368
#define ecs_abort(error_code,...)
Abort.
Definition log.h:359
constexpr bool enum_constant_is_valid()
Test if value is valid for enumeration.
Definition enum.hpp:154
Enumeration constant data.
Definition enum.hpp:183
Enumeration type data.
Definition enum.hpp:277
Provides utilities for enum reflection.
Definition enum.hpp:199
static constexpr U each_mask_range(U last_value, Args... args)
Iterates over the mask range (Low, High] of enum values between Low and High.
Definition enum.hpp:243
static constexpr U each_enum_range(U last_value, Args... args)
Iterates over the range [Low, High] of enum values between Low and High.
Definition enum.hpp:217
static constexpr U each_enum(Args... args)
Handles enum iteration for gathering reflection data.
Definition enum.hpp:266
Class that scans an enum for constants, extracts names & creates entities.
Definition enum.hpp:310
Entity.
Definition entity.hpp:30
Convenience type with enum reflection data.
Definition enum.hpp:414
int index_by_value(E value) const
Finds the index into the constants array for an enum value, if one exists.
Definition enum.hpp:477
bool is_valid(U value)
Checks if a given integral value is a valid enum value.
Definition enum.hpp:428
int index_by_value(U value) const
Finds the index into the constants array for a value, if one exists.
Definition enum.hpp:453
bool is_valid(E value)
Checks if a given enum value is valid.
Definition enum.hpp:443
Component reference.
Definition ref.hpp:85
The world.
Definition world.hpp:137