11#define FLECS_ENUM_MAX(T) _::to_constant<T, 128>::value
12#define FLECS_ENUM_MAX_COUNT (FLECS_ENUM_MAX(int) + 1)
14#ifndef FLECS_CPP_ENUM_REFLECTION_SUPPORT
15#if !defined(__clang__) && defined(__GNUC__)
16#if __GNUC__ > 7 || (__GNUC__ == 7 && __GNUC_MINOR__ >= 5)
17#define FLECS_CPP_ENUM_REFLECTION_SUPPORT 1
19#define FLECS_CPP_ENUM_REFLECTION_SUPPORT 0
22#define FLECS_CPP_ENUM_REFLECTION_SUPPORT 1
30template <
typename E,
int Value>
32#if defined(__clang__) && __clang_major__ >= 16
34 static constexpr E value = __builtin_bit_cast(E, Value);
36 static constexpr E value =
static_cast<E
>(Value);
40template <
typename E,
int Value>
53 static constexpr E value = FLECS_ENUM_MAX(E);
57#define FLECS_ENUM_LAST(T, Last)\
60 struct enum_last<T> {\
61 static constexpr T value = Last;\
67#if INTPTR_MAX == INT64_MAX
68 #ifdef ECS_TARGET_MSVC
69 #define ECS_SIZE_T_STR "unsigned __int64"
70 #elif defined(__clang__)
71 #define ECS_SIZE_T_STR "size_t"
73 #ifdef ECS_TARGET_WINDOWS
74 #define ECS_SIZE_T_STR "constexpr size_t; size_t = long long unsigned int"
76 #define ECS_SIZE_T_STR "constexpr size_t; size_t = long unsigned int"
80 #ifdef ECS_TARGET_MSVC
81 #define ECS_SIZE_T_STR "unsigned __int32"
82 #elif defined(__clang__)
83 #define ECS_SIZE_T_STR "size_t"
85 #ifdef ECS_TARGET_WINDOWS
86 #define ECS_SIZE_T_STR "constexpr size_t; size_t = unsigned int"
88 #define ECS_SIZE_T_STR "constexpr size_t; size_t = unsigned int"
94constexpr size_t enum_type_len() {
95 return ECS_FUNC_TYPE_LEN(, enum_type_len, ECS_FUNC_NAME)
96 - (
sizeof(ECS_SIZE_T_STR) - 1u);
103#if defined(ECS_TARGET_CLANG)
104#if ECS_CLANG_VERSION < 13
105template <
typename E, E C>
109 enum_type_len<E>() + 6 ] >=
'0') &&
111 enum_type_len<E>() + 6 ] <=
'9'));
114template <
typename E, E C>
117 enum_type_len<E>() + 6 ] !=
'(');
120#elif defined(ECS_TARGET_GNU)
121template <
typename E, E C>
124 enum_type_len<E>() + 8 ] !=
'(');
130template <
typename E, E C>
133 enum_type_len<E>() + 1] !=
'(';
137template <
typename E, E C>
139 static constexpr bool value = enum_constant_is_valid<E, C>();
143template <
typename E, E C>
144static const char* enum_constant_to_name() {
145 static const size_t len = ECS_FUNC_TYPE_LEN(
const char*, enum_constant_to_name, ECS_FUNC_NAME);
146 static char result[len + 1] = {};
147 return ecs_cpp_get_constant_name(
148 result, ECS_FUNC_NAME, string::length(ECS_FUNC_NAME),
176 flecs::entity_t
entity(E value)
const {
177 return data.constants[
static_cast<int>(value)].
id;
180 void init(flecs::world_t *
world, flecs::entity_t
id) {
181#if !FLECS_CPP_ENUM_REFLECTION_SUPPORT
182 ecs_abort(ECS_UNSUPPORTED,
"enum reflection requires gcc 7.5 or higher")
186 ecs_cpp_enum_init(
world,
id);
188 data.min = FLECS_ENUM_MAX(
int);
189 init< enum_last<E>::value >(
world);
195 static constexpr int to_int() {
196 return static_cast<int>(Value);
200 static constexpr E from_int() {
205 static constexpr int is_not_0() {
206 return static_cast<int>(Value != from_int<0>());
209 template <E Value, flecs::if_not_t< enum_constant_is_valid<E, Value>() > = 0>
210 static void init_constant(flecs::world_t*) { }
212 template <E Value, flecs::if_t< enum_constant_is_valid<E, Value>() > = 0>
213 static void init_constant(flecs::world_t *
world) {
214 int v = to_int<Value>();
215 const char *name = enum_constant_to_name<E, Value>();
216 data.constants[v].next = data.min;
222 data.constants[v].id = ecs_cpp_enum_constant_register(
223 world, data.id, data.constants[v].id, name, v);
226 template <E Value = FLECS_ENUM_MAX(E) >
227 static void init(flecs::world_t *
world) {
228 init_constant<Value>(
world);
229 if (is_not_0<Value>()) {
230 init<from_int<to_int<Value>() - is_not_0<Value>()>()>(
world);
238template <typename E, if_t< is_enum<E>::value > = 0>
239inline static void init_enum(flecs::world_t *
world, flecs::entity_t
id) {
243template <typename E, if_not_t< is_enum<E>::value > = 0>
244inline static void init_enum(flecs::world_t*, flecs::entity_t) { }
255 bool is_valid(
int value) {
256 return impl_.constants[value].id != 0;
267 int next(
int cur)
const {
268 return impl_.constants[cur].next;
275 flecs::world_t *world_;
#define ecs_abort(error_code,...)
Abort.
constexpr bool enum_constant_is_valid()
Test if value is valid for enumeration.
Enumeration constant data.
Class that scans an enum for constants, extracts names & creates entities.
Convenience type with enum reflection data.
Class that wraps around a flecs::id_t.