ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
records.cpp
1// #################################################################################################
2// ALib C++ Library
3//
4// Copyright 2013-2025 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6// #################################################################################################
7#include "alib_precompile.hpp"
8#if !defined(ALIB_C20_MODULES) || ((ALIB_C20_MODULES != 0) && (ALIB_C20_MODULES != 1))
9# error "Symbol ALIB_C20_MODULES has to be given to the compiler as either 0 or 1"
10#endif
11#if ALIB_C20_MODULES
12 module;
13#endif
14// ====================================== Global Fragment ======================================
16#if !ALIB_MONOMEM || !ALIB_CONTAINERS
17# include <unordered_map>
18#endif
19// =========================================== Module ==========================================
20#if ALIB_C20_MODULES
21 module ALib.EnumRecords;
23 import ALib.Lang;
24# if ALIB_MONOMEM && ALIB_CONTAINERS
25 import ALib.Monomem;
27# endif
28#else
29# include "ALib.EnumRecords.H"
31# include "ALib.Lang.H"
32# if ALIB_MONOMEM && ALIB_CONTAINERS
33# include "ALib.Monomem.H"
35# endif
36#endif
37// ====================================== Implementation =======================================
39
40namespace {
41#if ALIB_MONOMEM && ALIB_CONTAINERS
42 #if DOXYGEN
43 /// Global directory to find enum records.
45 EnumRecordKey, const void*,
48 #else
50 EnumRecordKey, const void*,
53 3.0, 6.0 );
54 #endif //if DOXYGEN
55#else
56 std::unordered_map< EnumRecordKey, const void*,
59#endif
60}
61
62void setEnumRecord( const std::type_info& rtti, integer elementValue, const void* record )
63{
64 #if ALIB_MONOMEM && ALIB_CONTAINERS
65 ENUM_RECORD_MAP.EmplaceIfNotExistent( EnumRecordKey(rtti, elementValue ), record );
66 #else
67 ENUM_RECORD_MAP.try_emplace( EnumRecordKey(rtti, elementValue ), record );
68 #endif
69}
70
71const void* getEnumRecord( const std::type_info& rtti, integer elementValue )
72{
73
74#if ALIB_MONOMEM && ALIB_CONTAINERS
75 auto it= ENUM_RECORD_MAP.Find( EnumRecordKey( rtti, elementValue ) );
76#else
77 auto it= ENUM_RECORD_MAP.find( EnumRecordKey( rtti, elementValue ) );
78#endif
79 if ( it != ENUM_RECORD_MAP.end() )
80 return it->second;
81
82 return nullptr;
83}
84
85#if ALIB_MONOMEM && ALIB_CONTAINERS
87 EnumRecordKey, const void*,
88 EnumRecordKey::Hash,
90#else
91std::unordered_map< EnumRecordKey, const void*,
94#endif
95{
96 return ENUM_RECORD_MAP;
97}
98
99} // namespace [alib::enumrecords::detail]
100
101
102// #################################################################################################
103// Implementation of parsing methods of built-in record types.
104// #################################################################################################
105namespace alib::enumrecords {
110
111
112
113// #################################################################################################
114// EnumRecordPrototype (Doxygen only)
115// #################################################################################################
116
117#if DOXYGEN
118namespace alib::enumrecords {
119//==================================================================================================
120/// This struct is \b not part of the library but only provided with the documentation of this
121/// \alibmod_nl.
122///
123/// The struct prototypes what module \alib_enumrecords_nl \b expects from custom types that are
124/// associated to enumerations as the type of \ref alib_enums_records "ALib Enum Records".
125///
126/// Usually, enum records are rather simple structs with fields of scalar or
127/// \https{POD types,en.cppreference.com/w/cpp/types/is_pod} like, \c int, \c double or
128/// \alib{strings;TString;String} and this way remain POD types themselves.
129///
130/// When parsed or otherwise initialized, String members do not need to copy data to an
131/// own buffer, because the input string for parsing
132/// an instance, as well as the parameters of alternative constructors, are deemed to be static data.
133///
134/// The life-cycle of instances of enumeration record is from bootstrapping an application
135/// to its termination. After creation, the data cannot be modified.
136//==================================================================================================
138{
139 /// Default constructor leaving the record undefined. For efficiency, this usually does
140 /// not initialize fields, as those will be overwritten by a subsequent invocation to #Parse.
141 ///
142 /// This constructor is only needed when method #Parse is given. Note that it is advised to
143 /// provide the parsing option and this way also this constructor.
144 EnumRecordPrototype() noexcept = default;
145
146 /// Constructor accepting all necessary arguments to completely define the record.
147 /// This constructor is needed only in the case that records should be defined in a statical
148 /// way which is an alternative to implementing the definition by parsing an (externalized)
149 /// initialization strings. Static definition can be performed with method
150 /// \alib{enumrecords;EnumRecords;Bootstrap(std::initializer_list<Initializer> definitions)}
151 /// but is not recommended, and the definition from parsable stings is preferred.
152 ///
153 /// Note that the parameter's passed when this constructor is invoked, have to be of
154 /// "static nature". For example, the buffers and contents of passed string values are
155 /// deemed to survive the life-cycle of an application. Usually, C++ string literals are passed.
156 ///
157 /// @param myArg1 Assigned to the first custom field.
158 /// @param myArg2 Assigned to the second custom field.
159 /// @param ... Further parameters, assigned to further fields.
160 EnumRecordPrototype( const MyType1& myArg1, MyType2 myArg2, ... ) noexcept;
161
162 /// Implementation has to parse the fields of this record from static interface struct
163 /// \alib{enumrecords::bootstrap;EnumRecordParser}.
164 ///
165 /// For - usually simple - enum records, the process of parsing is limited to reading
166 /// values separated by delimiters. Convenient methods to do so are given by static type
167 /// \alib{enumrecords::bootstrap;EnumRecordParser}. More complex parsing logic may be implemented by
168 /// using the "parser" substring found with \alib{enumrecords::bootstrap;EnumRecordParser::Input} and further
169 /// of its entities.<br>
170 /// Please refer to the documentation of \alib{enumrecords::bootstrap;EnumRecordParser} for all details.
171 /// A source code sample is given in chapter \ref alib_enums_records_resourced_parsing of the
172 /// Programmer's Manual of the module \alib_enumrecords_nl.
173 ///
174 /// The contents (buffer) of the string parsed is by contract of static nature. This means
175 /// that no copies of portions need to be allocated when used as a field value of string-type.
176 /// This is in alignment with the static nature of \ref alib_enums_records "ALib Enum Records"
177 /// and their creation during bootstrap, either from C++ string literals or
178 /// \ref alib_mod_resources "ALib Externalized Resources", which comply to the same contract.
179 ///
180 /// By the same token, in case of an error, an implementation should raise an exception in
181 /// debug-compilations, as parsing is deemed to succeed on static data, even if externalized.
183 void Parse() noexcept;
184}; // EnumRecordPrototype
185
186} // namespace [alib::enumrecords]
187#endif // DOXYGEN
188
#define ALIB_DLL
Definition alib.inl:496
HashMap< MonoAllocator, EnumRecordKey, const void *, EnumRecordKey::Hash, EnumRecordKey::EqualTo > ENUM_RECORD_MAP
Global directory to find enum records.
Definition records.cpp:47
Details of namespace alib::enumrecords.
HashMap< MonoAllocator, EnumRecordKey, const void *, EnumRecordKey::Hash, EnumRecordKey::EqualTo > & getInternalRecordMap()
Definition records.cpp:89
const void * getEnumRecord(const std::type_info &rtti, integer elementValue)
Definition records.cpp:71
void setEnumRecord(const std::type_info &rtti, integer elementValue, const void *record)
Definition records.cpp:62
ALIB_DLL TMonoAllocator< lang::HeapAllocator > GLOBAL_ALLOCATOR
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
String EnumElementName
The name of the enum element.
Definition records.inl:466
ALIB_DLL void Parse() noexcept
static ALIB_DLL void Get(String &result, bool isLastField=false)