14#define FLECS_ENUM_MAX(T) _::to_constant<T, 126>::value
15#define FLECS_ENUM_MAX_COUNT (FLECS_ENUM_MAX(int) + 1)
17#ifndef FLECS_CPP_ENUM_REFLECTION_SUPPORT
18#if !defined(__clang__) && defined(__GNUC__)
19#if __GNUC__ > 7 || (__GNUC__ == 7 && __GNUC_MINOR__ >= 5)
20#define FLECS_CPP_ENUM_REFLECTION_SUPPORT 1
22#define FLECS_CPP_ENUM_REFLECTION_SUPPORT 0
25#define FLECS_CPP_ENUM_REFLECTION_SUPPORT 1
29#if defined(__clang__) && __clang_major__ >= 16
31#define flecs_enum_cast(T, v) __builtin_bit_cast(T, v)
32#elif defined(__GNUC__) && __GNUC__ > 10
33#define flecs_enum_cast(T, v) __builtin_bit_cast(T, v)
35#define flecs_enum_cast(T, v) static_cast<T>(v)
42template <
typename E, underlying_type_t<E> Value>
44 static constexpr E value = flecs_enum_cast(E, Value);
47template <
typename E, underlying_type_t<E> Value>
60 static constexpr E value = FLECS_ENUM_MAX(E);
64#define FLECS_ENUM_LAST(T, Last)\
67 struct enum_last<T> {\
68 static constexpr T value = Last;\
74#if INTPTR_MAX == INT64_MAX
75 #ifdef ECS_TARGET_MSVC
77 #define ECS_SIZE_T_STR "unsigned __int64"
79 #define ECS_SIZE_T_STR "unsigned int"
81 #elif defined(__clang__)
82 #define ECS_SIZE_T_STR "size_t"
84 #ifdef ECS_TARGET_WINDOWS
85 #define ECS_SIZE_T_STR "constexpr size_t; size_t = long long unsigned int"
87 #define ECS_SIZE_T_STR "constexpr size_t; size_t = long unsigned int"
91 #ifdef ECS_TARGET_MSVC
93 #define ECS_SIZE_T_STR "unsigned __int32"
95 #define ECS_SIZE_T_STR "unsigned int"
97 #elif defined(__clang__)
98 #define ECS_SIZE_T_STR "size_t"
100 #ifdef ECS_TARGET_WINDOWS
101 #define ECS_SIZE_T_STR "constexpr size_t; size_t = unsigned int"
103 #define ECS_SIZE_T_STR "constexpr size_t; size_t = unsigned int"
109constexpr size_t enum_type_len() {
110 return ECS_FUNC_TYPE_LEN(, enum_type_len, ECS_FUNC_NAME)
111 - (
sizeof(ECS_SIZE_T_STR) - 1u);
118#if defined(ECS_TARGET_CLANG)
119#if ECS_CLANG_VERSION < 13
120template <
typename E, E C>
124 enum_type_len<E>() + 6 ] >=
'0') &&
126 enum_type_len<E>() + 6 ] <=
'9')) ||
128 enum_type_len<E>() + 6 ] ==
'-'));
131template <
typename E, E C>
134 enum_type_len<E>() + 6 ] !=
'(');
137#elif defined(ECS_TARGET_GNU)
138template <
typename E, E C>
141 enum_type_len<E>() + 8 ] !=
'(');
147template <
typename E, E C>
150 enum_type_len<E>() + 1] !=
'(';
155template <
typename E, underlying_type_t<E> C>
156constexpr bool enum_constant_is_valid_wrap() {
157 return enum_constant_is_valid<E, flecs_enum_cast(E, C)>();
160template <
typename E, E C>
162 static constexpr bool value = enum_constant_is_valid<E, C>();
166template <
typename E, E C>
167static const char* enum_constant_to_name() {
168 static const size_t len = ECS_FUNC_TYPE_LEN(
const char*, enum_constant_to_name, ECS_FUNC_NAME);
169 static char result[len + 1] = {};
170 return ecs_cpp_get_constant_name(
171 result, ECS_FUNC_NAME, string::length(ECS_FUNC_NAME),
192template <
typename E,
typename Handler>
194 using U = underlying_type_t<E>;
210 template <U Low, U High,
typename... Args>
212 return High - Low <= 1
214 ? Handler::template handle_constant<Low>(last_value, args...)
215 : Handler::template handle_constant<High>(Handler::template handle_constant<Low>(last_value, args...), args...)
236 template <U Low, U High,
typename... Args>
239 return (Low & High) || (High <= Low && High != high_bit)
241 : Handler::template handle_constant<High>(
259 template <U Value = static_cast<U>(FLECS_ENUM_MAX(E)),
typename... Args>
261 return each_mask_range<Value, high_bit>(each_enum_range<0, Value>(0, args...), args...);
264 static const U high_bit =
static_cast<U
>(1) << (
sizeof(U) * 8 - 1);
271 using U = underlying_type_t<E>;
276 struct reflection_count {
277 template <U Value, flecs::if_not_t< enum_constant_is_valid_wrap<E, Value>() > = 0>
278 static constexpr U handle_constant(U last_value) {
282 template <U Value, flecs::if_t< enum_constant_is_valid_wrap<E, Value>() > = 0>
283 static constexpr U handle_constant(U last_value) {
284 return 1 + last_value;
304 using U = underlying_type_t<E>;
312 struct reflection_init {
313 template <U Value, flecs::if_not_t< enum_constant_is_valid_wrap<E, Value>() > = 0>
314 static U handle_constant(U last_value, flecs::world_t*) {
319 template <U Value, flecs::if_t< enum_constant_is_valid_wrap<E, Value>() > = 0>
320 static U handle_constant(U last_value, flecs::world_t *
world) {
323 const char *name = enum_constant_to_name<E, flecs_enum_cast(E, Value)>();
336 ecs_assert(!(last_value > 0 && v < std::numeric_limits<U>::min() + last_value), ECS_UNSUPPORTED,
337 "Signed integer enums causes integer overflow when recording offset from high positive to"
338 " low negative. Consider using unsigned integers as underlying type.");
342 flecs_component_ids_index_get();
345 flecs::entity_t constant = ecs_cpp_enum_constant_register(
347 flecs_component_ids_set(
world,
363 flecs::entity_t
entity(E value)
const {
364 int index = index_by_value(value);
366 return data.constants[index].id;
371 void init(flecs::world_t *
world, flecs::entity_t
id) {
372#if !FLECS_CPP_ENUM_REFLECTION_SUPPORT
373 ecs_abort(ECS_UNSUPPORTED,
"enum reflection requires gcc 7.5 or higher")
378 data.has_contiguous =
true;
379 data.contiguous_until = 0;
394template <typename E, if_t< is_enum<E>::value > = 0>
395inline static void init_enum(flecs::world_t *
world, flecs::entity_t
id) {
399template <typename E, if_not_t< is_enum<E>::value > = 0>
400inline static void init_enum(flecs::world_t*, flecs::entity_t) { }
407 using U = underlying_type_t<E>;
425 return impl_.constants[index].index != 0;
436 return is_valid(
static_cast<U
>(value));
450 if (impl_.has_contiguous && value < impl_.contiguous_until && value >= 0) {
451 return static_cast<int>(value);
453 U accumulator = impl_.contiguous_until? impl_.contiguous_until - 1: 0;
454 for (
int i =
static_cast<int>(impl_.contiguous_until); i <= impl_.max; ++i) {
455 accumulator += impl_.constants[i].offset;
456 if (accumulator == value) {
481 int next(
int cur)
const {
489 flecs::world_t *world_;
490 _::enum_data_impl<E>& impl_;
#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.