ALib C++ Framework
by
Library Version: 2511 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 #"mainpage_license".
7//==================================================================================================
8
9// forward declarations
11{
12class Configuration;
14class Declaration;
15class 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 type definition for "invalid" data elements. This type is used everywhere as a
44/// generic 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 #"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/// #"ALIB_VARIABLES_DEFINE_TYPE".
58/// Derived types are to be registered with the configuration instance by invoking
59/// #"Configuration::RegisterType;*".
60///
61/// @see Chapter #"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 #"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
75 /// with <c>typeid()</c>. This method is available only in debug-compilations and is used
76 /// to assert that the correct types are read from declared variables.<br>
77 /// Types declared with macro #"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 #"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 configuration macro #"ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT".
88 /// @return The size of the variable's content type.
89 virtual size_t size() =0;
90
91 /// Virtual method which returns \c false in its default version.
92 /// Descendants may return \c true, if the values should automatically be written back to
93 /// configuration data sources, if such sources support this.
94 /// Of course, instead of returning a fixed value, implementations may also return a value
95 /// stored in the given \p{data} struct.<br>
96 /// The value can be requested with method #"Variable::IsWriteBack;*".
97 /// The macro #"ALIB_VARIABLES_DEFINE_TYPE" is not enabled to override this method.
98 /// Hence, variables that should support this flag, need to define their type without
99 /// the help of this macro.
100 /// @see Class #"IniFileFeeder" acknowledges and supports this flag.
101 /// @param data A pointer to the user data struct.
102 /// @return \c true if configuration systems should write values of this variable back.
103 virtual bool isWriteBack(detail::VDATA* data) const { (void) data; return false; }
104
105 /// Abstract virtual method. Descendants need to construct a custom instance at the given
106 /// \p{memory}. This is done using a "placement-new" as follows:
107 ///
108 /// new (memory) MyType();
109 ///
110 /// The pool allocator is \b not provided to allocate the custom type itself (this was already
111 /// done before calling this method). Instead, it may be used to allocate members
112 /// in the custom type. It may also be passed to the instance for further use during it's
113 /// lifecycle. However, in this case chapter #"alib_variables_multithreading" of the
114 /// Programmer's Manual has to be considered.
115 /// @param memory The pointer to the object of custom type.
116 /// @param pool The object pool of the configuration. May be used to dispose pool objects.
117 virtual void construct(detail::VDATA* memory, PoolAllocator& pool) =0;
118
119 /// Abstract virtual method. Descendants need to destruct a custom instance at the given
120 /// \p{memory}. This is done by calling the destructor as follows:
121 ///
122 /// reinterpret_cast<MyTypeName*>(memory)->~MyTypeName();
123 ///
124 /// The pool allocator is \b not provided to free the custom type itself (this will be done
125 /// automatically right after the call to this method). Instead, it may be used to free
126 /// members of the type, which had been allocated during construction or during the use.
127 ///
128 /// \note
129 /// However, for most types included with \alib, for example, #"AStringPA", if used as a
130 /// member of the custom type, no special action need to be taken and it is sufficient to call
131 /// the destructor of the custom type. This is because these types take a copy of the pool on
132 /// construction and thus their destructor disposes allocated pool objects autonomously.
133 /// In other words, if only such members are used, then method \c construct has to be
134 /// implemented to pass the pool object to the members, but this method simply invokes the
135 /// destructor of the custom type as shown above.
136 ///
137 /// @param memory The place to construct the custom type at.
138 /// @param pool The object pool of the configuration. May be used to allocate pool objects.
139 virtual void destruct(detail::VDATA* memory, PoolAllocator& pool) =0;
140
141
142 /// Abstract virtual method. Descendants need to implement this method and de-serialize
143 /// (aka parse) the custom type from the given string value.
144 /// \attention
145 /// Types declared with macro #"ALIB_VARIABLES_DEFINE_TYPE" only declare this method.
146 /// An implementation needs to be provided in a compilation unit.
147 ///
148 /// @param data A pointer to the user type which is to be initialized.
149 /// @param cfg The configuration that holds the variable.
150 /// @param escaper An escaper to be used to convert external strings to C++ native strings.
151 /// @param src The source string to parse. This may be assigned to a value of type
152 /// #"TSubstring" which already provides simple parser mechanics.
153 /// Likewise, #"util::TTokenizer" might be an easy helper to be
154 /// used for parsing.
155 virtual void imPort( detail::VDATA* data, Configuration& cfg, const StringEscaper& escaper,
156 const String& src ) =0;
157
158 /// Abstract virtual method. Descendants need to implement this method. It is invoked when a
159 /// variable is written into an external configuration source (in this case 'drain') or
160 /// otherwise needs to be serialized.
161 ///
162 /// Note that export functions are allowed to add #"NEW_LINE" codes into the export string.
163 /// This allows external configuration systems to nicely format their entries, in case those
164 /// are human-readable. See chapter #"alib_variables_external_custom" of the Programmer's
165 /// Manual for more information.
166 ///
167 /// \attention
168 /// Types declared with macro #"ALIB_VARIABLES_DEFINE_TYPE" only declare this method.
169 /// An implementation needs to be provided in a compilation unit. Otherwise linking software
170 /// fails.
171 ///
172 /// @param data A pointer to the user type which is to be serialized.
173 /// @param cfg The configuration that holds the variable.
174 /// @param escaper An escaper to be used to escape strings.
175 /// @param dest The destination string. Must not be reset prior writing, but appended.
176 ///
177 virtual void exPort( detail::VDATA* data, Configuration& cfg, const StringEscaper& escaper,
178 AString& dest ) =0;
179};
180
181} // namespace [alib::variables]
182
183//==================================================================================================
184// ==== Built-in Types
185//==================================================================================================
187{
188/// Variable content type used with boolean type <c>"B"</c>. When this type is imported,
189/// the value is tried to be parsed with the tokens in
190/// #"Configuration::BooleanTokens;*". If successful, the index of the pair of
191/// <c>true/false</c>-tokens is stored in field #"TokenIndex". When exported back to
192/// a configuration file or otherwise serialized or printed, then the right human-readable
193/// term, corresponding to the potentially now different #"Value" is used.
194struct Bool
195{
196 /// The boolean value. Defaults to \c false.
197 bool Value =false;
198
199 /// The index in the #"Configuration::BooleanTokens;list of tokens" found when
200 /// imported from a string. Can also be set programmatically to force a certain output
201 /// "format". Defaults to \c -1 which indicates that the value was not parsed.
202 /// On exporting, when \c -1, index \c 0 is used.
203 int8_t TokenIndex =-1;
204
205 /// Assignment operator.
206 /// @param newValue The value to set.
207 /// @return The new value.
208 bool operator=(bool newValue) { return Value= newValue; }
209
210 /// Implicit cast operator to \c bool.
211 /// @return Returns the stored value.
212 operator bool() const { return Value; }
213};
214
215
216 /// Type definition used with configuration variables of type <c>"SV,"</c>, which stores a
217 /// string array, imported by parsing a comma-separated string list.
218 ///
219 /// \attention
220 /// When exported, #"NEW_LINE" codes are added after each comma. This allows external
221 /// configuration systems to smoothly format longer lists of values. However, the
222 /// new line codes usually have to be detected with writing and eliminated on import.
223 /// Built-in type #"IniFile" processes such codes correctly.
225
226 /// Type definition used with configuration variables of type <c>"SV;"</c>, which stores a
227 /// string array, imported by parsing a string list separated by character <c>';'</c>.
228 ///
229 /// \attention
230 /// When exported, #"NEW_LINE" codes are added after each semicolon. This allows external
231 /// configuration systems to smoothly format longer lists of values. However, the
232 /// new line codes usually have to be detected with writing and eliminated on import.
233 /// Built-in type #"IniFile" processes such codes correctly.
235
236} // namespace [alib::variables]
237
238
239
240
241#if !DOXYGEN
242namespace alib::variables::detail {
243
244
245struct VMeta_integer : public VMeta
246{
247 ALIB_DLL String typeName() const override { return A_CHAR("I"); }
249 ALIB_DLL const std::type_info& dbgTypeID() override { return typeid(integer); }
250)
251 ALIB_DLL void construct(VDATA* dest, PoolAllocator&) override { new (dest) integer(); }
252 ALIB_DLL void destruct (VDATA* , PoolAllocator&) override {}
253 ALIB_DLL size_t size() override { return sizeof(integer); }
254 ALIB_DLL void imPort( VDATA*, Configuration&, const StringEscaper&, const String& ) override;
255 ALIB_DLL void exPort( VDATA*, Configuration&, const StringEscaper&, AString& ) override;
256};
257
258struct VMeta_float : public VMeta
259{
260 ALIB_DLL String typeName() const override { return A_CHAR("F"); }
262 ALIB_DLL const std::type_info& dbgTypeID() override { return typeid(double); }
263)
264 ALIB_DLL void construct(VDATA* dest, PoolAllocator&) override { new (dest) double(); }
265 ALIB_DLL void destruct (VDATA* , PoolAllocator&) override {}
266 ALIB_DLL size_t size() override { return sizeof(double); }
267 ALIB_DLL void imPort( VDATA*, Configuration&, const StringEscaper&, const String& ) override;
268 ALIB_DLL void exPort( VDATA*, Configuration&, const StringEscaper&, AString& ) override;
269};
270
271struct VMeta_String : public VMeta
272{
273 ALIB_DLL String typeName() const override { return A_CHAR("S"); }
275 ALIB_DLL const std::type_info& dbgTypeID() override { return typeid(AStringPA); }
276)
277 ALIB_DLL void construct(VDATA* dest, PoolAllocator& pool) override
278 { new (dest) AStringPA(pool); }
279 ALIB_DLL void destruct (VDATA* dest, PoolAllocator&) override
280 { reinterpret_cast<AStringPA*>(dest)->~AStringPA(); }
281 ALIB_DLL size_t size() override { return sizeof(AStringPA); }
282 ALIB_DLL void imPort( VDATA*, Configuration&, const StringEscaper&, const String& ) override;
283 ALIB_DLL void exPort( VDATA*, Configuration&, const StringEscaper&, AString& ) override;
284};
285
286} //namespace [alib::variables::detail]
287
288
290ALIB_VARIABLES_DEFINE_TYPE( alib::boxing:: , Box , A_CHAR("BOX") )
291ALIB_VARIABLES_DEFINE_TYPE_WITH_POOL_CONSTRUCTOR( alib::variables::, StringVectorComma , A_CHAR("SV,") )
292ALIB_VARIABLES_DEFINE_TYPE_WITH_POOL_CONSTRUCTOR( alib::variables::, StringVectorSemicolon, A_CHAR("SV;") )
293
294#endif // DOXYGEN
#define ALIB_DLL
Definition alib.inl:573
#define A_CHAR(STR)
Definition alib.inl:1325
#define ALIB_EXPORT
Definition alib.inl:562
#define ALIB_DBG(...)
Definition alib.inl:931
void typeName(const detail::VTable *vtable, AString &result)
VData< void * > VDATA
Definition vmeta.inl:45
alib::StringVectorPA StringVectorSemicolon
Definition vmeta.inl:234
alib::StringVectorPA StringVectorComma
Definition vmeta.inl:224
strings::TAString< character, PoolAllocator > AStringPA
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::util::StringEscaper StringEscaper
Type alias in namespace alib.
Definition escaper.inl:183
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2172
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace alib.
bool Value
The boolean value. Defaults to false.
Definition vmeta.inl:197
bool operator=(bool newValue)
Definition vmeta.inl:208
virtual void construct(detail::VDATA *memory, PoolAllocator &pool)=0
virtual void destruct(detail::VDATA *memory, PoolAllocator &pool)=0
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 bool isWriteBack(detail::VDATA *data) const
Definition vmeta.inl:103
virtual String typeName() const =0
const TReinterpret & As() const
Definition vmeta.inl:39
#define ALIB_VARIABLES_DEFINE_TYPE_WITH_POOL_CONSTRUCTOR(Namespace, CPPName, CfgTypeString)
#define ALIB_VARIABLES_DEFINE_TYPE(Namespace, CPPName, CfgTypeString)