LDMX Software
framework::config::Parameters Class Reference

Class encapsulating parameters for configuring a processor. More...

#include <Parameters.h>

Public Member Functions

template<typename T >
void add (const std::string &name, const T &value)
 Add a parameter to the parameter list.
 
template<typename T >
void addParameter (const std::string &name, const T &value)
 
bool exists (const std::string &name) const
 Check to see if a parameter exists.
 
template<typename T >
const T & get (const std::string &name) const
 Retrieve the parameter of the given name.
 
template<typename T >
const T & getParameter (const std::string &name) const
 
template<typename T >
const T & get (const std::string &name, const T &def) const
 Retrieve a parameter with a default specified.
 
template<typename T >
const T & getParameter (const std::string &name, const T &def) const
 
std::vector< std::string > keys () const
 Get a list of the keys available.
 

Private Attributes

std::map< std::string, std::any > parameters_
 container holding parameter names and their values
 

Detailed Description

Class encapsulating parameters for configuring a processor.

The storage of arbitrary parameters recursively is done using a map from parameter names (std::string) to parameter values. The values are stored in std::any which allows the user to store any object they wish in a memory-safe environment.

Definition at line 29 of file Parameters.h.

Member Function Documentation

◆ add()

template<typename T >
void framework::config::Parameters::add ( const std::string & name,
const T & value )
inline

Add a parameter to the parameter list.

If the parameter already exists in the list, throw an exception.

Parameters
[in]nameName of the parameter.
[in]valueThe value of the parameter.
Template Parameters
Ttype of parameter being added
Exceptions
Exceptionif a parameter by that name already exist in the list.

Definition at line 42 of file Parameters.h.

42 {
43 if (exists(name)) {
44 EXCEPTION_RAISE("Config",
45 "The parameter " + name +
46 " already exists in the list of parameters.");
47 }
48
49 parameters_[name] = value;
50 }
bool exists(const std::string &name) const
Check to see if a parameter exists.
Definition Parameters.h:63
std::map< std::string, std::any > parameters_
container holding parameter names and their values
Definition Parameters.h:142

References exists(), and parameters_.

Referenced by framework::config::getMembers().

◆ addParameter()

template<typename T >
void framework::config::Parameters::addParameter ( const std::string & name,
const T & value )
inline

Definition at line 53 of file Parameters.h.

53 {
54 add<T>(name, value);
55 }
void add(const std::string &name, const T &value)
Add a parameter to the parameter list.
Definition Parameters.h:42

◆ exists()

bool framework::config::Parameters::exists ( const std::string & name) const
inline

Check to see if a parameter exists.

Parameters
[in]namename of parameter to check
Returns
true if parameter exists in configuration set

Definition at line 63 of file Parameters.h.

63 {
64 return parameters_.find(name) != parameters_.end();
65 }

References parameters_.

Referenced by add(), get(), get(), and hcal::HcalGeometryProvider::getCondition().

◆ get() [1/2]

template<typename T >
const T & framework::config::Parameters::get ( const std::string & name) const
inline

Retrieve the parameter of the given name.

Exceptions
Exceptionif parameter of the given name isn't found
Exceptionif parameter is found but not of the input type
Template Parameters
Tthe data type to cast the parameter to.
Parameters
[in]namethe name of the parameter value to retrieve.
Returns
The user specified parameter of type T.

Definition at line 78 of file Parameters.h.

78 {
79 // Check if the variable exists in the map. If it doesn't,
80 // raise an exception.
81 if (not exists(name)) {
82 EXCEPTION_RAISE("Config", "Parameter '" + name +
83 "' does not exist in list of parameters.");
84 }
85
86 try {
87 return std::any_cast<const T&>(parameters_.at(name));
88 } catch (const std::bad_any_cast& e) {
89 EXCEPTION_RAISE(
90 "Config",
91 "Parameter '" + name + "' of type '" +
92 boost::core::demangle(parameters_.at(name).type().name()) +
93 "' is being cast to incorrect type '" +
94 boost::core::demangle(typeid(T).name()) + "'.");
95 }
96 }

References exists(), and parameters_.

Referenced by get().

◆ get() [2/2]

template<typename T >
const T & framework::config::Parameters::get ( const std::string & name,
const T & def ) const
inline

Retrieve a parameter with a default specified.

Return the input default if a parameter is not found in map.

Template Parameters
Ttype of parameter to return
Parameters
[in]nameparameter name to retrieve
[in,out]defdefault to use if parameter is not found
Returns
the user parameter of type T

Definition at line 114 of file Parameters.h.

114 {
115 if (not exists(name)) return def;
116
117 // get here knowing that name exists in parameters_
118 return get<T>(name);
119 }
const T & get(const std::string &name) const
Retrieve the parameter of the given name.
Definition Parameters.h:78

References exists(), and get().

◆ getParameter() [1/2]

template<typename T >
const T & framework::config::Parameters::getParameter ( const std::string & name) const
inline

Definition at line 99 of file Parameters.h.

99 {
100 return get<T>(name);
101 }

◆ getParameter() [2/2]

template<typename T >
const T & framework::config::Parameters::getParameter ( const std::string & name,
const T & def ) const
inline

Definition at line 122 of file Parameters.h.

122 {
123 return get<T>(name, def);
124 }

◆ keys()

std::vector< std::string > framework::config::Parameters::keys ( ) const
inline

Get a list of the keys available.

This may be helpful in debugging to make sure the parameters are spelled correctly.

Returns
list of all keys in this map of parameters

Definition at line 134 of file Parameters.h.

134 {
135 std::vector<std::string> key;
136 for (auto i : parameters_) key.push_back(i.first);
137 return key;
138 }

References parameters_.

Referenced by hcal::HcalGeometryProvider::getCondition().

Member Data Documentation

◆ parameters_

std::map<std::string, std::any> framework::config::Parameters::parameters_
private

container holding parameter names and their values

Definition at line 142 of file Parameters.h.

Referenced by add(), exists(), get(), and keys().


The documentation for this class was generated from the following file: