guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
entity_registry.cpp
1/*
2 Copyright (c) 2026 ETIB Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23#include "guillaume/ecs/entity_registry.hpp"
24
25#include <algorithm>
26
27#include "guillaume/ecs/level_order_traveler.hpp"
28
29namespace guillaume::ecs
30{
31
32 namespace
33 {
44 std::size_t &getTreeVersion(void)
45 {
46 static std::size_t version = 0;
47 return version;
48 }
49 } // namespace
50
51 void EntityRegistry::addEntity(std::shared_ptr<Entity> entity)
52 {
53 accessDirectEntities().push_back(entity);
54 ++getTreeVersion();
55 }
56
57 bool EntityRegistry::removeOne(Entity::Identifier identifier,
58 std::vector<Entity::Identifier> &orphans)
59 {
60 if (identifier == Entity::InvalidIdentifier) {
61 return false;
62 }
63
65 const auto iterator =
66 std::find_if(entities.begin(), entities.end(),
67 [identifier](const std::shared_ptr<Entity> &entity) {
68 return entity->getIdentifier() == identifier;
69 });
70 if (iterator == entities.end()) {
71 for (auto &entity: entities) {
72 auto *nested = dynamic_cast<EntityRegistry *>(entity.get());
73 if (nested != nullptr
74 && nested->removeOne(identifier, orphans)) {
75 return true;
76 }
77 }
78 return false;
79 }
80
81 // Keep the entity alive until it is fully released.
82 const std::shared_ptr<Entity> entity = *iterator;
83 if (auto *nested = dynamic_cast<EntityRegistry *>(entity.get())) {
84 nested->clearOwned(orphans);
85 }
86 const auto children = entity->releaseComponents();
87 orphans.insert(orphans.end(), children.begin(), children.end());
88 entities.erase(iterator);
89 ++getTreeVersion();
90 return true;
91 }
92
93 void EntityRegistry::clearOwned(std::vector<Entity::Identifier> &orphans)
94 {
95 auto &entities = accessDirectEntities();
96
97 for (auto &entity: entities) {
98 if (auto *nested = dynamic_cast<EntityRegistry *>(entity.get())) {
99 nested->clearOwned(orphans);
100 }
101 const auto children = entity->releaseComponents();
102 orphans.insert(orphans.end(), children.begin(), children.end());
103 }
104 entities.clear();
105 ++getTreeVersion();
106 }
107
109 {
110 std::vector<Entity::Identifier> orphans;
111
112 if (!removeOne(identifier, orphans)) {
113 return false;
114 }
115 // Entities parented to a removed one go with it, wherever below this
116 // registry they are registered. Most were owned and are already gone.
117 while (!orphans.empty()) {
118 const Entity::Identifier orphan = orphans.back();
119 orphans.pop_back();
120 removeOne(orphan, orphans);
121 }
122 return true;
123 }
124
126 {
127 std::vector<Entity::Identifier> orphans;
128
129 clearOwned(orphans);
130 }
131
132 std::vector<std::shared_ptr<Entity>>
134 {
135 if (_cachedVersion == getTreeVersion()) {
136 return _bfsCache;
137 }
138
139 _bfsCache.clear();
140 std::queue<EntityRegistry *> registries;
141
142 registries.push(this);
143 while (!registries.empty()) {
144 EntityRegistry *registry = registries.front();
145 registries.pop();
146
147 for (auto &entity: registry->accessDirectEntities()) {
148 _bfsCache.push_back(entity);
149
150 auto *childRegistry =
151 dynamic_cast<EntityRegistry *>(entity.get());
152 if (childRegistry != nullptr) {
153 registries.push(childRegistry);
154 }
155 }
156 }
157
158 _cachedVersion = getTreeVersion();
159 return _bfsCache;
160 }
161
162 std::vector<std::shared_ptr<Entity>>
164 {
165 if (_cachedVersion == getTreeVersion()) {
166 return _bfsCache;
167 }
168
169 _bfsCache.clear();
170 std::queue<const EntityRegistry *> registries;
171
172 registries.push(this);
173 while (!registries.empty()) {
174 const EntityRegistry *registry = registries.front();
175 registries.pop();
176
177 for (const auto &entity: registry->accessDirectEntities()) {
178 _bfsCache.push_back(entity);
179
180 auto *childRegistry =
181 dynamic_cast<const EntityRegistry *>(entity.get());
182 if (childRegistry != nullptr) {
183 registries.push(childRegistry);
184 }
185 }
186 }
187
188 _cachedVersion = getTreeVersion();
189 return _bfsCache;
190 }
191
192 std::vector<Entity::Identifier> EntityRegistry::getEntityWithSignature(
193 Entity::Signature systemSignature) const
194 {
195 LevelOrderTraveler traveler;
196 return getEntityWithSignature(systemSignature, traveler);
197 }
198
199 std::vector<Entity::Identifier> EntityRegistry::getEntityWithSignature(
200 Entity::Signature systemSignature,
201 const EntityTreeTraveler &traveler) const
202 {
203 std::vector<Entity::Identifier> matchingIdentifiers;
204 for (const auto &entity: traveler.travel(*this)) {
205 if ((entity->getSignature() & systemSignature) == systemSignature) {
206 matchingIdentifiers.push_back(entity->getIdentifier());
207 }
208 }
209
210 return matchingIdentifiers;
211 }
212
213} // namespace guillaume::ecs
virtual std::vector< std::shared_ptr< Entity > > & accessDirectEntities(void)=0
Give access to the list of entities this registry owns directly. Implemented by the class that provid...
std::vector< Entity::Identifier > getEntityWithSignature(Entity::Signature systemSignature) const
List the entities that have every component type of a signature, from the root down.
bool removeEntity(Entity::Identifier identifier)
Remove an entity, with its children and all their components.
std::vector< std::shared_ptr< Entity > > getEntitiesBreadthFirst(void)
List every entity of this registry and of the registries nested in it, level by level.
EntityRegistry(void)=default
Create a registry.
void addEntity(std::shared_ptr< Entity > entity)
Make this registry own an entity, so that it stays alive and systems visit it.
void clearEntities(void)
Remove every entity owned by this registry, with everything they own and all their components.
Decides in which order the entities of a scene are visited.
virtual std::vector< std::shared_ptr< Entity > > travel(EntityRegistry &entityRegistry) const =0
List every entity below a registry, in this traveler's order.
std::bitset< 128 > Signature
A set of component types, one bit per type (bit position given by ComponentTypeId,...
Definition entity.hpp:91
std::size_t Identifier
The key under which an entity's components are stored. Systems and components refer to entities by th...
Definition entity.hpp:86
static constexpr Identifier InvalidIdentifier
"No entity": never the identifier of a real entity
Definition entity.hpp:93
Visits owners before what they own: the entities owned by the scene first, then the entities those ow...
The entity-component-system core, independent of any widget.
Definition component.hpp:28
Widgets: the classes used to create entities with the right components and children.
Definition app_bar.hpp:46