ECSTASY
All in the name
Loading...
Searching...
No Matches
Instances.hpp
Go to the documentation of this file.
1
11
12#ifndef ECSTASY_STORAGE_INSTANCES_HPP_
13#define ECSTASY_STORAGE_INSTANCES_HPP_
14
15#include <concepts>
16#include <memory>
17#include <stdexcept>
18#include <typeindex>
19#include <unordered_map>
20
21namespace ecstasy
22{
31 template <typename Base>
32 class Instances {
33 public:
40 Instances() = default;
41
48 ~Instances() = default;
49
58 Instances(const Instances &other) = delete;
59
75 template <std::derived_from<Base> Derived, typename... Args>
76 Derived &emplace(Args &&...args)
77 {
78 std::type_index id(typeid(Derived));
79
80 if (_instances.contains(id)) [[unlikely]]
81 throw std::logic_error("Duplicate instance");
82
83 return *dynamic_cast<Derived *>(
84 _instances.emplace(std::make_pair(id, std::make_unique<Derived, Args...>(std::forward<Args>(args)...)))
85 .first->second.get());
86 }
87
100 [[nodiscard]] const Base &get(const std::type_index &type) const
101 {
102 return *getPtr(type);
103 }
104
117 template <std::derived_from<Base> Derived>
118 [[nodiscard]] const Derived &get() const
119 {
120 return *getPtr<Derived>();
121 }
122
135 [[nodiscard]] Base &get(const std::type_index &type)
136 {
137 return *getPtr(type);
138 }
139
152 template <std::derived_from<Base> Derived>
153 [[nodiscard]] Derived &get()
154 {
155 return *getPtr<Derived>();
156 }
157
170 [[nodiscard]] Base *getPtr(const std::type_index &type) const
171 {
172 const auto &valueIt = _instances.find(type);
173
174 if (valueIt == _instances.end()) [[unlikely]]
175 throw std::logic_error("Instance not found");
176 return valueIt->second.get();
177 }
178
191 template <std::derived_from<Base> Derived>
192 [[nodiscard]] Derived *getPtr() const
193 {
194 return dynamic_cast<Derived *>(getPtr(std::type_index(typeid(Derived))));
195 }
196
207 [[nodiscard]] bool contains(const std::type_index &type) const
208 {
209 return _instances.contains(type);
210 }
211
222 template <std::derived_from<Base> Derived>
223 [[nodiscard]] bool contains() const
224 {
225 return contains(std::type_index(typeid(Derived)));
226 }
227
234 void clear()
235 {
237 }
238
247 [[nodiscard]] constexpr const std::unordered_map<std::type_index, std::unique_ptr<Base>> &
248 getInner() const noexcept
249 {
250 return _instances;
251 }
252
253 private:
256 };
257} // namespace ecstasy
258
259#endif /* !ECSTASY_STORAGE_INSTANCES_HPP_ */
Set of instances inheriting from the Base type.
Definition Instances.hpp:32
const Base & get(const std::type_index &type) const
Get a const reference to the contained instance identified by type.
Instances()=default
Construct a new Instances storage.
Instances(const Instances &other)=delete
Instances aren't copy constructible.
constexpr const std::unordered_map< std::type_index, std::unique_ptr< Base > > & getInner() const noexcept
Get the backing container.
Derived & get()
Get a reference to the contained instance of Derived.
Derived & emplace(Args &&...args)
Emplace a new instance of type Derived in the storage.
Definition Instances.hpp:76
~Instances()=default
Destroy the Instances storage.
bool contains(const std::type_index &type) const
Check wheter the instance identified by type is contained in the set.
bool contains() const
Check whether an instance of Derived is contained in the set.
void clear()
Remove all stored instances.
const Derived & get() const
Get a const reference to the contained instance of Derived.
Base & get(const std::type_index &type)
Get a reference to the contained instance identified by type.
std::unordered_map< std::type_index, std::unique_ptr< Base > > _instances
The instances container.
Derived * getPtr() const
Get a pointer to the contained instance of Derived.
Base * getPtr(const std::type_index &type) const
Get a pointer to the contained instance identified by type.
T clear(T... args)
T contains(T... args)
T emplace(T... args)
T end(T... args)
T find(T... args)
T make_pair(T... args)
Namespace containing all symbols specific to ecstasy.
Definition ecstasy.hpp:30