allpix::Configuration
Generic configuration object storing keys. More…
#include <Configuration.hpp>
Public Functions
Name | |
---|---|
Configuration(const Configuration & ) =default Allow copying the configuration. |
|
Configuration & | operator=(const Configuration & ) =default Allow copying the configuration. |
Configuration(Configuration && ) =default Allow moving the configuration. |
|
Configuration & | operator=(Configuration && ) =default Allow moving the configuration. |
Configuration(std::string name ="", std::filesystem::path path ="") Construct a configuration object. |
|
bool | has(const std::string & key) const Check if key is defined. |
unsigned int | count(std::initializer_list< std::string > keys) const Check how many of the given keys are defined. |
template <typename T > T |
get(const std::string & key) const Get value of a key in requested type. |
template <typename T > T |
get(const std::string & key, const T & def) const Get value of a key in requested type or default value if it does not exists. |
template <typename T > std::vector< T > |
getArray(const std::string & key) const Get values for a key containing an array. |
template <typename T > std::vector< T > |
getArray(const std::string & key, const std::vector< T > def) const Get values for a key containing an array or default array if it does not exists. |
template <typename T > Matrix< T > |
getMatrix(const std::string & key) const Get values for a key containing a 2D matrix. |
template <typename T > Matrix< T > |
getMatrix(const std::string & key, const Matrix< T > def) const Get values for a key containing a 2D matrix. |
std::string | getText(const std::string & key) const Get literal value of a key as string. |
std::string | getText(const std::string & key, const std::string & def) const Get literal value of a key as string or a default if it does not exists. |
std::filesystem::path | getPath(const std::string & key, bool check_exists =false) const Get absolute path to file with paths relative to the configuration. |
std::filesystem::path | getPathWithExtension(const std::string & key, const std::string & extension, bool check_exists) const Get absolute path to file with paths relative to the configuration. |
std::vector< std::filesystem::path > | getPathArray(const std::string & key, bool check_exists =false) const Get array of absolute paths to files with paths relative to the configuration. |
template <typename T > void |
set(const std::string & key, const T & val, bool mark_used =false) Set value for a key in a given type. |
template <typename T > void |
set(const std::string & key, const T & val, std::initializer_list< std::string > units) Store value for a key in a given type, including units. |
template <typename T > void |
setArray(const std::string & key, const std::vector< T > & val, bool mark_used =false) Set list of values for a key in a given type. |
template <typename T > void |
setMatrix(const std::string & key, const Matrix< T > & val) Set matrix of values for a key in a given type. |
template <typename T > void |
setDefault(const std::string & key, const T & val) Set default value for a key only if it is not defined yet. |
template <typename T > void |
setDefaultArray(const std::string & key, const std::vector< T > & val) Set default list of values for a key only if it is not defined yet. |
void | setText(const std::string & key, const std::string & val) Set literal value of a key as string. |
void | setAlias(const std::string & new_key, const std::string & old_key, bool warn =false) Set alias name for an already existing key. |
unsigned int | countSettings() const Return total number of key / value pairs. |
const std::string & | getName() const Get name of the configuration header. |
std::filesystem::path | getFilePath() const Get path to the file containing the configuration if it has one. |
void | merge(const Configuration & other) Merge other configuration, only adding keys that are not yet defined in this configuration. |
std::vector< std::pair< std::string, std::string > > | getAll() const Get all key value pairs. |
std::vector< std::string > | getUnusedKeys() const Obtain all keys which have not been accessed yet. |
Detailed Description
class allpix::Configuration;
Generic configuration object storing keys.
The configuration holds a set of keys with arbitrary values that are internally stored as strings. It has special logic for reading paths (relative to the configuration file). All types are converted to their appropriate type using the library of String conversions.
Public Functions Documentation
function Configuration
Configuration(
const Configuration &
) =default
Allow copying the configuration.
function operator=
Configuration & operator=(
const Configuration &
) =default
Allow copying the configuration.
function Configuration
Configuration(
Configuration &&
) =default
Allow moving the configuration.
function operator=
Configuration & operator=(
Configuration &&
) =default
Allow moving the configuration.
function Configuration
explicit Configuration(
std::string name ="",
std::filesystem::path path =""
)
Construct a configuration object.
Parameters:
- name Name of the section header (empty section if not specified)
- path Path to the file containing the configuration (or empty if not stored in a file)
function has
bool has(
const std::string & key
) const
Check if key is defined.
Parameters:
- key Key to check for existence
Return: True if key exists, false otherwise
function count
unsigned int count(
std::initializer_list< std::string > keys
) const
Check how many of the given keys are defined.
Parameters:
- keys Keys to check for existence
Return: number of existing keys from the given list
function get
template <typename T >
T get(
const std::string & key
) const
Get value of a key in requested type.
Parameters:
- key Key to get value of
Return: Value of the key in the type of the requested template parameter
function get
template <typename T >
T get(
const std::string & key,
const T & def
) const
Get value of a key in requested type or default value if it does not exists.
Parameters:
- key Key to get value of
- def Default value to use if key is not defined
Return: Value of the key in the type of the requested template parameter or the default value if the key does not exists
function getArray
template <typename T >
std::vector< T > getArray(
const std::string & key
) const
Get values for a key containing an array.
Parameters:
- key Key to get values of
Return: List of values in the array in the requested template parameter
function getArray
template <typename T >
std::vector< T > getArray(
const std::string & key,
const std::vector< T > def
) const
Get values for a key containing an array or default array if it does not exists.
Parameters:
- key Key to get values of
- def Default value array to use if key is not defined
Return: List of values in the array in the requested template parameter or the default array if the key does not exist
function getMatrix
template <typename T >
Matrix< T > getMatrix(
const std::string & key
) const
Get values for a key containing a 2D matrix.
Parameters:
- key Key to get values of
Return: Matrix of values from the requested template parameter
function getMatrix
template <typename T >
Matrix< T > getMatrix(
const std::string & key,
const Matrix< T > def
) const
Get values for a key containing a 2D matrix.
Parameters:
- key Key to get values of
- def Default value matrix to use if key is not defined
Return: Matrix of values from the requested template parameter
function getText
std::string getText(
const std::string & key
) const
Get literal value of a key as string.
Parameters:
- key Key to get values of
Return: Literal value of the key
Note: This function does also not remove quotation marks in strings
function getText
std::string getText(
const std::string & key,
const std::string & def
) const
Get literal value of a key as string or a default if it does not exists.
Parameters:
- key Key to get values of
- def Default value to use if key is not defined
Return: Literal value of the key or the default value if the key does not exists
Note: This function does also not remove quotation marks in strings
function getPath
std::filesystem::path getPath(
const std::string & key,
bool check_exists =false
) const
Get absolute path to file with paths relative to the configuration.
Parameters:
- key Key to get path of
- check_exists If the file should be checked for existence (if yes always returns a canonical path)
Exceptions:
- InvalidValueError If the path did not exists while the check_exists parameter is given
Return: Absolute path to a file
For a relative path the absolute path of the configuration file is prepended. Absolute paths are not changed.
function getPathWithExtension
std::filesystem::path getPathWithExtension(
const std::string & key,
const std::string & extension,
bool check_exists
) const
Get absolute path to file with paths relative to the configuration.
Parameters:
- key Key to get path of
- extension File extension to be added to path if not present
- check_exists If the file should be checked for existence (if yes always returns a canonical path)
Exceptions:
- InvalidValueError If the path did not exists while the check_exists parameter is given
Return: Absolute path to a file
For a relative path the absolute path of the configuration file is prepended. Absolute paths are not changed.
function getPathArray
std::vector< std::filesystem::path > getPathArray(
const std::string & key,
bool check_exists =false
) const
Get array of absolute paths to files with paths relative to the configuration.
Parameters:
- key Key to get path of
- check_exists If the files should be checked for existence (if yes always returns a canonical path)
Exceptions:
- InvalidValueError If the path did not exists while the check_exists parameter is given
Return: List of absolute path to all the requested files
For all relative paths the absolute path of the configuration file is prepended. Absolute paths are not changed.
function set
template <typename T >
void set(
const std::string & key,
const T & val,
bool mark_used =false
)
Set value for a key in a given type.
Parameters:
- key Key to set value of
- val Value to assign to the key
- mark_used Flag whether key should be marked as “used” directly
function set
template <typename T >
void set(
const std::string & key,
const T & val,
std::initializer_list< std::string > units
)
Store value for a key in a given type, including units.
Parameters:
- key Key to set value of
- val Value to assign to the key
- units List of possible output units
function setArray
template <typename T >
void setArray(
const std::string & key,
const std::vector< T > & val,
bool mark_used =false
)
Set list of values for a key in a given type.
Parameters:
- key Key to set values of
- val List of values to assign to the key
- mark_used Flag whether key should be marked as “used” directly
function setMatrix
template <typename T >
void setMatrix(
const std::string & key,
const Matrix< T > & val
)
Set matrix of values for a key in a given type.
Parameters:
- key Key to set values of
- val List of values to assign to the key
function setDefault
template <typename T >
void setDefault(
const std::string & key,
const T & val
)
Set default value for a key only if it is not defined yet.
Parameters:
- key Key to possible set value of
- val Value to assign if the key is not defined yet
Note: This marks the default key as “used” automatically
function setDefaultArray
template <typename T >
void setDefaultArray(
const std::string & key,
const std::vector< T > & val
)
Set default list of values for a key only if it is not defined yet.
Parameters:
- key Key to possible set values of
- val List of values to assign to the key if the key is not defined yet
Note: This marks the default key as “used” automatically
function setText
void setText(
const std::string & key,
const std::string & val
)
Set literal value of a key as string.
Parameters:
- key Key to set value of
- val Literal string to assign key to
function setAlias
void setAlias(
const std::string & new_key,
const std::string & old_key,
bool warn =false
)
Set alias name for an already existing key.
Parameters:
- new_key New alias to be created
- old_key Key the alias is created for
- warn Optionally print a warning message to notify of deprecation
Note: This marks the old key as “used” automatically
The alias is only used if new key does not exist but old key does. The old key is automatically marked as used.
function countSettings
inline unsigned int countSettings() const
Return total number of key / value pairs.
Return: Number of settings
function getName
inline const std::string & getName() const
Get name of the configuration header.
Return: Configuration name
function getFilePath
inline std::filesystem::path getFilePath() const
Get path to the file containing the configuration if it has one.
Return: Absolute path to configuration file or empty if not linked to a file
Warning: Parameter should be used with care as not all configurations are required to have a file
function merge
void merge(
const Configuration & other
)
Merge other configuration, only adding keys that are not yet defined in this configuration.
Parameters:
- other Configuration to merge this one with
All keys that are already defined earlier in this configuration are not changed.
function getAll
std::vector< std::pair< std::string, std::string > > getAll() const
Get all key value pairs.
Return: List of all key value pairs
function getUnusedKeys
std::vector< std::string > getUnusedKeys() const
Obtain all keys which have not been accessed yet.
This method returns all keys from the configuration object which have not yet been accessed, Default values as well as aliases are marked as used automatically and are therefore never returned.
Updated on 2024-12-13 at 08:31:36 +0000