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