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
6#pragma once
7
16namespace flecs {
17
18namespace _ {
19
20template <typename T>
21inline const char* component_symbol_name() {
22 return nullptr;
23}
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";
39}
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";
51}
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_trivial<T>::value) {
67 // If the component is non-trivial, register component lifecycle actions.
68 // Depending on the type, not all callbacks may be available.
70 cl.ctor = ctor<T>(cl.flags);
71 cl.dtor = dtor<T>(cl.flags);
72
73 cl.copy = copy<T>(cl.flags);
74 cl.copy_ctor = copy_ctor<T>(cl.flags);
75 cl.move = move<T>(cl.flags);
76 cl.move_ctor = move_ctor<T>(cl.flags);
77
78 cl.ctor_move_dtor = ctor_move_dtor<T>(cl.flags);
79 cl.move_dtor = move_dtor<T>(cl.flags);
80
81 cl.flags &= ECS_TYPE_HOOKS_ILLEGAL;
83
84 if (cl.flags & (ECS_TYPE_HOOK_MOVE_ILLEGAL|ECS_TYPE_HOOK_MOVE_CTOR_ILLEGAL))
85 {
86 ecs_add_id(world, component, flecs::Sparse);
87 }
88 }
89}
90
91template <typename T>
92inline ecs_cpp_type_action_t lifecycle_action() {
93 if constexpr (std::is_trivial<T>::value) {
94 return nullptr;
95 } else {
96 return &register_lifecycle_actions<T>;
97 }
98}
99
100template <typename T>
101inline ecs_cpp_type_action_t enum_action() {
102#if FLECS_CPP_ENUM_REFLECTION_SUPPORT
103 if constexpr (is_enum_v<T>) {
104 return &_::init_enum<T>;
105 } else {
106 return nullptr;
107 }
108#else
109 return nullptr;
110#endif
111}
112
113template <typename T>
114struct type_impl {
115 static_assert(is_pointer<T>::value == false,
116 "pointer types are not allowed for components");
117
118 // Initialize the component identifier.
119 static void init(
120 bool allow_tag = true)
121 {
122 index(); // Make sure the global component index is initialized.
123
124 s_size = sizeof(T);
125 s_alignment = alignof(T);
126 if (is_empty<T>::value && allow_tag) {
127 s_size = 0;
128 s_alignment = 0;
129 }
130 }
131
132 static void init_builtin(
135 bool allow_tag = true)
136 {
137 init(allow_tag);
138 flecs_component_ids_set(world, index(), id);
139 }
140
141 // Register the component ID.
142 static entity_t register_id(
143 world_t *world, // The world
144 const char *name = nullptr, // User-provided name (overrides typename)
145 bool allow_tag = true, // Register empty types as zero-sized components
146 bool is_component = true, // Add flecs::Component to the result
147 bool explicit_registration = false, // Entered from world.component<T>()?
148 flecs::id_t id = 0) // User-provided component ID
149 {
150 init(allow_tag);
151 ecs_assert(index() != 0, ECS_INTERNAL_ERROR, NULL);
152
153 ecs_cpp_component_desc_t desc = {
154 id,
155 index(),
156 name,
157 type_name<T>(),
158 component_symbol_name<T>(),
159 size(),
160 alignment(),
161 lifecycle_action<T>(),
162 enum_action<T>(),
163 is_component,
164 explicit_registration
165 };
166
167 flecs::entity_t c = ecs_cpp_component_register(world, &desc);
168
169 ecs_assert(c != 0, ECS_INTERNAL_ERROR, NULL);
170
171 return c;
172 }
173
174 // Get the type (component) ID.
175 // If the type was not yet registered and automatic registration is allowed,
176 // this function will also register the type.
177 static entity_t id(world_t *world)
178 {
179#ifdef FLECS_CPP_NO_AUTO_REGISTRATION
181 "component '%s' must be registered before use",
182 type_name<T>());
183
184 flecs::entity_t c = flecs_component_ids_get(world, index());
185 ecs_assert(c != 0, ECS_INTERNAL_ERROR, NULL);
187 "component '%s' was deleted, reregister before using",
188 type_name<T>());
189#else
190 flecs::entity_t c = flecs_component_ids_get_alive(world, index());
191 if (!c) {
192 c = register_id(world);
193 }
194#endif
195 return c;
196 }
197
198 // Return the size of a component.
199 static size_t size() {
200 return s_size;
201 }
202
203 // Return the alignment of a component.
204 static size_t alignment() {
205 return s_alignment;
206 }
207
208 // Was the component already registered?
209 static bool registered(flecs::world_t *world) {
210 ecs_assert(world != nullptr, ECS_INVALID_PARAMETER, NULL);
211
212 if (!flecs_component_ids_get(world, index())) {
213 return false;
214 }
215
216 return true;
217 }
218
219 // This function is only used to test cross-translation-unit features. No
220 // code other than test cases should invoke this function.
221 static void reset() {
222 s_size = 0;
223 s_alignment = 0;
224 }
225
226 static int32_t index() {
227 static int32_t index_ = flecs_component_ids_index_get();
228 return index_;
229 }
230
231 static size_t s_size;
232 static size_t s_alignment;
233};
234
235// Global templated variables that hold the component identifier and other info.
236template <typename T> inline size_t type_impl<T>::s_size;
237template <typename T> inline size_t type_impl<T>::s_alignment;
238
239// Front-facing class for implicitly registering a component and obtaining
240// static component data.
241
242// Regular type.
243template <typename T>
244struct type<T, if_not_t< is_pair<T>::value >>
245 : type_impl<base_type_t<T>> { };
246
247// Pair type.
248template <typename T>
249struct type<T, if_t< is_pair<T>::value >>
250{
251 // Override the id() method to return the ID of a pair.
252 static id_t id(world_t *world = nullptr) {
253 return ecs_pair(
254 type< pair_first_t<T> >::id(world),
255 type< pair_second_t<T> >::id(world));
256 }
257};
258
259} // namespace _
260
267 using entity::entity;
268
271
278
284
291 {
292 world_ = world;
293
294 ecs_entity_desc_t desc = {};
295 desc.name = name;
296 desc.sep = "::";
297 desc.root_sep = "::";
298 desc.use_low_id = true;
299 id_ = ecs_entity_init(world, &desc);
300 }
301
309 explicit untyped_component(world_t *world, const char *name, const char *sep, const char *root_sep)
310 {
311 world_ = world;
312
313 ecs_entity_desc_t desc = {};
314 desc.name = name;
315 desc.sep = sep;
316 desc.root_sep = root_sep;
317 desc.use_low_id = true;
318 id_ = ecs_entity_init(world, &desc);
319 }
320
321protected:
322
329 if (h) {
330 return *h;
331 } else {
332 return {};
333 }
334 }
335
341 h.flags &= ECS_TYPE_HOOKS_ILLEGAL;
343 }
344
345public:
346
353 ecs_cmp_t compare_callback)
354{
355 ecs_assert(compare_callback, ECS_INVALID_PARAMETER, NULL);
357 h.cmp = compare_callback;
358 h.flags &= ~ECS_TYPE_HOOK_CMP_ILLEGAL;
359 if(h.flags & ECS_TYPE_HOOK_EQUALS_ILLEGAL) {
360 h.flags &= ~ECS_TYPE_HOOK_EQUALS_ILLEGAL;
361 h.equals = NULL;
362 }
363 set_hooks(h);
364 return *this;
365}
366
373 ecs_equals_t equals_callback)
374{
375 ecs_assert(equals_callback, ECS_INVALID_PARAMETER, NULL);
377 h.equals = equals_callback;
378 h.flags &= ~ECS_TYPE_HOOK_EQUALS_ILLEGAL;
379 set_hooks(h);
380 return *this;
381}
382
383# ifdef FLECS_META
385# endif
386# ifdef FLECS_METRICS
388# endif
389};
390
396template <typename T>
409 const char *name = nullptr,
410 bool allow_tag = true,
411 flecs::id_t id = 0)
412 {
413 world_ = world;
414 id_ = _::type<T>::register_id(world, name, allow_tag, true, true, id);
415 }
416
422 template <typename Func>
423 component<T>& on_add(Func&& func) {
424 using Delegate = typename _::each_delegate<typename std::decay<Func>::type, T>;
427 "on_add hook is already set");
428 BindingCtx *ctx = get_binding_ctx(h);
429 h.on_add = Delegate::run_add;
430 ctx->on_add = FLECS_NEW(Delegate)(FLECS_FWD(func));
431 ctx->free_on_add = _::free_obj<Delegate>;
432 set_hooks(h);
433 return *this;
434 }
435
441 template <typename Func>
442 component<T>& on_remove(Func&& func) {
443 using Delegate = typename _::each_delegate<
444 typename std::decay<Func>::type, T>;
447 "on_remove hook is already set");
448 BindingCtx *ctx = get_binding_ctx(h);
449 h.on_remove = Delegate::run_remove;
450 ctx->on_remove = FLECS_NEW(Delegate)(FLECS_FWD(func));
451 ctx->free_on_remove = _::free_obj<Delegate>;
452 set_hooks(h);
453 return *this;
454 }
455
461 template <typename Func>
462 component<T>& on_set(Func&& func) {
463 using Delegate = typename _::each_delegate<
464 typename std::decay<Func>::type, T>;
467 "on_set hook is already set");
468 BindingCtx *ctx = get_binding_ctx(h);
469 h.on_set = Delegate::run_set;
470 ctx->on_set = FLECS_NEW(Delegate)(FLECS_FWD(func));
471 ctx->free_on_set = _::free_obj<Delegate>;
472 set_hooks(h);
473 return *this;
474 }
475
481 template <typename Func>
482 component<T>& on_replace(Func&& func) {
483 using Delegate = typename _::each_delegate<
484 typename std::decay<Func>::type, T, T>;
487 "on_replace hook is already set");
488 BindingCtx *ctx = get_binding_ctx(h);
489 h.on_replace = Delegate::run_replace;
490 ctx->on_replace = FLECS_NEW(Delegate)(FLECS_FWD(func));
491 ctx->free_on_replace = _::free_obj<Delegate>;
492 set_hooks(h);
493 return *this;
494 }
495
497
503 ecs_cmp_t handler = _::compare<T>();
504 ecs_assert(handler != NULL, ECS_INVALID_OPERATION,
505 "Type does not have operator> or operator< const or is inaccessible");
506 on_compare(handler);
507 return *this;
508 }
509
510 using cmp_hook = int(*)(const T* a, const T* b, const ecs_type_info_t *ti);
511
517 component<T>& on_compare(cmp_hook callback) {
518 on_compare(reinterpret_cast<ecs_cmp_t>(callback));
519 return *this;
520 }
521
523
529 ecs_equals_t handler = _::equals<T>();
530 ecs_assert(handler != NULL, ECS_INVALID_OPERATION,
531 "Type does not have operator== const or is inaccessible");
532 on_equals(handler);
533 return *this;
534 }
535
536 using equals_hook = bool(*)(const T* a, const T* b, const ecs_type_info_t *ti);
537
543 component<T>& on_equals(equals_hook callback) {
544 on_equals(reinterpret_cast<ecs_equals_t>(callback));
545 return *this;
546 }
547
548# ifdef FLECS_META
550# endif
551
552private:
553 using BindingCtx = _::component_binding_ctx;
554
555 BindingCtx* get_binding_ctx(flecs::type_hooks_t& h){
556 BindingCtx *result = static_cast<BindingCtx*>(h.binding_ctx);
557 if (!result) {
558 result = FLECS_NEW(BindingCtx);
559 h.binding_ctx = result;
560 h.binding_ctx_free = _::free_obj<BindingCtx>;
561 }
562 return result;
563 }
564};
565
566}
567
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.
#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
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_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.
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.
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_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_flags32_t flags
Hook flags.
Definition flecs.h:988
ecs_cmp_t cmp
Compare hook.
Definition flecs.h:979
ecs_iter_action_t on_set
Callback that is invoked when an instance of the component is set.
Definition flecs.h:997
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_equals_t equals
Equals hook.
Definition flecs.h:982
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.
Definition entity.hpp:30
entity()
Default constructor.
Definition entity.hpp:32
Class that wraps around a flecs::id_t.
Definition decl.hpp:27
flecs::world world() const
Get the world.
Definition impl.hpp:58
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 class.
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.
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