A "dictionary" for parameters.
More...
#include <apf/parameter_map.h>
Inherits std::map< K, T >.
Inherited by apf::MimoProcessor ::Xput< Derived, interface_policy, query_policy >::Params.
|
template<typename... Args> |
| parameter_map (Args &&... args) |
| Constructor. More...
|
|
const std::string & | operator[] (const std::string &k) const |
| "Getter". More...
|
|
std::string & | operator[] (const std::string &k) |
| "Setter". More...
|
|
template<typename T > |
T | get (const std::string &k, const T &def) const |
| Get value converted to given type. More...
|
|
template<typename char_T > |
std::basic_string< char_T > | get (const std::string &k, const char_T *const def) const |
| Overloaded function for character array (aka C-string). More...
|
|
template<typename T > |
T | get (const std::string &k) const |
| Throwing getter. More...
|
|
template<typename T > |
const std::string & | set (const std::string &k, const T &v) |
| Set value. More...
|
|
bool | has_key (const std::string &k) const |
| Check if a given parameter is available. More...
|
|
A "dictionary" for parameters.
All values are saved as std::string's
.
Usage examples:
params.
set(
"one",
"first value");
params.
set(
"three", 3.1415);
std::string val1, val5;
int val2, val3, val4;
val1 = params["one"];
val2 = params.
get<
int>(
"two");
val3 = params.
get<
int>(
"one");
val4 = params.
get(
"one", 42);
{
do_something();
}
params["four"] = "42";
val5 = params["four"];
A "dictionary" for parameters.
T get(const std::string &k, const T &def) const
Get value converted to given type.
const std::string & set(const std::string &k, const T &v)
Set value.
bool has_key(const std::string &k) const
Check if a given parameter is available.
- Examples
- audiofile_simpleprocessor.cpp, dummy_example.cpp, jack_simpleprocessor.cpp, mex_simpleprocessor.cpp, and simpleprocessor.h.
Definition at line 67 of file parameter_map.h.
◆ parameter_map()
template<typename... Args>
apf::parameter_map::parameter_map |
( |
Args &&... |
args | ) |
|
|
inlineexplicit |
Constructor.
All parameters are forwarded to the std::map
constructor.
Definition at line 73 of file parameter_map.h.
◆ operator[]() [1/2]
const std::string & apf::parameter_map::operator[] |
( |
const std::string & |
k | ) |
const |
|
inline |
"Getter".
- Parameters
-
k | Name of the parameter which should be retrieved. |
- Returns
- const reference to the value referenced by
k
.
- Exceptions
-
std::out_of_range | if the key k doesn't exist. You should've checked beforehand with has_key() ... |
- See also
- has_key(), get()
Definition at line 84 of file parameter_map.h.
◆ operator[]() [2/2]
std::string & apf::parameter_map::operator[] |
( |
const std::string & |
k | ) |
|
|
inline |
"Setter".
Well, not really. It just gives you a reference where you can assign stuff to.
- Parameters
-
k | Name of the parameter which should be set. The parameter has to be in the map already, if not, an exception is thrown! If you want to add a new value, use set(). |
- Returns
- non-const reference to the value referenced by
k
. You can assign a std::string
to actually set a new value.
- Exceptions
-
std::out_of_range | if the key k doesn't exist yet. |
- See also
- has_key(), set()
Definition at line 106 of file parameter_map.h.
◆ get() [1/3]
template<typename T >
T apf::parameter_map::get |
( |
const std::string & |
k, |
|
|
const T & |
def |
|
) |
| const |
|
inline |
Get value converted to given type.
- Template Parameters
-
T | The desired type. Can be omitted if def is specified. |
- Parameters
-
k | Name of the parameter which should be retrieved. |
def | Default value for cases where conversion fails. |
- Returns
- Value referenced by
k
, converted to type T
. If the key k
isn't available, or if the conversion failed, def
is returned.
- Warning
- If the result is equal to the default value
def
, there is no way to know ...
- if the key was available
- if the conversion was successful
- To check the former, you can use has_key(), for the latter you have to get the value as string (with operator[]()) and convert it yourself (e.g. with apf::str::S2A()). Or you can use the throwing version of get<T>().
- Examples
- simpleprocessor.h.
Definition at line 135 of file parameter_map.h.
Referenced by get().
◆ get() [2/3]
template<typename char_T >
std::basic_string< char_T > apf::parameter_map::get |
( |
const std::string & |
k, |
|
|
const char_T *const |
def |
|
) |
| const |
|
inline |
Overloaded function for character array (aka C-string).
This is mainly used to specify a string literal as default value, which wouldn't work with the other get() version, e.g.
params.
set(
"id",
"item42");
std::string id1, id2, name1, name2;
id1 = params.
get(
"id" ,
"no_id_available");
id2 = params.
get(
"id" ,
"item42");
name1 = params.
get(
"name",
"Default Name");
name2 = params.
get(
"name",
"");
- Template Parameters
-
T | The given character type. Can be omitted if def is specified (which is normally the case!). |
- Parameters
-
k | Name of the parameter which should be retrieved. |
def | Default value for cases where conversion fails. |
- Returns
- Value referenced by
k
, converted to a std::basic_string<char_T>
. If the key k
isn't available, or if the conversion failed, def
is returned.
- Warning
- Same warning as for the other get() version with default value.
Definition at line 174 of file parameter_map.h.
References get().
◆ get() [3/3]
template<typename T >
T apf::parameter_map::get |
( |
const std::string & |
k | ) |
const |
|
inline |
Throwing getter.
- Template Parameters
-
- Parameters
-
k | Name of the parameter which should be retrieved. |
- Exceptions
-
std::out_of_range | if the key k is not available |
std::invalid_argument | if the content cannot be converted to T |
Definition at line 186 of file parameter_map.h.
◆ set()
template<typename T >
const std::string & apf::parameter_map::set |
( |
const std::string & |
k, |
|
|
const T & |
v |
|
) |
| |
|
inline |
◆ has_key()
bool apf::parameter_map::has_key |
( |
const std::string & |
k | ) |
const |
|
inline |
Check if a given parameter is available.
- Parameters
-
- Returns
- true if
k
is available
Definition at line 218 of file parameter_map.h.
The documentation for this struct was generated from the following file: