ALib C++ Library
Library Version: 2511 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;
22 import ALib.EnumRecords.Bootstrap;
23 import ALib.Lang;
24# if ALIB_MONOMEM && ALIB_CONTAINERS
25 import ALib.Monomem;
26 import ALib.Containers.HashTable;
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 #if ALIB_MONOMEM && ALIB_CONTAINERS
64 ENUM_RECORD_MAP.EmplaceIfNotExistent( EnumRecordKey(rtti, elementValue ), record );
65 #else
66 ENUM_RECORD_MAP.try_emplace( EnumRecordKey(rtti, elementValue ), record );
67 #endif
68}
69
70const void* getEnumRecord( const std::type_info& rtti, integer elementValue ) {
71
72#if ALIB_MONOMEM && ALIB_CONTAINERS
73 auto it= ENUM_RECORD_MAP.Find( EnumRecordKey( rtti, elementValue ) );
74#else
75 auto it= ENUM_RECORD_MAP.find( EnumRecordKey( rtti, elementValue ) );
76#endif
77 if ( it != ENUM_RECORD_MAP.end() )
78 return it->second;
79
80 return nullptr;
81}
82
83#if ALIB_MONOMEM && ALIB_CONTAINERS
85 EnumRecordKey, const void*,
86 EnumRecordKey::Hash,
87 EnumRecordKey::EqualTo >& getInternalRecordMap() { return ENUM_RECORD_MAP; }
88#else
89std::unordered_map< EnumRecordKey, const void*,
90 EnumRecordKey::Hash,
91 EnumRecordKey::EqualTo >& getInternalRecordMap() { return ENUM_RECORD_MAP; }
92#endif
93
94
95} // namespace [alib::enumrecords::detail]
96
97
98//##################################################################################################
99// Implementation of parsing methods of built-in record types.
100//##################################################################################################
101namespace alib::enumrecords {
106
107
108
109//##################################################################################################
110// EnumRecordPrototype (Doxygen only)
111//##################################################################################################
112
113#if DOXYGEN
114namespace alib::enumrecords {
115//==================================================================================================
116/// This struct is \b not part of the library but only provided with the documentation of this
117/// \alibmod_nl.
118///
119/// The struct prototypes what module \alib_enumrecords_nl \b expects from custom types that are
120/// associated to enumerations as the type of \ref alib_enums_records "ALib Enum Records".
121///
122/// Usually, enum records are rather simple structs with fields of scalar or
123/// \https{POD types,en.cppreference.com/w/cpp/types/is_pod} like, \c int, \c double or
124/// \alib{strings;TString;String} and this way remain POD types themselves.
125///
126/// When parsed or otherwise initialized, String members do not need to copy data to an
127/// own buffer, because the input string for parsing
128/// an instance, as well as the parameters of alternative constructors, are deemed to be static data.
129///
130/// The life-cycle of instances of enumeration record is from bootstrapping an application
131/// to its termination. After creation, the data cannot be modified.
132//==================================================================================================
134{
135 /// Default constructor leaving the record undefined. For efficiency, this usually does
136 /// not initialize fields, as those will be overwritten by a subsequent invocation to #Parse.
137 ///
138 /// This constructor is only needed when method #Parse is given. Note that it is advised to
139 /// provide the parsing option and this way also this constructor.
140 EnumRecordPrototype() noexcept = default;
141
142 /// Constructor accepting all necessary arguments to completely define the record.
143 /// This constructor is needed only in the case that records should be defined in a statical
144 /// way which is an alternative to implementing the definition by parsing an (externalized)
145 /// initialization strings. Static definition can be performed with method
146 /// \alib{enumrecords;EnumRecords;Bootstrap(std::initializer_list<Initializer> definitions)}
147 /// but is not recommended, and the definition from parsable stings is preferred.
148 ///
149 /// Note that the parameter's passed when this constructor is invoked, have to be of
150 /// "static nature". For example, the buffers and contents of passed string values are
151 /// deemed to survive the life-cycle of an application. Usually, C++ string literals are passed.
152 ///
153 /// @param myArg1 Assigned to the first custom field.
154 /// @param myArg2 Assigned to the second custom field.
155 /// @param ... Further parameters, assigned to further fields.
156 EnumRecordPrototype( const MyType1& myArg1, MyType2 myArg2, ... ) noexcept;
157
158 /// Implementation has to parse the fields of this record from static interface struct
159 /// \alib{enumrecords::bootstrap;EnumRecordParser}.
160 ///
161 /// For - usually simple - enum records, the process of parsing is limited to reading
162 /// values separated by delimiters. Convenient methods to do so are given by static type
163 /// \alib{enumrecords::bootstrap;EnumRecordParser}. More complex parsing logic may be implemented by
164 /// using the "parser" substring found with \alib{enumrecords::bootstrap;EnumRecordParser::Input} and further
165 /// of its entities.<br>
166 /// Please refer to the documentation of \alib{enumrecords::bootstrap;EnumRecordParser} for all details.
167 /// A source code sample is given in chapter \ref alib_enums_records_resourced_parsing of the
168 /// Programmer's Manual of the module \alib_enumrecords_nl.
169 ///
170 /// The contents (buffer) of the string parsed is by contract of static nature. This means
171 /// that no copies of portions need to be allocated when used as a field value of string-type.
172 /// This is in alignment with the static nature of \ref alib_enums_records "ALib Enum Records"
173 /// and their creation during bootstrap, either from C++ string literals or
174 /// \ref alib_mod_resources "ALib Externalized Resources", which comply to the same contract.
175 ///
176 /// By the same token, in case of an error, an implementation should raise an exception in
177 /// debug-compilations, as parsing is deemed to succeed on static data, even if externalized.
179 void Parse() noexcept;
180}; // EnumRecordPrototype
181
182} // namespace [alib::enumrecords]
183#endif // DOXYGEN
#define ALIB_DLL
Definition alib.inl:503
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:87
const void * getEnumRecord(const std::type_info &rtti, integer elementValue)
Definition records.cpp:70
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:430
ALIB_DLL void Parse() noexcept
static ALIB_DLL void Get(String &result, bool isLastField=false)