31#if defined(__GNUC__) || defined(_WIN32)
33inline static 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 return ecs_cpp_get_type_name(result, ECS_FUNC_NAME, len);
39#error "implicit component registration not supported"
45inline static const char* symbol_name() {
46 static const size_t len = ECS_FUNC_TYPE_LEN(
const char*, symbol_name, ECS_FUNC_NAME);
47 static char result[len + 1] = {};
48 return ecs_cpp_get_symbol_name(result, type_name<T>(), len);
51template <>
inline const char* symbol_name<uint8_t>() {
54template <>
inline const char* symbol_name<uint16_t>() {
57template <>
inline const char* symbol_name<uint32_t>() {
60template <>
inline const char* symbol_name<uint64_t>() {
63template <>
inline const char* symbol_name<int8_t>() {
66template <>
inline const char* symbol_name<int16_t>() {
69template <>
inline const char* symbol_name<int32_t>() {
72template <>
inline const char* symbol_name<int64_t>() {
75template <>
inline const char* symbol_name<float>() {
78template <>
inline const char* symbol_name<double>() {
86template<
typename T, enable_if_t<
87 std::is_trivial<T>::value ==
true
93template<
typename T, enable_if_t<
94 std::is_trivial<T>::value ==
false
96void register_lifecycle_actions(
105 cl.copy_ctor = copy_ctor<T>();
107 cl.move_ctor = move_ctor<T>();
109 cl.ctor_move_dtor = ctor_move_dtor<T>();
110 cl.move_dtor = move_dtor<T>();
130 bool allow_tag =
true)
132 if (s_reset_count != ecs_cpp_reset_count_get()) {
140 ecs_assert(allow_tag == s_allow_tag, ECS_INVALID_PARAMETER, NULL);
150 s_allow_tag = allow_tag;
152 s_alignment =
alignof(T);
153 if (is_empty<T>::value && allow_tag) {
158 s_reset_count = ecs_cpp_reset_count_get();
162 static entity_t id_explicit(world_t *
world =
nullptr,
163 const char *name =
nullptr,
bool allow_tag =
true, flecs::id_t
id = 0,
164 bool is_component =
true,
bool *existing =
nullptr)
170 ecs_assert(!
id || s_id ==
id, ECS_INCONSISTENT_COMPONENT_ID, NULL);
178 init(s_id ? s_id :
id, allow_tag);
180 ecs_assert(!
id || s_id ==
id, ECS_INTERNAL_ERROR, NULL);
182 const char *symbol =
nullptr;
187 symbol = symbol_name<T>();
190 entity_t
entity = ecs_cpp_component_register_explicit(
191 world, s_id,
id, name, type_name<T>(), symbol,
192 s_size, s_alignment, is_component, existing);
197 #if FLECS_CPP_ENUM_REFLECTION_SUPPORT
204 ECS_INTERNAL_ERROR, NULL);
215 static id_t
id(world_t *
world =
nullptr,
const char *name =
nullptr,
216 bool allow_tag =
true)
219 if (!registered(
world)) {
231 id_explicit(
world, name, allow_tag, 0,
true, &existing);
236 if (size() && !existing) {
237 register_lifecycle_actions<T>(
world, s_id);
249 ecs_assert(s_id != 0, ECS_INTERNAL_ERROR, NULL);
255 static size_t size() {
256 ecs_assert(s_id != 0, ECS_INTERNAL_ERROR, NULL);
261 static size_t alignment() {
262 ecs_assert(s_id != 0, ECS_INTERNAL_ERROR, NULL);
267 static bool registered(flecs::world_t *
world) {
268 if (s_reset_count != ecs_cpp_reset_count_get()) {
282 static void reset() {
289 static entity_t s_id;
290 static size_t s_size;
291 static size_t s_alignment;
292 static bool s_allow_tag;
293 static int32_t s_reset_count;
316 static id_t
id(world_t *
world =
nullptr) {
331 using entity::entity;
334# include "mixins/meta/untyped_component.inl"
355 flecs::world_t *
world,
356 const char *
name =
nullptr,
357 bool allow_tag =
true,
360 const char *n =
name;
361 bool implicit_name =
false;
363 n = _::type_name<T>();
371 implicit_name =
true;
379 ecs_cpp_component_validate(
world,
id, n, _::symbol_name<T>(),
389 const char *
start = strchr(n,
'<'), *last_elem = NULL;
391 const char *ptr =
start;
392 while (ptr[0] && (ptr[0] !=
':') && (ptr > n)) {
399 last_elem = strrchr(n,
':');
402 name = last_elem + 1;
408 id = ecs_cpp_component_register(
world,
id, n, _::symbol_name<T>(),
409 ECS_SIZEOF(T), ECS_ALIGNOF(T), implicit_name, &existing);
416 _::register_lifecycle_actions<T>(
world,
id);
425 template <
typename Func>
428 typename std::decay<Func>::type, T>;
431 "on_add hook is already set");
433 h.
on_add = Invoker::run_add;
434 ctx->on_add = FLECS_NEW(Invoker)(FLECS_FWD(func));
436 _::free_obj<Invoker>);
442 template <
typename Func>
445 typename std::decay<Func>::type, T>;
448 "on_remove hook is already set");
451 ctx->on_remove = FLECS_NEW(Invoker)(FLECS_FWD(func));
453 _::free_obj<Invoker>);
459 template <
typename Func>
462 typename std::decay<Func>::type, T>;
465 "on_set hook is already set");
467 h.
on_set = Invoker::run_set;
468 ctx->on_set = FLECS_NEW(Invoker)(FLECS_FWD(func));
470 _::free_obj<Invoker>);
483 BindingCtx *result =
static_cast<BindingCtx*
>(h.
binding_ctx);
485 result = FLECS_NEW(BindingCtx);
488 _::free_obj<BindingCtx>);
537 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.
#define ecs_assert(condition, error_code,...)
Assert.
const ecs_type_hooks_t * ecs_get_hooks_id(ecs_world_t *world, ecs_entity_t id)
Get hooks for component.
void ecs_set_hooks_id(ecs_world_t *world, ecs_entity_t id, const ecs_type_hooks_t *hooks)
Register 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.
void reset()
Reset static component ids.
transcribe_cv_t< remove_reference_t< P >, typename remove_reference_t< P >::second > pair_second_t
Get pair::second from pair while preserving cv qualifiers.
transcribe_cv_t< remove_reference_t< P >, typename remove_reference_t< P >::first > pair_first_t
Get pair::first from pair while preserving cv qualifiers.
void(* ecs_ctx_free_t)(void *ctx)
Function to cleanup context data.
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.
Class that wraps around a flecs::id_t.