Flecs v3.1
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}
136
139 flecs::entity_t id;
140 int next;
141};
142
145 flecs::entity_t id;
146 int min;
147 int max;
148 enum_constant_data constants[FLECS_ENUM_MAX_COUNT];
149};
150
152template <typename E>
153struct enum_type {
154 static enum_data_impl data;
155
156 static enum_type<E>& get() {
157 static _::enum_type<E> instance;
158 return instance;
159 }
160
161 flecs::entity_t entity(E value) const {
162 return data.constants[static_cast<int>(value)].id;
163 }
164
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")
168#endif
169
170 ecs_log_push();
171 ecs_cpp_enum_init(world, id);
172 data.id = id;
173 data.min = FLECS_ENUM_MAX(int);
174 init< enum_last<E>::value >(world);
175 ecs_log_pop();
176 }
177
178private:
179 template <E Value>
180 static constexpr int to_int() {
181 return static_cast<int>(Value);
182 }
183
184 template <int Value>
185 static constexpr E from_int() {
187 }
188
189 template <E Value>
190 static constexpr int is_not_0() {
191 return static_cast<int>(Value != from_int<0>());
192 }
193
194 template <E Value, flecs::if_not_t< enum_constant_is_valid<E, Value>() > = 0>
195 static void init_constant(flecs::world_t*) { }
196
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;
202 data.min = v;
203 if (!data.max) {
204 data.max = v;
205 }
206
207 data.constants[v].id = ecs_cpp_enum_constant_register(
208 world, data.id, data.constants[v].id, name, v);
209 }
210
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);
216 }
217 }
218};
219
220template <typename E>
222
223template <typename E, if_t< is_enum<E>::value > = 0>
224inline static void init_enum(flecs::world_t *world, flecs::entity_t id) {
225 _::enum_type<E>::get().init(world, id);
226}
227
228template <typename E, if_not_t< is_enum<E>::value > = 0>
229inline static void init_enum(flecs::world_t*, flecs::entity_t) { }
230
231} // namespace _
232
234template <typename E>
235struct enum_data {
236 enum_data(flecs::world_t *world, _::enum_data_impl& impl)
237 : world_(world)
238 , impl_(impl) { }
239
240 bool is_valid(int value) {
241 return impl_.constants[value].id != 0;
242 }
243
244 int first() const {
245 return impl_.min;
246 }
247
248 int last() const {
249 return impl_.max;
250 }
251
252 int next(int cur) const {
253 return impl_.constants[cur].next;
254 }
255
256 flecs::entity entity() const;
257 flecs::entity entity(int value) const;
258 flecs::entity entity(E value) const;
259
260 flecs::world_t *world_;
261 _::enum_data_impl& impl_;
262};
263
265template <typename E>
266enum_data<E> enum_type(flecs::world_t *world) {
267 _::cpp_type<E>::id(world); // Ensure enum is registered
268 auto& ref = _::enum_type<E>::get();
269 return enum_data<E>(world, ref.data);
270}
271
272} // 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:138
Enumeration type data.
Definition: enum.hpp:144
Class that scans an enum for constants, extracts names & creates entities.
Definition: enum.hpp:153
Entity.
Definition: entity.hpp:30
Convenience type with enum reflection data.
Definition: enum.hpp:235
Class that wraps around a flecs::id_t.
Definition: decl.hpp:27
Component reference.
Definition: ref.hpp:23
The world.
Definition: world.hpp:113