r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
Registry.hpp
Go to the documentation of this file.
1///
2/// @file Registry.hpp
3/// @brief This file contains the Registry class declaration
4/// @namespace ecs
5///
6
7#pragma once
8
9#include <functional>
10#include <memory>
11#include <typeindex>
12#include <unordered_map>
13#include <vector>
14
15#include "ECS/Entity.hpp"
16
17namespace ecs
18{
19 ///
20 /// @class Registry
21 /// @brief Class for managing entities and their components
22 /// @namespace ecs
23 ///
25 {
26 public:
27 Registry() = default;
28 ~Registry() = default;
29
30 Registry(const Registry &) = delete;
31 Registry &operator=(const Registry &) = delete;
32 Registry(Registry &&) = delete;
34
36 {
37 public:
39
40 template <typename T, typename... Args> EntityBuilder &with(Args &&...args)
41 {
42 m_registry.addComponent<T>(m_entity, std::forward<Args>(args)...);
43 return *this;
44 }
45
46 Entity build() const { return m_entity; }
47
48 private:
51 };
52
54 {
55 const Entity entity = ++m_lastEntity;
56 m_entities.push_back(entity);
57 return EntityBuilder(*this, entity);
58 }
59
60 template <typename T, typename... Args> T &addComponent(Entity e, Args &&...args)
61 {
62 auto &pool = getPool<T>();
63 T &comp = pool.add(e, std::forward<Args>(args)...);
64 for (auto &cb : m_onComponentAddedCallbacks)
65 {
66 cb(e, typeid(T));
67 }
68 return comp;
69 }
70
71 template <typename T> T *getComponent(Entity e)
72 {
73 auto &pool = getPool<T>();
74 return pool.get(e);
75 }
76
77 template <typename T> std::unordered_map<Entity, T> &getAll() { return getPool<T>().data; }
78
79 template <typename T> bool hasComponent(Entity e)
80 {
81 auto &pool = getPool<T>();
82 return pool.has(e);
83 }
84
85 template <typename T> void removeComponent(Entity e)
86 {
87 auto &pool = getPool<T>();
88 pool.remove(e);
89 }
90
91 void onComponentAdded(std::function<void(Entity, const std::type_info &)> cb)
92 {
93 m_onComponentAddedCallbacks.push_back(std::move(cb));
94 }
95
96 private:
97 class IPool
98 {
99 public:
100 virtual ~IPool() = default;
101 virtual void remove(Entity e) = 0;
102 };
103
104 template <typename T> class Pool final : public IPool
105 {
106 public:
107 std::unordered_map<Entity, T> data;
108
109 template <typename... Args> T &add(Entity e, Args &&...args)
110 {
111 return data.emplace(e, T{std::forward<Args>(args)...}).first->second;
112 }
113
115 {
116 auto it = data.find(e);
117 if (it != data.end())
118 {
119 return &it->second;
120 }
121 return nullptr;
122 }
123
124 bool has(Entity e) { return data.contains(e); }
125
126 void remove(Entity e) override { data.erase(e); }
127 };
128
129 template <typename T> Pool<T> &getPool()
130 {
131 const std::type_index ti(typeid(T));
132 if (!m_components.contains(ti))
133 {
134 m_components[ti] = std::make_unique<Pool<T>>();
135 }
136 return *static_cast<Pool<T> *>(m_components[ti].get());
137 }
139 std::vector<Entity> m_entities;
140 std::unordered_map<std::type_index, std::unique_ptr<IPool>> m_components;
141 std::vector<std::function<void(Entity, const std::type_info &)>> m_onComponentAddedCallbacks;
142
143 }; // class Registry
144
145} // namespace ecs
This file contains the entity definitions.
EntityBuilder(Registry &reg, Entity e)
Definition Registry.hpp:38
EntityBuilder & with(Args &&...args)
Definition Registry.hpp:40
virtual void remove(Entity e)=0
virtual ~IPool()=default
std::unordered_map< Entity, T > data
Definition Registry.hpp:107
void remove(Entity e) override
Definition Registry.hpp:126
T * get(Entity e)
Definition Registry.hpp:114
bool has(Entity e)
Definition Registry.hpp:124
T & add(Entity e, Args &&...args)
Definition Registry.hpp:109
Class for managing entities and their components.
Definition Registry.hpp:25
EntityBuilder createEntity()
Definition Registry.hpp:53
Registry & operator=(Registry &&)=delete
Registry & operator=(const Registry &)=delete
~Registry()=default
Registry()=default
Pool< T > & getPool()
Definition Registry.hpp:129
std::vector< Entity > m_entities
Definition Registry.hpp:139
Registry(Registry &&)=delete
std::unordered_map< std::type_index, std::unique_ptr< IPool > > m_components
Definition Registry.hpp:140
bool hasComponent(Entity e)
Definition Registry.hpp:79
std::vector< std::function< void(Entity, const std::type_info &)> > m_onComponentAddedCallbacks
Definition Registry.hpp:141
void onComponentAdded(std::function< void(Entity, const std::type_info &)> cb)
Definition Registry.hpp:91
T & addComponent(Entity e, Args &&...args)
Definition Registry.hpp:60
Registry(const Registry &)=delete
Entity m_lastEntity
Definition Registry.hpp:138
std::unordered_map< Entity, T > & getAll()
Definition Registry.hpp:77
T * getComponent(Entity e)
Definition Registry.hpp:71
void removeComponent(Entity e)
Definition Registry.hpp:85
std::uint32_t Entity
Definition Entity.hpp:13
constexpr Entity INVALID_ENTITY
Definition Entity.hpp:14