Flecs v3.2
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
enum.hpp
Go to the documentation of this file.
1
9#include <string.h>
10
11#define FLECS_ENUM_MAX(T) _::to_constant<T, 128>::value
12#define FLECS_ENUM_MAX_COUNT (FLECS_ENUM_MAX(int) + 1)
13
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
18#else
19#define FLECS_CPP_ENUM_REFLECTION_SUPPORT 0
20#endif
21#else
22#define FLECS_CPP_ENUM_REFLECTION_SUPPORT 1
23#endif
24#endif
25
26namespace flecs {
27
29namespace _ {
30template <typename E, int Value>
32#if defined(__clang__) && __clang_major__ >= 16
33 // https://reviews.llvm.org/D130058, https://reviews.llvm.org/D131307
34 static constexpr E value = __builtin_bit_cast(E, Value);
35#else
36 static constexpr E value = static_cast<E>(Value);
37#endif
38};
39
40template <typename E, int Value>
42}
43
45template <typename E>
46struct enum_data;
47
48template <typename E>
49static enum_data<E> enum_type(flecs::world_t *world);
50
51template <typename E>
52struct enum_last {
53 static constexpr E value = FLECS_ENUM_MAX(E);
54};
55
56/* Utility macro to override enum_last trait */
57#define FLECS_ENUM_LAST(T, Last)\
58 namespace flecs {\
59 template<>\
60 struct enum_last<T> {\
61 static constexpr T value = Last;\
62 };\
63 }
64
65namespace _ {
66
67#ifdef ECS_TARGET_MSVC
68#define ECS_SIZE_T_STR "unsigned __int64"
69#elif defined(__clang__)
70#define ECS_SIZE_T_STR "size_t"
71#else
72#ifdef ECS_TARGET_WINDOWS
73#define ECS_SIZE_T_STR "constexpr size_t; size_t = long long unsigned int"
74#else
75#define ECS_SIZE_T_STR "constexpr size_t; size_t = long unsigned int"
76#endif
77#endif
78
79template <typename E>
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);
83}
84
89#if defined(ECS_TARGET_CLANG)
90#if ECS_CLANG_VERSION < 13
91template <typename E, E C>
92constexpr bool enum_constant_is_valid() {
93 return !(
94 (ECS_FUNC_NAME[ECS_FUNC_NAME_FRONT(bool, enum_constant_is_valid) +
95 enum_type_len<E>() + 6 /* ', C = ' */] >= '0') &&
96 (ECS_FUNC_NAME[ECS_FUNC_NAME_FRONT(bool, enum_constant_is_valid) +
97 enum_type_len<E>() + 6 /* ', C = ' */] <= '9'));
98}
99#else
100template <typename E, E C>
101constexpr bool enum_constant_is_valid() {
102 return (ECS_FUNC_NAME[ECS_FUNC_NAME_FRONT(bool, enum_constant_is_valid) +
103 enum_type_len<E>() + 6 /* ', E C = ' */] != '(');
104}
105#endif
106#elif defined(ECS_TARGET_GNU)
107template <typename E, E C>
108constexpr bool enum_constant_is_valid() {
109 return (ECS_FUNC_NAME[ECS_FUNC_NAME_FRONT(constepxr bool, enum_constant_is_valid) +
110 enum_type_len<E>() + 8 /* ', E C = ' */] != '(');
111}
112#else
113/* Use different trick on MSVC, since it uses hexadecimal representation for
114 * invalid enum constants. We can leverage that msvc inserts a C-style cast
115 * into the name, and the location of its first character ('(') is known. */
116template <typename E, E C>
117constexpr bool enum_constant_is_valid() {
118 return ECS_FUNC_NAME[ECS_FUNC_NAME_FRONT(bool, enum_constant_is_valid) +
119 enum_type_len<E>() + 1] != '(';
120}
121#endif
122
123template <typename E, E C>
125 static constexpr bool value = enum_constant_is_valid<E, C>();
126};
127
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),
135 ECS_FUNC_NAME_BACK);
136}
137
140 flecs::entity_t id;
141 int next;
142};
143
146 flecs::entity_t id;
147 int min;
148 int max;
149 enum_constant_data constants[FLECS_ENUM_MAX_COUNT];
150};
151
153template <typename E>
154struct enum_type {
155 static enum_data_impl data;
156
157 static enum_type<E>& get() {
158 static _::enum_type<E> instance;
159 return instance;
160 }
161
162 flecs::entity_t entity(E value) const {
163 return data.constants[static_cast<int>(value)].id;
164 }
165
166 void init(flecs::world_t *world, flecs::entity_t id) {
167#if !FLECS_CPP_ENUM_REFLECTION_SUPPORT
168 ecs_abort(ECS_UNSUPPORTED, "enum reflection requires gcc 7.5 or higher")
169#endif
170
171 ecs_log_push();
172 ecs_cpp_enum_init(world, id);
173 data.id = id;
174 data.min = FLECS_ENUM_MAX(int);
175 init< enum_last<E>::value >(world);
176 ecs_log_pop();
177 }
178
179private:
180 template <E Value>
181 static constexpr int to_int() {
182 return static_cast<int>(Value);
183 }
184
185 template <int Value>
186 static constexpr E from_int() {
188 }
189
190 template <E Value>
191 static constexpr int is_not_0() {
192 return static_cast<int>(Value != from_int<0>());
193 }
194
195 template <E Value, flecs::if_not_t< enum_constant_is_valid<E, Value>() > = 0>
196 static void init_constant(flecs::world_t*) { }
197
198 template <E Value, flecs::if_t< enum_constant_is_valid<E, Value>() > = 0>
199 static void init_constant(flecs::world_t *world) {
200 int v = to_int<Value>();
201 const char *name = enum_constant_to_name<E, Value>();
202 data.constants[v].next = data.min;
203 data.min = v;
204 if (!data.max) {
205 data.max = v;
206 }
207
208 data.constants[v].id = ecs_cpp_enum_constant_register(
209 world, data.id, data.constants[v].id, name, v);
210 }
211
212 template <E Value = FLECS_ENUM_MAX(E) >
213 static void init(flecs::world_t *world) {
214 init_constant<Value>(world);
215 if (is_not_0<Value>()) {
216 init<from_int<to_int<Value>() - is_not_0<Value>()>()>(world);
217 }
218 }
219};
220
221template <typename E>
223
224template <typename E, if_t< is_enum<E>::value > = 0>
225inline static void init_enum(flecs::world_t *world, flecs::entity_t id) {
226 _::enum_type<E>::get().init(world, id);
227}
228
229template <typename E, if_not_t< is_enum<E>::value > = 0>
230inline static void init_enum(flecs::world_t*, flecs::entity_t) { }
231
232} // namespace _
233
235template <typename E>
236struct enum_data {
237 enum_data(flecs::world_t *world, _::enum_data_impl& impl)
238 : world_(world)
239 , impl_(impl) { }
240
241 bool is_valid(int value) {
242 return impl_.constants[value].id != 0;
243 }
244
245 int first() const {
246 return impl_.min;
247 }
248
249 int last() const {
250 return impl_.max;
251 }
252
253 int next(int cur) const {
254 return impl_.constants[cur].next;
255 }
256
257 flecs::entity entity() const;
258 flecs::entity entity(int value) const;
259 flecs::entity entity(E value) const;
260
261 flecs::world_t *world_;
262 _::enum_data_impl& impl_;
263};
264
266template <typename E>
267enum_data<E> enum_type(flecs::world_t *world) {
268 _::cpp_type<E>::id(world); // Ensure enum is registered
269 auto& ref = _::enum_type<E>::get();
270 return enum_data<E>(world, ref.data);
271}
272
273} // namespace flecs
#define ecs_abort(error_code,...)
Abort.
Definition: log.h:343
constexpr bool enum_constant_is_valid()
Test if value is valid for enumeration.
Definition: enum.hpp:117
Enumeration constant data.
Definition: enum.hpp:139
Enumeration type data.
Definition: enum.hpp:145
Class that scans an enum for constants, extracts names & creates entities.
Definition: enum.hpp:154
Entity.
Definition: entity.hpp:30
Convenience type with enum reflection data.
Definition: enum.hpp:236
Class that wraps around a flecs::id_t.
Definition: decl.hpp:27
Component reference.
Definition: ref.hpp:23
The world.
Definition: world.hpp:113