21inline const char* component_symbol_name() {
25template <>
inline const char* component_symbol_name<uint8_t>() {
28template <>
inline const char* component_symbol_name<uint16_t>() {
31template <>
inline const char* component_symbol_name<uint32_t>() {
34template <>
inline const char* component_symbol_name<uint64_t>() {
37template <>
inline const char* component_symbol_name<int8_t>() {
40template <>
inline const char* component_symbol_name<int16_t>() {
43template <>
inline const char* component_symbol_name<int32_t>() {
46template <>
inline const char* component_symbol_name<int64_t>() {
49template <>
inline const char* component_symbol_name<float>() {
52template <>
inline const char* component_symbol_name<double>() {
61void register_lifecycle_actions(
66 if constexpr (!std::is_trivial<T>::value) {
70 cl.
ctor = ctor<T>(cl.flags);
71 cl.dtor = dtor<T>(cl.flags);
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);
78 cl.ctor_move_dtor = ctor_move_dtor<T>(cl.flags);
79 cl.move_dtor = move_dtor<T>(cl.flags);
81 cl.flags &= ECS_TYPE_HOOKS_ILLEGAL;
84 if (cl.flags & (ECS_TYPE_HOOK_MOVE_ILLEGAL|ECS_TYPE_HOOK_MOVE_CTOR_ILLEGAL))
92inline ecs_cpp_type_action_t lifecycle_action() {
93 if constexpr (std::is_trivial<T>::value) {
96 return ®ister_lifecycle_actions<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>;
112 static_assert(is_pointer<T>::value ==
false,
113 "pointer types are not allowed for components");
117 bool allow_tag =
true)
122 s_alignment =
alignof(T);
123 if (is_empty<T>::value && allow_tag) {
129 static void init_builtin(
132 bool allow_tag =
true)
135 flecs_component_ids_set(
world, index(),
id);
141 const char *name =
nullptr,
142 bool allow_tag =
true,
143 bool is_component =
true,
144 bool explicit_registration =
false,
150 ecs_cpp_component_desc_t desc = {
155 component_symbol_name<T>(),
158 lifecycle_action<T>(),
161 explicit_registration
176#ifdef FLECS_CPP_NO_AUTO_REGISTRATION
178 "component '%s' must be registered before use",
184 "component '%s' was deleted, reregister before using",
189 c = register_id(
world);
196 static size_t size() {
201 static size_t alignment() {
209 if (!flecs_component_ids_get(
world, index())) {
218 static void reset() {
223 static int32_t index() {
224 static int32_t index_ = flecs_component_ids_index_get();
228 static size_t s_size;
229 static size_t s_alignment;
338 h.
flags &= ECS_TYPE_HOOKS_ILLEGAL;
354 h.
cmp = compare_callback;
355 h.
flags &= ~ECS_TYPE_HOOK_CMP_ILLEGAL;
356 if(h.
flags & ECS_TYPE_HOOK_EQUALS_ILLEGAL) {
357 h.
flags &= ~ECS_TYPE_HOOK_EQUALS_ILLEGAL;
374 h.
equals = equals_callback;
375 h.
flags &= ~ECS_TYPE_HOOK_EQUALS_ILLEGAL;
406 const char *
name =
nullptr,
407 bool allow_tag =
true,
419 template <
typename Func>
424 "on_add hook is already set");
426 h.
on_add = Delegate::run_add;
427 ctx->on_add = FLECS_NEW(Delegate)(FLECS_FWD(func));
428 ctx->free_on_add = _::free_obj<Delegate>;
438 template <
typename Func>
441 typename std::decay<Func>::type, T>;
444 "on_remove hook is already set");
447 ctx->on_remove = FLECS_NEW(Delegate)(FLECS_FWD(func));
448 ctx->free_on_remove = _::free_obj<Delegate>;
458 template <
typename Func>
461 typename std::decay<Func>::type, T>;
464 "on_set hook is already set");
466 h.
on_set = Delegate::run_set;
467 ctx->on_set = FLECS_NEW(Delegate)(FLECS_FWD(func));
468 ctx->free_on_set = _::free_obj<Delegate>;
478 template <
typename Func>
481 typename std::decay<Func>::type, T, T>;
484 "on_replace hook is already set");
487 ctx->on_replace = FLECS_NEW(Delegate)(FLECS_FWD(func));
488 ctx->free_on_replace = _::free_obj<Delegate>;
502 "Type does not have operator> or operator< const or is inaccessible");
507 using cmp_hook = int(*)(
const T* a,
const T* b,
const ecs_type_info_t *ti);
528 "Type does not have operator== const or is inaccessible");
533 using equals_hook = bool(*)(
const T* a,
const T* b,
const ecs_type_info_t *ti);
553 BindingCtx *result =
static_cast<BindingCtx*
>(h.
binding_ctx);
555 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.
#define ECS_INVALID_OPERATION
Invalid operation error code.
#define ECS_INVALID_PARAMETER
Invalid parameter error code.
#define ECS_INTERNAL_ERROR
Internal error code.
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.
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
ecs_entity_t entity_t
Entity type.
ecs_world_t world_t
World type.
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.
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.
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 the 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.
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.
Class that wraps around a flecs::id_t.
flecs::world world() const
Get the world.
flecs::id_t id_
The raw ID value.
flecs::world_t * world_
World is optional, but guarantees that entity identifiers extracted from the ID are valid.
Test if a type is a pair.
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.
enable_if_t< false==V, int > if_not_t
Convenience enable_if alias for negated conditions.
enable_if_t< V, int > if_t
Convenience enable_if alias using int as default type.