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_trivially_default_constructible<T>::value &&
67 std::is_trivially_copyable<T>::value))
72 cl.
ctor = ctor<T>(cl.flags);
73 cl.dtor = dtor<T>(cl.flags);
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);
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;
86 if (cl.flags & (ECS_TYPE_HOOK_MOVE_ILLEGAL|ECS_TYPE_HOOK_MOVE_CTOR_ILLEGAL))
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)
100 return ®ister_lifecycle_actions<T>;
105template <
typename T,
typename =
void>
110 flecs_meta_cpp_desc(static_cast<T*>(
nullptr))))> : std::true_type {};
114inline ecs_cpp_type_action_t enum_action() {
115#if FLECS_CPP_ENUM_REFLECTION_SUPPORT
121 if constexpr (is_enum_v<T>) {
122 return &_::init_enum<T>;
135 (void)world; (void)component;
136 if constexpr (has_cpp_meta_desc<T>::value) {
138 if (kind == EcsStructType &&
ecs_has_id(world, component,
142 if (kind == EcsEnumType &&
ecs_has_id(world, component,
146 if (kind == EcsBitmaskType &&
ecs_has_id(world, component,
151 flecs_meta_cpp_desc(
static_cast<T*
>(
nullptr)));
158 static_assert(is_pointer<T>::value ==
false,
159 "pointer types are not allowed for components");
163 bool allow_tag =
true)
165#ifdef FLECS_MULTI_WORLD
170 s_alignment =
alignof(T);
171 if (is_empty<T>::value && allow_tag) {
177 static void init_builtin(
180 bool allow_tag =
true)
183#ifdef FLECS_MULTI_WORLD
184 flecs_component_ids_set(
world, index(),
id);
194 const char *name =
nullptr,
195 bool allow_tag =
true,
196 bool is_component =
true,
197 bool explicit_registration =
false,
201#ifdef FLECS_MULTI_WORLD
207#ifdef FLECS_MULTI_WORLD
214 component_symbol_name<T>(),
217 lifecycle_action<T>(),
220 explicit_registration
239 if constexpr (policy == flecs::on_instantiate::override) {
242 if constexpr (policy == flecs::on_instantiate::inherit) {
246 if constexpr (policy == flecs::on_instantiate::dont_inherit) {
253 register_cpp_meta<T>(
world, c);
264#ifdef FLECS_CPP_NO_AUTO_REGISTRATION
266 "component '%s' must be registered before use",
269#ifdef FLECS_MULTI_WORLD
276 "component '%s' was deleted, reregister before using",
279#ifdef FLECS_MULTI_WORLD
288 c = register_id(
world);
295 static size_t size() {
300 static size_t alignment() {
308#ifdef FLECS_MULTI_WORLD
309 if (!flecs_component_ids_get(
world, index())) {
321 static void reset() {
324#ifndef FLECS_MULTI_WORLD
329#ifdef FLECS_MULTI_WORLD
330 static int32_t index() {
331 static int32_t index_ = flecs_component_ids_index_get();
338 static size_t s_size;
339 static size_t s_alignment;
345#ifndef FLECS_MULTI_WORLD
377 using entity::entity;
451 h.
flags &= ECS_TYPE_HOOKS_ILLEGAL;
467 h.
cmp = compare_callback;
468 h.
flags &= ~ECS_TYPE_HOOK_CMP_ILLEGAL;
469 if(h.
flags & ECS_TYPE_HOOK_EQUALS_ILLEGAL) {
470 h.
flags &= ~ECS_TYPE_HOOK_EQUALS_ILLEGAL;
487 h.
equals = equals_callback;
488 h.
flags &= ~ECS_TYPE_HOOK_EQUALS_ILLEGAL;
519 const char *name =
nullptr,
520 bool allow_tag =
true,
532 template <
typename Func>
537 "on_add hook is already set");
539 h.
on_add = Delegate::run_add;
540 ctx->on_add = FLECS_NEW(Delegate)(FLECS_FWD(func));
541 ctx->free_on_add = _::free_obj<Delegate>;
551 template <
typename Func>
554 typename std::decay<Func>::type, T>;
557 "on_remove hook is already set");
560 ctx->on_remove = FLECS_NEW(Delegate)(FLECS_FWD(func));
561 ctx->free_on_remove = _::free_obj<Delegate>;
571 template <
typename Func>
574 typename std::decay<Func>::type, T>;
577 "on_set hook is already set");
579 h.
on_set = Delegate::run_set;
580 ctx->on_set = FLECS_NEW(Delegate)(FLECS_FWD(func));
581 ctx->free_on_set = _::free_obj<Delegate>;
591 template <
typename Func>
594 typename std::decay<Func>::type, T, T>;
597 "on_replace hook is already set");
600 ctx->on_replace = FLECS_NEW(Delegate)(FLECS_FWD(func));
601 ctx->free_on_replace = _::free_obj<Delegate>;
615 template <
typename Func>
618 typename std::decay<Func>::type, T>;
621 "on_validate hook is already set");
624 ctx->on_validate = FLECS_NEW(Delegate)(FLECS_FWD(func));
625 ctx->free_on_validate = _::free_obj<Delegate>;
630 using untyped_component::on_compare;
639 "Type does not have operator> or operator< const or is inaccessible");
644 using cmp_hook = int(*)(
const T* a,
const T* b,
const ecs_type_info_t *ti);
652 on_compare(
reinterpret_cast<ecs_cmp_t>(callback));
656 using untyped_component::on_equals;
665 "Type does not have operator== const or is inaccessible");
670 using equals_hook = bool(*)(
const T* a,
const T* b,
const ecs_type_info_t *ti);
690 BindingCtx *result =
static_cast<BindingCtx*
>(h.
binding_ctx);
692 result = FLECS_NEW(BindingCtx);
FLECS_API ecs_entity_t ecs_cpp_component_register(ecs_world_t *world, const ecs_cpp_component_desc_t *desc)
Register a C++ component.
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.
#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.
bool ecs_has_id(const ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Test if an entity has a component.
#define ecs_add_pair(world, subject, first, second)
Add a pair to 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.
Component added to bitmask type entities.
Component added to enum type entities.
Component added to struct type entities.
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_on_validate_t on_validate
Callback that is invoked before the on_set/OnSet hooks and observers are invoked.
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_validate(Func &&func)
Register on_validate 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.
Trait that marks a component as DontFragment at compile time.
Class that wraps around a flecs::id_t.
Test if a type is a pair.
Trait that assigns an OnInstantiate policy to a component at compile time.
Trait that marks a component as Sparse at compile time.
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.
on_instantiate
OnInstantiate policies that can be assigned to a component at compile time with the flecs::on_instant...