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 /* Module is registered with world, initialize static data */
52 if (m) {
53 _::type<T>::init_builtin(world, m, false);
54
55 /* Module is not yet registered, register it now */
56 } else {
57 m = _::do_import<T>(world, symbol);
58 }
59
60 /* Module has been registered, but could have been for another world. Import
61 * if module hasn't been registered for this world. */
62 } else if (!m) {
63 m = _::do_import<T>(world, symbol);
64 }
65
66 return flecs::entity(world, m);
67}
68
69}
70
79template <typename Module>
80inline flecs::entity world::module(const char *name) const {
81 flecs::entity result = this->entity(_::type<Module>::register_id(
82 world_, nullptr, false));
83
84 if (name) {
85 flecs::entity prev_parent = result.parent();
86 ecs_add_path_w_sep(world_, result, 0, name, "::", "::");
87 flecs::entity parent = result.parent();
88 if (prev_parent != parent) {
89 // Module was reparented, cleanup old parent(s)
90 flecs::entity cur = prev_parent, next;
91 while (cur) {
92 next = cur.parent();
93
94 ecs_iter_t it = ecs_each_id(world_, ecs_pair(EcsChildOf, cur));
95 if (!ecs_iter_is_true(&it)) {
96 cur.destruct();
97
98 // Prevent increasing the generation count of the temporary
99 // parent. This allows entities created during
100 // initialization to keep non-recycled ids.
101 this->set_version(cur);
102 }
103
104 cur = next;
105 }
106 }
107 }
108
109 return result;
110}
111
112template <typename Module>
114 return flecs::_::import<Module>(*this);
115}
116
119}
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:346
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:1100
flecs::entity parent() const
Get parent of entity.
Definition impl.hpp:78
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