ALib C++ Library
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
enumrecordmap.inl
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of the module \alib_enumrecords of the \aliblong.
4///
5/// \emoji :copyright: 2013-2025 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8ALIB_EXPORT namespace alib { namespace enumrecords { namespace detail {
9
10//==================================================================================================
11/// The key-type of the central hash table that stores all enum records.
12/// Contains inner functors for hashing and comparing.
13//==================================================================================================
15{
16 /// Run-time type information on the enumeration type
17 const std::type_info& RTTI;
18
19 /// Integral value of the enumeration element.
21
22 /// Constructor.
23 /// @param rtti Assigned to #RTTI.
24 /// @param element Assigned to #Element.
25 EnumRecordKey(const std::type_info& rtti, integer element)
26 : RTTI (rtti)
27 , Element(element) {}
28
29 /// Hash functor for this key type.
30 struct Hash
31 {
32 /// Calculate hash code.
33 /// @param key The key to calculate a hash value for.
34 /// @return A hash code for this key.
35 std::size_t operator()( const EnumRecordKey & key ) const {
36 size_t result= key.RTTI.hash_code()
37 ^ size_t(key.Element);
38 #if ALIB_SIZEOF_INTEGER == 4
39 result^= (result >> 17);
40 #else
41 result^= (result >> 33);
42 #endif
43 return result;
44 }
45 };
46
47 /// Compare functor for this key type.
48 struct EqualTo
49 {
50 /// Compares two keys.
51 /// @param lhs The left-hand side value.
52 /// @param rhs The right-hand side value.
53 /// @return \c true if the two keys are equal, \c false otherwise.
54 bool operator()( const EnumRecordKey & lhs,
55 const EnumRecordKey & rhs ) const
56 {
57 return lhs.RTTI == rhs.RTTI
58 && lhs.Element == rhs.Element;
59 }
60 };
61}; // EnumRecordKey
62
63
64#if ALIB_MONOMEM && ALIB_CONTAINERS
65//==================================================================================================
66/// This detail function returns \e the central hash table that stores enum records of all types.
67/// A constant reference is returned, which is in alignment with the general contract of this
68/// concept that considers enum records as static data which must not be modified after
69/// bootstrapping a process.
70///
71/// Consequently, there are no mechanisms to protect parallel access, because after bootstrap
72/// only read operations are allowed.
73///
74/// Before bootstrapping, the returned map may be <c>const_cast</c>'ed to a mutable reference
75/// for the sake of modifying the base- and maximum load factors as well as for reserving
76/// a certain element capacity. The default values are already raised to \c 3.0 and \c 6.0,
77/// as the retrieval of enumeration records is not considered to be done in time-critical
78/// code units.
79///
80/// Direct access to this map is not recommended, and useful only in seldom cases.
81///
82/// A good sample for its use, is to provide debug-output of all defined records for a type in the
83/// case that a record for a certain enumeration element of that type was not found, and such
84/// output is required to be performed in a code unit that has lost (templated) compile-time type
85/// information.
86///
87/// \note
88/// Run-time access to single records is provided with function
89/// \alib{enumrecords;detail::getEnumRecord(const std::type_info& rtti; integer)} and encapsulated
90/// in type \alib{boxing;Enum} of the module \alib_boxing.
91///
92/// \attention
93/// In the absence of the module \alib_monomem in an \alibbuild, the returned type changes to
94/// <c>std::unordered_map</c>.
95///
96/// @return A constant reference to the internal hash map storing all enum records.
97//==================================================================================================
99 EnumRecordKey, const void*,
100 EnumRecordKey::Hash,
101 EnumRecordKey::EqualTo >& getInternalRecordMap();
102#else
103std::unordered_map< EnumRecordKey, const void*,
104 EnumRecordKey::Hash,
105 EnumRecordKey::EqualTo >& getInternalRecordMap();
106#endif
107
108}}} // namespace [alib::enumrecords::detail]
#define ALIB_EXPORT
Definition alib.inl:497
Details of namespace alib::enumrecords.
HashMap< MonoAllocator, EnumRecordKey, const void *, EnumRecordKey::Hash, EnumRecordKey::EqualTo > & getInternalRecordMap()
Definition records.cpp:87
lang::integer integer
Type alias in namespace alib.
Definition integers.inl:149
containers::HashMap< TAllocator, TKey, TMapped, THash, TEqual, THashCaching, TRecycling > HashMap
Type alias in namespace alib.
monomem::TMonoAllocator< lang::HeapAllocator > MonoAllocator
bool operator()(const EnumRecordKey &lhs, const EnumRecordKey &rhs) const
std::size_t operator()(const EnumRecordKey &key) const
EnumRecordKey(const std::type_info &rtti, integer element)
const std::type_info & RTTI
Run-time type information on the enumeration type.
integer Element
Integral value of the enumeration element.