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

Type registry class to store types in cross-platform way. More...

#include <TypeRegistry.hpp>

Collaboration diagram for ecstasy::rtti::TypeRegistry:

Public Types

using Predicate = std::function< bool(const std::pair< const std::size_t, std::unique_ptr< AType > > &)>
 Predicate type for custom type search.
 
using OptionalATypeReference = std::optional< std::reference_wrapper< AType > >
 Alias to an optional reference to a type (to be less verbose).
 

Public Member Functions

template<typename T >
ATyperegisterType (std::string_view name)
 Register a type in the registry.
 
template<is_comparable_with_atype T>
bool has (const T &target) const noexcept
 Check if a type is registered.
 
bool has (size_t name_hash) const noexcept
 Check if a type is registered from its hash.
 
template<typename T >
bool has () const noexcept
 Check if a type is registered.
 
OptionalATypeReference findIf (const Predicate &p) const noexcept
 Calls std::find_if on the types map using the given predicate.
 
ATypegetIf (const Predicate &p) const
 Get a reference to the AType instance matching the predicate.
 
template<is_comparable_with_atype T>
OptionalATypeReference find (const T &target) const noexcept
 Search for a registered type.
 
OptionalATypeReference find (std::size_t name_hash) const noexcept
 Search for a registered type from its hash.
 
template<typename T >
OptionalATypeReference find () const noexcept
 Search for a registered type.
 
template<is_comparable_with_atype T>
ATypeget (const T &target) const
 Get a reference to the AType instance matching the target.
 
ATypeget (std::size_t name_hash) const
 Get a reference to the AType instance from its name hash.
 
template<typename T >
ATypeget () const
 Get a reference to the AType instance matching the target.
 

Static Public Member Functions

static TypeRegistrygetInstance () noexcept
 Get the Instance object.
 

Private Member Functions

 TypeRegistry () noexcept=default
 Construct a new Type Registry instance.
 
 ~TypeRegistry () noexcept=default
 Destroy the Type Registry instance.
 

Private Attributes

std::unordered_map< std::size_t, std::unique_ptr< AType > > _types
 Map of types indexed by their (cross-platform) hash code.
 

Detailed Description

Type registry class to store types in cross-platform way.

Note
The class is a singleton and should be accessed through the getInstance() method.
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 (2024-10-24)

Definition at line 54 of file TypeRegistry.hpp.

Member Typedef Documentation

◆ OptionalATypeReference

Alias to an optional reference to a type (to be less verbose).

Definition at line 59 of file TypeRegistry.hpp.

◆ Predicate

Predicate type for custom type search.

Definition at line 57 of file TypeRegistry.hpp.

Constructor & Destructor Documentation

◆ TypeRegistry()

ecstasy::rtti::TypeRegistry::TypeRegistry ( )
privatedefaultnoexcept

Construct a new Type Registry instance.

Note
The constructor is private to prevent instantiation.

◆ ~TypeRegistry()

ecstasy::rtti::TypeRegistry::~TypeRegistry ( )
privatedefaultnoexcept

Destroy the Type Registry instance.

Note
The destructor is private to prevent deletion.

Member Function Documentation

◆ find() [1/3]

template<typename T >
OptionalATypeReference ecstasy::rtti::TypeRegistry::find ( ) const
inlinenoexcept

Search for a registered type.

Template Parameters
TType to search.
Returns
OptionalATypeReference Optional reference to the type if found, std::nullopt otherwise.
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 (2024-10-24)

Definition at line 216 of file TypeRegistry.hpp.

217 {
218 return find<std::type_info>(typeid(T));
219 }

◆ find() [2/3]

template<is_comparable_with_atype T>
OptionalATypeReference ecstasy::rtti::TypeRegistry::find ( const T &  target) const
inlinenoexcept

Search for a registered type.

Note
This method call std::find on the internal types map.
Template Parameters
TType to check. It must be equality comparable with AType.
Parameters
[in]targetValue to compare the types with.
Returns
OptionalATypeReference& Optional reference to the type if found, std::nullopt otherwise.
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 (2024-10-24)

Definition at line 182 of file TypeRegistry.hpp.

183 {
184 auto result = std::find_if(_types.begin(), _types.end(), [&target](const auto &pair) {
185 return *pair.second == target;
186 });
187
188 if (result == _types.end())
189 return std::nullopt;
190 return std::ref(*result->second);
191 }
std::unordered_map< std::size_t, std::unique_ptr< AType > > _types
Map of types indexed by their (cross-platform) hash code.
T find_if(T... args)
T ref(T... args)

◆ find() [3/3]

TypeRegistry::OptionalATypeReference ecstasy::rtti::TypeRegistry::find ( std::size_t  name_hash) const
noexcept

Search for a registered type from its hash.

Parameters
[in]name_hashHash of the type name.
Returns
OptionalATypeReference Optional reference to the type if found, std::nullopt otherwise.
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 (2024-10-31)

Definition at line 49 of file TypeRegistry.cpp.

50 {
51 if (_types.contains(name_hash))
52 return std::ref(*(_types.at(name_hash)));
53 return std::nullopt;
54 }

◆ findIf()

TypeRegistry::OptionalATypeReference ecstasy::rtti::TypeRegistry::findIf ( const Predicate p) const
noexcept

Calls std::find_if on the types map using the given predicate.

Parameters
[in]pPredicate to use for the search.
Returns
OptionalATypeReference Optional reference to the type if found, std::nullopt otherwise.
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 (2024-10-24)

Definition at line 31 of file TypeRegistry.cpp.

32 {
33 auto result = std::find_if(_types.begin(), _types.end(), p);
34
35 if (result == _types.end())
36 return std::nullopt;
37 return std::ref(*result->second);
38 }

◆ get() [1/3]

template<typename T >
AType & ecstasy::rtti::TypeRegistry::get ( ) const
inline

Get a reference to the AType instance matching the target.

Template Parameters
TType to search.
Returns
AType& Reference to the type.
Exceptions
std::out_of_rangeIf the type is not found.
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 (2024-10-24)

Definition at line 272 of file TypeRegistry.hpp.

273 {
274 return get<std::type_info>(typeid(T));
275 }

◆ get() [2/3]

template<is_comparable_with_atype T>
AType & ecstasy::rtti::TypeRegistry::get ( const T &  target) const
inline

Get a reference to the AType instance matching the target.

Template Parameters
TType to check. It must be equality comparable with AType.
Parameters
[in]targetValue to compare the types with.
Returns
AType& Reference to the type.
Exceptions
std::out_of_rangeIf the type is not found.
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 (2024-10-24)

Definition at line 236 of file TypeRegistry.hpp.

237 {
238 auto result = find(target);
239
240 if (result.has_value())
241 return result.value();
242 throw std::out_of_range("Type not found.");
243 }
OptionalATypeReference find() const noexcept
Search for a registered type.

◆ get() [3/3]

AType & ecstasy::rtti::TypeRegistry::get ( std::size_t  name_hash) const

Get a reference to the AType instance from its name hash.

Parameters
[in]name_hashHash of the type name.
Returns
AType& Reference to the type.
Exceptions
std::out_of_rangeIf the type is not found.
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 (2024-10-31)

Definition at line 56 of file TypeRegistry.cpp.

57 {
58 return *(_types.at(name_hash));
59 }

◆ getIf()

AType & ecstasy::rtti::TypeRegistry::getIf ( const Predicate p) const

Get a reference to the AType instance matching the predicate.

Parameters
[in]pPredicate to use for the search.
Returns
AType& Reference to the type.
Exceptions
std::out_of_rangeIf the type is not found.
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 (2024-10-24)

Definition at line 40 of file TypeRegistry.cpp.

41 {
42 auto result = findIf(p);
43
44 if (result.has_value())
45 return result.value();
46 throw std::out_of_range("Type not found.");
47 }
OptionalATypeReference findIf(const Predicate &p) const noexcept
Calls std::find_if on the types map using the given predicate.

◆ getInstance()

TypeRegistry & ecstasy::rtti::TypeRegistry::getInstance ( )
staticnoexcept

Get the Instance object.

Returns
TypeRegistry& The instance of the TypeRegistry.
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 (2024-10-24)

Definition at line 17 of file TypeRegistry.cpp.

18 {
19 static TypeRegistry instance;
20
21 return instance;
22 }
TypeRegistry() noexcept=default
Construct a new Type Registry instance.

◆ has() [1/3]

template<typename T >
bool ecstasy::rtti::TypeRegistry::has ( ) const
inlinenoexcept

Check if a type is registered.

Template Parameters
TType to check.
Returns
bool True if the type is registered, false otherwise.
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 (2024-10-24)

Definition at line 136 of file TypeRegistry.hpp.

137 {
138 return has<std::type_info>(typeid(T));
139 }

◆ has() [2/3]

template<is_comparable_with_atype T>
bool ecstasy::rtti::TypeRegistry::has ( const T &  target) const
inlinenoexcept

Check if a type is registered.

This calls find() and checks if the result is not std::nullopt.

Template Parameters
TType to check.
Parameters
[in]targetType to check.
Returns
bool True if the type is registered, false otherwise.
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 (2024-10-24)

Definition at line 108 of file TypeRegistry.hpp.

109 {
110 return find(target).has_value();
111 }
T has_value(T... args)

◆ has() [3/3]

bool ecstasy::rtti::TypeRegistry::has ( size_t  name_hash) const
noexcept

Check if a type is registered from its hash.

Parameters
[in]name_hashHash of the type name.
Returns
bool True if the type is registered, false otherwise.
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 (2024-10-31)

◆ registerType()

template<typename T >
AType & ecstasy::rtti::TypeRegistry::registerType ( std::string_view  name)
inline

Register a type in the registry.

Template Parameters
TType to register.
Parameters
[in]nameName of the type.
Returns
bool True if the type was successfully registered, false if it was already registered.
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 (2024-10-24)

Definition at line 84 of file TypeRegistry.hpp.

85 {
86 size_t hash = std::hash<std::string_view>{}(name);
87 OptionalATypeReference type = find(hash);
88
89 if (type.has_value())
90 return type.value();
91 else
92 return *_types.emplace(hash, std::make_unique<Type<T>>(name)).first->second;
93 }
std::optional< std::reference_wrapper< AType > > OptionalATypeReference
Alias to an optional reference to a type (to be less verbose).
T make_unique(T... args)
Type

Member Data Documentation

◆ _types

std::unordered_map<std::size_t, std::unique_ptr<AType> > ecstasy::rtti::TypeRegistry::_types
private

Map of types indexed by their (cross-platform) hash code.

Definition at line 279 of file TypeRegistry.hpp.


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