guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
entity.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.hpp"
24
25#include <atomic>
26#include <stdexcept>
27
28#include "guillaume/components/hierarchy.hpp"
29#include "guillaume/ecs/component_registry.hpp"
30
31namespace guillaume::ecs
32{
33
34 namespace
35 {
42 void propagateLayer(ComponentRegistry &registry,
43 Entity::Identifier identifier, std::int32_t layer)
44 {
45 auto &hierarchy =
46 registry.getComponent<components::Hierarchy>(identifier);
47
48 hierarchy.setLayer(layer);
49 for (const auto child: hierarchy.getChildIdentifiers()) {
50 if (registry.hasComponent<components::Hierarchy>(child)) {
51 propagateLayer(registry, child, layer + 1);
52 }
53 }
54 }
55
63 bool isSelfOrAncestor(const ComponentRegistry &registry,
64 Entity::Identifier ancestor,
65 Entity::Identifier identifier)
66 {
67 while (identifier != Entity::InvalidIdentifier) {
68 if (identifier == ancestor) {
69 return true;
70 }
71 if (!registry.hasComponent<components::Hierarchy>(identifier)) {
72 return false;
73 }
74 identifier =
75 registry.getComponent<components::Hierarchy>(identifier)
76 .getParentIdentifier();
77 }
78 return false;
79 }
80 } // namespace
81
82 Entity::Identifier Entity::getNextIdentifier(void)
83 {
84 static std::atomic<Identifier> currentId { InvalidIdentifier };
85 return currentId.fetch_add(1, std::memory_order_relaxed) + 1;
86 }
87
89 : utility::logging::Loggable<Entity, utility::logging::DefaultLogger>()
90 , _identifier(getNextIdentifier())
91 {
92 }
93
95 {
96 return _identifier;
97 }
98
100 {
101 return _signature;
102 }
103
104 void Entity::setSignature(const Signature &signature)
105 {
106 _signature = signature;
107 }
108
110 {
111 _hierarchyRegistry = &componentRegistry;
112 if (!componentRegistry.hasComponent<components::Hierarchy>(
113 _identifier)) {
114 componentRegistry.addComponent<components::Hierarchy>(_identifier);
115 }
117 }
118
119 std::int32_t Entity::getLayer(void) const noexcept
120 {
121 if (_hierarchyRegistry != nullptr
122 && _hierarchyRegistry->hasComponent<components::Hierarchy>(
123 _identifier)) {
124 const ComponentRegistry &registry = *_hierarchyRegistry;
125 return registry.getComponent<components::Hierarchy>(_identifier)
126 .getLayer();
127 }
128 return _layer;
129 }
130
131 Entity &Entity::setParent(std::shared_ptr<Entity> parent)
132 {
133 const Identifier parentIdentifier =
134 parent != nullptr ? parent->getIdentifier() : InvalidIdentifier;
135 const std::int32_t layer =
136 parent != nullptr ? parent->getLayer() + 1 : 0;
137
138 if (parent.get() == this) {
139 throw std::invalid_argument("An entity cannot be its own parent");
140 }
141 if (_hierarchyRegistry == nullptr) {
142 _parent = parent;
143 _layer = layer;
144 return *this;
145 }
146
147 auto &registry = *_hierarchyRegistry;
148 if (isSelfOrAncestor(registry, _identifier, parentIdentifier)) {
149 throw std::invalid_argument(
150 "An entity cannot be parented to one of its descendants");
151 }
152
153 auto &hierarchy =
154 registry.getComponent<components::Hierarchy>(_identifier);
155 const Identifier previous = hierarchy.getParentIdentifier();
156 if (previous != parentIdentifier) {
157 if (registry.hasComponent<components::Hierarchy>(previous)) {
158 registry.getComponent<components::Hierarchy>(previous)
159 .removeChild(_identifier);
160 }
161 if (registry.hasComponent<components::Hierarchy>(
162 parentIdentifier)) {
163 registry.getComponent<components::Hierarchy>(parentIdentifier)
164 .addChild(_identifier);
165 }
166 hierarchy.setParentIdentifier(parentIdentifier);
167 }
168 _parent = parent;
169 _layer = layer;
170 propagateLayer(registry, _identifier, layer);
171 return *this;
172 }
173
174 std::vector<Entity::Identifier> Entity::releaseComponents(void)
175 {
176 std::vector<Identifier> children;
177
178 _parent.reset();
179 if (_hierarchyRegistry == nullptr) {
180 return children;
181 }
182
183 auto &registry = *_hierarchyRegistry;
184 if (registry.hasComponent<components::Hierarchy>(_identifier)) {
185 const auto &hierarchy =
186 registry.getComponent<components::Hierarchy>(_identifier);
187 const Identifier parent = hierarchy.getParentIdentifier();
188
189 children = hierarchy.getChildIdentifiers();
190 if (registry.hasComponent<components::Hierarchy>(parent)) {
191 registry.getComponent<components::Hierarchy>(parent)
192 .removeChild(_identifier);
193 }
194 // No child may keep pointing at an entity that is gone.
195 for (const auto child: children) {
196 if (registry.hasComponent<components::Hierarchy>(child)) {
197 registry.getComponent<components::Hierarchy>(child)
198 .setParentIdentifier(InvalidIdentifier);
199 }
200 }
201 }
202 registry.removeEntity(_identifier);
203 _hierarchyRegistry = nullptr;
204 return children;
205 }
206
207 std::shared_ptr<Entity> Entity::getParent(void) const noexcept
208 {
209 return _parent.lock();
210 }
211} // namespace guillaume::ecs
Place of an entity in the entity tree: its parent, its direct children and its depth.
Definition hierarchy.hpp:61
Stores every component of a scene, per component type and per entity identifier, and knows which ones...
ComponentType & getComponent(const Entity::Identifier &entityIdentifier)
Get a component of an entity, to read or modify it.
ComponentType & addComponent(const Entity::Identifier &entityIdentifier, Args &&...args)
Construct a component for an entity, replacing the one it already has, if any.
bool hasComponent(const Entity::Identifier &entityIdentifier) const
Tell whether an entity has a component of a given type.
std::bitset< 128 > Signature
A set of component types, one bit per type (bit position given by ComponentTypeId,...
Definition entity.hpp:91
std::vector< Identifier > releaseComponents(void)
Drop the entity's place in the tree and all its components.
Definition entity.cpp:174
Entity & setParent(std::shared_ptr< Entity > parent)
Attach the entity under another one, or detach it.
Definition entity.cpp:131
Entity::Signature getSignature(void) const noexcept
Get the set of component types the entity declares.
Definition entity.cpp:99
Entity(void)
Create an entity with a fresh identifier, an empty signature, no parent and no component.
Definition entity.cpp:88
static Signature getSignatureFromTypes(void)
Build the signature that has exactly the bits of the given component types.
Entity::Identifier getIdentifier(void) const noexcept
Get the identifier of the entity.
Definition entity.cpp:94
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
std::int32_t getLayer(void) const noexcept
Get the depth of the entity in the tree.
Definition entity.cpp:119
std::shared_ptr< Entity > getParent(void) const noexcept
Get the parent entity.
Definition entity.cpp:207
void setSignature(void)
Replace the signature by the one made of the given component types.
void attachHierarchy(ComponentRegistry &componentRegistry)
Give the entity a components::Hierarchy in a registry and add that component to its signature.
Definition entity.cpp:109
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