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;\
68#define ECS_SIZE_T_STR "unsigned __int64"
69#elif defined(__clang__)
70#define ECS_SIZE_T_STR "size_t"
72#ifdef ECS_TARGET_WINDOWS
73#define ECS_SIZE_T_STR "constexpr size_t; size_t = long long unsigned int"
75#define ECS_SIZE_T_STR "constexpr size_t; size_t = long unsigned int"
80constexpr size_t enum_type_len() {
81 return ECS_FUNC_TYPE_LEN(, enum_type_len, ECS_FUNC_NAME)
82 - (
sizeof(ECS_SIZE_T_STR) - 1u);
89#if defined(ECS_TARGET_CLANG)
90#if ECS_CLANG_VERSION < 13
91template <
typename E, E C>
95 enum_type_len<E>() + 6 ] >=
'0') &&
97 enum_type_len<E>() + 6 ] <=
'9'));
100template <
typename E, E C>
103 enum_type_len<E>() + 6 ] !=
'(');
106#elif defined(ECS_TARGET_GNU)
107template <
typename E, E C>
110 enum_type_len<E>() + 8 ] !=
'(');
116template <
typename E, E C>
119 enum_type_len<E>() + 1] !=
'(';
123template <
typename E, E C>
125 static constexpr bool value = enum_constant_is_valid<E, C>();
129template <
typename E, E C>
130static const char* enum_constant_to_name() {
131 static const size_t len = ECS_FUNC_TYPE_LEN(
const char*, enum_constant_to_name, ECS_FUNC_NAME);
132 static char result[len + 1] = {};
133 return ecs_cpp_get_constant_name(
134 result, ECS_FUNC_NAME, string::length(ECS_FUNC_NAME));
161 flecs::entity_t
entity(E value)
const {
162 return data.constants[
static_cast<int>(value)].
id;
165 void init(flecs::world_t *
world, flecs::entity_t
id) {
166#if !FLECS_CPP_ENUM_REFLECTION_SUPPORT
167 ecs_abort(ECS_UNSUPPORTED,
"enum reflection requires gcc 7.5 or higher")
171 ecs_cpp_enum_init(
world,
id);
173 data.min = FLECS_ENUM_MAX(
int);
174 init< enum_last<E>::value >(
world);
180 static constexpr int to_int() {
181 return static_cast<int>(Value);
185 static constexpr E from_int() {
190 static constexpr int is_not_0() {
191 return static_cast<int>(Value != from_int<0>());
194 template <E Value, flecs::if_not_t< enum_constant_is_valid<E, Value>() > = 0>
195 static void init_constant(flecs::world_t*) { }
197 template <E Value, flecs::if_t< enum_constant_is_valid<E, Value>() > = 0>
198 static void init_constant(flecs::world_t *
world) {
199 int v = to_int<Value>();
200 const char *name = enum_constant_to_name<E, Value>();
201 data.constants[v].next = data.min;
207 data.constants[v].id = ecs_cpp_enum_constant_register(
208 world, data.id, data.constants[v].id, name, v);
211 template <E Value = FLECS_ENUM_MAX(E) >
212 static void init(flecs::world_t *
world) {
213 init_constant<Value>(
world);
214 if (is_not_0<Value>()) {
215 init<from_int<to_int<Value>() - is_not_0<Value>()>()>(
world);
223template <typename E, if_t< is_enum<E>::value > = 0>
224inline static void init_enum(flecs::world_t *
world, flecs::entity_t
id) {
228template <typename E, if_not_t< is_enum<E>::value > = 0>
229inline static void init_enum(flecs::world_t*, flecs::entity_t) { }
240 bool is_valid(
int value) {
241 return impl_.constants[value].id != 0;
252 int next(
int cur)
const {
253 return impl_.constants[cur].next;
260 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.