This class is a container for objects of type ConfigurationPlugin, and provides a single user interface to query configuration data from those.
By default, all category and variable names are case insensitive. This is at least true for the default plug-ins delivered with ALib .
Variables by default can contain other variables, which are recursively substituted by this method. For more information about the (adjustable) syntax, see Variable Substitution.
See documentation of namespace alib::config for more information on ALib external configuration variables.
Definition at line 62 of file configuration.hpp.
#include <configuration.hpp>
Inner Type Index: | |
class | Iterator |
Public Field Index: | |
alib::NumberFormat | NumberFormat |
CString | SubstitutionVariableDelimiters = A_CHAR(" $@,.;:\"\'+-*/\\§%&()[]{}<>=?'`~#") |
String | SubstitutionVariableEnd = nullptr |
String | SubstitutionVariableStart = A_CHAR("$") |
Variable | tempVar |
std::vector< String > | TrueValues |
Additional Inherited Members | |
Public Type Index: inherited from PluginContainer< ConfigurationPlugin, Priorities > | |
using | TPlugin |
using | TPriorities |
Number format definition used to read and write int and float values.
Definition at line 84 of file configuration.hpp.
The delimiters used to identify the end of a substitutable variable. If field SubstitutionVariableEnd is not empty, this field is ignored. Otherwise, all characters defined in this string are used to identify the end of a substitution variable.
Definition at line 107 of file configuration.hpp.
String SubstitutionVariableEnd = nullptr |
The end of a substitution variables. If this field is set, then field SubstitutionVariableDelimiters is ignored. If this field is nullptr (the default) or empty, it is ignored and characters in field SubstitutionVariableDelimiters are used to identify the end of the variable.
Definition at line 99 of file configuration.hpp.
The start string to identify substitution variables. Defaults to single character '$'
. If a string is set, i.e. "${"
, then field SubstitutionVariableEnd may be set accordingly, i.e. "}"
Definition at line 91 of file configuration.hpp.
Variable tempVar |
A temporary variable to be reused (allocate once pattern).
Definition at line 110 of file configuration.hpp.
std::vector<String> TrueValues |
Values considered to indicate "true". Defaults to { "1", "true", "t", "yes", "y", "on", "ok" }. See methods IsTrue.
Application specific values (e.g. localization languages) might be added by publicly accessing this field and adding new values (or removing existing).
Definition at line 81 of file configuration.hpp.
Configuration | ( | lang::CreateDefaults | addPlugins | ) |
Constructs a Configuration. If addDefaultPlugins is true
, registers the initial plug-ins as follows
Priority | Plug-in Type |
---|---|
Priorities::ProtectedValues | InMemoryPlugin |
Priorities::Environment | Environment |
Priorities::CLI | CLIArgs |
Priorities::DefaultValues | InMemoryPlugin |
addPlugins | If Yes the default plugins are added. |
Definition at line 49 of file configuration.cpp.
int FetchFromDefault | ( | ConfigurationPlugin & | dest, |
const String & | section = NullString() ) |
This method fetches all values from a plug-in of priority Priorities::DefaultValues , which are not present in the given plug-in dest and stores them in that. This is useful to collect all generated default values and store them in a user's configuration file. This way, the user can identify configurable options easily.
dest | The destination plug-in. |
section | Optional string denoting a section to fetch. Defaults to NullString(). |
Definition at line 63 of file configuration.cpp.
Creates an iterator object to return all variables within a section.
The iterator object returned, needs to be deleted by the caller.
sectionName | The name of the section to iterate. |
bool IsTrue | ( | const String & | value | ) |
Utility method that checks if a given value represents boolean true. Uses field TrueValues. Comparisons are made case insensitive.
value | The value to check |
true
, if the value represents 'true', false
if not. Definition at line 90 of file configuration.cpp.
Priorities Load | ( | Variable & | variable | ) |
Receives and optionally creates configuration variable.
If the variable was not found and Variable::DefaultValue in variable is set, the method adds the value to a plug-in of priority Priorities::DefaultValues , (respectively to the plug-in found at or below DefaultValues). For the conversion of the value, field ConfigurationPlugin::StringConverter of field a plug-in of priority DefaultValues, is used.
If no plug-in at or below priority DefaultValues is found, then the declared default value is added to the variable using the default implementation of XTernalizer::LoadFromString .
variable | The variable to receive. |
0
if not found. Definition at line 104 of file configuration.cpp.
Convenience method to set values in variable according to the provided string. For the conversion of the "externalized" string, method XTernalizer::LoadFromString of field ConfigurationPlugin::StringConverter of a plug-in of priority Priorities::DefaultValues , is used.
variable | The variable object. |
externalizedValue | The new value to write. |
Definition at line 217 of file configuration.cpp.
|
protected |
Implementation of method Load.
variable | The variable to get. |
substitute | If false , automatic variable substitutions are suppressed. |
Definition at line 230 of file configuration.cpp.
|
inline |
This method loads all variables of the given enum type in case no placeholders are contained in their name and a default value is given with the declaraition.
With that they become stored in this configuration's default plug-in and a later invocation of FetchFromDefault may be used to move those to a plug-in that persitently stores them, for example an INI-file.
The goal of this approach is to have all (most) variables presented in an external configuration file that is automatically created on a software's first run - regardless if that first run used the variables or not.
TEnum | Enumeration type equipped with ALib Enum Records of type VariableDecl . |
Priorities Protect | ( | Variable & | variable, |
const String & | externalizedValue = nullptr ) |
Convenience method that stores the variable with priority Priorities::ProtectedValues .
The variable value is determined as follows:
variable | The variable object. |
externalizedValue | Optional externalized value string. If given, the variable is set prior to writing. |
Definition at line 200 of file configuration.cpp.
|
inline |
Variant of method SetCommandLineArgs, accepting command line arguments of type wchar
.
argc | Parameter usually taken from standard C main() method (the number of arguments in argv ). |
argv | The command line arguments as a zero-terminated list of pointers to zero-terminated wchar_c strings. |
Definition at line 171 of file configuration.hpp.
|
inline |
Sets the command line arguments for default plug-in CLIArgs . This method should be called for instances of this class after construction.
argc | Parameter usually taken from standard C main() method (the number of arguments in argv ). Defaults to 0. |
argv | Parameter usually taken from standard C main() method (pointer to a zero-terminated list of zero-terminated strings comprising the command line arguments). Defaults to nullptr. |
Definition at line 152 of file configuration.hpp.
Priorities Store | ( | Variable & | variable, |
const String & | externalizedValue = nullptr ) |
Writes the variable into one of the plug-ins registered with this configuration object. The plug-ins are looped in descending order of priority, until a first plug-in confirms the variable to be written.
The first (most highly prioritized) plug-in to start the loop with, is defined by field Variable::Priority of the given variable .
If this is unset (Priorities::NONE ), which is the default value of the field for freshly declared variable objects, the starting priority value of the loop is detected.
Detection is made by searching the variable in the plug-ins prior to storing it. The search order is likewise by priority, starting with the highest. If the variable was not found, Priorities::DefaultValues is chosen. Usually the writing loop then will also already end there, because standard configuration sets have a write-enabled in-memory plug-in at that priority.
If the variable was found in the plug-in of priority Priorities::ProtectedValues , the method stops without storing anything.
This approach of storing variables, supports various use cases very nicely. Some of the frequent ones shall be named here:
- 1
, allows to store the variable just in the plug-in with highest possible priority.The method returns the priority of the configuration plug-in that the value was written to. This value is as well stored in field Variable::Priority of the given object.
A result of Priorities::NONE indicates that the variable was not written. This might only happen if
Optional parameter externalizedValue allows to provide a string that is parsed by the storing plug-in to reset the variable's values prior to writing.
variable | The variable object. |
externalizedValue | Optional externalized value string. If given, the variable is set prior to writing. |
Definition at line 123 of file configuration.cpp.
Priorities StoreDefault | ( | Variable & | variable, |
const String & | externalizedValue = nullptr ) |
Convenience method that stores the variable with priority Priorities::DefaultValues .
The variable value is determined as follows:
variable | The variable object. |
externalizedValue | Optional externalized value string. If given, the variable is set prior to writing. |
Definition at line 184 of file configuration.cpp.