31#if defined(__GNUC__) || defined(_WIN32)
33inline const char* type_name() {
34 static const size_t len = ECS_FUNC_TYPE_LEN(
const char*, type_name, ECS_FUNC_NAME);
35 static char result[len + 1] = {};
36 static const size_t front_len = ECS_FUNC_NAME_FRONT(
const char*, type_name);
37 static const char* cppTypeName = ecs_cpp_get_type_name(result, ECS_FUNC_NAME, len, front_len);
41#error "implicit component registration not supported"
47inline const char* symbol_name() {
48 static const size_t len = ECS_FUNC_TYPE_LEN(
const char*, symbol_name, ECS_FUNC_NAME);
49 static char result[len + 1] = {};
50 static const char* cppSymbolName = ecs_cpp_get_symbol_name(result, type_name<T>(), len);
54template <>
inline const char* symbol_name<uint8_t>() {
57template <>
inline const char* symbol_name<uint16_t>() {
60template <>
inline const char* symbol_name<uint32_t>() {
63template <>
inline const char* symbol_name<uint64_t>() {
66template <>
inline const char* symbol_name<int8_t>() {
69template <>
inline const char* symbol_name<int16_t>() {
72template <>
inline const char* symbol_name<int32_t>() {
75template <>
inline const char* symbol_name<int64_t>() {
78template <>
inline const char* symbol_name<float>() {
81template <>
inline const char* symbol_name<double>() {
89template<
typename T, enable_if_t<
90 std::is_trivial<T>::value ==
true
96template<
typename T, enable_if_t<
97 std::is_trivial<T>::value ==
false
99void register_lifecycle_actions(
104 cl.
ctor = ctor<T>(cl.flags);
105 cl.dtor = dtor<T>(cl.flags);
107 cl.copy = copy<T>(cl.flags);
108 cl.copy_ctor = copy_ctor<T>(cl.flags);
109 cl.move = move<T>(cl.flags);
110 cl.move_ctor = move_ctor<T>(cl.flags);
112 cl.ctor_move_dtor = ctor_move_dtor<T>(cl.flags);
113 cl.move_dtor = move_dtor<T>(cl.flags);
115 cl.flags &= ECS_TYPE_HOOKS_ILLEGAL;
118 if (cl.flags & (ECS_TYPE_HOOK_MOVE_ILLEGAL|ECS_TYPE_HOOK_MOVE_CTOR_ILLEGAL))
126 static_assert(is_pointer<T>::value ==
false,
127 "pointer types are not allowed for components");
131 bool allow_tag =
true)
133 s_index = flecs_component_ids_index_get();
134 s_allow_tag = allow_tag;
136 s_alignment =
alignof(T);
137 if (is_empty<T>::value && allow_tag) {
143 static void init_builtin(
144 flecs::world_t *
world,
146 bool allow_tag =
true)
149 flecs_component_ids_set(
world, s_index,
id);
153 static entity_t register_id(
155 const char *name =
nullptr,
156 bool allow_tag =
true,
157 bool is_component =
true,
158 bool explicit_registration =
false,
166 ecs_assert(s_index != 0, ECS_INTERNAL_ERROR, NULL);
169 bool registered =
false, existing =
false;
171 flecs::entity_t c = ecs_cpp_component_register(
172 world,
id, s_index, name, type_name<T>(),
173 symbol_name<T>(), size(), alignment(),
174 is_component, explicit_registration, ®istered, &existing);
182 if (size() && !existing) {
183 register_lifecycle_actions<T>(
world, c);
189 #if FLECS_CPP_ENUM_REFLECTION_SUPPORT
190 _::init_enum<T>(
world, c);
200 static entity_t
id(world_t *
world)
202#ifdef FLECS_CPP_NO_AUTO_REGISTRATION
204 "component '%s' must be registered before use",
207 flecs::entity_t c = flecs_component_ids_get(
world, s_index);
210 "component '%s' was deleted, reregister before using",
213 flecs::entity_t c = flecs_component_ids_get_alive(
world, s_index);
215 c = register_id(
world);
222 static size_t size() {
223 ecs_assert(s_index != 0, ECS_INTERNAL_ERROR, NULL);
228 static size_t alignment() {
229 ecs_assert(s_index != 0, ECS_INTERNAL_ERROR, NULL);
234 static bool registered(flecs::world_t *
world) {
241 if (!flecs_component_ids_get(
world, s_index)) {
250 static void reset() {
257 static int32_t s_index;
258 static size_t s_size;
259 static size_t s_alignment;
260 static bool s_allow_tag;
282 static id_t
id(world_t *
world =
nullptr) {
297 using entity::entity;
339 h.
flags &= ECS_TYPE_HOOKS_ILLEGAL;
348 ecs_assert(compare_callback, ECS_INVALID_PARAMETER, NULL);
350 h.
cmp = compare_callback;
351 h.
flags &= ~ECS_TYPE_HOOK_CMP_ILLEGAL;
352 if(h.
flags & ECS_TYPE_HOOK_EQUALS_ILLEGAL) {
353 h.
flags &= ~ECS_TYPE_HOOK_EQUALS_ILLEGAL;
363 ecs_assert(equals_callback, ECS_INVALID_PARAMETER, NULL);
365 h.
equals = equals_callback;
366 h.
flags &= ~ECS_TYPE_HOOK_EQUALS_ILLEGAL;
375# include "mixins/metrics/untyped_component.inl"
396 flecs::world_t *
world,
397 const char *
name =
nullptr,
398 bool allow_tag =
true,
406 template <
typename Func>
411 "on_add hook is already set");
413 h.
on_add = Delegate::run_add;
414 ctx->on_add = FLECS_NEW(Delegate)(FLECS_FWD(func));
415 ctx->free_on_add = _::free_obj<Delegate>;
421 template <
typename Func>
424 typename std::decay<Func>::type, T>;
427 "on_remove hook is already set");
430 ctx->on_remove = FLECS_NEW(Delegate)(FLECS_FWD(func));
431 ctx->free_on_remove = _::free_obj<Delegate>;
437 template <
typename Func>
440 typename std::decay<Func>::type, T>;
443 "on_set hook is already set");
445 h.
on_set = Delegate::run_set;
446 ctx->on_set = FLECS_NEW(Delegate)(FLECS_FWD(func));
447 ctx->free_on_set = _::free_obj<Delegate>;
453 using untyped_component::on_compare;
456 ecs_assert(handler != NULL, ECS_INVALID_OPERATION,
457 "Type does not have operator> or operator< const or is inaccessible");
465 on_compare(
reinterpret_cast<ecs_cmp_t>(callback));
470 using untyped_component::on_equals;
473 ecs_assert(handler != NULL, ECS_INVALID_OPERATION,
474 "Type does not have operator== const or is inaccessible");
487# include "mixins/meta/component.inl"
494 BindingCtx *result =
static_cast<BindingCtx*
>(h.
binding_ctx);
496 result = FLECS_NEW(BindingCtx);
void ecs_add_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t id)
Add a (component) id to an entity.
#define ecs_assert(condition, error_code,...)
Assert.
void ecs_set_hooks_id(ecs_world_t *world, ecs_entity_t id, const ecs_type_hooks_t *hooks)
Register hooks for component.
const ecs_type_hooks_t * ecs_get_hooks_id(const ecs_world_t *world, ecs_entity_t id)
Get 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_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.
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.