ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
variable.hpp
Go to the documentation of this file.
1/** ************************************************************************************************
2 * \file
3 * This header file is part of module \alib_config of the \aliblong.
4 *
5 * \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
6 * Published under \ref mainpage_license "Boost Software License".
7 **************************************************************************************************/
8#ifndef HPP_ALIB_CONFIG_VARIABLE
9#define HPP_ALIB_CONFIG_VARIABLE 1
10
11#if !defined (HPP_ALIB_CONFIG_PRIORITIES)
13#endif
14
15#if !defined (HPP_ALIB_STRINGS_STRING)
17#endif
18
19#if !defined (HPP_ALIB_C)
21#endif
22
23#if !defined (HPP_ALIB_MONOMEM_SELF_CONTAINED)
25#endif
26#if !defined (HPP_ALIB_MONOMEM_STDCONTAINERMA)
28#endif
29
30#if !defined (_GLIBCXX_VECTOR) && !defined(_VECTOR_)
31# include <vector>
32#endif
33
34namespace alib { namespace config {
35
36// forward declarations
37class Configuration;
38
39namespace detail {
40
41/**
42 * Fields of class \alib{config;Variable}, which is monotonically allocated and derived from
43 * \alib{monomem;SelfContained} and as such needs this special struct for field definition.
44 */
46{
47 /** The \b %Configuration that was recently used to request or store the value. */
49
50 /** The configuration variable category. */
52
53 /** The configuration variable name. */
55
56 /**
57 * The delimiter used for parsing and storing values by simple textual plug-ins which
58 * use the default version of \alib{config;XTernalizer} for in- and externalizing variables.
59 */
61
62 /**
63 * Hints for formatting textual configuration files. (Used by class
64 * \alib{config;IniFile} and potentially by custom plug-ins.
65 */
67
68 /**
69 * If set, attributes written in multi-lines are vertically aligned by this character or
70 * string. Use cases are "=", ":" or "->".<br> Used by \alib{config;IniFile} and
71 * potentially by custom plug-ins.
72 */
74
75 /** The configuration variable comments with placeholders replaced. */
76 String Comments = nullptr;
77
78 /** The configuration variable category and name concatenated with an underscore
79 * character \c '_'. This is useful e.g. for naming variables in log messages. */
81
82 /**
83 * The default value provided as an externalized string.
84 *
85 * The only occasion that this value is used is with method
86 * \alib{config;Configuration::Load}.
87 * If no plug-in has this variable defined and this field is not \e nulled, then the value
88 * is written into plug-in of priority \alib{config;Priorities;Priorities::DefaultValues},
89 * respectively - if this default plug-in was replaced by the user - into a plug-in found at
90 * or below this priority.
91 *
92 * In this case, the value is parsed using method
93 * \alib{config;XTernalizer::LoadFromString} of field
94 * \alib{config;ConfigurationPlugin::StringConverter} of the plug-in writing the value.
95 *
96 * \note
97 * The field is ignored when using the plug-in interface
98 * \alib{config;ConfigurationPlugin::Load} directly.
99 * To store this value 'manually' directly to a plug-in, invoke
100 * \alib{config;ConfigurationPlugin::Store} with providing value explicitly.
101 */
103
104 /**
105 * A value related to the priority of a configuration plug-in.
106 * The following values apply:
107 * - \alib{config;Priorities;Priorities::NONE} after creation or declaration (reuse).
108 * - The priority of the plug-in that loaded the value (after calling
109 * \alib{config;Configuration::Load}).
110 * - The priority of the plug-in that stored the value (after calling
111 * \alib{config;Configuration::Store}).
112 * - \c 0, if the last load or store operation failed.
113 * In addition prior to storing a variable, the value might be manually set. See
114 * documentation of \alib{config;Configuration::Store}) for details.
115 */
117
118 /** The values. */
119 std::vector<String, StdContMA<String>> values;
120
121 /**
122 * Constructor.
123 * @param fields Pointer to the fields pointer of the self-contained object.
124 * \note This is a pointer to the pointer, as at the point of invocation,
125 * the self-contained fields are not fully initialized, yet. This
126 * object itself is missing. However the allocator within the struct
127 * is initialized already If the pointer was given directly, the compiler
128 * would complain.
129 */
131 : values( (*fields)->allocator )
132 {}
133
134}; // struct VariableFields
135
136} // namespace detail
137
138/** ************************************************************************************************
139 * This class is used to load and store external configuration data with objects of class
140 * \alib{config;Configuration} and its plug-ins \alib{config;ConfigurationPlugin}.
141 *
142 * \note
143 * For general information about external configuration variables, see namespace documentation
144 * \ref alib::config "alib::config".
145 *
146 * <b>Construction/Redeclaration:</b><br>
147 * While constructors accepting attributes of a variable exist, it is recommended to
148 * declare all external configuration variables in a central place, using statically or dynamically
149 * allocated objects of type \alib{config;VariableDecl} and pass such record to the constructor of
150 * a variable.
151 *
152 * The class is \alib{monomem;SelfContained;self-contained} in a \alib{monomem;MonoAllocator}
153 * and supports is designed to be 'reused'. This is implementation is chosen to avoid repeated
154 * allocation/de-allocation of dynamic memory when a larger amount of variables are read or written.
155 * After invoking one of the overloaded methods #Declare, which share the same signatures as
156 * the overloaded constructors, a variable is freshly initialized and its monotonic allocator is reseet.
157 * *
158 * <b>Values:</b><br>
159 * A variable can contain zero, one or several values. If zero, then the variable was either not
160 * loaded, yet or the load operation failed (no configuration plug-in defined the variable).
161 * Method #Size reports the currently available values and methods #GetString(int), #GetInteger(int)
162 * and #GetFloat(int) return a value. Internally all values are stored as strings. If
163 * field #Config is set, its field \alib{config;Configuration::NumberFormat} is used for floating
164 * point conversion.
165 *
166 * When storing a variable that contains more than one value, field #Delim has to be set.
167 * Conversely, when reading a variable that contains multiple values, the delimiter has to be set
168 * prior to the load operation.
169 * \note
170 * This is not true when loading/storing a variable directly in a plug-in of type
171 * \alib{config;InMemoryPlugin} or might also not be true with custom configuration plug-in types
172 * which for example might store the values in a database.<br>
173 * However, with the default plug-ins \alib{config;CLIArgs}, \alib{config;Environment} and
174 * \alib{config;IniFile}, the delimiter is needed!
175 * Therefore, it is best practice to always define a proper delimiter if a variable contains
176 * multiple values.
177 *
178 * To set the contents of various fields, methods whose name is prefixed with <b>Replace</b> are
179 * given. Each of these methods allocates new storage in the internal monotonic allocator, which is
180 * freed only with method #Reset, which is internally called with each overloaded #Declare method.
181 * The using software has to ensure that #Reset is invoked on a regular basis, to not generate
182 * a "memory leak" by increasing the allocated space in the monotonic allocator.
183 * In common use-cases of the class, this is not an issue.
184 *
185 * <b>Loading and Storing:</b><br>
186 * There are two ways of loading and storing a variable:
187 * - Using the interface of class \alib{config;Configuration} which allows to load and store
188 * variables from different sources (plug-ins) in a prioritized way.
189 * - Using the interface of class \alib{config;ConfigurationPlugin} which may be used if the
190 * decision about the source or drain of a load/store operation is explicitly made by a code
191 * unit - instead of by the configuration.
192 *
193 * Storing empty variables (method #Size returns \c 0) deletes a variable from the those
194 * configuration plug-ins that are write enabled.
195 **************************************************************************************************/
196class Variable : protected monomem::SelfContained<detail::VariableFields>
197{
198 // #############################################################################################
199 // Constructors and Declaration
200 // #############################################################################################
201 public:
202 /** ****************************************************************************************
203 * Move constructor moves the entries and deletes them in source object.
204 * @param src The object to move.
205 ******************************************************************************************/
206 Variable(Variable&& src) noexcept
207 : SelfContained( std::move(src) )
208 {}
209
210 /** ****************************************************************************************
211 * Constructs an undefined Variable. Prior to using this, #Declare has to be invoked.
212 ******************************************************************************************/
214 : SelfContained( 2048, 100, &fields )
215 {
216 #if ALIB_DEBUG_MONOMEM
217 Allocator().LogDomain= A_CHAR("MA/CFG/VAR");
218 #endif
219
220 Reset();
221 }
222
223 /** ****************************************************************************************
224 * Constructs a variable from a declaration.
225 * Strings named \c "%1", \c "%2" ... \c "%N" found in the fields #Category, #Name,
226 * #Comments and #DefaultValue are replaced with given replacement values found
227 * in boxed argument (list) \p{replacements}.
228 *
229 * @param declaration The declaration data of the variable.
230 * @param replacements List of replacement values. Must be of boxed types that are
231 * \alib{strings;T_Append;appendable} to class \b AString.
232 * To provide more than one object, pass an object of class
233 * \alib{boxing;Boxes} or a boxed array.
234 ******************************************************************************************/
235 Variable( const VariableDecl& declaration, const Box& replacements )
236 : SelfContained( 2048, 100, &fields )
237 {
238 #if ALIB_DEBUG_MONOMEM
239 Allocator().LogDomain= A_CHAR("MA/CFG/VAR");
240 #endif
241 Declare( declaration, replacements );
242 }
243
244 /** ****************************************************************************************
245 * Constructs a variable using the declaration of another variable. The values are not
246 * copied.
247 *
248 * @param variable A variable to copy the declaration (which is comprised with fields
249 * #Category, #Name, #Fullname, #Delim, #Comments and #DefaultValue) from.
250 ******************************************************************************************/
251 Variable( const Variable& variable )
252 : SelfContained( 2048, 100, &fields )
253 {
254 #if ALIB_DEBUG_MONOMEM
255 Allocator().LogDomain= A_CHAR("MA/CFG/VAR");
256 #endif
257 Declare( variable );
258 }
259
260 /** ****************************************************************************************
261 * Constructs a variable from the given values
262 *
263 * @param category The category of the variable.
264 * @param name The name of the variable
265 * @param delim A proposal for a delimiter that might be used by some plug-ins
266 * to delimit different values from each other (e.g. INI files).
267 * @param comments Comment lines that might be added in the configuration storage
268 * (plug-in implementation dependent).
269 ******************************************************************************************/
270 Variable( const String& category, const String& name, character delim= '\0',
271 const String& comments = nullptr )
272 : SelfContained( 2048, 100, &fields )
273 {
274 #if ALIB_DEBUG_MONOMEM
275 Allocator().LogDomain= A_CHAR("MA/CFG/VAR");
276 #endif
277 Declare( category, name, delim, comments );
278 }
279
280 #if defined(ALIB_DOX)
281 /** ****************************************************************************************
282 * Constructs a variable from an enum element equipped with
283 * \ref alib_enums_records "ALib Enum Records" of type \alib{config;VariableDecl}.
284 *
285 * @tparam TEnum The type of parameter \p{declaration}
286 * @tparam TEnableIf Not to be specified. Used by the compiler to select this
287 * constructor only for associated custom C++ enum types.
288 * @param declaration Element of an enum class that is representing configuration
289 * variables.
290 ******************************************************************************************/
291 template<typename TEnum, typename TEnableIf=void>
292 inline
293 Variable( TEnum declaration );
294 #else
295 template<typename TEnum, typename TEnableIf=
296 ATMP_VOID_IF( EnumRecords<TEnum>::template AreOfType<VariableDecl>() ) >
297 Variable( TEnum declaration )
298 : SelfContained( 2048, 100, &fields )
299 {
300 #if ALIB_DEBUG_MONOMEM
301 Allocator().LogDomain= A_CHAR("MA/CFG/VAR");
302 #endif
303 Declare( declaration );
304 }
305 #endif
306
307#if defined(ALIB_DOX)
308 /** ****************************************************************************************
309 * Constructs a variable from an enum element equipped with
310 * \ref alib_enums_records "ALib Enum Records" of type \alib{config;VariableDecl}.
311 *
312 * Strings named \c "%1", \c "%2" ... \c "%N" found in the fields #Category, #Name,
313 * #Comments and #DefaultValue are replaced with given replacement values found
314 * in boxed argument (list) \p{replacements}.
315 *
316 * @tparam TEnum The type of parameter \p{declaration}
317 * @tparam TEnableIf Not to be specified. Used by the compiler to select this
318 * constructor only for associated custom C++ enum types.
319 * @param declaration Element of an enum class that is representing configuration
320 * variables.
321 * @param replacements List of replacement values. Must be of boxed types that are
322 * \alib{strings;T_Append;appendable} to class \b AString.
323 * To provide more than one object, pass an object of class
324 * \alib{boxing;Boxes} or a boxed array of boxes.
325 ******************************************************************************************/
326 template<typename TEnum, typename TEnableIf= void>
327 Variable( TEnum declaration, const Box& replacements );
328#else
329 template<typename TEnum, typename TEnableIf=
330 ATMP_VOID_IF( EnumRecords<TEnum>::template AreOfType<VariableDecl>() ) >
331 Variable( TEnum declaration, const Box& replacements )
332 : SelfContained( 2048, 100, &fields )
333 {
334 #if ALIB_DEBUG_MONOMEM
335 Allocator().LogDomain= A_CHAR("MA/CFG/VAR");
336 #endif
337 Declare( declaration, replacements );
338 }
339#endif
340
341 /** ****************************************************************************************
342 * Re-initializes this variable from a given declaration.
343 * Placeholders \c "%1", \c "%2" ... \c "%N" found in fields #Category, #Name, #Comments
344 * and #DefaultValue are replaced with the replacement values given with boxed argument
345 * (list) \p{replacements}.
346 *
347 * @param declaration The declaration data of the variable.
348 * @param replacements List of replacement values. Must be of boxed types that are
349 * \alib{strings;T_Append;appendable} to class \b AString.
350 * To provide more than one object, pass an object of class
351 * \alib{boxing;Boxes} or a boxed array.
352 * @return \c *this to allow concatenated operations.
353 ******************************************************************************************/
355 Variable& Declare( const VariableDecl& declaration, const Box& replacements );
356
357
358 #if defined(ALIB_DOX)
359 /** ****************************************************************************************
360 * Re-initializes this variable and resets its declaration.
361 * Internally uses an instance of class \alib{config;VariableDecl} constructed with
362 * enum element \p{declaration}.
363 *
364 * @param declaration Element of an enum class that is representing configuration
365 * variables.
366 * @tparam TEnum The type of parameter \p{declaration}
367 * @return \c *this to allow concatenated operations.
368 ******************************************************************************************/
369 template<typename TEnum>
370 Variable& Declare( TEnum declaration );
371 #else
372 template<typename TEnum>
373 ATMP_T_IF( Variable&, EnumRecords<TEnum>::template AreOfType<VariableDecl>() )
374 Declare( TEnum declaration )
375 {
376 VariableDecl decl( declaration );
377 return Declare( decl, nullptr );
378 }
379 #endif
380
381
382 /** ****************************************************************************************
383 * Declares a variable using the declaration of another variable.
384 * The values are not copied.
385 *
386 * @param variable A variable to copy the declaration (which is comprised with fields
387 * #Category, #Name, #Fullname, #Delim, #Comments and #DefaultValue) from.
388 * @return \c *this to allow concatenated operations.
389 ******************************************************************************************/
391 Variable& Declare ( const Variable& variable );
392
393 /** ****************************************************************************************
394 * Re-initializes the variable using the specific values given.
395 *
396 * @param category The category of the variable.
397 * @param name The name of the variable
398 * @param delim A proposal for a delimiter that might be used by some plug-ins
399 * to delimit different values from each other (e.g. INI files).
400 * Defaults to \c '\0'.
401 * @param comments Comment lines that might be added in the configuration storage
402 * (plug-in implementation dependent).
403 * Defaults to \c nullptr.
404 *
405 * @return \c *this to allow concatenated operations.
406 ******************************************************************************************/
408 Variable& Declare( const String& category,
409 const String& name,
410 character delim = '\0',
411 const String& comments = nullptr );
412
413
414 /** ****************************************************************************************
415 * Re-initializes the variable using the specific values given.
416 * This overload accepts complementary string character types for each argument.
417 *
418 * @param category The category of the variable.
419 * @param name The name of the variable
420 * @param delim A proposal for a delimiter that might be used by some plug-ins
421 * to delimit different values from each other (e.g. INI files).
422 * Defaults to \c '\0'.
423 * @param comments Comment lines that might be added in the configuration storage
424 * (plug-in implementation dependent).
425 * Defaults to \c nullptr.
426 *
427 * @return \c *this to allow concatenated operations.
428 ******************************************************************************************/
430 Variable& Declare( const ComplementString& category,
431 const ComplementString& name,
432 complementChar delim = '\0',
433 const ComplementString& comments = nullptr );
434
435 /** ****************************************************************************************
436 * Clears all fields and resets internal monotonic allocator.
437 *
438 * @param nameAndCategory If <b>CurrentData::CurrentData::Keep</b> is given, the
439 * name and category of the variable will be restored after
440 * the reset.<br>
441 * Defaults to <b>CurrentData::Clear</b>.
442 *
443 * @return \c *this to allow concatenated operations.
444 ******************************************************************************************/
447
448
449 // #############################################################################################
450 // Interface
451 // #############################################################################################
452 public:
453 /**
454 * The \b %Configuration that was recently used to request or store the value.
455 * @return The associated configuration object or \c nullptr if no configuration is
456 * assigned.
457 */
458 const Configuration* Config() const
459 {
460 return Self().Config;
461 }
462
463 /**
464 * The \b %Configuration that was recently used to request or store the value.
465 * @return The associated configuration object or \c nullptr if no configuration is
466 * assigned.
467 */
469 {
470 return Self().Config;
471 }
472
473 /**
474 * Sets the \b %Configuration to load or store the value.
475 * @param config The configuration to use.
476 */
477 void SetConfig( Configuration* config )
478 {
479 Self().Config= config;
480 }
481
482
483 /**
484 * Returns this configuration variable's category.
485 * @return The category of this variable.
486 */
487 const String& Category() const
488 {
489 return Self().Category;
490 }
491
492 /**
493 * Returns this configuration variable's name.
494 * @return The name of this variable.
495 */
496 const String& Name() const
497 {
498 return Self().Name;
499 }
500
501 /**
502 * The delimiter used for parsing and storing values by simple textual plug-ins which
503 * use the default version of \alib{config;XTernalizer} for in- and externalizing variables.
504 * @return The delimiter character of this variable.
505 */
507 {
508 return Self().Delim;
509 }
510
511 /**
512 * Sets the delimiter used for parsing and storing values.
513 * \see
514 * Method #Delim for more informaiton.
515 * @param delim The configuration to use.
516 */
517 void SetDelim( character delim )
518 {
519 Self().Delim= delim;
520 }
521
522 /**
523 * Hints for formatting textual configuration files. (Used by class \alib{config;IniFile}
524 * and potentially by custom plug-ins.
525 * @return The format hints.
526 */
528 {
529 return Self().FmtHints;
530 }
531
532 /**
533 * Sets the hints for formatting textual configuration files.
534 * (Used by class \alib{config;IniFile} and potentially by custom plug-ins.
535 *
536 * @param hints The hints to be used.
537 */
539 {
540 Self().FmtHints= hints;
541 }
542
543 /**
544 * If set, attributes written in multi-lines are vertically aligned by this character or
545 * string. Use cases are "=", ":" or "->".<br>
546 * Used by \alib{config;IniFile} and potentially by custom plug-ins.
547 * @return The alignment of attributes.
548 */
550 {
551 return Self().FormatAttrAlignment;
552 }
553
554 /**
555 * Returns this configuration variable's comments.
556 * @return The comments of this variable.
557 */
558 const String& Comments() const
559 {
560 return Self().Comments;
561 }
562
563 /**
564 * The configuration variable category and name concatenated with an underscore
565 * character \c '_'. This is useful e.g. for naming variables in log messages.
566 *
567 * \note
568 * This method is not declared <c>const</c>, as the full name might be assembled
569 * with the invocation of this method.
570 *
571 * @return The name and category of this variable separated by \c '_'.
572 */
573 ALIB_API const String& Fullname();
574
575 /**
576 * The default value provided as an externalized string.
577 *
578 * The only occasion that this value is used is with method
579 * \alib{config;Configuration::Load}.
580 * If no plug-in has this variable defined and this field is not \e nulled, then the value
581 * is written into plug-in of priority \alib{config;Priorities;Priorities::DefaultValues},
582 * respectively - if this default plug-in was replaced by the user - into a plug-in found at
583 * or below this priority.
584 *
585 * In this case, the value is parsed using method
586 * \alib{config;XTernalizer::LoadFromString} of field
587 * \alib{config;ConfigurationPlugin::StringConverter} of the plug-in writing the value.
588 *
589 * \note
590 * The field is ignored when using the plug-in interface
591 * \alib{config;ConfigurationPlugin::Load} directly.
592 * To store this value 'manually' directly to a plug-in, invoke
593 * \alib{config;ConfigurationPlugin::Store} with providing value explicitly.
594 *
595 * @return The default value of this variagble.
596 */
597 const String& DefaultValue() const
598 {
599 return Self().DefaultValue;
600 }
601
602 /**
603 * A value related to the priority of a configuration plug-in.
604 * The following values apply:
605 * - \alib{config;Priorities;Priorities::NONE} after creation or declaration (reuse).
606 * - The priority of the plug-in that loaded the value (after calling
607 * \alib{config;Configuration::Load}).
608 * - The priority of the plug-in that stored the value (after calling
609 * \alib{config;Configuration::Store}).
610 * - \c 0, if the last load or store operation failed.
611 * In addition prior to storing a variable, the value might be manually set. See
612 * documentation of
613 * \alib{config;Configuration::Store}) for details.
614 *
615 * @return The priority setting for this variable.
616 */
618 {
619 return Self().Priority;
620 }
621
622 /**
623 * Sets the priority.
624 * \see
625 * Method #Priority for more information.
626 * @param priority The priority to use.
627 */
628 void SetPriority( Priorities priority )
629 {
630 Self().Priority= priority;
631 }
632
633
634 /**
635 * Sets the value received by #Category.
636 * \note
637 * This method allocates a new string object in the internal \alib{monomem;MonoAllocator}
638 * which will only be released with resetting this variable instance.
639 *
640 * @param newValue The new category to set.
641 */
642 void ReplaceCategory( const String& newValue )
643 {
644 Self().Category = Allocator().EmplaceString( newValue );
645 }
646
647 /**
648 * Sets the value received by #Name.
649 * \note
650 * This method allocates a new string object in the internal \alib{monomem;MonoAllocator}
651 * which will only be released with resetting this variable instance.
652 *
653 * @param newValue The new name to set.
654 */
655 void ReplaceName( const String& newValue )
656 {
657 Self().Name = Allocator().EmplaceString( newValue );
658 }
659
660 /**
661 * Sets the value received by #Comments.
662 * \note
663 * This method allocates a new string object in the internal \alib{monomem;MonoAllocator}
664 * which will only be released with resetting this variable instance.
665 *
666 * @param newValue The new comments to set.
667 */
668 void ReplaceComments( const String& newValue )
669 {
670 Self().Comments = Allocator().EmplaceString( newValue );
671 }
672
673 /**
674 * Sets the value received by #DefaultValue.
675 * \note
676 * This method allocates a new string object in the internal \alib{monomem;MonoAllocator}
677 * which will only be released with resetting this variable instance.
678 *
679 * @param newValue The new default value to set.
680 */
681 void ReplaceDefaultValue( const String& newValue )
682 {
683 Self().DefaultValue = Allocator().EmplaceString( newValue );
684 }
685
686 /**
687 * Sets the value received by #FormatAttrAlignment.
688 * \note
689 * This method allocates a new string object in the internal \alib{monomem;MonoAllocator}
690 * which will only be released with resetting this variable instance.
691 *
692 * @param newValue The new alignment to set.
693 */
694 void ReplaceFormatAttrAlignment( const String& newValue )
695 {
696 Self().FormatAttrAlignment= Allocator().EmplaceString( newValue );
697 }
698
699
700 // #############################################################################################
701 // Value access
702 // #############################################################################################
703 /** ****************************************************************************************
704 * Returns the number of values set in this object.
705 *
706 * @return The number of values set.
707 ******************************************************************************************/
708 integer Size() const
709 {
710 return static_cast<integer>(Self().values.size());
711 }
712
713 /** ****************************************************************************************
714 * Clears the value, respectively the list of values set.
715 * After invoking this method, #Size will return \p{startIdx} and #GetString will
716 * return \c nullptr for any index greater or equal to \p{startIdx}.
717 *
718 * @param startIdx The index of the first value to be cleared. Defaults to \c 0.
719 * @return \c *this to allow concatenated operations.
720 ******************************************************************************************/
721 Variable& ClearValues(int startIdx= 0)
722 {
723 auto& vector= Self().values;
724
725 if( startIdx == 0 )
726 vector.clear();
727 else
728 vector.erase( vector.begin() + startIdx, vector.end() );
729 return *this;
730 }
731
732 /** ****************************************************************************************
733 * Adds a value to the end of the list of values.
734 * @param value The string value to add.
735 ******************************************************************************************/
736 ALIB_API void Add(const String& value);
737
738 /** ****************************************************************************************
739 * Adds an integral value to the end of the list of values.
740 * @param value The value to add.
741 ******************************************************************************************/
742 ALIB_API void Add(int64_t value);
743
744 /** ****************************************************************************************
745 * Adds an integral value to the end of the list of values.
746 * @param value The value to add.
747 ******************************************************************************************/
748 void Add(int32_t value) { Add( static_cast<int64_t>(value) ); }
749
750 /** ****************************************************************************************
751 * Adds a floating point value to the end of the list of values.
752 * @param value The value to add.
753 ******************************************************************************************/
754 ALIB_API void Add(double value);
755
756 /** ****************************************************************************************
757 * Replaces the value at \p{idx} with the given string.
758 *
759 * @param idx The index of the value to replace.
760 * @param replacement The string value to set.
761 ******************************************************************************************/
762 ALIB_API void ReplaceValue( int idx, const String& replacement );
763
764 /** ****************************************************************************************
765 * Replaces the value at \p{idx} with the values of the given other variable.
766 *
767 * @param idx The index of the value to replace.
768 * @param replVariable The variable providing the replacement values.
769 ******************************************************************************************/
770 ALIB_API void ReplaceValue( int idx, Variable& replVariable );
771
772 /** ****************************************************************************************
773 * Returns the stored value with the given index.
774 * Valid values for parameter \p{idx} are between \b 0 and #Size <b> - 1</b>.<br>
775 * In debug-compilations an error is raised if the given \p{idx} is out of range.
776 *
777 * @param idx The index of the value to be retrieved. Defaults to \c 0.
778 * @return The value at \p{idx}.
779 ******************************************************************************************/
780 const String& GetString( int idx= 0 )
781 {
782 ALIB_ASSERT_ERROR( idx >= 0 && idx < Size(), "CONFIG",
783 NString128( "Variable field index '") << idx << "' is out of range." )
784 return Self().values[static_cast<size_t>(idx)];
785 }
786
787 /** ****************************************************************************************
788 * Returns the value at \p{idx} interpreted as an integer.
789 * If the index is invalid, \c 0 is returned.
790 * @param idx The index of the value to be retrieved. Defaults to \c 0.
791 * @return The value at \p{idx} interpreted as an integral value.
792 ******************************************************************************************/
793 ALIB_API integer GetInteger(int idx= 0);
794
795 /** ****************************************************************************************
796 * Returns the value at \p{idx} interpreted as a double value.
797 * If the index is invalid, \c 0.0 is returned.
798 * Parsing is done using field \c NumberFormat of field #Config, respectively, if this is
799 * not set, the static singleton \alib{strings;TNumberFormat::Global;NumberFormat::Global}.
800 *
801 * @param idx The index of the value to be retrieved. Defaults to \c 0.
802 * @return The value at \p{idx} interpreted as a double value.
803 ******************************************************************************************/
804 ALIB_API double GetFloat(int idx= 0);
805
806 /** ****************************************************************************************
807 * Returns \c true if the first value represents a boolean 'true'.
808 * Evaluation is done using field #Config, respectively if this is not set, the static
809 * singleton \alib{strings;TNumberFormat::Global;NumberFormat::Global}.
810 *
811 * @param idx The index of the value to be retrieved. Defaults to \c 0.
812 * @return The value at \p{idx} interpreted as a boolean value.
813 ******************************************************************************************/
814 ALIB_API bool IsTrue(int idx= 0);
815
816 /** ****************************************************************************************
817 * Searches in the values of this variable for the pattern <c>attrName = result</c> and
818 * sets parameter \p{result} to the string following this pattern.
819 *
820 * @param attrName The name of the attribute searched.
821 * @param[out] result A sub-string with the result.
822 * @param attrDelim The delimiter to search for. Defaults to '='.
823 * @return \c true if the attribute was found, \c false otherwise.
824 * In the latter case, output argument \p{result} will be a \e nulled string.
825 ******************************************************************************************/
826 ALIB_API bool GetAttribute( const String& attrName , Substring& result,
827 character attrDelim= A_CHAR('=') );
828
829 // #############################################################################################
830 // protected methods
831 // #############################################################################################
832 protected:
833
834}; // class Variable
835
836} // namespace alib[::config]
837
838/// Type alias in namespace \b alib.
840
841} // namespace [alib]
842
843#endif // HPP_ALIB_CONFIG_VARIABLE
void Add(int32_t value)
Definition variable.hpp:748
Configuration * Config()
Definition variable.hpp:468
void SetFmtHints(FormatHints hints)
Definition variable.hpp:538
ALIB_API bool IsTrue(int idx=0)
Definition variable.cpp:242
const String & DefaultValue() const
Definition variable.hpp:597
integer Size() const
Definition variable.hpp:708
void ReplaceDefaultValue(const String &newValue)
Definition variable.hpp:681
void ReplaceFormatAttrAlignment(const String &newValue)
Definition variable.hpp:694
const String & FormatAttrAlignment() const
Definition variable.hpp:549
void ReplaceComments(const String &newValue)
Definition variable.hpp:668
ALIB_API bool GetAttribute(const String &attrName, Substring &result, character attrDelim=A_CHAR('='))
Definition variable.cpp:250
FormatHints FmtHints() const
Definition variable.hpp:527
ALIB_API const String & Fullname()
Definition variable.cpp:266
const Configuration * Config() const
Definition variable.hpp:458
void SetPriority(Priorities priority)
Definition variable.hpp:628
ALIB_API void Add(const String &value)
Definition variable.cpp:223
Variable(TEnum declaration)
ALIB_API void ReplaceValue(int idx, const String &replacement)
Definition variable.cpp:188
Variable(Variable &&src) noexcept
Definition variable.hpp:206
Variable(const Variable &variable)
Definition variable.hpp:251
character Delim() const
Definition variable.hpp:506
Variable(TEnum declaration, const Box &replacements)
ALIB_API Variable & Declare(const VariableDecl &declaration, const Box &replacements)
Definition variable.cpp:77
ALIB_API double GetFloat(int idx=0)
Definition variable.cpp:246
const String & Comments() const
Definition variable.hpp:558
const String & Name() const
Definition variable.hpp:496
void ReplaceName(const String &newValue)
Definition variable.hpp:655
void SetDelim(character delim)
Definition variable.hpp:517
const String & Category() const
Definition variable.hpp:487
const String & GetString(int idx=0)
Definition variable.hpp:780
Variable(const VariableDecl &declaration, const Box &replacements)
Definition variable.hpp:235
Variable & ClearValues(int startIdx=0)
Definition variable.hpp:721
Priorities Priority() const
Definition variable.hpp:617
Variable & Declare(TEnum declaration)
ALIB_API Variable & Reset(lang::CurrentData nameAndCategory=lang::CurrentData::Clear)
Definition variable.cpp:25
void ReplaceCategory(const String &newValue)
Definition variable.hpp:642
ALIB_API integer GetInteger(int idx=0)
Definition variable.cpp:244
Variable(const String &category, const String &name, character delim='\0', const String &comments=nullptr)
Definition variable.hpp:270
void SetConfig(Configuration *config)
Definition variable.hpp:477
strings::TString< TChar > EmplaceString(const strings::TString< TChar > &src)
#define A_CHAR(STR)
#define ATMP_VOID_IF(Cond)
Definition tmp.hpp:52
#define ALIB_API
Definition alib.hpp:538
#define ALIB_ASSERT_ERROR(cond,...)
Definition alib.hpp:984
#define ATMP_T_IF(T, Cond)
Definition tmp.hpp:53
@ Clear
Chooses to clear existing data.
Definition alib.cpp:57
NLocalString< 128 > NString128
Type alias name for TLocalString<nchar,128> .
characters::complementChar complementChar
Type alias in namespace alib.
config::Configuration Configuration
Type alias in namespace alib.
characters::character character
Type alias in namespace alib.
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:286
VariableFields(monomem::SelfContained< VariableFields >::Fields **fields)
Definition variable.hpp:130
std::vector< String, StdContMA< String > > values
Definition variable.hpp:119