Flecs v4.1
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
impl.hpp
Go to the documentation of this file.
1
6#pragma once
7
8namespace flecs {
9
10namespace _ {
11
12template <typename T>
13ecs_entity_t do_import(world& world, const char *symbol) {
14 ecs_trace("#[magenta]import#[reset] %s", _::type_name<T>());
16
17 ecs_entity_t scope = ecs_set_scope(world, 0);
18
19 // Initialize module component type and don't allow it to be registered as a
20 // tag, as this would prevent calling emplace().
21 auto c_ = component<T>(world, nullptr, false);
22
23 // Make module component sparse so that it'll never move in memory. This
24 // guarantees that a module destructor can be reliably used to clean up
25 // module resources.
26 c_.add(flecs::Sparse);
27
28#ifdef FLECS_CONSTRAINT_TRAITS
29 c_.add(flecs::Singleton);
30#endif
31
32 ecs_set_scope(world, c_);
33 world.emplace<T>(world);
34 ecs_set_scope(world, scope);
35
36 ecs_add_id(world, c_, EcsModule);
37
38 // It should now be possible to look up the module.
39 ecs_entity_t m = ecs_lookup_symbol(world, symbol, false, false);
40 ecs_assert(m != 0, ECS_MODULE_UNDEFINED, "%s", symbol);
41 ecs_assert(m == c_, ECS_INTERNAL_ERROR, nullptr);
42
44
45 return m;
46}
47
48template <typename T>
49flecs::entity import(world& world) {
50 char *symbol = ecs_cpp_get_symbol_name(nullptr, type_name<T>(), 0);
51
52 ecs_entity_t m = ecs_lookup_symbol(world, symbol, true, false);
53
54 if (!_::type<T>::registered(world)) {
55 // Module is registered with world, initialize static data.
56 if (m) {
57 _::type<T>::init_builtin(world, m, false);
58
59 // Module is not yet registered, register it now.
60 } else {
61 m = _::do_import<T>(world, symbol);
62 }
63
64 // Module has been registered, but could have been for another world.
65 // Import if module hasn't been registered for this world.
66 } else if (!m) {
67 m = _::do_import<T>(world, symbol);
68 }
69
70 ecs_os_free(symbol);
71 return flecs::entity(world, m);
72}
73
74}
75
84template <typename Module>
85inline flecs::entity world::module(const char *name) const {
86 flecs::entity result = this->entity(
87 _::type<Module>::register_id(world_, nullptr, false));
88
89 if (name) {
90 flecs::entity prev_parent = result.parent();
91 ecs_add_path_w_sep(world_, result, 0, name, "::", "::");
92 flecs::entity parent = result.parent();
93 if (prev_parent != parent) {
94 // Module was reparented, clean up old parent(s).
95 flecs::entity cur = prev_parent, next;
96 while (cur) {
97 // Stop if the old parent is an ancestor of the new parent.
98 flecs::entity p = parent;
99 while (p && p != cur) {
100 p = p.parent();
101 }
102 if (p == cur) {
103 break;
104 }
105
106 next = cur.parent();
107
108 // Move entities that were created in the old parent to the
109 // renamed module.
111 ecs_iter_t it = ecs_children(world_, cur);
112 while (ecs_children_next(&it)) {
113 for (int32_t i = 0; i < it.count; i ++) {
115 result);
116 }
117 }
119
120 cur.destruct();
121 this->set_version(cur);
122 cur = next;
123 }
124 }
125 }
126
127 return result;
128}
129
130template <typename Module>
132 return flecs::_::import<Module>(*this);
133}
134
137}
FLECS_API char * ecs_cpp_get_symbol_name(char *symbol_name, const char *type_name, size_t len)
Get symbol name from type name.
void ecs_add_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Add a (component) ID to an entity.
const ecs_entity_t EcsChildOf
Used to express parent-child relationships.
const ecs_entity_t EcsModule
Tag added to module entities.
#define ECS_MODULE_UNDEFINED
Module undefined error code.
Definition log.h:712
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:473
#define ecs_log_push()
Push log indentation at the default level.
Definition log.h:458
#define ECS_INTERNAL_ERROR
Internal error code.
Definition log.h:681
#define ecs_trace(...)
Tracing macro.
Definition log.h:309
#define ecs_log_pop()
Pop log indentation at the default level.
Definition log.h:460
bool ecs_defer_end(ecs_world_t *world)
End a block of operations to defer.
bool ecs_defer_begin(ecs_world_t *world)
Defer operations until the end of the frame.
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:393
flecs::entity import()
Import a module.
flecs::entity module(const char *name=nullptr) const
Define a module.
flecs::entity entity(Args &&... args) const
Create an entity.
bool ecs_children_next(ecs_iter_t *it)
Progress an iterator created with ecs_children().
ecs_iter_t ecs_children(const ecs_world_t *world, ecs_entity_t parent)
Iterate children of a parent.
#define ecs_add_pair(world, subject, first, second)
Add a pair to an entity.
Definition flecs_c.h:275
ecs_entity_t ecs_lookup_symbol(const ecs_world_t *world, const char *symbol, bool lookup_as_path, bool recursive)
Look up an entity by its symbol name.
ecs_entity_t ecs_add_path_w_sep(ecs_world_t *world, ecs_entity_t entity, ecs_entity_t parent, const char *path, const char *sep, const char *prefix)
Add a specified path to an entity.
ecs_entity_t ecs_set_scope(ecs_world_t *world, ecs_entity_t scope)
Set the current scope.
Iterator.
Definition flecs.h:1190
int32_t count
Number of entities to iterate.
Definition flecs.h:1197
const ecs_entity_t * entities
Entity identifiers.
Definition flecs.h:1198
flecs::entity parent() const
Get parent of entity.
Definition impl.hpp:68
Entity.
Definition entity.hpp:30
void destruct() const
Delete an entity.
Definition entity.hpp:416
world_t * world_
Pointer to the underlying C world.
Definition world.hpp:1423
void set_version(flecs::entity_t e) const
Set the version of an entity to the provided value.
Definition world.hpp:1275