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(
65 (void)world; (void)component;
66 if constexpr (!(std::is_trivially_default_constructible<T>::value &&
67 std::is_trivially_copyable<T>::value))
75 cl.
copy = transfer<T, false, false>(cl.
flags);
77 cl.
move = transfer<T, true, false>(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>;
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
169 s_has_data = !is_empty<T>::value || !allow_tag;
172 static void init_builtin(
175 bool allow_tag =
true)
178#ifdef FLECS_MULTI_WORLD
179 flecs_component_ids_set(
world, index(),
id);
189 const char *name =
nullptr,
190 bool allow_tag =
true,
191 bool is_component =
true,
192 bool explicit_registration =
false,
196#ifdef FLECS_MULTI_WORLD
202#ifdef FLECS_MULTI_WORLD
209 component_symbol_name<T>(),
212 lifecycle_action<T>(),
215 explicit_registration
230 if constexpr (flecs::on_instantiate_trait<T>::declared) {
232 flecs::on_instantiate_trait<T>::value;
234 if constexpr (policy == flecs::on_instantiate::override) {
237 if constexpr (policy == flecs::on_instantiate::inherit) {
241 if constexpr (policy == flecs::on_instantiate::dont_inherit) {
248 register_cpp_meta<T>(
world, c);
259#ifdef FLECS_CPP_NO_AUTO_REGISTRATION
261 "component '%s' must be registered before use",
264#ifdef FLECS_MULTI_WORLD
271 "component '%s' was deleted, reregister before using",
274#ifdef FLECS_MULTI_WORLD
283 c = register_id(
world);
290 static size_t size() {
291 return s_has_data ?
sizeof(T) : 0;
295 static size_t alignment() {
296 return s_has_data ?
alignof(T) : 0;
303#ifdef FLECS_MULTI_WORLD
304 if (!flecs_component_ids_get(
world, index())) {
316 static void reset() {
318#ifndef FLECS_MULTI_WORLD
323#ifdef FLECS_MULTI_WORLD
324 static int32_t index() {
325 static int32_t index_ = flecs_component_ids_index_get();
332 static bool s_has_data;
336template <
typename T>
inline bool type_impl<T>::s_has_data;
337#ifndef FLECS_MULTI_WORLD
443 h.
flags &= ECS_TYPE_HOOKS_ILLEGAL;
459 h.
cmp = compare_callback;
460 h.
flags &= ~ECS_TYPE_HOOK_CMP_ILLEGAL;
461 if(h.
flags & ECS_TYPE_HOOK_EQUALS_ILLEGAL) {
462 h.
flags &= ~ECS_TYPE_HOOK_EQUALS_ILLEGAL;
479 h.
equals = equals_callback;
480 h.
flags &= ~ECS_TYPE_HOOK_EQUALS_ILLEGAL;
511 const char *
name =
nullptr,
512 bool allow_tag =
true,
524 template <
typename Func>
528 BindingCtx::OnAdd>(FLECS_FWD(func));
536 template <
typename Func>
540 BindingCtx::OnRemove>(FLECS_FWD(func));
548 template <
typename Func>
552 BindingCtx::OnSet>(FLECS_FWD(func));
560 template <
typename Func>
564 BindingCtx::OnReplace>(FLECS_FWD(func));
576 template <
typename Func>
580 BindingCtx::OnValidate>(FLECS_FWD(func));
592 "Type does not have operator> or operator< const or is inaccessible");
597 using cmp_hook = int(*)(
const T* a,
const T* b,
const ecs_type_info_t *ti);
618 "Type does not have operator== const or is inaccessible");
623 using equals_hook = bool(*)(
const T* a,
const T* b,
const ecs_type_info_t *ti);
642 template <
typename Delegate, auto Hook, BindingCtx::Hook Slot,
typename Func>
646 "component hook is already set");
647 if constexpr (std::is_same_v<Delegate,
650 h.*Hook = Delegate::run;
652 h.*Hook = Delegate::template run_hook<Slot>;
654 BindingCtx *ctx = get_binding_ctx(h);
655 ctx->callbacks[Slot] = FLECS_NEW(Delegate)(FLECS_FWD(func));
656 ctx->free[Slot] = _::free_obj<Delegate>;
662 BindingCtx *result =
static_cast<BindingCtx*
>(h.
binding_ctx);
664 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_type_hooks_t type_hooks_t
Type hooks type.
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.
bool(*) ecs_equals_t(const void *a_ptr, const void *b_ptr, const ecs_type_info_t *type_info)
Equals operator hook.
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_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_copy_t copy_ctor
Ctor + copy.
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_move_t move_dtor
Move + dtor.
ecs_flags32_t flags
Hook flags.
ecs_cmp_t cmp
Compare hook.
ecs_copy_t copy
copy assignment.
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_move_t move
move assignment.
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_move_t move_ctor
Ctor + move.
ecs_equals_t equals
Equals hook.
ecs_move_t ctor_move_dtor
Ctor + move + dtor (or move_ctor + dtor).
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.
flecs::string_view name() const
Return the entity name.
entity()
Default constructor.
Class that wraps around a flecs::id_t.
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.
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.
entity()
Default constructor.
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...