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

| 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
5
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 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 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 look up the module.
37 ecs_entity_t m = ecs_lookup_symbol(world, symbol, false, false);
38 ecs_assert(m != 0, ECS_MODULE_UNDEFINED, "%s", symbol);
39 ecs_assert(m == c_, ECS_INTERNAL_ERROR, NULL);
40
41 ecs_log_pop();
42
43 return m;
44}
45
46template <typename T>
47 flecs::entity import(world& world) {
48 char *symbol = ecs_cpp_get_symbol_name(NULL, type_name<T>(), 0);
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.
63 // Import if module hasn't been registered for this world.
64 } else if (!m) {
65 m = _::do_import<T>(world, symbol);
66 }
67
68 ecs_os_free(symbol);
69 return flecs::entity(world, m);
70}
71
72}
73
81
82template <typename Module>
83 inline flecs::entity world::module(const char *name) const {
84 flecs::entity result = this->entity(
85 _::type<Module>::register_id(world_, nullptr, false));
86
87 if (name) {
88 flecs::entity prev_parent = result.parent();
89 ecs_add_path_w_sep(world_, result, 0, name, "::", "::");
90 flecs::entity parent = result.parent();
91 if (prev_parent != parent) {
92 // Module was reparented, clean up old parent(s).
93 flecs::entity cur = prev_parent, next;
94 while (cur) {
95 next = cur.parent();
96 cur.destruct();
97 this->set_version(cur);
98 cur = next;
99 }
100 }
101 }
102
103 return result;
104}
105
106template <typename Module>
107 inline flecs::entity world::import() {
108 return flecs::_::import<Module>(*this);
109}
110
112
113}
ecs_add_id
void ecs_add_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Add a (component) ID to an entity.
EcsModule
const ecs_entity_t EcsModule
Tag added to module entities.
ECS_MODULE_UNDEFINED
#define ECS_MODULE_UNDEFINED
Module undefined error code.
Definition log.h:702
ecs_assert
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:473
ecs_log_push
#define ecs_log_push()
Push log indentation at the default level.
Definition log.h:458
ECS_INTERNAL_ERROR
#define ECS_INTERNAL_ERROR
Internal error code.
Definition log.h:671
ecs_trace
#define ecs_trace(...)
Tracing macro.
Definition log.h:309
ecs_log_pop
#define ecs_log_pop()
Pop log indentation at the default level.
Definition log.h:460
ecs_entity_t
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:381
flecs::world::import
flecs::entity import()
Import a module.
Definition impl.hpp:107
flecs::world::module
flecs::entity module(const char *name=nullptr) const
Define a module.
Definition impl.hpp:83
flecs::world::entity
flecs::entity entity(Args &&... args) const
Create an entity.
Definition impl.hpp:190
ecs_lookup_symbol
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_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 a specified path to an entity.
ecs_set_scope
ecs_entity_t ecs_set_scope(ecs_world_t *world, ecs_entity_t scope)
Set the current scope.
flecs::_
Int to enum.
Definition component.hpp:18
flecs::_::type
Definition flecs.hpp:39
flecs::entity_view::parent
flecs::entity parent() const
Get parent of entity.
Definition impl.hpp:68
flecs::entity
Entity.
Definition entity.hpp:30
flecs::entity::destruct
void destruct() const
Delete an entity.
Definition entity.hpp:416
flecs::world::world_
world_t * world_
Pointer to the underlying C world.
Definition world.hpp:1545
flecs::world::set_version
void set_version(flecs::entity_t e) const
Set the version of an entity to the provided value.
Definition world.hpp:1401
flecs::world::entity
flecs::entity entity(Args &&... args) const
Create an entity.