Flecs v4.0
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>());
15 ecs_log_push();
16
17 ecs_entity_t scope = ecs_set_scope(world, 0);
18
19 // Initialize module component type & 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 cleanup
25 // module resources.
26 c_.add(flecs::Sparse);
27
28 c_.add(flecs::Singleton);
29
30 ecs_set_scope(world, c_);
31 world.emplace<T>(world);
32 ecs_set_scope(world, scope);
33
34 ecs_add_id(world, c_, EcsModule);
35
36 // It should now be possible to lookup the module
37 ecs_entity_t m = ecs_lookup_symbol(world, symbol, false, false);
38 ecs_assert(m != 0, ECS_MODULE_UNDEFINED, symbol);
39 ecs_assert(m == c_, ECS_INTERNAL_ERROR, NULL);
40
41 ecs_log_pop();
42
43 return m;
44}
45
46template <typename T>
47flecs::entity import(world& world) {
48 const char *symbol = _::symbol_name<T>();
49
50 ecs_entity_t m = ecs_lookup_symbol(world, symbol, true, false);
51
52 if (!_::type<T>::registered(world)) {
53 /* Module is registered with world, initialize static data */
54 if (m) {
55 _::type<T>::init_builtin(world, m, false);
56
57 /* Module is not yet registered, register it now */
58 } else {
59 m = _::do_import<T>(world, symbol);
60 }
61
62 /* Module has been registered, but could have been for another world. Import
63 * if module hasn't been registered for this world. */
64 } else if (!m) {
65 m = _::do_import<T>(world, symbol);
66 }
67
68 return flecs::entity(world, m);
69}
70
71}
72
81template <typename Module>
82inline flecs::entity world::module(const char *name) const {
83 flecs::entity result = this->entity(
84 _::type<Module>::register_id(world_, nullptr, false));
85
86 if (name) {
87 flecs::entity prev_parent = result.parent();
88 ecs_add_path_w_sep(world_, result, 0, name, "::", "::");
89 flecs::entity parent = result.parent();
90 if (prev_parent != parent) {
91 // Module was reparented, cleanup old parent(s)
92 flecs::entity cur = prev_parent, next;
93 while (cur) {
94 next = cur.parent();
95
96 ecs_iter_t it = ecs_each_id(world_, ecs_pair(EcsChildOf, cur));
97 if (!ecs_iter_is_true(&it)) {
98 cur.destruct();
99
100 // Prevent increasing the generation count of the temporary
101 // parent. This allows entities created during
102 // initialization to keep non-recycled ids.
103 this->set_version(cur);
104 }
105
106 cur = next;
107 }
108 }
109 }
110
111 return result;
112}
113
114template <typename Module>
116 return flecs::_::import<Module>(*this);
117}
118
121}
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_assert(condition, error_code,...)
Assert.
Definition log.h:368
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:361
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.
ecs_iter_t ecs_each_id(const ecs_world_t *world, ecs_id_t component)
Iterate all entities with specified (component id).
bool ecs_iter_is_true(ecs_iter_t *it)
Test if iterator is true.
ecs_entity_t ecs_lookup_symbol(const ecs_world_t *world, const char *symbol, bool lookup_as_path, bool recursive)
Lookup 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 specified path to entity.
ecs_entity_t ecs_set_scope(ecs_world_t *world, ecs_entity_t scope)
Set the current scope.
Iterator.
Definition flecs.h:1142
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:365
void set_version(flecs::entity_t e) const
Set version of entity to provided.
Definition world.hpp:1278