ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
vmeta.inl
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of module \alib_variables of the \aliblong.
4///
5/// \emoji :copyright: 2013-2025 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8
9// forward declarations
11{
12 class Configuration;
14 class Declaration;
15 class Variable;
16}
17
19
20/// This struct is used as the reinterpretation type of generic pointers to ones reflecting
21/// the effective custom type of a configuration variable. While reinterpretation casts do not
22/// need such templated model, its use increases readability of the code.
23/// @tparam T The variable's data type.
24template<typename T> struct VData
25{
26 T custom; ///< The custom data that this object stores.
27
28 /// Reinterprets the <c>this</c>-pointer to a <c>VData<TReinterpret>*</c> and returns
29 /// member #custom.
30 /// @tparam TReinterpret The type to reinterpret to.
31 /// @return A reference to the custom data.
32 template<typename TReinterpret> inline TReinterpret& As()
33 { return reinterpret_cast< VData<TReinterpret>*>(this)->custom; }
34
35 /// Reinterprets the <c>this</c>-pointer to a <c>const VData<TReinterpret>*</c> and returns
36 /// member #custom.
37 /// @tparam TReinterpret The type to reinterpret to.
38 /// @return A const reference to the custom data.
39 template<typename TReinterpret> inline const TReinterpret& As() const
40 { return reinterpret_cast<const VData<TReinterpret>*>(this)->custom; }
41};
42
43/// Convenience definition for "invalid" data element which is used everywhere as a generic
44/// pointer type, which is reinterpreted when to the target type, when needed.
46
47} //namespace [alib::variables::detail]
48
50
51/// Abstract, virtual struct which provides meta-information about types storable in the
52/// \b StringTree nodes of class \alib{variables;Configuration}. A pointer to a singleton of this
53/// type is stored together with a reinterpreted pointer to the custom data record.
54///
55/// To register a custom data type with the configuration system, this type has to inherited and
56/// all methods implemented. It is recommended to define custom derived types using macro
57/// \ref ALIB_VARIABLES_DEFINE_TYPE.
58/// Derived types are to be registered with the configuration instance by invoking
59/// \alib{variables;Configuration::RegisterType}.
60///
61/// @see Chapter \ref alib_variables_types_custom of the Programmer's Manual of camp \alib_variables.
62struct VMeta
63{
64 /// Virtual empty destructor.
65 virtual ~VMeta() {}
66
67 /// Abstract virtual method. Descendants need to return the type name they care of.<br>
68 /// Types declared with macro \ref ALIB_VARIABLES_DEFINE_TYPE implement this method
69 /// rightfully.
70 /// @return The type's name.
71 virtual String typeName() const = 0;
72
73 #if ALIB_DEBUG
74 /// Abstract virtual method. Descendants need to return the <c>std::type_info&</c> received with
75 /// <c>typeid()</c>. This method is available only in debug-compilations and is used to assert
76 /// that the correct types are read from declared variables.<br>
77 /// Types declared with macro \ref ALIB_VARIABLES_DEFINE_TYPE implement this method
78 /// rightfully.
79 /// @return The C++ type ID.
80 virtual const std::type_info& dbgTypeID() = 0;
81 #endif
82
83 /// Abstract virtual method. Descendants need to return '<c>sizeof(T)</c>', with \p{T} being
84 /// the custom type. Types declared with macro \ref ALIB_VARIABLES_DEFINE_TYPE implement
85 /// this method rightfully. With that, it is also asserted that the alignment of the custom
86 /// type is not greater than '<c>alignof(uint64_t)</c>', respectively not greater than
87 /// what is specified with the compiler-symbol \ref ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT.
88 /// @return The size of the variable's content type.
89 virtual size_t size() = 0;
90
91 /// Abstract virtual method. Descendants need to construct a custom instance at the given
92 /// \p{memory}. This is done using a "placement-new" as follows:
93 ///
94 /// new (memory) MyType();
95 ///
96 /// The pool allocator is \b not provided to allocate the custom type itself (this was already
97 /// done before calling this method). Instead, it may be used to allocate members
98 /// in the custom type. It may also be passed to the instance for further use during it's
99 /// lifecycle. However, in this case chapter \ref alib_variables_multithreading of the
100 /// Programmer's Manual has to be considered.
101 /// @param memory The pointer to the object of custom type.
102 /// @param pool The object pool of the configuration. May be used to dispose pool objects.
103 virtual void construct(void* memory, PoolAllocator& pool) = 0;
104
105 /// Abstract virtual method. Descendants need to destruct a custom instance at the given
106 /// \p{memory}. This is done by calling the destructor as follows:
107 ///
108 /// reinterpret_cast<MyTypeName*>(memory)->~MyTypeName();
109 ///
110 /// The pool allocator is \b not provided to free the custom type itself (this will be done
111 /// automatically right after the call to this method). Instead, it may be used to free
112 /// members of the type, which had been allocated during construction or during the use.
113 ///
114 /// \note
115 /// However, for most types included with \alib, for example \alib{AStringPA}, if used as a
116 /// member of the custom type, no special action need to be taken and it is sufficient to call
117 /// the destructor of the custom type. This is because these types take a copy of the pool on
118 /// construction and thus their destructor disposes allocated pool objects autonomously.
119 /// In other words, if only such members are used, then method \c construct has to be
120 /// implemented to pass the pool object to the members, but this method simply invokes the
121 /// destructor of the custom type as shown above.
122 ///
123 /// @param memory The place to construct the custom type at.
124 /// @param pool The object pool of the configuration. May be used to allocate pool objects.
125 virtual void destruct(void* memory, PoolAllocator& pool) = 0;
126
127
128 /// Abstract virtual method. Descendants need to implement this method and de-serialize
129 /// (aka parse) the custom type from the given string value.
130 /// \attention
131 /// Types declared with macro \ref ALIB_VARIABLES_DEFINE_TYPE only declare this method.
132 /// An implementation needs to be provided in a compilation unit.
133 ///
134 /// @param data A pointer to the user type which is to be initialized.
135 /// @param cfg The configuration that holds the variable.
136 /// @param escaper An escaper to be used to convert external strings to C++ native strings.
137 /// @param src The source string to parse. This may be assigned to a value of type
138 /// \alib{strings;TSubstring} which already provides simple parser mechanics.
139 /// Likewise, \alib{strings::util;TTokenizer} might be an easy helper to be
140 /// used for parsing.
141 virtual void imPort( detail::VDATA* data, Configuration& cfg, const StringEscaper& escaper,
142 const String& src ) =0;
143
144 /// Abstract virtual method. Descendants need to implement this method. It is invoked when a
145 /// variable is written into an external configuration source (in this case 'drain') or
146 /// otherwise needs to be serialized.
147 ///
148 /// Note that export functions are allowed to add \alib{NEW_LINE} codes into the export string.
149 /// This allows external configuration systems to nicely format their entries, in case those
150 /// are human-readable. See chapter \ref alib_variables_external_custom of the Programmer's
151 /// Manual for more information.
152 ///
153 /// \attention
154 /// Types declared with macro \ref ALIB_VARIABLES_DEFINE_TYPE only declare this method.
155 /// An implementation needs to be provided in a compilation unit. Otherwise linking software
156 /// fails.
157 ///
158 /// @param data A pointer to the user type which is to be serialized.
159 /// @param cfg The configuration that holds the variable.
160 /// @param escaper An escaper to be used to escape strings.
161 /// @param dest The destination string. Must not be reset prior writing, but appended.
162 ///
163 virtual void exPort( detail::VDATA* data, Configuration& cfg, const StringEscaper& escaper,
164 AString& dest ) =0;
165};
166
167} // namespace [alib::variables]
168
169// =================================================================================================
170// ==== Built-in Types
171// =================================================================================================
173{
174 /// Variable content type used with boolean type <c>"B"</c>. When this type is imported,
175 /// the value is tried to be parsed with the tokens in
176 /// \alib{variables;Configuration::BooleanTokens}. If successful, the index of the pair of
177 /// <c>true/false</c>-tokens is stored in field #TokenIndex. When exported back to
178 /// a configuration file or otherwise serialized or printed, then the right human-readable
179 /// term, corresponding to the potentially now different #Value is used.
180 struct Bool
181 {
182 /// The boolean value. Defaults to \c false.
183 bool Value = false;
184
185 /// The index in the \alib{variables;Configuration::BooleanTokens;list of tokens} found when
186 /// imported from a string. Can also be set programmatically to force a certain output
187 /// "format". Defaults to \c -1 which indicates that the value was not parsed.
188 /// On exporting, when \c -1, index \c 0 is used.
189 int8_t TokenIndex = -1;
190
191 /// Assignment operator.
192 /// @param newValue The value to set.
193 /// @return The new value.
194 bool operator=(bool newValue) { return Value= newValue; }
195
196 /// Implicit cast operator to \c bool.
197 /// @return Returns the stored value.
198 operator bool() const {return Value;}
199 };
200
201
202 /// Type definition used with configuration variables of type <c>"SV,"</c>, which stores a
203 /// string array, imported by parsing a comma-separated string list.
204 ///
205 /// \attention
206 /// When exported, \alib{NEW_LINE} codes are added after each comma. This allows external
207 /// configuration systems to smoothly format longer lists of values. However, the
208 /// new line codes usually have to be detected with writing and eliminated on import.
209 /// Built-in type \alib{variables;IniFile} processes such codes correctly.
211
212 /// Type definition used with configuration variables of type <c>"SV;"</c>, which stores a
213 /// string array, imported by parsing a string list separated by character <c>';'</c>.
214 ///
215 /// \attention
216 /// When exported, \alib{NEW_LINE} codes are added after each semicolon. This allows external
217 /// configuration systems to smoothly format longer lists of values. However, the
218 /// new line codes usually have to be detected with writing and eliminated on import.
219 /// Built-in type \alib{variables;IniFile} processes such codes correctly.
221
222} // namespace [alib::variables]
223
224
225
226
227#if !DOXYGEN
228namespace alib::variables::detail {
229
230
231struct VMeta_integer : public VMeta
232{
233 ALIB_DLL String typeName() const override { return A_CHAR("I"); }
235 ALIB_DLL const std::type_info& dbgTypeID() override { return typeid(integer); }
236)
237 ALIB_DLL void construct(void* dest, PoolAllocator&) override { new (dest) integer(); }
238 ALIB_DLL void destruct (void* , PoolAllocator&) override {}
239 ALIB_DLL size_t size() override { return sizeof(integer); }
240 ALIB_DLL void imPort( VDATA*, Configuration&, const StringEscaper&, const String& ) override;
241 ALIB_DLL void exPort( VDATA*, Configuration&, const StringEscaper&, AString& ) override;
242};
243
244struct VMeta_float : public VMeta
245{
246 ALIB_DLL String typeName() const override { return A_CHAR("F"); }
248 ALIB_DLL const std::type_info& dbgTypeID() override { return typeid(double); }
249)
250 ALIB_DLL void construct(void* dest, PoolAllocator&) override { new (dest) double(); }
251 ALIB_DLL void destruct (void* , PoolAllocator&) override {}
252 ALIB_DLL size_t size() override { return sizeof(double); }
253 ALIB_DLL void imPort( VDATA*, Configuration&, const StringEscaper&, const String& ) override;
254 ALIB_DLL void exPort( VDATA*, Configuration&, const StringEscaper&, AString& ) override;
255};
256
257struct VMeta_String : public VMeta
258{
259 ALIB_DLL String typeName() const override { return A_CHAR("S"); }
261 ALIB_DLL const std::type_info& dbgTypeID() override { return typeid(AStringPA); }
262)
263 ALIB_DLL void construct(void* dest, PoolAllocator& pool) override
264 { new (dest) AStringPA(pool); }
265 ALIB_DLL void destruct (void* dest, PoolAllocator&) override
266 { reinterpret_cast<AStringPA*>(dest)->~AStringPA(); }
267 ALIB_DLL size_t size() override { return sizeof(AStringPA); }
268 ALIB_DLL void imPort( VDATA*, Configuration&, const StringEscaper&, const String& ) override;
269 ALIB_DLL void exPort( VDATA*, Configuration&, const StringEscaper&, AString& ) override;
270};
271
272} //namespace [alib::variables::detail]
273
274
276ALIB_VARIABLES_DEFINE_TYPE( alib::boxing::, Box , A_CHAR("BOX") )
277ALIB_VARIABLES_DEFINE_TYPE_WITH_POOL_CONSTRUCTOR( alib::variables::, StringVectorComma , A_CHAR("SV,") )
278ALIB_VARIABLES_DEFINE_TYPE_WITH_POOL_CONSTRUCTOR( alib::variables::, StringVectorSemicolon, A_CHAR("SV;") )
279
280#endif // DOXYGEN
281
#define ALIB_DLL
Definition alib.inl:496
#define A_CHAR(STR)
#define ALIB_VARIABLES_DEFINE_TYPE_WITH_POOL_CONSTRUCTOR(Namespace, CPPName, CfgTypeString)
#define ALIB_EXPORT
Definition alib.inl:488
#define ALIB_DBG(...)
Definition alib.inl:836
#define ALIB_VARIABLES_DEFINE_TYPE(Namespace, CPPName, CfgTypeString)
void typeName(const detail::VTable *vtable, AString &result)
VData< void * > VDATA
Definition vmeta.inl:45
alib::StringVectorPA StringVectorSemicolon
Definition vmeta.inl:220
alib::StringVectorPA StringVectorComma
Definition vmeta.inl:210
strings::util::StringEscaper StringEscaper
Type alias in namespace alib.
Definition escaper.inl:199
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace alib.
variables::Configuration Configuration
Type alias in namespace alib.
lang::integer integer
Type alias in namespace alib.
Definition integers.inl:149
monomem::TPoolAllocator< MonoAllocator > PoolAllocator
strings::util::TStringVector< character, PoolAllocator > StringVectorPA
Type alias in namespace alib.
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2381
strings::TAString< character, PoolAllocator > AStringPA
Type alias in namespace alib.
bool Value
The boolean value. Defaults to false.
Definition vmeta.inl:183
bool operator=(bool newValue)
Definition vmeta.inl:194
virtual size_t size()=0
virtual void exPort(detail::VDATA *data, Configuration &cfg, const StringEscaper &escaper, AString &dest)=0
virtual void imPort(detail::VDATA *data, Configuration &cfg, const StringEscaper &escaper, const String &src)=0
virtual ~VMeta()
Virtual empty destructor.
Definition vmeta.inl:65
virtual const std::type_info & dbgTypeID()=0
virtual void destruct(void *memory, PoolAllocator &pool)=0
virtual void construct(void *memory, PoolAllocator &pool)=0
virtual String typeName() const =0
const TReinterpret & As() const
Definition vmeta.inl:39