14#define FLECS_ENUM_MAX(T) _::to_constant<T, 126>::value
15#define FLECS_ENUM_MAX_COUNT (FLECS_ENUM_MAX(int) + 1)
18#ifdef FLECS_CPP_NO_ENUM_REFLECTION
19#define FLECS_CPP_ENUM_REFLECTION_SUPPORT 0
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
28#define FLECS_CPP_ENUM_REFLECTION_SUPPORT 0
31#define FLECS_CPP_ENUM_REFLECTION_SUPPORT 1
35#if defined(__clang__) && __clang_major__ >= 16
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)
41#define flecs_enum_cast(T, v) static_cast<T>(v)
48template <
typename E, underlying_type_t<E> Value>
50 static constexpr E value = flecs_enum_cast(E, Value);
53template <
typename E, underlying_type_t<E> Value>
66 static constexpr E value = FLECS_ENUM_MAX(E);
70#define FLECS_ENUM_LAST(T, Last)\
73 struct enum_last<T> {\
74 static constexpr T value = Last;\
80#if INTPTR_MAX == INT64_MAX
81 #ifdef ECS_TARGET_MSVC
83 #define ECS_SIZE_T_STR "unsigned __int64"
85 #define ECS_SIZE_T_STR "unsigned int"
87 #elif defined(__clang__)
88 #define ECS_SIZE_T_STR "size_t"
90 #ifdef ECS_TARGET_WINDOWS
91 #define ECS_SIZE_T_STR "constexpr size_t; size_t = long long unsigned int"
93 #define ECS_SIZE_T_STR "constexpr size_t; size_t = long unsigned int"
97 #ifdef ECS_TARGET_MSVC
99 #define ECS_SIZE_T_STR "unsigned __int32"
101 #define ECS_SIZE_T_STR "unsigned int"
103 #elif defined(__clang__)
104 #define ECS_SIZE_T_STR "size_t"
106 #ifdef ECS_TARGET_WINDOWS
107 #define ECS_SIZE_T_STR "constexpr size_t; size_t = unsigned int"
109 #define ECS_SIZE_T_STR "constexpr size_t; size_t = unsigned int"
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);
124#if defined(ECS_TARGET_CLANG)
125#if ECS_CLANG_VERSION < 13
126template <
typename E, E C>
130 enum_type_len<E>() + 6 ] >=
'0') &&
132 enum_type_len<E>() + 6 ] <=
'9')) ||
134 enum_type_len<E>() + 6 ] ==
'-'));
137template <
typename E, E C>
140 enum_type_len<E>() + 6 ] !=
'(');
143#elif defined(ECS_TARGET_GNU)
144template <
typename E, E C>
147 enum_type_len<E>() + 8 ] !=
'(');
153template <
typename E, E C>
156 enum_type_len<E>() + 1] !=
'(';
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)>();
166template <
typename E, E C>
168 static constexpr bool value = enum_constant_is_valid<E, C>();
172template <
typename E, E C>
173static const char* enum_constant_to_name() {
174 static const size_t len = ECS_FUNC_TYPE_LEN(
175 const char*, enum_constant_to_name, ECS_FUNC_NAME);
176 static char result[len + 1] = {};
177 return ecs_cpp_get_constant_name(
178 result, ECS_FUNC_NAME, string::length(ECS_FUNC_NAME),
192template <
typename E,
typename Handler>
194 using U = underlying_type_t<E>;
212 template <U Low, U High,
typename... Args>
214 return High - Low <= 1
216 ? Handler::template handle_constant<Low>(last_value, args...)
217 : Handler::template handle_constant<High>(
218 Handler::template handle_constant<Low>(last_value, args...),
242 template <U Low, U High,
typename... Args>
246 return (Low & High) || (High <= Low && High != high_bit)
248 : Handler::template handle_constant<High>(
266 template <U Value = static_cast<U>(FLECS_ENUM_MAX(E)),
typename... Args>
268 return each_mask_range<Value, high_bit>(
269 each_enum_range<0, Value>(0, args...), args...);
273 using UU =
typename std::make_unsigned<U>::type;
274 static const U high_bit =
275 static_cast<U
>(
static_cast<UU
>(1) << (
sizeof(UU) * 8 - 1));
292 using U = underlying_type_t<E>;
297 struct reflection_count {
299 flecs::if_not_t< enum_constant_is_valid_wrap<E, Value>() > = 0>
300 static constexpr U handle_constant(U last_value) {
305 flecs::if_t< enum_constant_is_valid_wrap<E, Value>() > = 0>
306 static constexpr U handle_constant(U last_value) {
307 return 1 + last_value;
318 struct reflection_init {
320 flecs::if_not_t< enum_constant_is_valid_wrap<E, Value>() > = 0>
321 static U handle_constant(U last_value,
This&) {
327 flecs::if_t< enum_constant_is_valid_wrap<E, Value>() > = 0>
328 static U handle_constant(U last_value,
This& me) {
331 const char *name = enum_constant_to_name<E, flecs_enum_cast(E, Value)>();
337 if (me.has_contiguous &&
static_cast<U
>(me.max) == v && me.contiguous_until == v) {
338 ++me.contiguous_until;
343 else if (!me.contiguous_until && me.has_contiguous) {
344 me.has_contiguous =
false;
348 v < std::numeric_limits<U>::min() + last_value),
350 "Signed integer enums causes integer overflow when recording "
351 "offset from high positive to low negative. Consider using "
352 "unsigned integers as underlying type.");
354 me.constants[me.max].value = v;
355 me.constants[me.max].offset = v - last_value;
356 me.constants[me.max].name = name;
357 if (!me.constants[me.max].index) {
358 me.constants[me.max].index =
359 flecs_component_ids_index_get();
371 has_contiguous =
true;
372 contiguous_until = 0;
383 flecs::entity_t
entity(E value)
const {
384 int index = index_by_value(value);
386 return constants[index].id;
391 void register_for_world(flecs::world_t *
world, flecs::entity_t
id) {
392#if !FLECS_CPP_ENUM_REFLECTION_SUPPORT
393 ecs_abort(ECS_UNSUPPORTED,
"enum reflection requires gcc 7.5 or higher")
399 for (U v = 0; v < static_cast<U>(max + 1); v ++) {
400 if (constants[v].index) {
401 flecs::entity_t
constant = ecs_cpp_enum_constant_register(
world,
405 flecs_component_ids_set(
world, constants[v].index,
constant);
421 static constexpr unsigned int constants_size =
430template <typename E, if_t< is_enum<E>::value > = 0>
431inline static void init_enum(flecs::world_t *
world, flecs::entity_t
id) {
435template <typename E, if_not_t< is_enum<E>::value > = 0>
436inline static void init_enum(flecs::world_t*, flecs::entity_t) { }
443 using U = underlying_type_t<E>;
461 return impl_.constants[index].index != 0;
472 return is_valid(
static_cast<U
>(value));
486 if (impl_.has_contiguous && value < impl_.contiguous_until && value >= 0) {
487 return static_cast<int>(value);
489 U accumulator = impl_.contiguous_until? impl_.contiguous_until - 1: 0;
490 for (
int i =
static_cast<int>(impl_.contiguous_until); i <= impl_.max; ++i) {
491 accumulator += impl_.constants[i].offset;
492 if (accumulator == value) {
517 int next(
int cur)
const {
525 flecs::world_t *world_;
526 _::enum_type<E>& impl_;
component< T > & constant(const char *name, T value)
Add constant.
#define ecs_assert(condition, error_code,...)
Assert.
#define ecs_abort(error_code,...)
Abort.
constexpr bool enum_constant_is_valid()
Test if value is valid for enumeration.
Enumeration constant data.
Provides utilities for enum reflection.
static constexpr U each_enum_range(U last_value, Args &... args)
Iterates over the range [Low, High] of enum values between Low and High.
static constexpr U each_enum(Args &... args)
Handles enum iteration for gathering reflection data.
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.
Class that scans an enum for constants, extracts names & creates entities.
Convenience type with enum reflection data.
int index_by_value(E value) const
Finds the index into the constants array for an enum value, if one exists.
bool is_valid(U value)
Checks if a given integral value is a valid enum value.
int index_by_value(U value) const
Finds the index into the constants array for a value, if one exists.
bool is_valid(E value)
Checks if a given enum value is valid.