LDMX Software
Parameters.h
1#ifndef FRAMEWORK_PARAMETERS_H
2#define FRAMEWORK_PARAMETERS_H
3
4/*~~~~~~~~~~~~~~~~*/
5/* C++ StdLib */
6/*~~~~~~~~~~~~~~~~*/
7#include <any>
8#include <boost/core/demangle.hpp>
9#include <map>
10#include <string>
11#include <typeinfo>
12#include <vector>
13
14#include "Framework/Exception/Exception.h"
15
17
27 public:
38 template <typename T>
39 void add(const std::string& name, const T& value) {
40 if (exists(name)) {
41 EXCEPTION_RAISE("Config",
42 "The parameter " + name +
43 " already exists in the list of parameters.");
44 }
45
46 parameters_[name] = value;
47 }
48
49 template <typename T>
50 void addParameter(const std::string& name, const T& value) {
51 add<T>(name, value);
52 }
53
60 bool exists(const std::string& name) const {
61 return parameters_.find(name) != parameters_.end();
62 }
63
74 template <typename T>
75 const T& get(const std::string& name) const {
76 // Check if the variable exists in the map. If it doesn't,
77 // raise an exception.
78 if (not exists(name)) {
79 EXCEPTION_RAISE("Config", "Parameter '" + name +
80 "' does not exist in list of parameters.");
81 }
82
83 try {
84 return std::any_cast<const T&>(parameters_.at(name));
85 } catch (const std::bad_any_cast& e) {
86 EXCEPTION_RAISE(
87 "Config",
88 "Parameter '" + name + "' of type '" +
89 boost::core::demangle(parameters_.at(name).type().name()) +
90 "' is being cast to incorrect type '" +
91 boost::core::demangle(typeid(T).name()) + "'.");
92 }
93 }
94
95 template <typename T>
96 const T& getParameter(const std::string& name) const {
97 return get<T>(name);
98 }
99
110 template <typename T>
111 const T& get(const std::string& name, const T& def) const {
112 if (not exists(name)) return def;
113
114 // get here knowing that name exists in parameters_
115 return get<T>(name);
116 }
117
118 template <typename T>
119 const T& getParameter(const std::string& name, const T& def) const {
120 return get<T>(name, def);
121 }
122
131 std::vector<std::string> keys() const {
132 std::vector<std::string> key;
133 for (auto i : parameters_) key.push_back(i.first);
134 return key;
135 }
136
137 private:
139 std::map<std::string, std::any> parameters_;
140
141}; // Parameters
142} // namespace framework::config
143
144#endif // FRAMEWORK_PARAMETERS_H
Class encapsulating parameters for configuring a processor.
Definition Parameters.h:26
std::vector< std::string > keys() const
Get a list of the keys available.
Definition Parameters.h:131
void add(const std::string &name, const T &value)
Add a parameter to the parameter list.
Definition Parameters.h:39
const T & get(const std::string &name) const
Retrieve the parameter of the given name.
Definition Parameters.h:75
const T & get(const std::string &name, const T &def) const
Retrieve a parameter with a default specified.
Definition Parameters.h:111
bool exists(const std::string &name) const
Check to see if a parameter exists.
Definition Parameters.h:60
std::map< std::string, std::any > parameters_
container holding parameter names and their values
Definition Parameters.h:139
python execution and parameter extraction
Definition Parameters.h:16