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(
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),
198template <
typename E,
typename Handler>
200 using U = underlying_type_t<E>;
216 template <U Low, U High,
typename... Args>
218 return High - Low <= 1
220 ? Handler::template handle_constant<Low>(last_value, args...)
221 : Handler::template handle_constant<High>(Handler::template handle_constant<Low>(last_value, args...), args...)
242 template <U Low, U High,
typename... Args>
245 return (Low & High) || (High <= Low && High != high_bit)
247 : Handler::template handle_constant<High>(
265 template <U Value = static_cast<U>(FLECS_ENUM_MAX(E)),
typename... Args>
267 return each_mask_range<Value, high_bit>(each_enum_range<0, Value>(0, args...), args...);
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));
279 using U = underlying_type_t<E>;
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) {
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;
312 using U = underlying_type_t<E>;
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*) {
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) {
331 const char *name = enum_constant_to_name<E, flecs_enum_cast(E, Value)>();
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.");
350 flecs_component_ids_index_get();
353 flecs::entity_t
constant = ecs_cpp_enum_constant_register(
355 flecs_component_ids_set(
world,
371 flecs::entity_t
entity(E value)
const {
372 int index = index_by_value(value);
374 return data.constants[index].id;
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")
386 data.has_contiguous =
true;
387 data.contiguous_until = 0;
402template <typename E, if_t< is_enum<E>::value > = 0>
403inline static void init_enum(flecs::world_t *
world, flecs::entity_t
id) {
407template <typename E, if_not_t< is_enum<E>::value > = 0>
408inline static void init_enum(flecs::world_t*, flecs::entity_t) { }
415 using U = underlying_type_t<E>;
433 return impl_.constants[index].index != 0;
444 return is_valid(
static_cast<U
>(value));
458 if (impl_.has_contiguous && value < impl_.contiguous_until && value >= 0) {
459 return static_cast<int>(value);
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) {
489 int next(
int cur)
const {
497 flecs::world_t *world_;
498 _::enum_data_impl<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_mask_range(U last_value, Args... args)
Iterates over the mask range (Low, High] of enum values between Low and High.
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.
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.