guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
entity.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 <cstddef>
27#include <cstdint>
28#include <memory>
29#include <vector>
30
31#include <utility/logging/loggable.hpp>
32#include <utility/logging/default_logger.hpp>
33
34#include "guillaume/ecs/component.hpp"
35#include "guillaume/ecs/component_type_id.hpp"
36
37namespace guillaume::ecs
38{
40
77 class Entity:
78 protected utility::logging::Loggable<Entity,
79 utility::logging::DefaultLogger>
80 {
81 public:
86 using Identifier = std::size_t;
91 using Signature = std::bitset<128>;
92
93 constexpr static Identifier InvalidIdentifier =
94 0;
95
96 private:
102 static Identifier getNextIdentifier(void);
103
104 public:
118 template<InheritFromComponent... ComponentTypes>
120
121 private:
122 const Identifier _identifier;
123 Signature _signature;
124 std::int32_t _layer {
125 0
126 };
127 std::weak_ptr<Entity> _parent;
128 ComponentRegistry *_hierarchyRegistry {
129 nullptr
130 };
131
132 protected:
145 void attachHierarchy(ComponentRegistry &componentRegistry);
146
155 template<InheritFromComponent... ComponentTypes>
156 void setSignature(void);
157
158 public:
163 Entity(void);
164
171 virtual ~Entity(void) = default;
172
177 Entity::Identifier getIdentifier(void) const noexcept;
178
183 Entity::Signature getSignature(void) const noexcept;
184
194 void setSignature(const Signature &signature);
195
205 std::int32_t getLayer(void) const noexcept;
206
228 Entity &setParent(std::shared_ptr<Entity> parent);
229
242 std::vector<Identifier> releaseComponents(void);
243
249 std::shared_ptr<Entity> getParent(void) const noexcept;
250 };
251
257 template<typename Type>
258 concept InheritFromEntity = std::is_base_of_v<Entity, Type>;
259
260} // namespace guillaume::ecs
261
262// Include the implementation of the Entity template methods
263#include "guillaume/ecs/entity.tpp"
An ecs::ComponentRegistry whose storages for the general-purpose components (geometry,...
Stores every component of a scene, per component type and per entity identifier, and knows which ones...
virtual ~Entity(void)=default
Destroy the entity object.
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
Constrains a template parameter to a type derived from Component (or Component itself).
Constrains a template parameter to a type derived from Entity (or Entity itself).
Definition entity.hpp:258
The entity-component-system core, independent of any widget.
Definition component.hpp:28