ECSTASY
All in the name
Loading...
Searching...
No Matches
ecstasy::SystemInstances Class Reference

Set of instances inheriting from the Base type. More...

#include <SystemInstances.hpp>

Collaboration diagram for ecstasy::SystemInstances:

Classes

class  Comparer
 

Public Member Functions

 SystemInstances ()=default
 Construct a new SystemInstances storage.
 
 ~SystemInstances ()=default
 Destroy the SystemInstances storage.
 
 SystemInstances (const SystemInstances &other)=delete
 SystemInstances aren't copy constructible.
 
template<std::derived_from< ISystem > Derived, size_t Priority = 0, typename... Args>
Derived & emplace (Args &&...args)
 Emplace a new instance of type Derived in the storage.
 
const ISystemget (const std::type_index &type) const
 Get a const reference to the contained instance identified by type.
 
template<std::derived_from< ISystem > Derived>
const Derived & get () const
 Get a const reference to the contained instance of Derived.
 
ISystemget (const std::type_index &type)
 Get a reference to the contained instance identified by type.
 
template<std::derived_from< ISystem > Derived>
Derived & get ()
 Get a reference to the contained instance of Derived.
 
ISystemgetPtr (const std::type_index &type) const
 Get a pointer to the contained instance identified by type.
 
template<std::derived_from< ISystem > Derived>
Derived * getPtr () const
 Get a pointer to the contained instance of Derived.
 
bool contains (const std::type_index &type) const
 Check wheter the instance identified by type is contained in the set.
 
template<std::derived_from< ISystem > Derived>
bool contains () const
 Check whether an instance of Derived is contained in the set.
 
void clear ()
 Remove all stored instances.
 
constexpr const std::map< key_type, std::unique_ptr< ISystem >, Comparer > & getInner () const noexcept
 Get the backing container.
 

Private Types

using key_type = std::pair< std::type_index, size_t >
 The internal map key type.
 

Private Attributes

std::map< key_type, std::unique_ptr< ISystem >, Comparer_instances
 The internal map of instances.
 

Detailed Description

Set of instances inheriting from the Base type.

Template Parameters
Basebase type for all instances.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-17)

Definition at line 33 of file SystemInstances.hpp.

Member Typedef Documentation

◆ key_type

The internal map key type.

Definition at line 36 of file SystemInstances.hpp.

Constructor & Destructor Documentation

◆ SystemInstances() [1/2]

ecstasy::SystemInstances::SystemInstances ( )
default

Construct a new SystemInstances storage.

Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-17)

◆ ~SystemInstances()

ecstasy::SystemInstances::~SystemInstances ( )
default

Destroy the SystemInstances storage.

All elements are freed.

Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-17)

◆ SystemInstances() [2/2]

ecstasy::SystemInstances::SystemInstances ( const SystemInstances other)
delete

SystemInstances aren't copy constructible.

Parameters
[in]otherinstance to copy.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-17)

Member Function Documentation

◆ clear()

void ecstasy::SystemInstances::clear ( )
inline

Remove all stored instances.

Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-17)

Definition at line 310 of file SystemInstances.hpp.

311 {
312 _instances.clear();
313 }
std::map< key_type, std::unique_ptr< ISystem >, Comparer > _instances
The internal map of instances.

◆ contains() [1/2]

template<std::derived_from< ISystem > Derived>
bool ecstasy::SystemInstances::contains ( ) const
inline

Check whether an instance of Derived is contained in the set.

Template Parameters
DerivedThe type of instance to test, must inherit from ISystem.
Returns
Whether the given instance type is contained in the set.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-17)

Definition at line 299 of file SystemInstances.hpp.

300 {
301 return contains(std::type_index(typeid(Derived)));
302 }
bool contains() const
Check whether an instance of Derived is contained in the set.

◆ contains() [2/2]

bool ecstasy::SystemInstances::contains ( const std::type_index type) const
inline

Check wheter the instance identified by type is contained in the set.

Parameters
[in]typethe instance type index.
Returns
Whether the given type match one of the instance contained in the set.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-17)

Definition at line 283 of file SystemInstances.hpp.

284 {
285 return _instances.contains(type);
286 }

◆ emplace()

template<std::derived_from< ISystem > Derived, size_t Priority = 0, typename... Args>
Derived & ecstasy::SystemInstances::emplace ( Args &&...  args)
inline

Emplace a new instance of type Derived in the storage.

Template Parameters
DerivedThe type of the instance to create. It must inherit from Base.
ArgsThe arguments to pass to the constructor of Derived.
Parameters
[in]argsThe arguments to pass to the constructor of Derived.
Returns
Derived& A reference to the newly created object.
Exceptions
std::logic_errorIf an instance of type Derived was already set.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-17)

Definition at line 151 of file SystemInstances.hpp.

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 }
T make_pair(T... args)

◆ get() [1/4]

template<std::derived_from< ISystem > Derived>
Derived & ecstasy::SystemInstances::get ( )
inline

Get a reference to the contained instance of Derived.

Template Parameters
DerivedType of the instance to get, must inherit ISystem.
Returns
Derived& A reference to the instance of Derived contained within.
Exceptions
std::logic_errorIf no instance of Derived is in the set.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-17)

Definition at line 229 of file SystemInstances.hpp.

230 {
231 return *getPtr<Derived>();
232 }

◆ get() [2/4]

template<std::derived_from< ISystem > Derived>
const Derived & ecstasy::SystemInstances::get ( ) const
inline

Get a const reference to the contained instance of Derived.

Template Parameters
DerivedType of the instance to get, must inherit ISystem.
Returns
const Derived& A reference to the instance of Derived contained within.
Exceptions
std::logic_errorIf no instance of Derived is in the set.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-17)

Definition at line 194 of file SystemInstances.hpp.

195 {
196 return *getPtr<Derived>();
197 }

◆ get() [3/4]

ISystem & ecstasy::SystemInstances::get ( const std::type_index type)
inline

Get a reference to the contained instance identified by type.

Parameters
[in]typethe instance type index.
Returns
ISystem& A reference to the instance of ISystem contained within.
Exceptions
std::logic_errorIf no instance matching type is in the set.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-17)

Definition at line 211 of file SystemInstances.hpp.

212 {
213 return *getPtr(type);
214 }
Derived * getPtr() const
Get a pointer to the contained instance of Derived.

◆ get() [4/4]

const ISystem & ecstasy::SystemInstances::get ( const std::type_index type) const
inline

Get a const reference to the contained instance identified by type.

Parameters
[in]typethe instance type index.
Returns
const ISystem& A reference to the instance of ISystem contained within.
Exceptions
std::logic_errorIf no instance matching type is in the set.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-17)

Definition at line 176 of file SystemInstances.hpp.

177 {
178 return *getPtr(type);
179 }

◆ getInner()

constexpr const std::map< key_type, std::unique_ptr< ISystem >, Comparer > & ecstasy::SystemInstances::getInner ( ) const
inlineconstexprnoexcept

Get the backing container.

Returns
const std::unordered_map<std::type_index, std::unique_ptr<ISystem>>& backing container.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-17)

Definition at line 323 of file SystemInstances.hpp.

324 {
325 return _instances;
326 }

◆ getPtr() [1/2]

template<std::derived_from< ISystem > Derived>
Derived * ecstasy::SystemInstances::getPtr ( ) const
inline

Get a pointer to the contained instance of Derived.

Template Parameters
DerivedType of the instance to get, must inherit ISystem.
Returns
Derived* A pointer to the instance of Derived contained within.
Exceptions
std::logic_errorIf no instance of Derived is in the set.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-17)

Definition at line 268 of file SystemInstances.hpp.

269 {
270 return dynamic_cast<Derived *>(getPtr(std::type_index(typeid(Derived))));
271 }

◆ getPtr() [2/2]

ISystem * ecstasy::SystemInstances::getPtr ( const std::type_index type) const
inline

Get a pointer to the contained instance identified by type.

Parameters
[in]typethe instance type index.
Returns
ISystem* A pointer to the instance of ISystem contained within.
Exceptions
std::logic_errorIf no instance matching type is in the set.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-10-17)

Definition at line 246 of file SystemInstances.hpp.

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 }

Member Data Documentation

◆ _instances

std::map<key_type, std::unique_ptr<ISystem>, Comparer> ecstasy::SystemInstances::_instances
private

The internal map of instances.

Definition at line 330 of file SystemInstances.hpp.


The documentation for this class was generated from the following file: