ECSTASY
All in the name
Loading...
Searching...
No Matches
SystemInstances.hpp
Go to the documentation of this file.
1
11
12#ifndef ECSTASY_STORAGE_SYSTEM_INSTANCES_HPP_
13#define ECSTASY_STORAGE_SYSTEM_INSTANCES_HPP_
14
15#include <concepts>
16#include <map>
17#include <memory>
18#include <stdexcept>
19#include <typeindex>
20
21namespace ecstasy
22{
23 class ISystem;
24
34 private:
37
44 class Comparer {
45 public:
47
54 Comparer() = default;
55
67 bool operator()(const key_type &a, const std::type_index &b) const
68 {
69 return a.first < b;
70 }
71
83 bool operator()(const std::type_index &a, const key_type &b) const
84 {
85 return a < b.first;
86 }
87
99 bool operator()(const key_type &a, const key_type &b) const
100 {
101 if (a.second == b.second)
102 return a.first < b.first;
103 else
104 return a.second < b.second;
105 }
106 };
107
108 public:
115 SystemInstances() = default;
116
123 ~SystemInstances() = default;
124
133 SystemInstances(const SystemInstances &other) = delete;
134
150 template <std::derived_from<ISystem> Derived, size_t Priority = 0, typename... Args>
151 Derived &emplace(Args &&...args)
152 {
153 std::type_index id(typeid(Derived));
154
155 if (_instances.contains(id)) [[unlikely]]
156 throw std::logic_error("Duplicate instance");
157
158 return *dynamic_cast<Derived *>(_instances
159 .emplace(std::make_pair(std::make_pair(id, Priority),
160 std::make_unique<Derived, Args...>(std::forward<Args>(args)...)))
161 .first->second.get());
162 }
163
176 [[nodiscard]] const ISystem &get(const std::type_index &type) const
177 {
178 return *getPtr(type);
179 }
180
193 template <std::derived_from<ISystem> Derived>
194 [[nodiscard]] const Derived &get() const
195 {
196 return *getPtr<Derived>();
197 }
198
211 [[nodiscard]] ISystem &get(const std::type_index &type)
212 {
213 return *getPtr(type);
214 }
215
228 template <std::derived_from<ISystem> Derived>
229 [[nodiscard]] Derived &get()
230 {
231 return *getPtr<Derived>();
232 }
233
246 [[nodiscard]] ISystem *getPtr(const std::type_index &type) const
247 {
248 const auto &valueIt = _instances.find(type);
249
250 if (valueIt == _instances.end()) [[unlikely]]
251 throw std::logic_error("Instance not found");
252 return valueIt->second.get();
253 }
254
267 template <std::derived_from<ISystem> Derived>
268 [[nodiscard]] Derived *getPtr() const
269 {
270 return dynamic_cast<Derived *>(getPtr(std::type_index(typeid(Derived))));
271 }
272
283 [[nodiscard]] bool contains(const std::type_index &type) const
284 {
285 return _instances.contains(type);
286 }
287
298 template <std::derived_from<ISystem> Derived>
299 [[nodiscard]] bool contains() const
300 {
301 return contains(std::type_index(typeid(Derived)));
302 }
303
310 void clear()
311 {
312 _instances.clear();
313 }
314
323 [[nodiscard]] constexpr const std::map<key_type, std::unique_ptr<ISystem>, Comparer> &getInner() const noexcept
324 {
325 return _instances;
326 }
327
328 private:
331 };
332} // namespace ecstasy
333
334#endif /* !ECSTASY_STORAGE_SYSTEM_INSTANCES_HPP_ */
System interface, base class of all systems.
Definition ISystem.hpp:28
bool operator()(const key_type &a, const key_type &b) const
Compare two keys.
bool operator()(const key_type &a, const std::type_index &b) const
Compare a key with a type index.
bool operator()(const std::type_index &a, const key_type &b) const
Compare a type index with a key.
Comparer()=default
Construct a new Comparer object.
Set of instances inheriting from the Base type.
~SystemInstances()=default
Destroy the SystemInstances storage.
Derived & emplace(Args &&...args)
Emplace a new instance of type Derived in the storage.
Derived * getPtr() const
Get a pointer to the contained instance of Derived.
const ISystem & get(const std::type_index &type) const
Get a const reference to the contained instance identified by type.
ISystem * getPtr(const std::type_index &type) const
Get a pointer to the contained instance identified by type.
ISystem & get(const std::type_index &type)
Get a reference to the contained instance identified by type.
bool contains() const
Check whether an instance of Derived is contained in the set.
bool contains(const std::type_index &type) const
Check wheter the instance identified by type is contained in the set.
constexpr const std::map< key_type, std::unique_ptr< ISystem >, Comparer > & getInner() const noexcept
Get the backing container.
SystemInstances()=default
Construct a new SystemInstances storage.
const Derived & get() const
Get a const reference to the contained instance of Derived.
std::map< key_type, std::unique_ptr< ISystem >, Comparer > _instances
The internal map of instances.
SystemInstances(const SystemInstances &other)=delete
SystemInstances aren't copy constructible.
Derived & get()
Get a reference to the contained instance of Derived.
void clear()
Remove all stored instances.
T make_pair(T... args)
Namespace containing all symbols specific to ecstasy.
Definition ecstasy.hpp:30