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 ecs_set_scope(world, c_);
29 world.emplace<T>(world);
30 ecs_set_scope(world, scope);
31
32 ecs_add_id(world, c_, EcsModule);
33
34 // It should now be possible to lookup the module
35 ecs_entity_t m = ecs_lookup_symbol(world, symbol, false, false);
36 ecs_assert(m != 0, ECS_MODULE_UNDEFINED, symbol);
37 ecs_assert(m == c_, ECS_INTERNAL_ERROR, NULL);
38
39 ecs_log_pop();
40
41 return m;
42}
43
44template <typename T>
45flecs::entity import(world& world) {
46 const char *symbol = _::symbol_name<T>();
47
48 ecs_entity_t m = ecs_lookup_symbol(world, symbol, true, false);
49
50 if (!_::type<T>::registered(world)) {
51
52 /* Module is registered with world, initialize static data */
53 if (m) {
54 _::type<T>::init(m, false);
55
56 /* Module is not yet registered, register it now */
57 } else {
58 m = _::do_import<T>(world, symbol);
59 }
60
61 /* Module has been registered, but could have been for another world. Import
62 * if module hasn't been registered for this world. */
63 } else if (!m) {
64 m = _::do_import<T>(world, symbol);
65 }
66
67 return flecs::entity(world, m);
68}
69
70}
71
80template <typename Module>
81inline flecs::entity world::module(const char *name) const {
82 flecs::entity result = this->entity(_::type<Module>::id(
83 world_, nullptr, false));
84
85 if (name) {
86 flecs::entity prev_parent = result.parent();
87 ecs_add_path_w_sep(world_, result, 0, name, "::", "::");
88 flecs::entity parent = result.parent();
89 if (prev_parent != parent) {
90 // Module was reparented, cleanup old parent(s)
91 flecs::entity cur = prev_parent, next;
92 while (cur) {
93 next = cur.parent();
94
95 ecs_iter_t it = ecs_each_id(world_, ecs_pair(EcsChildOf, cur));
96 if (!ecs_iter_is_true(&it)) {
97 cur.destruct();
98
99 // Prevent increasing the generation count of the temporary
100 // parent. This allows entities created during
101 // initialization to keep non-recycled ids.
102 this->set_version(cur);
103 }
104
105 cur = next;
106 }
107 }
108 }
109
110 return result;
111}
112
113template <typename Module>
115 return flecs::_::import<Module>(*this);
116}
117
120}
void ecs_add_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t id)
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:352
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:347
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 id)
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:1062
flecs::entity parent() const
Get parent of entity.
Definition impl.hpp:73
Entity.
Definition entity.hpp:30
void destruct() const
Delete an entity.
Definition entity.hpp:307
void set_version(flecs::entity_t e) const
Set version of entity to provided.
Definition world.hpp:1163