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>;
115 static_assert(is_pointer<T>::value ==
false,
116 "pointer types are not allowed for components");
120 bool allow_tag =
true)
125 s_alignment =
alignof(T);
126 if (is_empty<T>::value && allow_tag) {
132 static void init_builtin(
135 bool allow_tag =
true)
138 flecs_component_ids_set(
world, index(),
id);
144 const char *name =
nullptr,
145 bool allow_tag =
true,
146 bool is_component =
true,
147 bool explicit_registration =
false,
153 ecs_cpp_component_desc_t desc = {
158 component_symbol_name<T>(),
161 lifecycle_action<T>(),
164 explicit_registration
179#ifdef FLECS_CPP_NO_AUTO_REGISTRATION
181 "component '%s' must be registered before use",
187 "component '%s' was deleted, reregister before using",
192 c = register_id(
world);
199 static size_t size() {
204 static size_t alignment() {
212 if (!flecs_component_ids_get(
world, index())) {
221 static void reset() {
226 static int32_t index() {
227 static int32_t index_ = flecs_component_ids_index_get();
231 static size_t s_size;
232 static size_t s_alignment;
341 h.
flags &= ECS_TYPE_HOOKS_ILLEGAL;
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;
377 h.
equals = equals_callback;
378 h.
flags &= ~ECS_TYPE_HOOK_EQUALS_ILLEGAL;
409 const char *
name =
nullptr,
410 bool allow_tag =
true,
422 template <
typename Func>
427 "on_add hook is already set");
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>;
441 template <
typename Func>
444 typename std::decay<Func>::type, T>;
447 "on_remove hook is already set");
450 ctx->on_remove = FLECS_NEW(Delegate)(FLECS_FWD(func));
451 ctx->free_on_remove = _::free_obj<Delegate>;
461 template <
typename Func>
464 typename std::decay<Func>::type, T>;
467 "on_set hook is already set");
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>;
481 template <
typename Func>
484 typename std::decay<Func>::type, T, T>;
487 "on_replace hook is already set");
490 ctx->on_replace = FLECS_NEW(Delegate)(FLECS_FWD(func));
491 ctx->free_on_replace = _::free_obj<Delegate>;
505 "Type does not have operator> or operator< const or is inaccessible");
510 using cmp_hook = int(*)(
const T* a,
const T* b,
const ecs_type_info_t *ti);
531 "Type does not have operator== const or is inaccessible");
536 using equals_hook = bool(*)(
const T* a,
const T* b,
const ecs_type_info_t *ti);
556 BindingCtx *result =
static_cast<BindingCtx*
>(h.
binding_ctx);
558 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.