ECSTASY
All in the name
Loading...
Searching...
No Matches
ecstasy::Instances< Base > Class Template Reference

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

#include <Instances.hpp>

Collaboration diagram for ecstasy::Instances< Base >:

Public Member Functions

 Instances ()=default
 Construct a new Instances storage.
 
 ~Instances ()=default
 Destroy the Instances storage.
 
 Instances (const Instances &other)=delete
 Instances aren't copy constructible.
 
template<std::derived_from< Base > Derived, typename... Args>
Derived & emplace (Args &&...args)
 Emplace a new instance of type Derived in the storage.
 
const Base & get (const std::type_index &type) const
 Get a const reference to the contained instance identified by type.
 
template<std::derived_from< Base > Derived>
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.
 
template<std::derived_from< Base > Derived>
Derived & get ()
 Get a reference to the contained instance of Derived.
 
Base * getPtr (const std::type_index &type) const
 Get a pointer to the contained instance identified by type.
 
template<std::derived_from< Base > 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< Base > Derived>
bool contains () const
 Check whether an instance of Derived is contained in the set.
 
void clear ()
 Remove all stored instances.
 
constexpr const std::unordered_map< std::type_index, std::unique_ptr< Base > > & getInner () const noexcept
 Get the backing container.
 

Private Attributes

std::unordered_map< std::type_index, std::unique_ptr< Base > > _instances
 The instances container.
 

Detailed Description

template<typename Base>
class ecstasy::Instances< Base >

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 32 of file Instances.hpp.

Constructor & Destructor Documentation

◆ Instances() [1/2]

template<typename Base >
ecstasy::Instances< Base >::Instances ( )
default

Construct a new Instances 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)

◆ ~Instances()

template<typename Base >
ecstasy::Instances< Base >::~Instances ( )
default

Destroy the Instances 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)

◆ Instances() [2/2]

template<typename Base >
ecstasy::Instances< Base >::Instances ( const Instances< Base > &  other)
delete

Instances 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()

template<typename Base >
void ecstasy::Instances< Base >::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 234 of file Instances.hpp.

235 {
237 }
std::unordered_map< std::type_index, std::unique_ptr< Base > > _instances
The instances container.
T clear(T... args)

◆ contains() [1/2]

template<typename Base >
template<std::derived_from< Base > Derived>
bool ecstasy::Instances< Base >::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 Base.
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 223 of file Instances.hpp.

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

◆ contains() [2/2]

template<typename Base >
bool ecstasy::Instances< Base >::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 207 of file Instances.hpp.

208 {
209 return _instances.contains(type);
210 }
T contains(T... args)

◆ emplace()

template<typename Base >
template<std::derived_from< Base > Derived, typename... Args>
Derived & ecstasy::Instances< Base >::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 76 of file Instances.hpp.

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

◆ get() [1/4]

template<typename Base >
template<std::derived_from< Base > Derived>
Derived & ecstasy::Instances< Base >::get ( )
inline

Get a reference to the contained instance of Derived.

Template Parameters
DerivedType of the instance to get, must inherit Base.
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 153 of file Instances.hpp.

154 {
155 return *getPtr<Derived>();
156 }

◆ get() [2/4]

template<typename Base >
template<std::derived_from< Base > Derived>
const Derived & ecstasy::Instances< Base >::get ( ) const
inline

Get a const reference to the contained instance of Derived.

Template Parameters
DerivedType of the instance to get, must inherit Base.
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 118 of file Instances.hpp.

119 {
120 return *getPtr<Derived>();
121 }

◆ get() [3/4]

template<typename Base >
Base & ecstasy::Instances< Base >::get ( const std::type_index type)
inline

Get a reference to the contained instance identified by type.

Parameters
[in]typethe instance type index.
Returns
Base& A reference to the instance of Base 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 135 of file Instances.hpp.

136 {
137 return *getPtr(type);
138 }
Derived * getPtr() const
Get a pointer to the contained instance of Derived.

◆ get() [4/4]

template<typename Base >
const Base & ecstasy::Instances< Base >::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 Base& A reference to the instance of Base 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 100 of file Instances.hpp.

101 {
102 return *getPtr(type);
103 }

◆ getInner()

template<typename Base >
constexpr const std::unordered_map< std::type_index, std::unique_ptr< Base > > & ecstasy::Instances< Base >::getInner ( ) const
inlineconstexprnoexcept

Get the backing container.

Returns
const std::unordered_map<std::type_index, std::unique_ptr<Base>>& 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 248 of file Instances.hpp.

249 {
250 return _instances;
251 }

◆ getPtr() [1/2]

template<typename Base >
template<std::derived_from< Base > Derived>
Derived * ecstasy::Instances< Base >::getPtr ( ) const
inline

Get a pointer to the contained instance of Derived.

Template Parameters
DerivedType of the instance to get, must inherit Base.
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 192 of file Instances.hpp.

193 {
194 return dynamic_cast<Derived *>(getPtr(std::type_index(typeid(Derived))));
195 }

◆ getPtr() [2/2]

template<typename Base >
Base * ecstasy::Instances< Base >::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
Base* A pointer to the instance of Base 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 170 of file Instances.hpp.

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

Member Data Documentation

◆ _instances

template<typename Base >
std::unordered_map<std::type_index, std::unique_ptr<Base> > ecstasy::Instances< Base >::_instances
private

The instances container.

Definition at line 255 of file Instances.hpp.


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