Flecs v4.1
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
component.hpp
Go to the documentation of this file.
1
5
6#pragma once
7
9 * @defgroup cpp_components Components
10 * @ingroup cpp_core
11 * Registering and working with components.
12 *
13 * @{
14 */
15
16namespace flecs {
17
18namespace _ {
19
20template <typename T>
21inline const char* component_symbol_name() {
22 return nullptr;
24
25template <> inline const char* component_symbol_name<uint8_t>() {
26 return "u8";
27}
28template <> inline const char* component_symbol_name<uint16_t>() {
29 return "u16";
30}
31template <> inline const char* component_symbol_name<uint32_t>() {
32 return "u32";
33}
34template <> inline const char* component_symbol_name<uint64_t>() {
35 return "u64";
36}
37template <> inline const char* component_symbol_name<int8_t>() {
38 return "i8";
40template <> inline const char* component_symbol_name<int16_t>() {
41 return "i16";
42}
43template <> inline const char* component_symbol_name<int32_t>() {
44 return "i32";
45}
46template <> inline const char* component_symbol_name<int64_t>() {
47 return "i64";
48}
49template <> inline const char* component_symbol_name<float>() {
50 return "f32";
52template <> inline const char* component_symbol_name<double>() {
53 return "f64";
54}
55
56// If the type is trivial, don't register lifecycle actions. While the functions
57// that obtain the lifecycle callback do detect whether the callback is required,
58// adding a special case for trivial types eases the burden a bit on the
59// compiler, as it reduces the number of templates to evaluate.
60template<typename T>
61void register_lifecycle_actions(
64{
65 (void)world; (void)component;
66 if constexpr (!(std::is_trivially_default_constructible<T>::value &&
67 std::is_trivially_copyable<T>::value))
68 {
69 // If the component is non-trivial, register component lifecycle actions.
70 // Depending on the type, not all callbacks may be available.
72 cl.ctor = ctor<T>(cl.flags);
73 cl.dtor = dtor<T>(cl.flags);
74
75 cl.copy = copy<T>(cl.flags);
76 cl.copy_ctor = copy_ctor<T>(cl.flags);
77 cl.move = move<T>(cl.flags);
78 cl.move_ctor = move_ctor<T>(cl.flags);
79
80 cl.ctor_move_dtor = ctor_move_dtor<T>(cl.flags);
81 cl.move_dtor = move_dtor<T>(cl.flags);
83 cl.flags &= ECS_TYPE_HOOKS_ILLEGAL;
85
86 if (cl.flags & (ECS_TYPE_HOOK_MOVE_ILLEGAL|ECS_TYPE_HOOK_MOVE_CTOR_ILLEGAL))
87 {
88 ecs_add_id(world, component, flecs::Sparse);
89 }
90 }
91}
93template <typename T>
94inline ecs_cpp_type_action_t lifecycle_action() {
95 if constexpr (std::is_trivially_default_constructible<T>::value &&
96 std::is_trivially_copyable<T>::value)
97 {
98 return nullptr;
99 } else {
100 return &register_lifecycle_actions<T>;
101 }
102}
104#ifdef FLECS_META
105template <typename T, typename = void>
106struct has_cpp_meta_desc : std::false_type {};
107
108template <typename T>
109struct has_cpp_meta_desc<T, decltype(void(
110 flecs_meta_cpp_desc(static_cast<T*>(nullptr))))> : std::true_type {};
111#endif
112
113template <typename T>
114inline ecs_cpp_type_action_t enum_action() {
115#if FLECS_CPP_ENUM_REFLECTION_SUPPORT
116#ifdef FLECS_META
117 if constexpr (has_cpp_meta_desc<T>::value) {
118 return nullptr;
119 } else
120#endif
121 if constexpr (is_enum_v<T>) {
122 return &_::init_enum<T>;
123 } else {
124 return nullptr;
125 }
126#else
127 return nullptr;
128#endif
129}
130
131#ifdef FLECS_META
132
133template <typename T>
134inline void register_cpp_meta(ecs_world_t *world, ecs_entity_t component) {
135 (void)world; (void)component;
136 if constexpr (has_cpp_meta_desc<T>::value) {
137 ecs_type_kind_t kind = flecs_meta_cpp_kind(static_cast<T*>(nullptr));
138 if (kind == EcsStructType && ecs_has_id(world, component,
139 ecs_id(EcsStruct))) {
140 return;
141 }
142 if (kind == EcsEnumType && ecs_has_id(world, component,
143 ecs_id(EcsEnum))) {
144 return;
145 }
146 if (kind == EcsBitmaskType && ecs_has_id(world, component,
147 ecs_id(EcsBitmask))) {
148 return;
149 }
151 flecs_meta_cpp_desc(static_cast<T*>(nullptr)));
152 }
153}
154#endif
155
156template <typename T>
157struct type_impl {
158 static_assert(is_pointer<T>::value == false,
159 "pointer types are not allowed for components");
160
161 // Initialize the component identifier.
162 static void init(
163 bool allow_tag = true)
164 {
165 index(); // Make sure the global component index is initialized.
166
167 s_size = sizeof(T);
168 s_alignment = alignof(T);
169 if (is_empty<T>::value && allow_tag) {
170 s_size = 0;
171 s_alignment = 0;
172 }
173 }
174
175 static void init_builtin(
178 bool allow_tag = true)
179 {
180 init(allow_tag);
181 flecs_component_ids_set(world, index(), id);
182 }
183
184 // Register the component ID.
185 static entity_t register_id(
186 world_t *world, // The world
187 const char *name = nullptr, // User-provided name (overrides typename)
188 bool allow_tag = true, // Register empty types as zero-sized components
189 bool is_component = true, // Add flecs::Component to the result
190 bool explicit_registration = false, // Entered from world.component<T>()?
191 flecs::id_t id = 0) // User-provided component ID
192 {
193 init(allow_tag);
194 ecs_assert(index() != 0, ECS_INTERNAL_ERROR, nullptr);
195
196 ecs_cpp_component_desc_t desc = {
197 id,
198 index(),
199 name,
200 type_name<T>(),
201 component_symbol_name<T>(),
202 size(),
203 alignment(),
204 lifecycle_action<T>(),
205 enum_action<T>(),
206 is_component,
207 explicit_registration
208 };
209
210 flecs::entity_t c = ecs_cpp_component_register(world, &desc);
211
212 ecs_assert(c != 0, ECS_INTERNAL_ERROR, nullptr);
213
214#ifdef FLECS_META
215 register_cpp_meta<T>(world, c);
216#endif
217
218 return c;
219 }
220
221 // Get the type (component) ID.
222 // If the type was not yet registered and automatic registration is allowed,
223 // this function will also register the type.
224 static entity_t id(world_t *world)
225 {
226#ifdef FLECS_CPP_NO_AUTO_REGISTRATION
227 ecs_assert(registered(world), ECS_INVALID_OPERATION,
228 "component '%s' must be registered before use",
229 type_name<T>());
230
231 flecs::entity_t c = flecs_component_ids_get(world, index());
232 ecs_assert(c != 0, ECS_INTERNAL_ERROR, nullptr);
234 "component '%s' was deleted, reregister before using",
235 type_name<T>());
236#else
237 flecs::entity_t c = flecs_component_ids_get_alive(world, index());
238 if (!c) {
239 c = register_id(world);
240 }
241#endif
242 return c;
243 }
244
245 // Return the size of a component.
246 static size_t size() {
247 return s_size;
248 }
249
250 // Return the alignment of a component.
251 static size_t alignment() {
252 return s_alignment;
253 }
254
255 // Was the component already registered?
256 static bool registered(flecs::world_t *world) {
257 ecs_assert(world != nullptr, ECS_INVALID_PARAMETER, nullptr);
258
259 if (!flecs_component_ids_get(world, index())) {
260 return false;
261 }
262
263 return true;
264 }
265
266 // This function is only used to test cross-translation-unit features. No
267 // code other than test cases should invoke this function.
268 static void reset() {
269 s_size = 0;
270 s_alignment = 0;
271 }
272
273 static int32_t index() {
274 static int32_t index_ = flecs_component_ids_index_get();
275 return index_;
276 }
277
278 static size_t s_size;
279 static size_t s_alignment;
280};
281
282// Global templated variables that hold the component identifier and other info.
283template <typename T> inline size_t type_impl<T>::s_size;
284template <typename T> inline size_t type_impl<T>::s_alignment;
285
286// Front-facing class for implicitly registering a component and obtaining
287// static component data.
288
289// Regular type.
290template <typename T>
291struct type<T, if_not_t< is_pair<T>::value >>
292 : type_impl<base_type_t<T>> { };
293
294// Pair type.
295template <typename T>
296struct type<T, if_t< is_pair<T>::value >>
297{
298 // Override the id() method to return the ID of a pair.
299 static id_t id(world_t *world = nullptr) {
300 return ecs_pair(
301 type< pair_first_t<T> >::id(world),
302 type< pair_second_t<T> >::id(world));
303 }
304};
305
306} // namespace _
307
314 using entity::entity;
315
318
325
331
338 {
339 world_ = world;
340
341 ecs_entity_desc_t desc = {};
342 desc.name = name;
343 desc.sep = "::";
344 desc.root_sep = "::";
345 desc.use_low_id = true;
346 id_ = ecs_entity_init(world, &desc);
347 }
348
356 explicit untyped_component(world_t *world, const char *name, const char *sep, const char *root_sep)
357 {
358 world_ = world;
359
360 ecs_entity_desc_t desc = {};
361 desc.name = name;
362 desc.sep = sep;
363 desc.root_sep = root_sep;
364 desc.use_low_id = true;
365 id_ = ecs_entity_init(world, &desc);
366 }
367
368protected:
369
376 if (h) {
377 return *h;
378 } else {
379 return {};
380 }
381 }
382
388 h.flags &= ECS_TYPE_HOOKS_ILLEGAL;
390 }
391
392public:
393
400 ecs_cmp_t compare_callback)
401{
402 ecs_assert(compare_callback, ECS_INVALID_PARAMETER, nullptr);
404 h.cmp = compare_callback;
405 h.flags &= ~ECS_TYPE_HOOK_CMP_ILLEGAL;
406 if(h.flags & ECS_TYPE_HOOK_EQUALS_ILLEGAL) {
407 h.flags &= ~ECS_TYPE_HOOK_EQUALS_ILLEGAL;
408 h.equals = nullptr;
409 }
410 set_hooks(h);
411 return *this;
412}
413
420 ecs_equals_t equals_callback)
421{
422 ecs_assert(equals_callback, ECS_INVALID_PARAMETER, nullptr);
424 h.equals = equals_callback;
425 h.flags &= ~ECS_TYPE_HOOK_EQUALS_ILLEGAL;
426 set_hooks(h);
427 return *this;
428}
429
430# ifdef FLECS_META
432# endif
433# ifdef FLECS_METRICS
435# endif
436};
437
443template <typename T>
456 const char *name = nullptr,
457 bool allow_tag = true,
458 flecs::id_t id = 0)
459 {
460 world_ = world;
461 id_ = _::type<T>::register_id(world, name, allow_tag, true, true, id);
462 }
463
469 template <typename Func>
470 component<T>& on_add(Func&& func) {
471 using Delegate = typename _::each_delegate<typename std::decay<Func>::type, T>;
474 "on_add hook is already set");
475 BindingCtx *ctx = get_binding_ctx(h);
476 h.on_add = Delegate::run_add;
477 ctx->on_add = FLECS_NEW(Delegate)(FLECS_FWD(func));
478 ctx->free_on_add = _::free_obj<Delegate>;
479 set_hooks(h);
480 return *this;
481 }
482
488 template <typename Func>
489 component<T>& on_remove(Func&& func) {
490 using Delegate = typename _::each_delegate<
491 typename std::decay<Func>::type, T>;
494 "on_remove hook is already set");
495 BindingCtx *ctx = get_binding_ctx(h);
496 h.on_remove = Delegate::run_remove;
497 ctx->on_remove = FLECS_NEW(Delegate)(FLECS_FWD(func));
498 ctx->free_on_remove = _::free_obj<Delegate>;
499 set_hooks(h);
500 return *this;
501 }
502
508 template <typename Func>
509 component<T>& on_set(Func&& func) {
510 using Delegate = typename _::each_delegate<
511 typename std::decay<Func>::type, T>;
514 "on_set hook is already set");
515 BindingCtx *ctx = get_binding_ctx(h);
516 h.on_set = Delegate::run_set;
517 ctx->on_set = FLECS_NEW(Delegate)(FLECS_FWD(func));
518 ctx->free_on_set = _::free_obj<Delegate>;
519 set_hooks(h);
520 return *this;
521 }
522
528 template <typename Func>
529 component<T>& on_replace(Func&& func) {
530 using Delegate = typename _::each_delegate<
531 typename std::decay<Func>::type, T, T>;
534 "on_replace hook is already set");
535 BindingCtx *ctx = get_binding_ctx(h);
536 h.on_replace = Delegate::run_replace;
537 ctx->on_replace = FLECS_NEW(Delegate)(FLECS_FWD(func));
538 ctx->free_on_replace = _::free_obj<Delegate>;
539 set_hooks(h);
540 return *this;
541 }
542
544
550 ecs_cmp_t handler = _::compare<T>();
551 ecs_assert(handler != nullptr, ECS_INVALID_OPERATION,
552 "Type does not have operator> or operator< const or is inaccessible");
553 on_compare(handler);
554 return *this;
555 }
556
557 using cmp_hook = int(*)(const T* a, const T* b, const ecs_type_info_t *ti);
558
564 component<T>& on_compare(cmp_hook callback) {
565 on_compare(reinterpret_cast<ecs_cmp_t>(callback));
566 return *this;
567 }
568
570
576 ecs_equals_t handler = _::equals<T>();
577 ecs_assert(handler != nullptr, ECS_INVALID_OPERATION,
578 "Type does not have operator== const or is inaccessible");
579 on_equals(handler);
580 return *this;
581 }
582
583 using equals_hook = bool(*)(const T* a, const T* b, const ecs_type_info_t *ti);
584
590 component<T>& on_equals(equals_hook callback) {
591 on_equals(reinterpret_cast<ecs_equals_t>(callback));
592 return *this;
593 }
594
595# ifdef FLECS_META
597# endif
598
599private:
600 using BindingCtx = _::component_binding_ctx;
601
602 BindingCtx* get_binding_ctx(flecs::type_hooks_t& h){
603 BindingCtx *result = static_cast<BindingCtx*>(h.binding_ctx);
604 if (!result) {
605 result = FLECS_NEW(BindingCtx);
606 h.binding_ctx = result;
607 h.binding_ctx_free = _::free_obj<BindingCtx>;
608 }
609 return result;
610 }
611};
612
613}
614
Meta component mixin.
void ecs_add_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Add a (component) ID to an entity.
FLECS_API const ecs_entity_t ecs_id(EcsDocDescription)
Component ID for EcsDocDescription.
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:473
#define ECS_INVALID_OPERATION
Invalid operation error code.
Definition log.h:669
#define ECS_INVALID_PARAMETER
Invalid parameter error code.
Definition log.h:671
#define ECS_INTERNAL_ERROR
Internal error code.
Definition log.h:681
FLECS_API int ecs_meta_from_desc(ecs_world_t *world, ecs_entity_t component, ecs_type_kind_t kind, const char *desc)
Populate meta information from type descriptor.
ecs_type_kind_t
Type kinds supported by meta addon.
Definition meta.h:151
const ecs_type_hooks_t * ecs_get_hooks_id(const ecs_world_t *world, ecs_entity_t component)
Get hooks for a component.
void ecs_set_hooks_id(ecs_world_t *world, ecs_entity_t component, const ecs_type_hooks_t *hooks)
Register hooks for a component.
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:385
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
Definition flecs.h:429
ecs_type_hooks_t type_hooks_t
Type hooks type.
Definition c_types.hpp:34
ecs_id_t id_t
ID type.
Definition c_types.hpp:20
ecs_entity_t entity_t
Entity type.
Definition c_types.hpp:21
ecs_world_t world_t
World type.
Definition c_types.hpp:18
transcribe_cv_t< remove_reference_t< P >, typename raw_type_t< P >::second > pair_second_t
Get pair::second from a pair while preserving cv qualifiers.
Definition pair.hpp:112
transcribe_cv_t< remove_reference_t< P >, typename raw_type_t< P >::first > pair_first_t
Get pair::first from a pair while preserving cv qualifiers.
Definition pair.hpp:108
ecs_entity_t ecs_entity_init(ecs_world_t *world, const ecs_entity_desc_t *desc)
Find or create an entity.
bool ecs_has_id(const ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Test if an entity has a component.
int(* ecs_cmp_t)(const void *a_ptr, const void *b_ptr, const ecs_type_info_t *type_info)
Compare hook to compare component instances.
Definition flecs.h:683
bool(* ecs_equals_t)(const void *a_ptr, const void *b_ptr, const ecs_type_info_t *type_info)
Equals operator hook.
Definition flecs.h:689
bool ecs_is_alive(const ecs_world_t *world, ecs_entity_t e)
Test whether an entity is alive.
Meta component mixin.
Metrics component mixin.
Int to enum.
Definition component.hpp:18
Component added to bitmask type entities.
Definition meta.h:305
Component added to enum type entities.
Definition meta.h:285
Component added to struct type entities.
Definition meta.h:264
Used with ecs_entity_init().
Definition flecs.h:1046
const char * sep
Optional custom separator for hierarchical names.
Definition flecs.h:1058
const char * root_sep
Optional, used for identifiers relative to the root.
Definition flecs.h:1062
const char * name
Name of the entity.
Definition flecs.h:1053
bool use_low_id
When set to true, a low id (typically reserved for components) will be used to create the entity,...
Definition flecs.h:1074
ecs_copy_t copy_ctor
Ctor + copy.
Definition flecs.h:961
ecs_iter_action_t on_remove
Callback that is invoked when an instance of the component is removed.
Definition flecs.h:1002
void * binding_ctx
Language binding context.
Definition flecs.h:1011
ecs_move_t move_dtor
Move + dtor.
Definition flecs.h:976
ecs_flags32_t flags
Hook flags.
Definition flecs.h:988
ecs_cmp_t cmp
Compare hook.
Definition flecs.h:979
ecs_copy_t copy
copy assignment.
Definition flecs.h:957
ecs_iter_action_t on_set
Callback that is invoked when an instance of the component is set.
Definition flecs.h:997
ecs_move_t move
move assignment.
Definition flecs.h:958
ecs_xtor_t ctor
ctor.
Definition flecs.h:955
ecs_iter_action_t on_replace
Callback that is invoked with the existing and new value before the value is assigned.
Definition flecs.h:1008
ecs_iter_action_t on_add
Callback that is invoked when an instance of a component is added.
Definition flecs.h:992
ecs_ctx_free_t binding_ctx_free
Callback to free binding_ctx.
Definition flecs.h:1015
ecs_move_t move_ctor
Ctor + move.
Definition flecs.h:964
ecs_equals_t equals
Equals hook.
Definition flecs.h:982
ecs_move_t ctor_move_dtor
Ctor + move + dtor (or move_ctor + dtor).
Definition flecs.h:970
ecs_xtor_t dtor
dtor.
Definition flecs.h:956
Type that contains component information (passed to ctors/dtors/...).
Definition flecs.h:1023
Component class.
component< T > & on_remove(Func &&func)
Register on_remove hook.
component(flecs::world_t *world, const char *name=nullptr, bool allow_tag=true, flecs::id_t id=0)
Register a component.
component< T > & on_replace(Func &&func)
Register on_replace hook.
component< T > & on_compare(cmp_hook callback)
Type-safe variant of the compare op function.
component< T > & on_add(Func &&func)
Register on_add hook.
component< T > & on_equals()
Register an operator equals hook using type T's equality operator.
component< T > & on_set(Func &&func)
Register on_set hook.
component< T > & on_equals(equals_hook callback)
Type-safe variant of the equals op function.
component< T > & on_compare()
Register an operator compare hook using type T's comparison operators.
flecs::string_view name() const
Return the entity name.
entity()
Default constructor.
Definition entity.hpp:32
Class that wraps around a flecs::id_t.
Definition decl.hpp:27
flecs::id_t id_
The raw ID value.
Definition decl.hpp:149
flecs::world_t * world_
World is optional, but guarantees that entity identifiers extracted from the ID are valid.
Definition decl.hpp:147
Test if a type is a pair.
Definition pair.hpp:98
untyped_component(flecs::world_t *world, flecs::entity_t id)
Construct from world and entity ID.
untyped_component(world_t *world, const char *name, const char *sep, const char *root_sep)
Construct from world, name, and scope separators.
untyped_component & on_compare(ecs_cmp_t compare_callback)
Register a custom compare hook for this component.
flecs::type_hooks_t get_hooks() const
Get the type hooks for this component.
untyped_component()
Default constructor.
untyped_component(flecs::entity_t id)
Construct from entity ID.
void set_hooks(flecs::type_hooks_t &h)
Set the type hooks for this component.
untyped_component(flecs::world_t *world, const char *name)
Construct from world and name.
entity()
Default constructor.
Definition entity.hpp:32
untyped_component & on_equals(ecs_equals_t equals_callback)
Register a custom equals hook for this component.
The world.
Definition world.hpp:246
enable_if_t< false==V, int > if_not_t
Convenience enable_if alias for negated conditions.
Definition utils.hpp:172
enable_if_t< V, int > if_t
Convenience enable_if alias using int as default type.
Definition utils.hpp:168