ALib C++ Framework
by
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
recordbootstrap.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 #"mainpage_license".
7//==================================================================================================
8
10
12
13/// Helper-struct used with bulk-initialization function #"Bootstrap(std::initializer_list)".
14/// @tparam TEnum The enum type to define records for.
15template<typename TEnum>
16 requires enumrecords::HasRecords<TEnum>
18 /// The enum's associated record type.
20
21 /// The enumeration element.
22 TEnum element;
23
24 /// The static data record.
26
27 /// Constructor taking variadic template arguments to construct the record.
28 ///
29 /// @tparam TArgs Types of the variadic arguments \p{args}.
30 /// @param elem The enum element.
31 /// @param args Variadic arguments forwarded to constructor of field #".record".
32 template<typename... TArgs>
33 Initializer(TEnum elem, TArgs &&... args) noexcept
34 : element(elem)
35 , record(std::forward<TArgs>(args)...) {}
36};
37
38/// Defines a record for a single element of \p{TEnum}.
39///
40/// \note
41/// This method is rather provided for completeness, than for actual use, because it is
42/// preferred to bootstrap enum records as "bulk" data.
43/// Furthermore, it is preferred to use overloaded versions that accept static string data
44/// used to parse the data from. This is more efficient in respect to the footprint of
45/// an application, and - if strings are #"alib_mod_resources;resourced" -
46/// far more flexible.
47///
48/// \see Chapter #"alib_enums_records_firststep_init" for a sample of how this method
49/// can be invoked.
50///
51/// @tparam TArgs Types of the variadic arguments \p{args}.
52/// @tparam TEnum The enum type to define records for.
53/// @param element The enum element.
54/// @param args Variadic arguments forwarded to constructor of the custom record to
55/// create and store.
56template<typename TEnum, typename... TArgs>
58void Bootstrap(TEnum element, TArgs&&... args) noexcept {
60 auto** lastP = records.getPointerToLast();
61 #if ALIB_MONOMEM
63 #else
64 *lastP= HeapAllocator()()
65 #endif
66 .New<typename detail::EnumRecordHook<
67 TEnum>::Node>(element, std::forward<TArgs>(args)...);
68
69 detail::setEnumRecord(typeid(TEnum), integer(element), &(*lastP)->record);
70 (*lastP)->next = nullptr;
71}
72
73
74/// Associates elements of \p{TEnum} with records, as specified by the given list of
75/// \p{definitions}.
76///
77/// The use of struct #"bootstrap::Initializer;*" allows placing
78/// the enumeration element together with the construction parameters of the custom record
79/// type into one comma-separated argument list, without the need to place extra curly braces
80/// around the arguments of the record.
81/// (Such would have been necessary if, for example, <c>std::pair</c> had been used).
82///
83/// \note
84/// It is preferred to use overloaded versions that parse definitions from
85/// static string data. This is more efficient in respect to the footprint of
86/// an application, and - if strings are #"alib_mod_resources;resourced" -
87/// far more flexible.
88///
89/// \see Chapter #"alib_enums_records_firststep_init" for a sample of how this method
90/// can be invoked.
91///
92/// @param definitions List of static enum records to store.
93template<typename TEnum>
95void Bootstrap(std::initializer_list<Initializer<TEnum> > definitions) {
96 auto* table = definitions.begin();
98 auto** lastP = records.getPointerToLast();
99 for (size_t i = 0; i != definitions.size(); ++i) {
100 #if ALIB_MONOMEM
102 #else
103 *lastP= HeapAllocator()()
104 #endif
105 .New<typename detail::EnumRecordHook<
106 TEnum>::Node>(table[i].element, table[i].record);
107
108 detail::setEnumRecord(typeid(TEnum), integer(table[i].element), &(*lastP)->record);
109 lastP = &(*lastP)->next;
110 }
111
112 (*lastP) = nullptr;
113}
114
115/// Reads a list of enum data records from given string \p{input}.
116///
117/// The contents (buffer) of the given substring have to be of a static nature (by contract).
118/// This means that parsing will not create copies of portions of the string but still
119/// use them later.
120/// Consequently, the given string's buffer has to survive the life-cycle of an application.
121///
122/// This is due to the static nature of #"alib_enums_records;ALib Enum Records" and their
123/// creation during bootstrap, either from C++ string literals or
124/// #"alib_mod_resources;ALib Externalized Resources", which comply to the same contract.
125///
126/// \par Availability
127/// This method is available only if \alib_strings is included in the \alibbuild.
128///
129/// \see Chapter #"alib_enums_records_resourced_parsing" for a sample of how this method
130/// can be invoked.
131///
132/// @param input The string used for parsing the enum records to store.
133/// @param innerDelim The delimiter used for separating the fields of a record.
134/// Defaults to <c>','</c>.
135/// @param outerDelim The character delimiting enum records.
136/// Defaults to <c>','</c>.
137template<typename TEnum>
139void Bootstrap(const String &input,
140 character innerDelim=',',
141 character outerDelim=',') {
142 EnumRecordParser::Initialize(input, innerDelim, outerDelim, NULL_NSTRING, NULL_NSTRING);
143
145 auto** lastP = records.getPointerToLast();
146
147 for (;;) {
148 #if ALIB_MONOMEM
149 auto* element = (*lastP = monomem::GLOBAL_ALLOCATOR()
150 #else
151 auto* element= (*lastP= HeapAllocator()()
152 #endif
153 .New<typename detail::EnumRecordHook<TEnum>::Node>());
154 EnumRecordParser::Get(element->integral);
155 element->record.Parse();
156
157 detail::setEnumRecord(typeid(TEnum), integer(element->integral), &element->record);
158
159 // next?
160 lastP = &element->next;
161 if (EnumRecordParser::Input.IsEmpty())
162 break;
164 }
166 (*lastP) = nullptr;
167}
168
169#include "ALib.Lang.CIMethods.H"
170} // namespace [alib::enumrecords::bootstrap]
171
172//##################################################################################################
173// enumrecords::detail::bootstrap()/shutdown()
174//##################################################################################################
176
177//==================================================================================================
178/// Frees resources and shuts down module \alib_enumrecords.
179/// Multiple invocations of this method are forbidden.
180/// The #"alib_mod_bs;standard bootstrap" code of \alib, hence the (overloaded)
181/// functions #"alib::Shutdown" will call this function.
182//==================================================================================================
183void shutdown();
184} // namespace [alib::enumrecords::detail]
#define ALIB_EXPORT
Definition alib.inl:562
void Bootstrap(camp::Camp &camp, const NString &name, character innerDelim=',', character outerDelim=',')
Definition camp.inl:265
Details of namespace #"alib::enumrecords;2".
void setEnumRecord(const std::type_info &rtti, integer elementValue, const void *record)
Definition records.cpp:62
TMonoAllocator< lang::HeapAllocator > GLOBAL_ALLOCATOR
constexpr NString NULL_NSTRING
A nulled string of the narrow character type.
Definition string.inl:2263
lang::HeapAllocator HeapAllocator
Type alias in namespace alib.
lang::integer integer
Type alias in namespace alib.
Definition integers.inl:149
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2172
characters::character character
Type alias in namespace alib.
void Type
The data type associated with elements of TEnum.
Definition records.inl:38
static void Get(String &result, bool isLastField=false)
static Substring Input
The remaining input string.
static void Initialize(const String &input, character innerDelim, character outerDelim, const NString &resourceCategory, const NString &resourceName)
TEnum element
The enumeration element.
Initializer(TEnum elem, TArgs &&... args) noexcept
typename RecordsTraits< TEnum >::Type TRecord
The enum's associated record type.
TRecord record
The static data record.
A node of the forward list that contains the custom record data.
Definition records.inl:110