guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
entity_registry.hpp
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#pragma once
24
25#include <functional>
26#include <memory>
27#include <optional>
28#include <queue>
29#include <vector>
30
31#include "guillaume/ecs/entity.hpp"
32
33namespace guillaume::ecs
34{
36
75 {
76 private:
84 mutable std::vector<std::shared_ptr<Entity>> _bfsCache;
85
90 mutable std::size_t _cachedVersion { 0 };
91
104 bool removeOne(Entity::Identifier identifier,
105 std::vector<Entity::Identifier> &orphans);
106
113 void clearOwned(std::vector<Entity::Identifier> &orphans);
114
115 protected:
121 virtual std::vector<std::shared_ptr<Entity>> &
123
129 virtual const std::vector<std::shared_ptr<Entity>> &
130 accessDirectEntities(void) const = 0;
131
132 public:
136 EntityRegistry(void) = default;
137
144 virtual ~EntityRegistry(void) = default;
145
156 void addEntity(std::shared_ptr<Entity> entity);
157
179 bool removeEntity(Entity::Identifier identifier);
180
189 void clearEntities(void);
190
196 const std::vector<std::shared_ptr<Entity>> &getEntities(void) const
197 {
198 return accessDirectEntities();
199 }
200
211 std::vector<std::shared_ptr<Entity>> getEntitiesBreadthFirst(void);
212
219 std::vector<std::shared_ptr<Entity>>
220 getEntitiesBreadthFirst(void) const;
221
234 template<InheritFromEntity EntityType>
235 std::shared_ptr<EntityType> getEntity(Entity::Identifier identifier)
236 {
237 if (identifier == Entity::InvalidIdentifier) {
238 return nullptr;
239 }
240 for (auto &entity: accessDirectEntities()) {
241 if (entity->getIdentifier() == identifier) {
242 return std::dynamic_pointer_cast<EntityType>(entity);
243 }
244 }
245 return nullptr;
246 }
247
259 template<InheritFromEntity EntityType> std::shared_ptr<const EntityType>
261 {
262 if (identifier == Entity::InvalidIdentifier) {
263 return nullptr;
264 }
265 for (const auto &entity: accessDirectEntities()) {
266 if (entity->getIdentifier() == identifier) {
267 return std::dynamic_pointer_cast<const EntityType>(entity);
268 }
269 }
270 return nullptr;
271 }
272
284 std::vector<Entity::Identifier>
285 getEntityWithSignature(Entity::Signature systemSignature) const;
286
300 std::vector<Entity::Identifier>
302 const EntityTreeTraveler &traveler) const;
303 };
304} // namespace guillaume::ecs
virtual ~EntityRegistry(void)=default
Virtual destructor.
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.
virtual const std::vector< std::shared_ptr< Entity > > & accessDirectEntities(void) const =0
Give read access to the list of entities this registry owns directly.
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.
std::shared_ptr< EntityType > getEntity(Entity::Identifier identifier)
Find an entity owned directly by this registry, by identifier.
const std::vector< std::shared_ptr< Entity > > & getEntities(void) const
Get the entities this registry owns directly.
std::shared_ptr< const EntityType > getEntity(Entity::Identifier identifier) const
Find an entity owned directly by this registry, by identifier (const overload).
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.
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
The entity-component-system core, independent of any widget.
Definition component.hpp:28