Air-Trap 1.0.0
A multiplayer R-Type clone game engine built with C++23 and ECS architecture
Loading...
Searching...
No Matches
Registry.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** R-Type
4** File description:
5** Registry.hpp
6*/
7
8#ifndef RTYPE_REGISTRY_HPP_
9 #define RTYPE_REGISTRY_HPP_
10
12 #include "RType/ECS/Entity.hpp"
14 #include "RType/ECS/ZipView.hpp"
15 #include "RType/Logger.hpp"
16 #include "RType/Error.hpp"
17
18 #include <array>
19 #include <concepts>
20 #include <deque>
21 #include <expected>
22 #include <functional>
23 #include <memory>
24 #include <mutex>
25 #include <ranges>
26 #include <shared_mutex>
27 #include <span>
28 #include <tuple>
29 #include <typeindex>
30 #include <type_traits>
31 #include <unordered_map>
32 #include <utility>
33 #include <vector>
34
35namespace rtp::ecs
36{
37 template <typename From, typename To>
38 using ConstLike =
39 std::conditional_t<std::is_const_v<std::remove_reference_t<From>>,
40 std::add_const_t<To>, To>;
41
42 template <typename From, typename To>
44
45 class Registry {
46 public:
47 ~Registry() noexcept = default;
48
49 [[nodiscard]]
50 auto spawn(void) -> std::expected<Entity, rtp::Error>;
51
52 void kill(Entity entity);
53
54 template <Component T, typename Self>
55 auto subscribe(this Self &self)
56 -> std::expected<std::reference_wrapper<ConstLike<Self,
57 SparseArray<T>>>,
58 rtp::Error>;
59
60 [[nodiscard]]
61 bool isAlive(Entity entity) const noexcept;
62
63 template <Component T, typename... Args>
64 auto add(Entity entity, Args &&...args)
65 -> std::expected<std::reference_wrapper<T>, rtp::Error>;
66
67 template <Component T, typename Self>
68 [[nodiscard]]
69 auto get(this const Self &self)
70 -> std::expected<std::reference_wrapper<ConstLike<Self,
71 SparseArray<T>>>,
72 rtp::Error>;
73
74 template <Component T>
75 [[nodiscard]]
76 bool has(Entity entity) const noexcept;
77
78 template <Component T>
79 void remove(Entity entity) noexcept;
80
81 void clear(void) noexcept;
82
83 void purge(void) noexcept;
84
85 template <Component... Ts, typename Self>
86 [[nodiscard]]
87 auto view(this Self &self);
88
89 template <Component... Ts, typename Self>
90 [[nodiscard]]
91 auto zipView(this Self &self);
92
93 [[nodiscard]]
94 std::size_t entityCount(void) const noexcept;
95
96 template <Component... Ts>
97 [[nodiscard]]
98 std::size_t componentCount(void) const noexcept;
99
100
101 private:
102 std::unordered_map<std::type_index,
103 std::unique_ptr<ISparseArray>> _arrays;
104 std::vector<std::uint32_t> _generations;
105 std::deque<std::size_t> _freeIndices;
106 mutable std::shared_mutex _mutex;
108 private:
109 template <Component T, typename Self>
110 [[nodiscard]]
111 auto getArray(this Self &self)
112 -> ConstLikeRef<Self, SparseArray<T>>;
113
114 template <Component T, Component... Ts, typename Self>
115 [[nodiscard]]
116 auto &getSmallestArray(this Self &self);
117
118 template <Component... Ts>
119 [[nodiscard]]
120 bool hasAllComponents(Entity entity) const noexcept;
121
122 };
123}
124
125#include "Registry.tpp"
126
127#endif /* !RTYPE_REGISTRY_HPP_ */
Concepts for validating component types in the ECS.
Entity identifier for ECS architecture.
Error handling system with categorized error codes.
Logger declaration with support for multiple log levels.
Sparse array container for efficient component storage in ECS.
Comprehensive error object with severity and retry tracking.
Definition Error.hpp:156
Represents an entity in the ECS (Entity-Component-System) architecture.
Definition Entity.hpp:63
Type-erased base interface for sparse arrays.
bool has(Entity entity) const noexcept
bool hasAllComponents(Entity entity) const noexcept
auto spawn(void) -> std::expected< Entity, rtp::Error >
Definition Registry.cpp:51
std::size_t entityCount(void) const noexcept
Definition Registry.cpp:127
auto subscribe(this Self &self) -> std::expected< std::reference_wrapper< ConstLike< Self, SparseArray< T > > >, rtp::Error >
std::deque< std::size_t > _freeIndices
Recyclable entity indices.
Definition Registry.hpp:105
auto getArray(this Self &self) -> ConstLikeRef< Self, SparseArray< T > >
void kill(Entity entity)
Definition Registry.cpp:73
std::unordered_map< std::type_index, std::unique_ptr< ISparseArray > > _arrays
Registered component arrays.
Definition Registry.hpp:103
std::size_t componentCount(void) const noexcept
std::vector< std::uint32_t > _generations
Generation counters for entities.
Definition Registry.hpp:104
auto view(this Self &self)
void clear(void) noexcept
Definition Registry.cpp:102
auto & getSmallestArray(this Self &self)
std::shared_mutex _mutex
Mutex for thread-safe operations.
Definition Registry.hpp:106
bool isAlive(Entity entity) const noexcept
Definition Registry.cpp:90
auto add(Entity entity, Args &&...args) -> std::expected< std::reference_wrapper< T >, rtp::Error >
auto get(this const Self &self) -> std::expected< std::reference_wrapper< ConstLike< Self, SparseArray< T > > >, rtp::Error >
void purge(void) noexcept
Definition Registry.cpp:113
auto zipView(this Self &self)
void remove(Entity entity) noexcept
~Registry() noexcept=default
Sparse array container for component storage.
Concept for valid ECS component types.
File : RenderSystem.hpp License: MIT Author : Elias Josué HAJJAR LLAUQUEN elias-josue....
std::conditional_t< std::is_const_v< std::remove_reference_t< From > >, std::add_const_t< To >, To > ConstLike
Definition Registry.hpp:40
ConstLike< From, To > & ConstLikeRef
Definition Registry.hpp:43
STL namespace.