guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
component_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 <exception>
26#include <map>
27#include <memory>
28#include <string>
29#include <type_traits>
30#include <typeindex>
31#include <utility>
32
33#include <utility/logging/loggable.hpp>
34#include <utility/logging/default_logger.hpp>
35
36#include <utility/demangle.hpp>
37
38#include "guillaume/ecs/component.hpp"
39#include "guillaume/ecs/component_storage.hpp"
40#include "guillaume/ecs/entity.hpp"
41
42namespace guillaume::ecs
43{
44
53 template<InheritFromComponent ComponentType>
54 class EntityComponentNotFoundException: public std::exception
55 {
56 private:
58 _entityIdentifier;
59
60 std::string _componentTypeName {
61 utility::demangle<ComponentType>()
62 };
64 mutable std::string _message;
65
66 public:
73 const Entity::Identifier &entityIdentifier);
74
80 const char *what(void) const noexcept override;
81 };
82
134 public utility::logging::Loggable<ComponentRegistry,
135 utility::logging::DefaultLogger>
136 {
137 private:
138 std::map<std::type_index, std::unique_ptr<IComponentStorage>>
139 _storages;
140
147 template<InheritFromComponent ComponentType>
148 ComponentStorage<ComponentType> &getOrCreateStorage(void);
149
157 template<InheritFromComponent ComponentType>
158 void registerComponent(const Entity::Identifier &entityIdentifier);
159
160 protected:
169 template<InheritFromComponent ComponentType>
171
172 public:
176 ComponentRegistry(void) = default;
177
181 ~ComponentRegistry(void) = default;
182
194 template<InheritFromComponent... NeededComponentTypes>
196 const Entity::Identifier &entityIdentifier);
197
213 template<InheritFromComponent ComponentType, typename... Args>
214 ComponentType &addComponent(const Entity::Identifier &entityIdentifier,
215 Args &&...args);
216
224 template<InheritFromComponent ComponentType>
225 bool hasComponent(const Entity::Identifier &entityIdentifier) const;
226
233 template<InheritFromComponent ComponentType>
234 void removeComponent(const Entity::Identifier &entityIdentifier);
235
244 void removeEntity(const Entity::Identifier &entityIdentifier);
245
254 bool hasChanged(const Entity::Identifier &entityIdentifier) const;
255
263 void resetChangedFlags(void);
264
274 template<InheritFromComponent ComponentType>
275 ComponentType &getComponent(const Entity::Identifier &entityIdentifier);
276
286 template<InheritFromComponent ComponentType> const ComponentType &
287 getComponent(const Entity::Identifier &entityIdentifier) const;
288 };
289
295 template<typename Type>
297 std::is_base_of_v<ComponentRegistry, Type>;
298
299} // namespace guillaume::ecs
300
301// Include the implementation of the ComponentRegistry template class.
302#include "guillaume/ecs/component_registry.tpp"
void registerComponentsForEntity(const Entity::Identifier &entityIdentifier)
Give an entity one default-constructed component of each listed type.
void removeEntity(const Entity::Identifier &entityIdentifier)
Delete every component of an entity, of every type.
void resetChangedFlags(void)
Mark every component of every entity as unchanged.
ComponentType & getComponent(const Entity::Identifier &entityIdentifier)
Get a component of an entity, to read or modify it.
const ComponentType & getComponent(const Entity::Identifier &entityIdentifier) const
Get a component of an entity, read-only.
void removeComponent(const Entity::Identifier &entityIdentifier)
Delete one component of an entity. Does nothing when the entity does not have it.
ComponentType & addComponent(const Entity::Identifier &entityIdentifier, Args &&...args)
Construct a component for an entity, replacing the one it already has, if any.
~ComponentRegistry(void)=default
Destroy the registry and every component it stores.
ComponentRegistry(void)=default
Create a registry that holds no component.
bool hasComponent(const Entity::Identifier &entityIdentifier) const
Tell whether an entity has a component of a given type.
bool hasChanged(const Entity::Identifier &entityIdentifier) const
Tell whether at least one component of an entity was modified since the flags were last reset.
void registerNewComponentType(void)
Create the (empty) storage of a component type ahead of its first use.
Holds every component of one type, indexed by entity identifier.
const char * what(void) const noexcept override
Get the exception message.
EntityComponentNotFoundException(const Entity::Identifier &entityIdentifier)
Build the exception and its message.
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
Constrains a template parameter to a type derived from ComponentRegistry (or ComponentRegistry itself...
Constrains a template parameter to a type derived from Component (or Component itself).
The entity-component-system core, independent of any widget.
Definition component.hpp:28