23inline const char* symbol_name() {
24 static const size_t len = ECS_FUNC_TYPE_LEN(
const char*, symbol_name, ECS_FUNC_NAME);
25 static char result[len + 1] = {};
26 static const char* cppSymbolName = ecs_cpp_get_symbol_name(result, type_name<T>(), len);
30template <>
inline const char* symbol_name<uint8_t>() {
33template <>
inline const char* symbol_name<uint16_t>() {
36template <>
inline const char* symbol_name<uint32_t>() {
39template <>
inline const char* symbol_name<uint64_t>() {
42template <>
inline const char* symbol_name<int8_t>() {
45template <>
inline const char* symbol_name<int16_t>() {
48template <>
inline const char* symbol_name<int32_t>() {
51template <>
inline const char* symbol_name<int64_t>() {
54template <>
inline const char* symbol_name<float>() {
57template <>
inline const char* symbol_name<double>() {
66void register_lifecycle_actions(
71 if constexpr (!std::is_trivial<T>::value) {
75 cl.
ctor = ctor<T>(cl.flags);
76 cl.dtor = dtor<T>(cl.flags);
78 cl.copy = copy<T>(cl.flags);
79 cl.copy_ctor = copy_ctor<T>(cl.flags);
80 cl.move = move<T>(cl.flags);
81 cl.move_ctor = move_ctor<T>(cl.flags);
83 cl.ctor_move_dtor = ctor_move_dtor<T>(cl.flags);
84 cl.move_dtor = move_dtor<T>(cl.flags);
86 cl.flags &= ECS_TYPE_HOOKS_ILLEGAL;
89 if (cl.flags & (ECS_TYPE_HOOK_MOVE_ILLEGAL|ECS_TYPE_HOOK_MOVE_CTOR_ILLEGAL))
98 static_assert(is_pointer<T>::value ==
false,
99 "pointer types are not allowed for components");
103 bool allow_tag =
true)
105 s_index = flecs_component_ids_index_get();
106 s_allow_tag = allow_tag;
108 s_alignment =
alignof(T);
109 if (is_empty<T>::value && allow_tag) {
115 static void init_builtin(
116 flecs::world_t *
world,
118 bool allow_tag =
true)
121 flecs_component_ids_set(
world, s_index,
id);
125 static entity_t register_id(
127 const char *name =
nullptr,
128 bool allow_tag =
true,
129 bool is_component =
true,
130 bool explicit_registration =
false,
138 ecs_assert(s_index != 0, ECS_INTERNAL_ERROR, NULL);
141 bool registered =
false, existing =
false;
143 flecs::entity_t c = ecs_cpp_component_register(
144 world,
id, s_index, name, type_name<T>(),
145 symbol_name<T>(), size(), alignment(),
146 is_component, explicit_registration, ®istered, &existing);
154 if (size() && !existing) {
155 register_lifecycle_actions<T>(
world, c);
161 #if FLECS_CPP_ENUM_REFLECTION_SUPPORT
162 _::init_enum<T>(
world, c);
172 static entity_t
id(world_t *
world)
174#ifdef FLECS_CPP_NO_AUTO_REGISTRATION
176 "component '%s' must be registered before use",
179 flecs::entity_t c = flecs_component_ids_get(
world, s_index);
182 "component '%s' was deleted, reregister before using",
185 flecs::entity_t c = flecs_component_ids_get_alive(
world, s_index);
187 c = register_id(
world);
194 static size_t size() {
195 ecs_assert(s_index != 0, ECS_INTERNAL_ERROR, NULL);
200 static size_t alignment() {
201 ecs_assert(s_index != 0, ECS_INTERNAL_ERROR, NULL);
206 static bool registered(flecs::world_t *
world) {
213 if (!flecs_component_ids_get(
world, s_index)) {
222 static void reset() {
229 static int32_t s_index;
230 static size_t s_size;
231 static size_t s_alignment;
232 static bool s_allow_tag;
254 static id_t
id(world_t *
world =
nullptr) {
269 using entity::entity;
311 h.
flags &= ECS_TYPE_HOOKS_ILLEGAL;
320 ecs_assert(compare_callback, ECS_INVALID_PARAMETER, NULL);
322 h.
cmp = compare_callback;
323 h.
flags &= ~ECS_TYPE_HOOK_CMP_ILLEGAL;
324 if(h.
flags & ECS_TYPE_HOOK_EQUALS_ILLEGAL) {
325 h.
flags &= ~ECS_TYPE_HOOK_EQUALS_ILLEGAL;
335 ecs_assert(equals_callback, ECS_INVALID_PARAMETER, NULL);
337 h.
equals = equals_callback;
338 h.
flags &= ~ECS_TYPE_HOOK_EQUALS_ILLEGAL;
347# include "mixins/metrics/untyped_component.inl"
368 flecs::world_t *
world,
369 const char *
name =
nullptr,
370 bool allow_tag =
true,
378 template <
typename Func>
383 "on_add hook is already set");
385 h.
on_add = Delegate::run_add;
386 ctx->on_add = FLECS_NEW(Delegate)(FLECS_FWD(func));
387 ctx->free_on_add = _::free_obj<Delegate>;
393 template <
typename Func>
396 typename std::decay<Func>::type, T>;
399 "on_remove hook is already set");
402 ctx->on_remove = FLECS_NEW(Delegate)(FLECS_FWD(func));
403 ctx->free_on_remove = _::free_obj<Delegate>;
409 template <
typename Func>
412 typename std::decay<Func>::type, T>;
415 "on_set hook is already set");
417 h.
on_set = Delegate::run_set;
418 ctx->on_set = FLECS_NEW(Delegate)(FLECS_FWD(func));
419 ctx->free_on_set = _::free_obj<Delegate>;
425 template <
typename Func>
428 typename std::decay<Func>::type, T, T>;
431 "on_set hook is already set");
434 ctx->on_replace = FLECS_NEW(Delegate)(FLECS_FWD(func));
435 ctx->free_on_replace = _::free_obj<Delegate>;
441 using untyped_component::on_compare;
444 ecs_assert(handler != NULL, ECS_INVALID_OPERATION,
445 "Type does not have operator> or operator< const or is inaccessible");
453 on_compare(
reinterpret_cast<ecs_cmp_t>(callback));
458 using untyped_component::on_equals;
461 ecs_assert(handler != NULL, ECS_INVALID_OPERATION,
462 "Type does not have operator== const or is inaccessible");
475# include "mixins/meta/component.inl"
482 BindingCtx *result =
static_cast<BindingCtx*
>(h.
binding_ctx);
484 result = FLECS_NEW(BindingCtx);
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.
const ecs_type_hooks_t * ecs_get_hooks_id(const ecs_world_t *world, ecs_entity_t component)
Get hooks for component.
void ecs_set_hooks_id(ecs_world_t *world, ecs_entity_t component, const ecs_type_hooks_t *hooks)
Register hooks for component.
ecs_id_t ecs_entity_t
An entity identifier.
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
transcribe_cv_t< remove_reference_t< P >, typename raw_type_t< P >::second > pair_second_t
Get pair::second from pair while preserving cv qualifiers.
transcribe_cv_t< remove_reference_t< P >, typename raw_type_t< P >::first > pair_first_t
Get pair::first from pair while preserving cv qualifiers.
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.
bool(* ecs_equals_t)(const void *a_ptr, const void *b_ptr, const ecs_type_info_t *type_info)
Equals operator hook.
bool ecs_is_alive(const ecs_world_t *world, ecs_entity_t e)
Test whether an entity is alive.
Used with ecs_entity_init().
const char * sep
Optional custom separator for hierarchical names.
const char * root_sep
Optional, used for identifiers relative to root.
const char * name
Name of the entity.
bool use_low_id
When set to true, a low id (typically reserved for components) will be used to create the entity,...
ecs_iter_action_t on_remove
Callback that is invoked when an instance of the component is removed.
void * binding_ctx
Language binding context.
ecs_flags32_t flags
Hook flags.
ecs_cmp_t cmp
Compare hook.
ecs_iter_action_t on_set
Callback that is invoked when an instance of the component is set.
ecs_iter_action_t on_replace
Callback that is invoked with the existing and new value before the value is assigned.
ecs_iter_action_t on_add
Callback that is invoked when an instance of a component is added.
ecs_ctx_free_t binding_ctx_free
Callback to free binding_ctx.
ecs_equals_t equals
Equals hook.
Type that contains component information (passed to ctors/dtors/...)
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.
bool(*)(const T *a, const T *b, const ecs_type_info_t *ti) equals_hook
Type safe variant of equals op function.
component< T > & on_add(Func &&func)
Register on_add hook.
component< T > & on_set(Func &&func)
Register on_set hook.
int(*)(const T *a, const T *b, const ecs_type_info_t *ti) cmp_hook
Type safe variant of compare op function.
flecs::string_view name() const
Return the entity name.
Class that wraps around a flecs::id_t.