guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
system.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 <bitset>
26#include <chrono>
27#include <functional>
28#include <stdexcept>
29#include <vector>
30
31#include <utility/logging/loggable.hpp>
32#include <utility/logging/default_logger.hpp>
33#include <utility/demangle.hpp>
34
35#include "guillaume/ecs/component.hpp"
36#include "guillaume/ecs/component_registry.hpp"
37#include "guillaume/ecs/component_type_id.hpp"
38#include "guillaume/ecs/entity.hpp"
39#include "guillaume/ecs/entity_registry.hpp"
40#include "guillaume/ecs/system_phase.hpp"
41#include "guillaume/ecs/entity_tree_traveler.hpp"
42
43namespace guillaume::ecs
44{
45
116 class System:
117 protected utility::logging::Loggable<System,
118 utility::logging::DefaultLogger>
119 {
120 private:
121 Phase _phase;
123 _signature;
124 ecs::ComponentRegistry *_activeComponentRegistry {
125 nullptr
126 };
128 ecs::EntityRegistry *_activeEntityRegistry {
129 nullptr
130 };
132
133 protected:
143 template<InheritFromComponent... ComponentTypes>
144 void setSignature(void);
145
153
160 const ecs::EntityRegistry &getEntityRegistry(void) const;
161
169
178
191 template<InheritFromComponent ComponentType> bool
192 hasComponent(const ecs::Entity::Identifier &entityIdentifier) const;
193
205 template<InheritFromComponent ComponentType> ComponentType &
206 getComponent(const ecs::Entity::Identifier &entityIdentifier);
207
218 template<InheritFromComponent ComponentType> const ComponentType &
219 getComponent(const ecs::Entity::Identifier &entityIdentifier) const;
220
236 template<InheritFromComponent ComponentType>
237 bool requireComponent(const ecs::Entity::Identifier &entityIdentifier);
238
239 public:
250 System(Phase phase);
251
255 virtual ~System(void) = default;
256
261 Phase getPhase(void) const noexcept;
262
268 Entity::Signature getSignature(void) const noexcept;
269
285 void routine(ecs::ComponentRegistry &componentRegistry,
286 ecs::EntityRegistry &entityRegistry,
287 const ecs::EntityTreeTraveler &traveler);
288
304 void bindComponentRegistry(ecs::ComponentRegistry &componentRegistry);
305
309 void unbindComponentRegistry(void);
310
320 virtual void
321 update(const ecs::Entity::Identifier &entityIdentifier) = 0;
322
330 virtual void prepare(void)
331 {
332 }
333
341 virtual void cleanup(void)
342 {
343 }
344 };
345
350 template<typename Type>
351 concept InheritFromSystem = std::is_base_of_v<System, Type>;
352
353} // namespace guillaume::ecs
354
355// Include the System template implementation
356#include "guillaume/ecs/system.tpp"
Stores every component of a scene, per component type and per entity identifier, and knows which ones...
Owns entities and gives the systems the list of entities to visit.
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
Phase getPhase(void) const noexcept
Get the phase of the frame in which the system runs.
Definition system.cpp:71
const ComponentType & getComponent(const ecs::Entity::Identifier &entityIdentifier) const
Get a component of an entity from the bound registry, read-only.
virtual void cleanup(void)
Called once per pass, after the last update().
Definition system.hpp:341
ecs::EntityRegistry & getEntityRegistry(void)
Get the entity registry of the scene being processed.
Definition system.cpp:35
void unbindComponentRegistry(void)
Drop the binding made by bindComponentRegistry().
Definition system.cpp:118
ecs::ComponentRegistry & getComponentRegistry(void)
Get the component registry of the scene being processed.
Definition system.cpp:53
void setSignature(void)
Replace the signature by the one made of the given component types.
bool hasComponent(const ecs::Entity::Identifier &entityIdentifier) const
Tell whether an entity has a component, in the bound registry.
virtual void prepare(void)
Called once per pass, before the first update().
Definition system.hpp:330
ComponentType & getComponent(const ecs::Entity::Identifier &entityIdentifier)
Get a component of an entity from the bound registry, to read or modify it.
bool requireComponent(const ecs::Entity::Identifier &entityIdentifier)
Tell whether an entity has a component, and log a warning naming the entity and the component when it...
virtual ~System(void)=default
Virtual destructor.
System(Phase phase)
Create a system that runs in a given phase, with an empty signature.
Definition system.cpp:27
void routine(ecs::ComponentRegistry &componentRegistry, ecs::EntityRegistry &entityRegistry, const ecs::EntityTreeTraveler &traveler)
Run the system once over a scene: prepare(), update() on each matching entity, cleanup().
Definition system.cpp:81
void bindComponentRegistry(ecs::ComponentRegistry &componentRegistry)
Bind a component registry so update() can be called directly, without routine().
Definition system.cpp:113
Entity::Signature getSignature(void) const noexcept
Get the component types an entity must declare to be visited by this system.
Definition system.cpp:76
virtual void update(const ecs::Entity::Identifier &entityIdentifier)=0
Apply the system's rule to one entity. Implemented by each system.
Constrains a template parameter to a type derived from Component (or Component itself).
Constrains a template parameter to a type derived from System.
Definition system.hpp:351
The entity-component-system core, independent of any widget.
Definition component.hpp:28
Phase
The four steps of a frame. Every system belongs to one of them.