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