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