ALib C++ Library
Library Version: 2510 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
30 /// Hash functor for this key type.
31 struct Hash
32 {
33 /// Calculate hash code.
34 /// @param key The key to calculate a hash value for.
35 /// @return A hash code for this key.
36 std::size_t operator()( const EnumRecordKey & key ) const
37 {
38 size_t result= key.RTTI.hash_code()
39 ^ size_t(key.Element);
40 #if ALIB_SIZEOF_INTEGER == 4
41 result^= (result >> 17);
42 #else
43 result^= (result >> 33);
44 #endif
45 return result;
46 }
47 };
48
49 /// Compare functor for this key type.
50 struct EqualTo
51 {
52 /// Compares two keys.
53 /// @param lhs The left-hand side value.
54 /// @param rhs The right-hand side value.
55 /// @return \c true if the two keys are equal, \c false otherwise.
56 bool operator()( const EnumRecordKey & lhs,
57 const EnumRecordKey & rhs ) const
58 {
59 return lhs.RTTI == rhs.RTTI
60 && lhs.Element == rhs.Element;
61 }
62 };
63}; // EnumRecordKey
64
65
66#if ALIB_MONOMEM && ALIB_CONTAINERS
67//==================================================================================================
68/// This detail function returns \e the central hash table that stores enum records of all types.
69/// A constant reference is returned, which is in alignment with the general contract of this
70/// concept that considers enum records as static data which must not be modified after
71/// bootstrapping a process.
72///
73/// Consequently, there are no mechanisms to protect parallel access, because after bootstrap
74/// only read operations are allowed.
75///
76/// Before bootstrapping, the returned map may be <c>const_cast</c>'ed to a mutable reference
77/// for the sake of modifying the base- and maximum load factors as well as for reserving
78/// a certain element capacity. The default values are already raised to \c 3.0 and \c 6.0,
79/// as the retrieval of enumeration records is not considered to be done in time-critical
80/// code units.
81///
82/// Direct access to this map is not recommended, and useful only in seldom cases.
83///
84/// A good sample for its use, is to provide debug-output of all defined records for a type in the
85/// case that a record for a certain enumeration element of that type was not found, and such
86/// output is required to be performed in a code unit that has lost (templated) compile-time type
87/// information.
88///
89/// \note
90/// Run-time access to single records is provided with function
91/// \alib{enumrecords;detail::getEnumRecord(const std::type_info& rtti; integer)} and encapsulated
92/// in type \alib{boxing;Enum} of the module \alib_boxing.
93///
94/// \attention
95/// In the absence of the module \alib_monomem in an \alibbuild, the returned type changes to
96/// <c>std::unordered_map</c>.
97///
98/// @return A constant reference to the internal hash map storing all enum records.
99//==================================================================================================
101 EnumRecordKey, const void*,
102 EnumRecordKey::Hash,
103 EnumRecordKey::EqualTo >& getInternalRecordMap();
104#else
105std::unordered_map< EnumRecordKey, const void*,
106 EnumRecordKey::Hash,
107 EnumRecordKey::EqualTo >& getInternalRecordMap();
108#endif
109
110}}} // namespace [alib::enumrecords::detail]
#define ALIB_EXPORT
Definition alib.inl:488
Details of namespace alib::enumrecords.
HashMap< MonoAllocator, EnumRecordKey, const void *, EnumRecordKey::Hash, EnumRecordKey::EqualTo > & getInternalRecordMap()
Definition records.cpp:89
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.