guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
component_storage.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 <unordered_map>
26#include <utility>
27
28#include "guillaume/ecs/component.hpp"
29#include "guillaume/ecs/entity.hpp"
30
31namespace guillaume::ecs
32{
33
44 {
45 public:
49 virtual ~IComponentStorage(void) = default;
50
56 virtual void remove(const Entity::Identifier &entityIdentifier) = 0;
57
63 virtual bool has(const Entity::Identifier &entityIdentifier) const = 0;
64
72 virtual bool
73 hasChanged(const Entity::Identifier &entityIdentifier) const = 0;
74
78 virtual void resetChangedFlags(void) = 0;
79 };
80
94 template<InheritFromComponent ComponentType> class ComponentStorage:
96 {
97 public:
101 using Storage = std::unordered_map<Entity::Identifier, ComponentType>;
102
103 private:
104 Storage _components;
105
106 public:
110 ComponentStorage(void) = default;
111
115 ~ComponentStorage(void) override = default;
116
128 template<typename... Args> ComponentType &
129 emplace(const Entity::Identifier &entityIdentifier, Args &&...args);
130
137 ComponentType *find(const Entity::Identifier &entityIdentifier);
138
145 const ComponentType *
146 find(const Entity::Identifier &entityIdentifier) const;
147
153 void remove(const Entity::Identifier &entityIdentifier) override;
154
160 bool has(const Entity::Identifier &entityIdentifier) const override;
161
170 const Entity::Identifier &entityIdentifier) const override;
171
175 void resetChangedFlags(void) override;
176 };
177
178} // namespace guillaume::ecs
179
180// Include the implementation of the ComponentStorage template class.
181#include "guillaume/ecs/component_storage.tpp"
void resetChangedFlags(void) override
Mark every component of this storage as unchanged.
void remove(const Entity::Identifier &entityIdentifier) override
Delete the component stored for an entity. Does nothing when the entity has none.
bool has(const Entity::Identifier &entityIdentifier) const override
Tell whether a component is stored for an entity.
ComponentType & emplace(const Entity::Identifier &entityIdentifier, Args &&...args)
Construct a component for an entity, replacing the one it already has, if any.
bool hasChanged(const Entity::Identifier &entityIdentifier) const override
Tell whether the component stored for an entity is marked as changed.
const ComponentType * find(const Entity::Identifier &entityIdentifier) const
Look up the component of an entity, read-only.
ComponentType * find(const Entity::Identifier &entityIdentifier)
Look up the component of an entity.
~ComponentStorage(void) override=default
Destroy the storage and every component it holds.
ComponentStorage(void)=default
Create an empty storage.
std::unordered_map< Entity::Identifier, ComponentType > Storage
The container: one component per entity identifier.
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
What ComponentRegistry can do with a component storage without knowing which component type it holds.
virtual bool has(const Entity::Identifier &entityIdentifier) const =0
Tell whether a component is stored for an entity.
virtual void resetChangedFlags(void)=0
Mark every component of this storage as unchanged.
virtual bool hasChanged(const Entity::Identifier &entityIdentifier) const =0
Tell whether the component stored for an entity is marked as changed.
virtual void remove(const Entity::Identifier &entityIdentifier)=0
Delete the component stored for an entity. Does nothing when the entity has none.
virtual ~IComponentStorage(void)=default
Virtual destructor.
The entity-component-system core, independent of any widget.
Definition component.hpp:28