This is the central interface type into the module ALib Variables. The class is a very lightweight (size of two times sizeof(void*)
) object, because it rather is a pointer to a variable instead of an implementation of its data structures. The types' true nature is inherited from class StringTree::Cursor which is the base of this class. The inheritance is defined protected
and this class received its own dedicated interface reflecting the domain of configuration variables. Nevertheless, the protected base type is accessible with the explicit cast method AsCursor. While a reader might think this is a contradiction, this was made by design: Casting to a StringTree::Cursor is only necessary for implementing ConfigurationPlugin or similar extraordinary things. For the common use, the dedicated interface is all that is needed.
Definition at line 736 of file configuration.inl.
Public Method Index: | |
Variable ()=default | |
Variable (Configuration &cfg) | |
Variable (Configuration &cfg, const Declaration *decl) | |
Variable (Configuration &cfg, const String &name, const String &typeName, const String &defaultValue=NULL_STRING) | |
template<typename TEnum> | |
Variable (Configuration &cfg, TEnum Enum) | |
template<typename TEnum> requires std::is_enum_v<TEnum> | |
Variable (Configuration &cfg, TEnum Enum, const Box replacements) | |
Variable (const Cursor &cursor) | |
Variable (const Cursor &cursor, const String &typeName, const String &defaultValue=NULL_STRING) | |
Cursor & | AsCursor () |
const Cursor & | AsCursor () const |
ALIB_DLL Variable & | Declare (const Declaration *declaration) |
ALIB_DLL Variable & | Declare (const String &name, const String &typeName, const String &defaultValue=NULL_STRING) |
template<typename TEnum> requires ( EnumRecords<TEnum>::template AreOfType<Declaration>() ) | |
Variable & | Declare (TEnum Enum) |
template<typename TEnum> requires ( EnumRecords<TEnum>::template AreOfType<Declaration>() ) | |
Variable & | Declare (TEnum Enum, const Box &replacements) |
ALIB_DLL bool | Define (Priority requestedPriority=Priority::Standard) |
ALIB_DLL void | Delete () |
AString & | Export (AString &dest, const StringEscaper *escaper=nullptr) const |
template<typename T> | |
T & | Get () |
template<typename T> | |
const T & | Get () const |
bool | GetBool () |
Box & | GetBox () |
Configuration & | GetConfiguration () const |
const Declaration * | GetDeclaration () const |
double | GetDouble () |
float | GetFloat () |
integer | GetInt () |
bool | GetOrSetDefault (bool value, Priority priority=Priority::DefaultValues) |
String & | GetOrSetDefault (const String &value, Priority priority=Priority::DefaultValues) |
bool | GetOrSetDefault (integer value, Priority priority=Priority::DefaultValues) |
Priority | GetPriority () const |
AStringPA & | GetString () |
String & | GetString (int idx) |
StringVectorPA & | GetStrings () |
ALIB_DLL void | Import (const String &src, Priority priority, const StringEscaper *escaper=nullptr) |
bool | IsDeclared () const |
bool | IsDefined () const |
bool | IsDefinedExternally () const |
bool | IsNotDefined () const |
AString & | Name (AString &target) const |
operator bool () | |
operator const String & () | |
operator const Substring () | |
operator double () | |
operator float () | |
operator integer () | |
bool | operator= (bool val) |
Calls Get<Bool>() and assigns val. | |
const String & | operator= (const String &val) |
Calls Get<AStringPA>() and resets the string to val. | |
double | operator= (double val) |
Calls Get<double>() and assigns val. | |
float | operator= (float val) |
Calls Get<double>() and assigns val. | |
integer | operator= (integer val) |
Calls Get<integer>() and assigns val. | |
int | Size () |
ALIB_DLL bool | Try (const Declaration *decl) |
bool | Try (const String &name) |
ALIB_DLL bool | Try (const String &name, const String &typeName) |
Protected Method Index: | |
void | create (const String &typeName, const String &defaultValue) |
VMeta * | getMeta () const |
const String & | substitute (const String &importString, AString &buf, const StringEscaper *escaper) |
Private Type Index: | |
using | Cursor = Configuration::Cursor |
|
friend |
Definition at line 738 of file configuration.inl.
|
private |
The base cursor type of the internal StringTree. This type is used to perform cursor operations on Configuration instances.
Definition at line 742 of file configuration.inl.
|
default |
Default constructor.
|
inline |
Constructs an instance of this type from its base type. This constructor is for advanced use when direct operations with class StringTree and its cursor and iterator types are performed.
Note that the variable has to be declared already, when this constructor is used.
cursor | The StringTree cursor. |
Definition at line 788 of file configuration.inl.
|
inline |
Constructs an instance of this type from its base type. This constructor is for advanced use when direct operations with class StringTree and its cursor and iterator types are performed.
Note that the previous variable declaratin will be destroyed, in case a variable had been declared at this cursor already.
cursor | The StringTree cursor. |
typeName | The type name. |
defaultValue | A default value, if available. Defaults to nulled string. |
Definition at line 799 of file configuration.inl.
|
inlineexplicit |
Constructs an undeclared variable "cursor" associated with the given configuration object.
cfg | The configuration to use. |
Definition at line 809 of file configuration.inl.
|
inline |
Constructs and declares this variable, without using or allocating a declaration struct.
cfg | The configuration to use. |
name | The name of the variable. |
typeName | The type of the variable. |
defaultValue | An optional default value. Defaults to nulled string. |
Definition at line 819 of file configuration.inl.
|
inline |
Constructs and declares this variable.
cfg | The configuration to use. |
decl | The declaration to use. |
Definition at line 830 of file configuration.inl.
|
inline |
Constructs and declares this variable.
TEnum | The type of parameter Enum |
cfg | The configuration to use. |
Enum | Element of an enum type that represents resourced configuration variables. |
Definition at line 841 of file configuration.inl.
|
inline |
Constructs and declares this variable.
TEnum | The type of parameter Enum |
cfg | The configuration to use. |
Enum | Element of an enum type that represents resourced configuration variables. |
replacements | Replacement values. |
Definition at line 856 of file configuration.inl.
|
inline |
This is an explicit cast operator
to the protected base class.
Cursor
. Definition at line 869 of file configuration.inl.
|
inline |
const
version of the cast operator
to the protected base class.
Definition at line 874 of file configuration.inl.
|
protected |
Called by declaration methods. Creates necessary internal data structs, searches configuration plug-ins for data, and, in case not found, optionally applies the given defaultValue
typeName | The type name. |
defaultValue | A default value, if available. |
Definition at line 118 of file variable.cpp.
Variable & alib::variables::Variable::Declare | ( | const Declaration * | declaration | ) |
Moves this variable cursor (aka StringTree::Cursor) to represent the variable of the given declaration. If the variable does not exist, yet it is created. If the variable exists, the given decl is ignored. In debug compilations, an error is raised in case the variable existed but has a different declaration attached.
If the variable is created and the declaration contains a default value, such value is imported from the string. In this case, DefaultValues is returned.
declaration | The declaration singleton. |
*this
to allow concatenated operations. Definition at line 241 of file variable.cpp.
Variable & alib::variables::Variable::Declare | ( | const String & | name, |
const String & | typeName, | ||
const String & | defaultValue = NULL_STRING ) |
Declares this variable, without using or allocating a declaration struct.
name | The name of the variable. |
typeName | The type of the variable. |
defaultValue | An optional default value. Defaults to nulled string. |
*this
to allow concatenated operations. Definition at line 201 of file variable.cpp.
|
inline |
Declares this variable.
TEnum | The type of parameter Enum |
Enum | Element of an enum type that represents resourced configuration variables. |
Definition at line 910 of file configuration.inl.
Declares this variable.
TEnum | The type of parameter Enum |
Enum | Element of an enum type that represents resourced configuration variables. |
replacements | Replacement values. |
Definition at line 925 of file configuration.inl.
|
nodiscard |
Increases the reported priority of this variable to the value given. If the current value is higher than the given one, it is not increased and false
is returned. In this case, a program should not write the variable's value.
[[nodiscard]]
.(void)
before the call.requestedPriority | The new, higher priority to set. |
true
, if the current priority was lower or equal to the given one, false
otherwise. Definition at line 280 of file variable.cpp.
void alib::variables::Variable::Delete | ( | ) |
Deletes a variable. After this method is executed, the internal cursor will be invalid and further operations on this variable instance can only be made after a next declaration.
Definition at line 293 of file variable.cpp.
|
inline |
Exports the contents of this variable to the given buffer. This is done by calling VMeta::exPort on the virtual meta object associated with this variable's type.
dest | The destination string. Must not be reset prior writing, but appended. |
escaper | The StringEscaper to use for exporting a C++ string to a (probably) escaped string format. Defaults to nullptr which indicates that no conversion (aka 'escaping') has to be performed. Usually, if a ConfigurationPlugin invokes this method to write-back the contents of a variable, it would pass its own suitable implementation here. |
Definition at line 1135 of file configuration.inl.
|
inline |
Returns a reference to the variable's value. To receive a value the variable has to be defined, which is either done, by
If this is not done, in debug-compilations an error is raised. In release-builds, it is defined (!) behavior: A default constructed value of T is returned. Hence, the whole concept of defining a variable and methods IsDefined, etc. is rather a contract than a necessity. This is explained in section 4. Variable Definition of the Programmer's Manual of this ALib Camp.
Template parameter T has to be specified in alignment with the declaration of the variable. It is the users responsibility to specify the right type, as internally a reinterpret_cast
has to be performed. In other words, providing a wrong type is undefined behavior.
In debug-compilations, an error is raised if the type does not match the declaration.
T | The type to receive. |
Definition at line 1170 of file configuration.inl.
|
inline |
const
-version of overloaded method.
T | The type to receive. |
Definition at line 1190 of file configuration.inl.
|
inline |
Definition at line 1213 of file configuration.inl.
|
inline |
Definition at line 1217 of file configuration.inl.
|
inline |
Returns a reference
to the configuration this variable resides in.
Definition at line 1058 of file configuration.inl.
|
inline |
Returns the declaration struct of this variable, in case it was constructed or declared with an overloaded constructor or method Declare, which accepts such struct. Otherwise, a nullptr is returned.
nullptr
, if no declaration struct was given. Otherwise, the singleton declaration object provided. Definition at line 1054 of file configuration.inl.
|
inline |
Definition at line 1216 of file configuration.inl.
|
inline |
float
. Definition at line 1215 of file configuration.inl.
|
inline |
Definition at line 1214 of file configuration.inl.
|
inlineprotected |
Returns the meta-information object.
Definition at line 764 of file configuration.inl.
|
inline |
Convenience method. If IsDefined returns true
, the value of this variable is returned. Otherwise the variable gets defined with the given priority and value is imported.
value | The value to import in case the variable is not defined. |
priority | The priority to set on import in case the variable is not defined. Defaults to DefaultValues. |
false
if data of any priority is available in this variable, true
if not. Definition at line 1254 of file configuration.inl.
|
inline |
Convenience method. If IsDefined returns true
, the value of this variable is returned. Otherwise the variable gets defined with the given priority and value is imported.
value | The value to import in case the variable is not defined. |
priority | The priority to set on import in case the variable is not defined. Defaults to DefaultValues. |
false
if data of any priority is available in this variable, true
if not. Definition at line 1238 of file configuration.inl.
|
inline |
Convenience method. If IsDefined returns true
, the value of this variable is returned. Otherwise the variable gets defined with the given priority and value is imported.
value | The value to import in case the variable is not defined. |
priority | The priority to set on import in case the variable is not defined. Defaults to DefaultValues. |
false
if data of any priority is available in this variable, true
if not. Definition at line 1270 of file configuration.inl.
|
inline |
The priority of a variable indicates "who" or "what" defined the variable and provided the current value. It may be defaulted values, values coming from external configuration files, from command line arguments, environment variables or from common code entities ('hard-coded' values). Besides the elements in enumeration Priority, also any value in between may be given. Doing so, allows fine-tuning which entity exacly is allowed to write values. For example, if a variable's priority is set in code to Environment, then environment variables and CLI arguments may still overwrite a value. If set to Environment + 1, then environment variables would not overwrite, but still CLI arguments could.
A priority of Protected disallows any code entity to make further changes, but those that again use this priority.
Definition at line 1075 of file configuration.inl.
|
inline |
Definition at line 1218 of file configuration.inl.
|
inline |
idx | The index of the requested string. |
Definition at line 1220 of file configuration.inl.
|
inline |
Definition at line 1219 of file configuration.inl.
void alib::variables::Variable::Import | ( | const String & | src, |
Priority | priority, | ||
const StringEscaper * | escaper = nullptr ) |
Imports a variable value from a serialized string representation. Internally, this is performed by calling VMeta::imPort on the virtual meta object associated with this variable's type.
src | The source string. |
priority | The priority of the import. If the variable is already defined in a higher priority, nothing is done. |
escaper | The StringEscaper to use for importing an escaped string. Defaults to nullptr which indicates that a programmatically defined C++ string is to be parsed. Usually, if a ConfigurationPlugin invokes this method it would pass its own suitable implementation here. |
Definition at line 357 of file variable.cpp.
|
inline |
Returns true
if this variable is declared. If false
is returned, this the is just a path node inside the StringTree of the Configuration.
false
will be returned only if:
true
if the underlying StringTree::Cursor of the Configuration that this variable is associated with, points to a declared variable. Otherwise false
is returned. Definition at line 945 of file configuration.inl.
|
inline |
This is a shortcut to Priority() != Priority::NONE
and thus, this method checks whether any data is defined.
Variables which are declared using a Declaration that contains a default value, will have this default value parsed and contain a data set of priority DefaultValues.
true
if data of any priority is available in this variable, false
if not. Definition at line 1085 of file configuration.inl.
|
inline |
This is a shortcut to Priority() > Priority::Standard
and thus, this method checks whether an explicit definition with a priority higher than Standard was performed, which - by contract - is done only when external data sources are read, for example configuration files, environment variables, or command line parameters.
true
if data contained has a higher priority than Standard, false
if not. Definition at line 1105 of file configuration.inl.
|
inline |
This is the negation of method IsDefined.
false
if data of any priority is available in this variable, true
if not. Definition at line 1095 of file configuration.inl.
Assembles the variable's name from its path within the internal StringTree.
target | The string buffer to append this variable's name to. |
Definition at line 1006 of file configuration.inl.
|
inline |
Definition at line 1206 of file configuration.inl.
|
inline |
Definition at line 1210 of file configuration.inl.
|
inline |
Definition at line 1211 of file configuration.inl.
|
inline |
Definition at line 1209 of file configuration.inl.
|
inline |
float
. Definition at line 1208 of file configuration.inl.
|
inline |
Definition at line 1207 of file configuration.inl.
|
inline |
Calls Get<Bool>() and assigns val.
val | The value to assign. |
Definition at line 1223 of file configuration.inl.
Calls Get<AStringPA>() and resets the string to val.
val | The value to assign. |
Definition at line 1227 of file configuration.inl.
|
inline |
Calls Get<double>() and assigns val.
val | The value to assign. |
Definition at line 1226 of file configuration.inl.
|
inline |
Calls Get<double>() and assigns val.
val | The value to assign. |
Definition at line 1225 of file configuration.inl.
Calls Get<integer>() and assigns val.
val | The value to assign. |
Definition at line 1224 of file configuration.inl.
|
inline |
Definition at line 1221 of file configuration.inl.
|
protected |
Performs variable substitution. This method is called before any call to VMeta::imPort.
importString | The import string. |
buf | The substitution buffer. |
escaper | The escaper used to externalize substitution values. |
Definition at line 26 of file variable.cpp.
bool alib::variables::Variable::Try | ( | const Declaration * | decl | ) |
Tries to move the underlying StringTree::Cursor of the Configuration that this variable is associated with, to the variable name found with given declaration. If this succeeds, it is checked if the variable has no declaration record set, and if not, the one given is stored with the variable.
decl | The declaration of the variable to use. |
true
if the variable name specified with the given declaration existed and was declared, or if a preset value existed. Otherwise false
is returned and the variable's value is not allowed to be accessed. Definition at line 338 of file variable.cpp.
|
inline |
Tries to move the underlying StringTree::Cursor of the Configuration that this variable is associated with, to the given path (aka variable name). If this succeeds, the result of IsDeclared() is returned. Otherwise false
.
Note that this method still returns false
if a preset value exists for the variable, because preset values can only be imported if a variable's type is known, hence when a variable is declared. But this method explicitly avoids declarations.
name | The name of the variable to try. |
true
if the variable name specified with the given declaration existed and was declared. Otherwise false
is returned and the variable's value is not allowed to be accessed. Definition at line 962 of file configuration.inl.
Tries to move the underlying StringTree::Cursor of the Configuration that this variable is associated with, to the given path (aka variable name).
name | The name of the variable. |
typeName | The type of the variable. |
true
if the variable name specified with the given declaration existed and was declared. Otherwise false
is returned and the variable's value is not allowed to be accessed. Definition at line 303 of file variable.cpp.