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(
108 cl.copy_ctor = copy_ctor<T>();
110 cl.move_ctor = move_ctor<T>();
112 cl.ctor_move_dtor = ctor_move_dtor<T>();
113 cl.move_dtor = move_dtor<T>();
117 if (cl.move == ecs_move_illegal || cl.move_ctor == ecs_move_ctor_illegal) {
134 static_assert(is_pointer<T>::value ==
false,
135 "pointer types are not allowed for components");
140 bool allow_tag =
true)
142 if (s_reset_count != ecs_cpp_reset_count_get()) {
150 ecs_assert(allow_tag == s_allow_tag, ECS_INVALID_PARAMETER, NULL);
160 s_allow_tag = allow_tag;
162 s_alignment =
alignof(T);
163 if (is_empty<T>::value && allow_tag) {
168 s_reset_count = ecs_cpp_reset_count_get();
172 static entity_t id_explicit(world_t *
world =
nullptr,
173 const char *name =
nullptr,
bool allow_tag =
true, flecs::id_t
id = 0,
174 bool is_component =
true,
bool *existing =
nullptr)
180 ecs_assert(!
id || s_id ==
id, ECS_INCONSISTENT_COMPONENT_ID, NULL);
188 init(s_id ? s_id :
id, allow_tag);
190 ecs_assert(!
id || s_id ==
id, ECS_INTERNAL_ERROR, NULL);
192 const char *symbol =
nullptr;
197 symbol = symbol_name<T>();
200 entity_t
entity = ecs_cpp_component_register_explicit(
201 world, s_id,
id, name, type_name<T>(), symbol,
202 s_size, s_alignment, is_component, existing);
207 #if FLECS_CPP_ENUM_REFLECTION_SUPPORT
214 ECS_INTERNAL_ERROR, NULL);
225 static id_t
id(world_t *
world =
nullptr,
const char *name =
nullptr,
226 bool allow_tag =
true)
229#ifndef FLECS_CPP_NO_AUTO_REGISTRATION
230 if (!registered(
world)) {
242 id_explicit(
world, name, allow_tag, 0,
true, &existing);
247 if (size() && !existing) {
248 register_lifecycle_actions<T>(
world, s_id);
264 "component '%s' was not registered before use",
269 ecs_assert(s_id != 0, ECS_INTERNAL_ERROR, NULL);
275 static size_t size() {
276 ecs_assert(s_id != 0, ECS_INTERNAL_ERROR, NULL);
281 static size_t alignment() {
282 ecs_assert(s_id != 0, ECS_INTERNAL_ERROR, NULL);
287 static bool registered(flecs::world_t *
world) {
288 if (s_reset_count != ecs_cpp_reset_count_get()) {
302 static void reset() {
309 static entity_t s_id;
310 static size_t s_size;
311 static size_t s_alignment;
312 static bool s_allow_tag;
313 static int32_t s_reset_count;
336 static id_t
id(world_t *
world =
nullptr) {
351 using entity::entity;
357# include "mixins/metrics/untyped_component.inl"
378 flecs::world_t *
world,
379 const char *
name =
nullptr,
380 bool allow_tag =
true,
383 const char *n =
name;
384 bool implicit_name =
false;
386 n = _::type_name<T>();
394 implicit_name =
true;
402 ecs_cpp_component_validate(
world,
id, n, _::symbol_name<T>(),
412 const char *start = strchr(n,
'<'), *last_elem = NULL;
414 const char *ptr = start;
415 while (ptr[0] && (ptr[0] !=
':') && (ptr > n)) {
424 name = last_elem + 1;
430 id = ecs_cpp_component_register(
world,
id, n, _::symbol_name<T>(),
431 ECS_SIZEOF(T), ECS_ALIGNOF(T), implicit_name, &existing);
438 _::register_lifecycle_actions<T>(
world,
id);
447 template <
typename Func>
452 "on_add hook is already set");
454 h.
on_add = Delegate::run_add;
455 ctx->on_add = FLECS_NEW(Delegate)(FLECS_FWD(func));
456 ctx->free_on_add = _::free_obj<Delegate>;
462 template <
typename Func>
465 typename std::decay<Func>::type, T>;
468 "on_remove hook is already set");
471 ctx->on_remove = FLECS_NEW(Delegate)(FLECS_FWD(func));
472 ctx->free_on_remove = _::free_obj<Delegate>;
478 template <
typename Func>
481 typename std::decay<Func>::type, T>;
484 "on_set hook is already set");
486 h.
on_set = Delegate::run_set;
487 ctx->on_set = FLECS_NEW(Delegate)(FLECS_FWD(func));
488 ctx->free_on_set = _::free_obj<Delegate>;
494# include "mixins/meta/component.inl"
501 BindingCtx *result =
static_cast<BindingCtx*
>(h.
binding_ctx);
503 result = FLECS_NEW(BindingCtx);
554 ecs_cpp_reset_count_inc();
flecs::entity_t type_id()
Get id currently assigned to component.
ecs_entity_t ecs_set_with(ecs_world_t *world, ecs_id_t id)
Set current with id.
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.
uint64_t ecs_id_t
Ids are the things that can be added to an entity.
void reset()
Reset static component ids.
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.
bool ecs_exists(const ecs_world_t *world, ecs_entity_t entity)
Test whether an entity exists.
ecs_entity_t ecs_get_scope(const ecs_world_t *world)
Get the current scope.
const char * ecs_get_symbol(const ecs_world_t *world, ecs_entity_t entity)
Get the symbol of an entity.
ecs_entity_t ecs_set_scope(ecs_world_t *world, ecs_entity_t scope)
Set the current scope.
Type that contains component lifecycle callbacks.
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_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.
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_add(Func &&func)
Register on_add hook.
component< T > & on_set(Func &&func)
Register on_set hook.
flecs::string_view name() const
Return the entity name.
entity_t id() const
Get entity id.
Class that wraps around a flecs::id_t.