Falaise
4.0.1
SuperNEMO Software Toolkit
|
Class holding a set of key-value properties. More...
#include <falaise/config/property_set.h>
Public Member Functions | |
property_set ()=default | |
Default constructor. More... | |
property_set (datatools::properties const &ps) | |
Construct from an existing datatools::properties. More... | |
bool | is_empty () const |
Returns true if no key-value pairs are held. More... | |
std::vector< std::string > | get_names () const |
Returns a vector of all keys in the property_set. More... | |
bool | has_key (std::string const &key) const |
Returns true if the property_set stores a pair with the supplied key. More... | |
bool | is_key_to_property (std::string const &key) const |
Returns true if the key's value is a property/atom. More... | |
bool | is_key_to_sequence (std::string const &key) const |
Returns true if the keys's value is sequence. More... | |
bool | is_key_to_property_set (std::string const &key) const |
Returns true if the key's value is a property_set. More... | |
std::string | to_string () const |
Returns a string representation of the property_set. More... | |
template<typename T > | |
T | get (std::string const &key) const |
Return the value of type T held at supplied key. More... | |
template<typename T > | |
T | get (std::string const &key, T const &default_value) const |
operator datatools::properties () const | |
Convert back to datatools::properties. More... | |
template<typename T > | |
void | put (std::string const &key, T const &value) |
Insert key-value pair in property_set. More... | |
template<typename T > | |
void | put_or_replace (std::string const &key, T const &value) |
Insert key-value pair in property_set, replacing value if key exists. More... | |
bool | erase (std::string const &key) |
Erase the name-value pair stored at key. More... | |
template<> | |
property_set | get (std::string const &key) const |
template<> | |
property_set | get (std::string const &key, property_set const &default_value) const |
template<> | |
void | put (std::string const &key, property_set const &value) |
template<> | |
void | put_or_replace (std::string const &key, property_set const &value) |
template<> | |
void | put_impl_ (std::string const &key, falaise::config::path const &value) |
Private specialization of put_impl_ for path. More... | |
template<> | |
void | put_impl_ (std::string const &key, falaise::config::quantity const &value) |
Private specialization of put_impl_ for quantity. More... | |
template<> | |
void | get_impl_ (std::string const &key, falaise::config::path &result) const |
template<> | |
void | get_impl_ (std::string const &key, falaise::config::quantity &result) const |
Class holding a set of key-value properties.
Provides a convenient adaptor interface over datatools::properties, targeted at developers of modules for Falaise needing key-value storage and validation of input parameters. It may be used directly as
or it can be constructed from an existing instance of datatools::properties
or from a datatools::properties file
Storage and retrieval of parameters is typesafe in the sense that:
Support for path and quantity_t is provided to support validation of input configuration scripts. These types model the path
and dimension
decorators for properties parameters:
When extracting parameters, you can ensure that that you have a true path or physical quantity of the right dimension:
Support for put and get of property_set values is provided to support datatools::properties constructs of the form:
The foo
and bar
properties an be extracted either by their full keys:
or by getting the table
key as a property_set, then its properties which will be stored using their subkey names:
This can be extended to further levels of nesting if required. However, note that datatools::properties configuration of the form:
are not considered get-able as property_set
s. This is due to the ambiguity of the table
key which property_set regards as an atomic property of type std::string
. Here, the table.x
and table.y` keys can be extracted only via their fully qualified keys. It is therefore strongly recommended that you structure your configuration in the "pure table" form above for clarity and ease of use.
The default value form of get can be used to implement optional configuration, for example
Combined, these various ways of using property_set allow simple and reliable usage for configuration and configuration validation in Falaise modules.
|
default |
Default constructor.
falaise::config::property_set::property_set | ( | datatools::properties const & | ps | ) |
Construct from an existing datatools::properties.
[in] | ps | properties |
bool falaise::config::property_set::erase | ( | std::string const & | key | ) |
Erase the name-value pair stored at key.
[in] | key | key for pair to erase |
T falaise::config::property_set::get | ( | std::string const & | key | ) | const |
Return the value of type T held at supplied key.
T | type of value to be returned |
[in] | key | key for value to find |
missing_key_error | if key is not found |
wrong_type_error | if value at key is not of type T |
T falaise::config::property_set::get | ( | std::string const & | key, |
T const & | default_value | ||
) | const |
Return the value of type T associated with key, or a default if the key is not present
T | type of value to be returned |
[in] | key | key for value to find |
[in] | default_value | value to return if key is not found |
wrong_type_error | if key is found and associated value is not type T |
|
inline |
|
inline |
|
inline |
|
inline |
std::vector<std::string> falaise::config::property_set::get_names | ( | ) | const |
Returns a vector of all keys in the property_set.
bool falaise::config::property_set::has_key | ( | std::string const & | key | ) | const |
Returns true if the property_set stores a pair with the supplied key.
A nested key, e.g. foo.bar
, may be supplied.
[in] | key | name of key to check for existence |
bool falaise::config::property_set::is_empty | ( | ) | const |
Returns true if no key-value pairs are held.
bool falaise::config::property_set::is_key_to_property | ( | std::string const & | key | ) | const |
Returns true if the key's value is a property/atom.
A nested key, e.g. foo.bar
, may be supplied.
[in] | key | name of the key to check |
bool falaise::config::property_set::is_key_to_property_set | ( | std::string const & | key | ) | const |
Returns true if the key's value is a property_set.
A nested key, e.g. foo.bar
, may be supplied.
A key is only considered to have a property_set value if it only exists as a prefix to a set of nested keys, e.g.
Keys pure
and pure.nested
are considered as property_set values. In mixed properties/values such as
bad
is not considered as a property_set.
[in] | key | name of the key to check |
bool falaise::config::property_set::is_key_to_sequence | ( | std::string const & | key | ) | const |
Returns true if the keys's value is sequence.
A nested key, e.g. foo.bar
, may be supplied.
[in] | key | name of the key to check |
falaise::config::property_set::operator datatools::properties | ( | ) | const |
Convert back to datatools::properties.
void falaise::config::property_set::put | ( | std::string const & | key, |
T const & | value | ||
) |
Insert key-value pair in property_set.
Copies of the key and value are stored
T | type of value to store |
[in] | key | key to store value at |
[in] | value | value to store |
wrong_type_error | if value's type is not storable |
existing_key_error | if key is already stored |
|
inline |
|
inline |
Private specialization of put_impl_ for path.
|
inline |
Private specialization of put_impl_ for quantity.
void falaise::config::property_set::put_or_replace | ( | std::string const & | key, |
T const & | value | ||
) |
Insert key-value pair in property_set, replacing value if key exists.
Copies of the key and value are stored. For existing keys the value will be overwritten, and the types of the old and new value do not need to match.
T | type of value to store |
[in] | key | key to store value at |
[in] | value | value to store |
wrong_type_error | if value's type is not storable |
|
inline |
std::string falaise::config::property_set::to_string | ( | ) | const |
Returns a string representation of the property_set.