#include <configuration.h>
Interface to the SMASH configuration files.
The configuration is created from a YAML file and then stores a nested map of maps (normally a tree, but YAML allows it to be cyclic - even though we don't want that feature).
For the typical usage in SMASH one needs to read the value once. In that case, use the Configuration::take function:
Note the curly braces in the function call. It is a std::initializer_list of strings. This allows an arbitrary nesting depth via the same function. But as a consequence the keys must all be given as constant strings at compile time.
If you need to access the configuration values from a run-time string you can use Configuration::operator[]. This returns a Configuration object that references the respective sub-tree.
By taking values (instead of just reading), the configuration object should be empty at the end of the initialization. If the object is not empty, SMASH will print a warning (using Configuration::unused_values_report). This can be important for the user to discover typos in his configuration file (or command line parameters).
Definition at line 313 of file configuration.h.
Classes | |
struct | FileDoesNotExist |
Thrown if the file does not exist. More... | |
struct | IncorrectTypeInAssignment |
Thrown when the types in the config file and C++ don't match. More... | |
struct | ParseError |
Thrown for YAML parse errors. More... | |
class | Value |
Return type of Configuration::take that automatically determines the target type. More... | |
Public Member Functions | |
Configuration (const bf::path &path) | |
Reads config.yaml from the specified path. More... | |
Configuration (const bf::path &path, const bf::path &filename) | |
Reads a YAML config file from the specified path. More... | |
Configuration (const char *yaml) | |
Configuration (const Configuration &)=default | |
If you want to copy this you're doing it wrong. More... | |
Configuration & | operator= (const Configuration &)=default |
If you want to copy this you're doing it wrong. More... | |
Configuration (Configuration &&)=default | |
Moving is fine. More... | |
Configuration & | operator= (Configuration &&)=default |
Moving is fine. More... | |
void | merge_yaml (const std::string &yaml) |
Merge the configuration in yaml into the existing tree. More... | |
std::vector< std::string > | list_upmost_nodes () |
Lists all YAML::Nodes from the configuration setup. More... | |
Value | take (std::initializer_list< const char * > keys) |
The default interface for SMASH to read configuration values. More... | |
template<typename T > | |
T | take (std::initializer_list< const char * > keys, T default_value) |
Value | read (std::initializer_list< const char * > keys) const |
Additional interface for SMASH to read configuration values without removing them. More... | |
template<typename T > | |
T | read (std::initializer_list< const char * > keys, T default_value) |
void | remove_all_but (const std::string &key) |
Removes all entries in the map except for key . More... | |
template<typename T > | |
Configuration | operator[] (T &&key) |
Access to the YAML::Node behind the requested keys . More... | |
template<typename T > | |
Configuration & | operator= (T &&value) |
Assignment overwrites the value of the current YAML node. More... | |
bool | has_value_including_empty (std::initializer_list< const char * > keys) const |
Returns if there is a (maybe empty) value behind the requested keys . More... | |
bool | has_value (std::initializer_list< const char * > keys) const |
Returns whether there is a non-empty value behind the requested keys . More... | |
std::string | unused_values_report () const |
Returns a string listing the key/value pairs that have not been taken yet. More... | |
std::string | to_string () const |
Returns a YAML string of the current tree. More... | |
Private Member Functions | |
Configuration (const YAML::Node &node) | |
Creates a subobject that has its root node at the given node. More... | |
Private Attributes | |
YAML::Node | root_node_ |
the general_config.yaml contents - fully parsed More... | |
|
explicit |
Reads config.yaml from the specified path.
[in] | path | The directory where the SMASH config files are located. |
Definition at line 89 of file configuration.cc.
|
explicit |
Reads a YAML config file from the specified path.
[in] | path | The directory where the SMASH config files are located. |
[in] | filename | The filename (without path) of the YAML config file, in case you don't want the default "config.yaml". |
Definition at line 93 of file configuration.cc.
|
inlineexplicit |
Definition at line 814 of file configuration.h.
|
default |
If you want to copy this you're doing it wrong.
|
default |
Moving is fine.
|
inlineprivate |
Creates a subobject that has its root node at the given node.
Definition at line 969 of file configuration.h.
|
default |
If you want to copy this you're doing it wrong.
|
default |
Moving is fine.
void smash::Configuration::merge_yaml | ( | const std::string & | yaml | ) |
Merge the configuration in yaml
into the existing tree.
The function parses the string in yaml
into its internal tree representation. Then it merges the nodes from the new tree into the existing tree. The merge resolves conflicts by taking the value from yaml
.
[in] | yaml | A string with YAML (or JSON) content that is to be merged. |
Definition at line 118 of file configuration.cc.
std::vector< std::string > smash::Configuration::list_upmost_nodes | ( | ) |
Lists all YAML::Nodes from the configuration setup.
Definition at line 132 of file configuration.cc.
Configuration::Value smash::Configuration::take | ( | std::initializer_list< const char * > | keys | ) |
The default interface for SMASH to read configuration values.
The function returns the value at the specified keys
and removes it from the Configuration object. Therefore, a subsequent call to take or has_value with the same keys
returns an undefined value / false
. By removing the value, the Configuration object keeps track which settings were never read.
[in] | keys | You can pass an arbitrary number of keys inside curly braces, following the nesting structure in the config file. Example: Group: Key: ValueCall string value = config.take({"Group", "Key"}); |
Definition at line 140 of file configuration.cc.
|
inline |
Definition at line 867 of file configuration.h.
Configuration::Value smash::Configuration::read | ( | std::initializer_list< const char * > | keys | ) | const |
Additional interface for SMASH to read configuration values without removing them.
The function returns the value at the specified keys
but does not remove it from the Configuration object. Semantically, this means the value was not used.
[in] | keys | You can pass an arbitrary number of keys inside curly braces, following the nesting structure in the config file. |
Definition at line 158 of file configuration.cc.
|
inline |
Definition at line 892 of file configuration.h.
void smash::Configuration::remove_all_but | ( | const std::string & | key | ) |
Removes all entries in the map except for key
.
[in] | key | The key of the map entry to keep. |
Definition at line 163 of file configuration.cc.
|
inline |
Access to the YAML::Node behind the requested keys
.
If you want to read a value use the read function above. Use the subscript operator if you want to assign a new value. The YAML::Node class will automatically convert the data you assign to a string representation suitable for the YAML file.
[in] | key | The name of the key to be looked up |
Definition at line 921 of file configuration.h.
|
inline |
Assignment overwrites the value of the current YAML node.
[in] | value | An arbitrary value that yaml-cpp can convert into YAML representation. Any builtin type, strings, maps, and vectors can be used here. |
Definition at line 933 of file configuration.h.
bool smash::Configuration::has_value_including_empty | ( | std::initializer_list< const char * > | keys | ) | const |
Returns if there is a (maybe empty) value behind the requested keys
.
[in] | keys | List of keys to be checked for |
Definition at line 175 of file configuration.cc.
bool smash::Configuration::has_value | ( | std::initializer_list< const char * > | keys | ) | const |
Returns whether there is a non-empty value behind the requested keys
.
[in] | keys | List of keys to be checked for |
Definition at line 181 of file configuration.cc.
std::string smash::Configuration::unused_values_report | ( | ) | const |
Returns a string listing the key/value pairs that have not been taken yet.
Definition at line 186 of file configuration.cc.
std::string smash::Configuration::to_string | ( | ) | const |
Returns a YAML string of the current tree.
This differs from the above in that it does not remove empty maps.
Definition at line 192 of file configuration.cc.
|
private |
the general_config.yaml contents - fully parsed
Definition at line 973 of file configuration.h.