guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
system.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/system.hpp"
24
25namespace guillaume::ecs
26{
28 : _phase(phase)
29 , _signature()
30 , _activeComponentRegistry(nullptr)
31 , _activeEntityRegistry(nullptr)
32 {
33 }
34
36 {
37 if (_activeEntityRegistry == nullptr) {
38 throw std::runtime_error(
39 "No active entity registry bound to system");
40 }
41 return *_activeEntityRegistry;
42 }
43
45 {
46 if (_activeEntityRegistry == nullptr) {
47 throw std::runtime_error(
48 "No active entity registry bound to system");
49 }
50 return *_activeEntityRegistry;
51 }
52
54 {
55 if (_activeComponentRegistry == nullptr) {
56 throw std::runtime_error(
57 "No active component registry bound to system");
58 }
59 return *_activeComponentRegistry;
60 }
61
63 {
64 if (_activeComponentRegistry == nullptr) {
65 throw std::runtime_error(
66 "No active component registry bound to system");
67 }
68 return *_activeComponentRegistry;
69 }
70
71 Phase System::getPhase(void) const noexcept
72 {
73 return _phase;
74 }
75
77 {
78 return _signature;
79 }
80
81 void System::routine(ecs::ComponentRegistry &componentRegistry,
82 ecs::EntityRegistry &entityRegistry,
83 const ecs::EntityTreeTraveler &traveler)
84 {
85 _activeComponentRegistry = &componentRegistry;
86 _activeEntityRegistry = &entityRegistry;
87 getLogger().debug() << "System routine started";
88
89 prepare();
90
91 auto traversedEntities = traveler.travel(entityRegistry);
92
93 for (const auto &entity: traversedEntities) {
94 if ((entity->getSignature() & getSignature()) != getSignature()) {
95 continue;
96 }
97
98 // Systems are the only place behavior lives: entities are not
99 // called back, whatever changed.
100 update(entity->getIdentifier());
101 }
102
103 cleanup();
104
105 getLogger().debug() << "System routine finished. Visited entities: "
106 << traversedEntities.size();
107
108 _activeComponentRegistry = nullptr;
109 _activeEntityRegistry = nullptr;
110 }
111
112 void
114 {
115 _activeComponentRegistry = &componentRegistry;
116 }
117
119 {
120 _activeComponentRegistry = nullptr;
121 }
122} // namespace guillaume::ecs
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.
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
Phase getPhase(void) const noexcept
Get the phase of the frame in which the system runs.
Definition system.cpp:71
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
virtual void prepare(void)
Called once per pass, before the first update().
Definition system.hpp:330
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.
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.