Flecs v4.1
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 <limits>
10
11// 126, so that FLECS_ENUM_MAX_COUNT is 127 which is the largest value
12// representable by an int8_t.
13#define FLECS_ENUM_MAX(T) _::to_constant<T, 126>::value
14#define FLECS_ENUM_MAX_COUNT (FLECS_ENUM_MAX(int) + 1)
15
16// Flag to turn off enum reflection
17#ifdef FLECS_CPP_NO_ENUM_REFLECTION
18#define FLECS_CPP_ENUM_REFLECTION_SUPPORT 0
19#endif
20
21// Test if we're using a compiler that supports the required features
22#ifndef FLECS_CPP_ENUM_REFLECTION_SUPPORT
23#if !defined(__clang__) && defined(__GNUC__)
24#if __GNUC__ > 7 || (__GNUC__ == 7 && __GNUC_MINOR__ >= 5)
25#define FLECS_CPP_ENUM_REFLECTION_SUPPORT 1
26#else
27#define FLECS_CPP_ENUM_REFLECTION_SUPPORT 0
28#endif
29#else
30#define FLECS_CPP_ENUM_REFLECTION_SUPPORT 1
31#endif
32#endif
33
34#if defined(__clang__) && __clang_major__ >= 16
35// https://reviews.llvm.org/D130058, https://reviews.llvm.org/D131307
36#define flecs_enum_cast(T, v) __builtin_bit_cast(T, v)
37#elif defined(__GNUC__) && __GNUC__ > 10
38#define flecs_enum_cast(T, v) __builtin_bit_cast(T, v)
39#else
40#define flecs_enum_cast(T, v) static_cast<T>(v)
41#endif
42
43namespace flecs {
44
46namespace _ {
47template <typename E, underlying_type_t<E> Value>
49 static constexpr E value = flecs_enum_cast(E, Value);
50};
51
52template <typename E, underlying_type_t<E> Value>
54}
55
57template <typename E>
58struct enum_data;
59
60template <typename E>
61static enum_data<E> enum_type(flecs::world_t *world);
62
63template <typename E>
64struct enum_last {
65 static constexpr E value = FLECS_ENUM_MAX(E);
66};
67
68/* Utility macro to override enum_last trait */
69#define FLECS_ENUM_LAST(T, Last)\
70 namespace flecs {\
71 template<>\
72 struct enum_last<T> {\
73 static constexpr T value = Last;\
74 };\
75 }
76
77namespace _ {
78
79#if INTPTR_MAX == INT64_MAX
80 #ifdef ECS_TARGET_MSVC
81 #if _MSC_VER >= 1929
82 #define ECS_SIZE_T_STR "unsigned __int64"
83 #else
84 #define ECS_SIZE_T_STR "unsigned int"
85 #endif
86 #elif defined(__clang__)
87 #define ECS_SIZE_T_STR "size_t"
88 #else
89 #ifdef ECS_TARGET_WINDOWS
90 #define ECS_SIZE_T_STR "constexpr size_t; size_t = long long unsigned int"
91 #else
92 #define ECS_SIZE_T_STR "constexpr size_t; size_t = long unsigned int"
93 #endif
94 #endif
95#else
96 #ifdef ECS_TARGET_MSVC
97 #if _MSC_VER >= 1929
98 #define ECS_SIZE_T_STR "unsigned __int32"
99 #else
100 #define ECS_SIZE_T_STR "unsigned int"
101 #endif
102 #elif defined(__clang__)
103 #define ECS_SIZE_T_STR "size_t"
104 #else
105 #ifdef ECS_TARGET_WINDOWS
106 #define ECS_SIZE_T_STR "constexpr size_t; size_t = unsigned int"
107 #else
108 #define ECS_SIZE_T_STR "constexpr size_t; size_t = unsigned int"
109 #endif
110 #endif
111#endif
112
113template <typename E>
114constexpr size_t enum_type_len() {
115 return ECS_FUNC_TYPE_LEN(, enum_type_len, ECS_FUNC_NAME)
116 - (sizeof(ECS_SIZE_T_STR) - 1u);
117}
118
123#if defined(ECS_TARGET_CLANG)
124#if ECS_CLANG_VERSION < 13
125template <typename E, E C>
126constexpr bool enum_constant_is_valid() {
127 return !((
128 (ECS_FUNC_NAME[ECS_FUNC_NAME_FRONT(bool, enum_constant_is_valid) +
129 enum_type_len<E>() + 6 /* ', C = ' */] >= '0') &&
130 (ECS_FUNC_NAME[ECS_FUNC_NAME_FRONT(bool, enum_constant_is_valid) +
131 enum_type_len<E>() + 6 /* ', C = ' */] <= '9')) ||
132 (ECS_FUNC_NAME[ECS_FUNC_NAME_FRONT(bool, enum_constant_is_valid) +
133 enum_type_len<E>() + 6 /* ', C = ' */] == '-'));
134}
135#else
136template <typename E, E C>
137constexpr bool enum_constant_is_valid() {
138 return (ECS_FUNC_NAME[ECS_FUNC_NAME_FRONT(bool, enum_constant_is_valid) +
139 enum_type_len<E>() + 6 /* ', E C = ' */] != '(');
140}
141#endif
142#elif defined(ECS_TARGET_GNU)
143template <typename E, E C>
144constexpr bool enum_constant_is_valid() {
145 return (ECS_FUNC_NAME[ECS_FUNC_NAME_FRONT(constexpr bool, enum_constant_is_valid) +
146 enum_type_len<E>() + 8 /* ', E C = ' */] != '(');
147}
148#else
149/* Use different trick on MSVC, since it uses hexadecimal representation for
150 * invalid enum constants. We can leverage that msvc inserts a C-style cast
151 * into the name, and the location of its first character ('(') is known. */
152template <typename E, E C>
153constexpr bool enum_constant_is_valid() {
154 return ECS_FUNC_NAME[ECS_FUNC_NAME_FRONT(bool, enum_constant_is_valid) +
155 enum_type_len<E>() + 1] != '(';
156}
157#endif
158
159/* Without this wrapper __builtin_bit_cast doesn't work */
160template <typename E, underlying_type_t<E> C>
161constexpr bool enum_constant_is_valid_wrap() {
162 return enum_constant_is_valid<E, flecs_enum_cast(E, C)>();
163}
164
165template <typename E, E C>
167 static constexpr bool value = enum_constant_is_valid<E, C>();
168};
169
171template <typename E, E C>
172static const char* enum_constant_to_name() {
173 static const size_t len = ECS_FUNC_TYPE_LEN(
174 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
191template <typename E, typename Handler>
193 using U = underlying_type_t<E>;
194
211 template <U Low, U High, typename... Args>
212 static constexpr U each_enum_range(U last_value, Args&... args) {
213 return High - Low <= 1
214 ? High == Low
215 ? Handler::template handle_constant<Low>(last_value, args...)
216 : Handler::template handle_constant<High>(
217 Handler::template handle_constant<Low>(last_value, args...),
218 args...)
219 : each_enum_range<(Low + High) / 2 + 1, High>(
220 each_enum_range<Low, (Low + High) / 2>(last_value, args...),
221 args...
222 );
223 }
224
241 template <U Low, U High, typename... Args>
242 static constexpr U each_mask_range(U last_value, Args&... args) {
243 // If Low shares any bits with Current Flag, or if High is less
244 // 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>(
268 each_enum_range<0, Value>(0, args...), args...);
269 }
270 /* to avoid warnings with bit manipulation, calculate the high bit with an
271 unsigned type of the same size: */
272 using UU = typename std::make_unsigned<U>::type;
273 static const U high_bit =
274 static_cast<U>(static_cast<UU>(1) << (sizeof(UU) * 8 - 1));
275};
276
278template<typename T>
280 int32_t index; // Global index used to obtain world local entity id
281 T value;
282 T offset;
283 const char *name;
284};
285
287template <typename E>
288struct enum_type {
289private:
290 using This = enum_type<E>;
291 using U = underlying_type_t<E>;
292
296 struct reflection_count {
297 template <U Value>
298 static constexpr U handle_constant(U last_value) {
299 if constexpr (enum_constant_is_valid_wrap<E, Value>()) {
300 return 1 + last_value;
301 } else {
302 return last_value;
303 }
304 }
305 };
306
314 struct reflection_init {
315 template <U Value>
316 static U handle_constant(U last_value, This& me) {
317 if constexpr (enum_constant_is_valid_wrap<E, Value>()) {
318 // Constant is valid, so fill reflection data.
319 auto v = Value;
320 const char *name = enum_constant_to_name<E, flecs_enum_cast(E, Value)>();
321
322 ++me.max; // Increment cursor as we build constants array.
323
324 // If the enum was previously contiguous, and continues to be
325 // through the current value...
326 if (me.has_contiguous && static_cast<U>(me.max) == v && me.contiguous_until == v) {
327 ++me.contiguous_until;
328 }
329
330 // else, if the enum was never contiguous and hasn't been set as not
331 // contiguous...
332 else if (!me.contiguous_until && me.has_contiguous) {
333 me.has_contiguous = false;
334 }
335
336 ecs_assert(!(last_value > 0 &&
337 v < std::numeric_limits<U>::min() + last_value),
338 ECS_UNSUPPORTED,
339 "Signed integer enums causes integer overflow when recording "
340 "offset from high positive to low negative. Consider using "
341 "unsigned integers as underlying type.");
342
343 me.constants[me.max].value = v;
344 me.constants[me.max].offset = v - last_value;
345 me.constants[me.max].name = name;
346 if (!me.constants[me.max].index) {
347 me.constants[me.max].index =
348 flecs_component_ids_index_get();
349 }
350
351 return v;
352 } else {
353 // Search for constant failed. Pass last valid value through.
354 return last_value;
355 }
356 }
357 };
358public:
359
360 enum_type() {
361 // Initialize/reset reflection data values to default state.
362 min = 0;
363 max = -1;
364 has_contiguous = true;
365 contiguous_until = 0;
366
367#if FLECS_CPP_ENUM_REFLECTION_SUPPORT
369 template each_enum< static_cast<U>(enum_last<E>::value) >(*this);
370#endif
371 }
372
373 static enum_type<E>& get() {
374 static _::enum_type<E> instance;
375 return instance;
376 }
377
378 flecs::entity_t entity(E value) const {
379 int index = index_by_value(value);
380 if (index >= 0) {
381 return constants[index].id;
382 }
383 return 0;
384 }
385
386 void register_for_world(flecs::world_t *world, flecs::entity_t id) {
387#if !FLECS_CPP_ENUM_REFLECTION_SUPPORT
388 ecs_abort(ECS_UNSUPPORTED, "enum reflection requires gcc 7.5 or higher")
389#endif
390
391 ecs_log_push();
392 ecs_cpp_enum_init(world, id, type<U>::id(world));
393
394 for (U v = 0; v < static_cast<U>(max + 1); v ++) {
395 if (constants[v].index) {
396 flecs::entity_t constant = ecs_cpp_enum_constant_register(world,
397 type<E>::id(world), 0, constants[v].name, &constants[v].value,
398 type<U>::id(world), sizeof(U));
399
400 flecs_component_ids_set(world, constants[v].index, constant);
401 }
402 }
403
404 ecs_log_pop();
405 }
406
407 int min;
408 int max;
409
410 // If enum constants start not-sparse, contiguous_until will be the index of
411 // the first sparse value, or end of the constants array
412 U contiguous_until;
413
414 // Compile-time generated count of enum constants.
415 static constexpr unsigned int constants_size =
417 template each_enum< static_cast<U>(enum_last<E>::value) >();
418
419 // Constants array is sized to the number of found-constants, or 1
420 // to avoid 0-sized array.
421 #ifdef FLECS_CPP_ENUM_REFLECTION
422 enum_constant<U> constants[constants_size? constants_size: 1] = {};
423 bool has_contiguous;
424 #else
425 // If we're not using enum reflection, we cannot statically determine the
426 // upper bound of the enum, so use 128.
427 enum_constant<U> constants[128] = {};
428 bool has_contiguous = true; // Assume contiguous ids
429 #endif
430};
431
432template <typename E>
433inline static void init_enum(flecs::world_t *world, flecs::entity_t id) {
434 (void)world; (void)id;
435 if constexpr (is_enum_v<E>) {
436 _::enum_type<E>::get().register_for_world(world, id);
437 }
438}
439
440} // namespace _
441
443template <typename E>
444struct enum_data {
445 using U = underlying_type_t<E>;
446
447 enum_data(flecs::world_t *world, _::enum_type<E>& impl)
448 : world_(world)
449 , impl_(impl) { }
450
458 bool is_valid(U value) {
459 int index = index_by_value(value);
460 if (index < 0) {
461 return false;
462 }
463 return impl_.constants[index].index != 0;
464 }
465
473 bool is_valid(E value) {
474 return is_valid(static_cast<U>(value));
475 }
476
483 int index_by_value(U value) const {
484 if (impl_.max < 0) {
485 return -1;
486 }
487
488 // Check if value is in contiguous lookup section
489 if (impl_.has_contiguous && value < impl_.contiguous_until && value >= 0) {
490 return static_cast<int>(value);
491 }
492 U accumulator = impl_.contiguous_until? impl_.contiguous_until - 1: 0;
493 for (int i = static_cast<int>(impl_.contiguous_until); i <= impl_.max; ++i) {
494 accumulator += impl_.constants[i].offset;
495 if (accumulator == value) {
496 return i;
497 }
498 }
499 return -1;
500 }
501
508 int index_by_value(E value) const {
509 return index_by_value(static_cast<U>(value));
510 }
511
512 int first() const {
513 return impl_.min;
514 }
515
516 int last() const {
517 return impl_.max;
518 }
519
520 int next(int cur) const {
521 return cur + 1;
522 }
523
524 flecs::entity entity() const;
525 flecs::entity entity(U value) const;
526 flecs::entity entity(E value) const;
527
534 #ifdef FLECS_CPP_NO_ENUM_REFLECTION
535 void register_constant(flecs::world_t *world, U v, flecs::entity_t e) {
536 if (v < 128) {
537 if (!impl_.constants[v].index) {
538 impl_.constants[v].index = flecs_component_ids_index_get();
539 }
540
541 flecs_component_ids_set(world, impl_.constants[v].index, e);
542
543 impl_.max ++;
544
545 if (impl_.contiguous_until <= v) {
546 impl_.contiguous_until = v + 1;
547 }
548 }
549 }
550 #endif
551
552 flecs::world_t *world_;
553 _::enum_type<E>& impl_;
554};
555
557template <typename E>
558enum_data<E> enum_type(flecs::world_t *world) {
559 _::type<E>::id(world); // Ensure enum is registered
560 auto& ref = _::enum_type<E>::get();
561 return enum_data<E>(world, ref);
562}
563
564} // 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:153
Enumeration constant data.
Definition enum.hpp:279
Provides utilities for enum reflection.
Definition enum.hpp:192
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:212
static constexpr U each_enum(Args &... args)
Handles enum iteration for gathering reflection data.
Definition enum.hpp:266
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:242
Class that scans an enum for constants, extracts names & creates entities.
Definition enum.hpp:288
Entity.
Definition entity.hpp:30
Convenience type with enum reflection data.
Definition enum.hpp:444
int index_by_value(E value) const
Finds the index into the constants array for an enum value, if one exists.
Definition enum.hpp:508
bool is_valid(U value)
Checks if a given integral value is a valid enum value.
Definition enum.hpp:458
flecs::world_t * world_
Manually register constant for enum.
Definition enum.hpp:552
int index_by_value(U value) const
Finds the index into the constants array for a value, if one exists.
Definition enum.hpp:483
bool is_valid(E value)
Checks if a given enum value is valid.
Definition enum.hpp:473
Component reference.
Definition ref.hpp:89
The world.
Definition world.hpp:174