This class is used to load and store external configuration data with objects of class Configuration and its plug-ins ConfigurationPlugin .
Construction/Redeclaration:
While constructors accepting attributes of a variable exist, it is recommended to declare all external configuration variables in a central place, using statically or dynamically allocated objects of type VariableDecl and pass such record to the constructor of a variable.
The class is self-contained in a MonoAllocator and supports is designed to be 'reused'. This is implementation is chosen to avoid repeated allocation/de-allocation of dynamic memory when a larger amount of variables are read or written. After invoking one of the overloaded methods Declare, which share the same signatures as the overloaded constructors, a variable is freshly initialized and its monotonic allocator is reseet.
When storing a variable that contains more than one value, field Delim has to be set. Conversely, when reading a variable that contains multiple values, the delimiter has to be set prior to the load operation.
To set the contents of various fields, methods whose name is prefixed with Replace are given. Each of these methods allocates new storage in the internal monotonic allocator, which is freed only with method Reset, which is internally called with each overloaded Declare method. The using software has to ensure that Reset is invoked on a regular basis, to not generate a "memory leak" by increasing the allocated space in the monotonic allocator. In common use-cases of the class, this is not an issue.
Loading and Storing:
There are two ways of loading and storing a variable:
Storing empty variables (method Size returns 0
) deletes a variable from the those configuration plug-ins that are write enabled.
Definition at line 196 of file variable.hpp.
#include <variable.hpp>
Public Method Index: | |
Variable () | |
Variable (const String &category, const String &name, character delim='\0', const String &comments=nullptr) | |
Variable (const Variable &variable) | |
Variable (const VariableDecl &declaration, const Box &replacements) | |
template<typename TEnum , typename TEnableIf = void> | |
Variable (TEnum declaration) | |
template<typename TEnum , typename TEnableIf = void> | |
Variable (TEnum declaration, const Box &replacements) | |
Variable (Variable &&src) noexcept | |
ALIB_API void | Add (const String &value) |
ALIB_API void | Add (double value) |
void | Add (int32_t value) |
ALIB_API void | Add (int64_t value) |
const String & | Category () const |
Variable & | ClearValues (int startIdx=0) |
const String & | Comments () const |
Configuration * | Config () |
const Configuration * | Config () const |
ALIB_API Variable & | Declare (const ComplementString &category, const ComplementString &name, complementChar delim='\0', const ComplementString &comments=nullptr) |
ALIB_API Variable & | Declare (const String &category, const String &name, character delim='\0', const String &comments=nullptr) |
ALIB_API Variable & | Declare (const Variable &variable) |
ALIB_API Variable & | Declare (const VariableDecl &declaration, const Box &replacements) |
template<typename TEnum > | |
Variable & | Declare (TEnum declaration) |
const String & | DefaultValue () const |
character | Delim () const |
FormatHints | FmtHints () const |
const String & | FormatAttrAlignment () const |
ALIB_API const String & | Fullname () |
ALIB_API bool | GetAttribute (const String &attrName, Substring &result, character attrDelim=A_CHAR('=')) |
ALIB_API double | GetFloat (int idx=0) |
ALIB_API integer | GetInteger (int idx=0) |
const String & | GetString (int idx=0) |
ALIB_API bool | IsTrue (int idx=0) |
const String & | Name () const |
Priorities | Priority () const |
void | ReplaceCategory (const String &newValue) |
void | ReplaceComments (const String &newValue) |
void | ReplaceDefaultValue (const String &newValue) |
void | ReplaceFormatAttrAlignment (const String &newValue) |
void | ReplaceName (const String &newValue) |
ALIB_API void | ReplaceValue (int idx, const String &replacement) |
ALIB_API void | ReplaceValue (int idx, Variable &replVariable) |
ALIB_API Variable & | Reset (lang::CurrentData nameAndCategory=lang::CurrentData::Clear) |
void | SetConfig (Configuration *config) |
void | SetDelim (character delim) |
void | SetFmtHints (FormatHints hints) |
void | SetPriority (Priorities priority) |
integer | Size () const |
Move constructor moves the entries and deletes them in source object.
src | The object to move. |
Definition at line 206 of file variable.hpp.
|
inline |
Constructs an undefined Variable. Prior to using this, Declare has to be invoked.
Definition at line 213 of file variable.hpp.
|
inline |
Constructs a variable from a declaration. Strings named "%1"
, "%2"
... "%N"
found in the fields Category, Name, Comments and DefaultValue are replaced with given replacement values found in boxed argument (list) replacements .
declaration | The declaration data of the variable. |
replacements | List of replacement values. Must be of boxed types that are appendable to class AString. To provide more than one object, pass an object of class Boxes or a boxed array. |
Definition at line 235 of file variable.hpp.
Constructs a variable using the declaration of another variable. The values are not copied.
variable | A variable to copy the declaration (which is comprised with fields Category, Name, Fullname, Delim, Comments and DefaultValue) from. |
Definition at line 251 of file variable.hpp.
|
inline |
Constructs a variable from the given values
category | The category of the variable. |
name | The name of the variable |
delim | A proposal for a delimiter that might be used by some plug-ins to delimit different values from each other (e.g. INI files). |
comments | Comment lines that might be added in the configuration storage (plug-in implementation dependent). |
Definition at line 270 of file variable.hpp.
|
inline |
Constructs a variable from an enum element equipped with ALib Enum Records of type VariableDecl .
TEnum | The type of parameter declaration |
TEnableIf | Not to be specified. Used by the compiler to select this constructor only for associated custom C++ enum types. |
declaration | Element of an enum class that is representing configuration variables. |
Variable | ( | TEnum | declaration, |
const Box & | replacements ) |
Constructs a variable from an enum element equipped with ALib Enum Records of type VariableDecl .
Strings named "%1"
, "%2"
... "%N"
found in the fields Category, Name, Comments and DefaultValue are replaced with given replacement values found in boxed argument (list) replacements .
TEnum | The type of parameter declaration |
TEnableIf | Not to be specified. Used by the compiler to select this constructor only for associated custom C++ enum types. |
declaration | Element of an enum class that is representing configuration variables. |
replacements | List of replacement values. Must be of boxed types that are appendable to class AString. To provide more than one object, pass an object of class Boxes or a boxed array of boxes. |
void Add | ( | const String & | value | ) |
Adds a value to the end of the list of values.
value | The string value to add. |
Definition at line 223 of file variable.cpp.
void Add | ( | double | value | ) |
Adds a floating point value to the end of the list of values.
value | The value to add. |
Definition at line 235 of file variable.cpp.
|
inline |
Adds an integral value to the end of the list of values.
value | The value to add. |
Definition at line 748 of file variable.hpp.
void Add | ( | int64_t | value | ) |
Adds an integral value to the end of the list of values.
value | The value to add. |
Definition at line 229 of file variable.cpp.
|
inline |
Returns this configuration variable's category.
Definition at line 487 of file variable.hpp.
|
inline |
Clears the value, respectively the list of values set. After invoking this method, Size will return startIdx and GetString will return nullptr
for any index greater or equal to startIdx .
startIdx | The index of the first value to be cleared. Defaults to 0 . |
*this
to allow concatenated operations. Definition at line 721 of file variable.hpp.
|
inline |
Returns this configuration variable's comments.
Definition at line 558 of file variable.hpp.
|
inline |
The Configuration that was recently used to request or store the value.
nullptr
if no configuration is assigned. Definition at line 468 of file variable.hpp.
|
inline |
The Configuration that was recently used to request or store the value.
nullptr
if no configuration is assigned. Definition at line 458 of file variable.hpp.
Variable & Declare | ( | const ComplementString & | category, |
const ComplementString & | name, | ||
complementChar | delim = '\0', | ||
const ComplementString & | comments = nullptr ) |
Re-initializes the variable using the specific values given. This overload accepts complementary string character types for each argument.
category | The category of the variable. |
name | The name of the variable |
delim | A proposal for a delimiter that might be used by some plug-ins to delimit different values from each other (e.g. INI files). Defaults to '\0' . |
comments | Comment lines that might be added in the configuration storage (plug-in implementation dependent). Defaults to nullptr . |
*this
to allow concatenated operations. Definition at line 167 of file variable.cpp.
Variable & Declare | ( | const String & | category, |
const String & | name, | ||
character | delim = '\0', | ||
const String & | comments = nullptr ) |
Re-initializes the variable using the specific values given.
category | The category of the variable. |
name | The name of the variable |
delim | A proposal for a delimiter that might be used by some plug-ins to delimit different values from each other (e.g. INI files). Defaults to '\0' . |
comments | Comment lines that might be added in the configuration storage (plug-in implementation dependent). Defaults to nullptr . |
*this
to allow concatenated operations. Definition at line 151 of file variable.cpp.
Declares a variable using the declaration of another variable. The values are not copied.
variable | A variable to copy the declaration (which is comprised with fields Category, Name, Fullname, Delim, Comments and DefaultValue) from. |
*this
to allow concatenated operations. Definition at line 61 of file variable.cpp.
Variable & Declare | ( | const VariableDecl & | declaration, |
const Box & | replacements ) |
Re-initializes this variable from a given declaration. Placeholders "%1"
, "%2"
... "%N"
found in fields Category, Name, Comments and DefaultValue are replaced with the replacement values given with boxed argument (list) replacements .
declaration | The declaration data of the variable. |
replacements | List of replacement values. Must be of boxed types that are appendable to class AString. To provide more than one object, pass an object of class Boxes or a boxed array. |
*this
to allow concatenated operations. Definition at line 77 of file variable.cpp.
Variable & Declare | ( | TEnum | declaration | ) |
Re-initializes this variable and resets its declaration. Internally uses an instance of class VariableDecl constructed with enum element declaration .
declaration | Element of an enum class that is representing configuration variables. |
TEnum | The type of parameter declaration |
*this
to allow concatenated operations.
|
inline |
The default value provided as an externalized string.
The only occasion that this value is used is with method Configuration::Load . If no plug-in has this variable defined and this field is not nulled, then the value is written into plug-in of priority Priorities::DefaultValues , respectively - if this default plug-in was replaced by the user - into a plug-in found at or below this priority.
In this case, the value is parsed using method XTernalizer::LoadFromString of field ConfigurationPlugin::StringConverter of the plug-in writing the value.
Definition at line 597 of file variable.hpp.
|
inline |
The delimiter used for parsing and storing values by simple textual plug-ins which use the default version of XTernalizer for in- and externalizing variables.
Definition at line 506 of file variable.hpp.
|
inline |
Hints for formatting textual configuration files. (Used by class IniFile and potentially by custom plug-ins.
Definition at line 527 of file variable.hpp.
|
inline |
If set, attributes written in multi-lines are vertically aligned by this character or string. Use cases are "=", ":" or "->".
Used by IniFile and potentially by custom plug-ins.
Definition at line 549 of file variable.hpp.
const String & Fullname | ( | ) |
The configuration variable category and name concatenated with an underscore character '_'
. This is useful e.g. for naming variables in log messages.
const
, as the full name might be assembled with the invocation of this method.'_'
. Definition at line 266 of file variable.cpp.
bool GetAttribute | ( | const String & | attrName, |
Substring & | result, | ||
character | attrDelim = A_CHAR('=') ) |
Searches in the values of this variable for the pattern attrName = result
and sets parameter result to the string following this pattern.
attrName | The name of the attribute searched. | |
[out] | result | A sub-string with the result. |
attrDelim | The delimiter to search for. Defaults to '='. |
true
if the attribute was found, false
otherwise. In the latter case, output argument result will be a nulled string. Definition at line 250 of file variable.cpp.
double GetFloat | ( | int | idx = 0 | ) |
Returns the value at idx interpreted as a double value. If the index is invalid, 0.0
is returned. Parsing is done using field NumberFormat
of field Config, respectively, if this is not set, the static singleton NumberFormat::Global .
idx | The index of the value to be retrieved. Defaults to 0 . |
Definition at line 246 of file variable.cpp.
integer GetInteger | ( | int | idx = 0 | ) |
Returns the value at idx interpreted as an integer. If the index is invalid, 0
is returned.
idx | The index of the value to be retrieved. Defaults to 0 . |
Definition at line 244 of file variable.cpp.
|
inline |
Returns the stored value with the given index. Valid values for parameter idx are between 0 and Size - 1.
In debug-compilations an error is raised if the given idx is out of range.
idx | The index of the value to be retrieved. Defaults to 0 . |
Definition at line 780 of file variable.hpp.
bool IsTrue | ( | int | idx = 0 | ) |
Returns true
if the first value represents a boolean 'true'. Evaluation is done using field Config, respectively if this is not set, the static singleton NumberFormat::Global .
idx | The index of the value to be retrieved. Defaults to 0 . |
Definition at line 242 of file variable.cpp.
|
inline |
Returns this configuration variable's name.
Definition at line 496 of file variable.hpp.
|
inline |
A value related to the priority of a configuration plug-in. The following values apply:
0
, if the last load or store operation failed. In addition prior to storing a variable, the value might be manually set. See documentation of Configuration::Store ) for details.Definition at line 617 of file variable.hpp.
|
inline |
Sets the value received by Category.
newValue | The new category to set. |
Definition at line 642 of file variable.hpp.
|
inline |
Sets the value received by Comments.
newValue | The new comments to set. |
Definition at line 668 of file variable.hpp.
|
inline |
Sets the value received by DefaultValue.
newValue | The new default value to set. |
Definition at line 681 of file variable.hpp.
|
inline |
Sets the value received by FormatAttrAlignment.
newValue | The new alignment to set. |
Definition at line 694 of file variable.hpp.
|
inline |
Sets the value received by Name.
newValue | The new name to set. |
Definition at line 655 of file variable.hpp.
void ReplaceValue | ( | int | idx, |
const String & | replacement ) |
Replaces the value at idx with the given string.
idx | The index of the value to replace. |
replacement | The string value to set. |
Definition at line 188 of file variable.cpp.
void ReplaceValue | ( | int | idx, |
Variable & | replVariable ) |
Replaces the value at idx with the values of the given other variable.
idx | The index of the value to replace. |
replVariable | The variable providing the replacement values. |
Definition at line 200 of file variable.cpp.
Variable & Reset | ( | lang::CurrentData | nameAndCategory = lang::CurrentData::Clear | ) |
Clears all fields and resets internal monotonic allocator.
nameAndCategory | If CurrentData::CurrentData::Keep is given, the name and category of the variable will be restored after the reset. Defaults to CurrentData::Clear. |
*this
to allow concatenated operations. Definition at line 25 of file variable.cpp.
|
inline |
Sets the Configuration to load or store the value.
config | The configuration to use. |
Definition at line 477 of file variable.hpp.
|
inline |
Sets the delimiter used for parsing and storing values.
delim | The configuration to use. |
Definition at line 517 of file variable.hpp.
|
inline |
Sets the hints for formatting textual configuration files. (Used by class IniFile and potentially by custom plug-ins.
hints | The hints to be used. |
Definition at line 538 of file variable.hpp.
|
inline |
Sets the priority.
priority | The priority to use. |
Definition at line 628 of file variable.hpp.
|
inline |
Returns the number of values set in this object.
Definition at line 708 of file variable.hpp.