Flecs: include/flecs/addons/cpp/mixins/module/impl.hpp Source File
Description: A fast entity component system (ECS) for C & C++

| 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>
13 ecs_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>
45 flecs::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>
81 inline 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
100 cur = next;
101 }
102 }
103 }
104
105 return result;
106}
107
108template <typename Module>
109 inline flecs::entity world::import() {
110 return flecs::_::import<Module>(*this);
111}
112
115}
ecs_add_id
void ecs_add_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t id)
Add a (component) id to an entity.
EcsChildOf
const ecs_entity_t EcsChildOf
Used to express parent-child relationships.
EcsModule
const ecs_entity_t EcsModule
Tag added to module entities.
ecs_assert
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:352
ecs_entity_t
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:347
flecs::world::import
flecs::entity import()
Import a module.
Definition impl.hpp:109
flecs::world::module
flecs::entity module(const char *name=nullptr) const
Define a module.
Definition impl.hpp:81
flecs::world::entity
flecs::entity entity(Args &&... args) const
Create an entity.
Definition impl.hpp:195
ecs_each_id
ecs_iter_t ecs_each_id(const ecs_world_t *world, ecs_id_t id)
Iterate all entities with specified (component id).
ecs_iter_is_true
bool ecs_iter_is_true(ecs_iter_t *it)
Test if iterator is true.
ecs_lookup_symbol
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_add_path_w_sep
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_set_scope
ecs_entity_t ecs_set_scope(ecs_world_t *world, ecs_entity_t scope)
Set the current scope.
ecs_iter_t
Iterator.
Definition flecs.h:1062
flecs::_::type
Definition flecs.hpp:38
flecs::entity_view::parent
flecs::entity parent() const
Get parent of entity.
Definition impl.hpp:73
flecs::entity
Entity.
Definition entity.hpp:30
flecs::entity::destruct
void destruct() const
Delete an entity.
Definition entity.hpp:307