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 <iostream>
10#include <limits>
11#include <map>
12#include <string>
13#include <type_traits>
14#include <typeinfo>
15#include <vector>
16
17#include "Framework/Exception/Exception.h"
18
20
30 public:
41 template <typename T>
42 void add(const std::string& name, const T& value) {
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 }
51
52 template <typename T>
53 void addParameter(const std::string& name, const T& value) {
54 add<T>(name, value);
55 }
56
63 bool exists(const std::string& name) const {
64 return parameters_.find(name) != parameters_.end();
65 }
66
77 template <typename T>
78 const T& get(const std::string& name) const {
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 }
97
98 template <typename T>
99 const T& getParameter(const std::string& name) const {
100 return get<T>(name);
101 }
102
113 template <typename T>
114 const T& get(const std::string& name, const T& def) const {
115 if (not exists(name)) return def;
116
117 // get here knowing that name exists in parameters_
118 return get<T>(name);
119 }
120
121 template <typename T>
122 const T& getParameter(const std::string& name, const T& def) const {
123 return get<T>(name, def);
124 }
125
134 std::vector<std::string> keys() const {
135 std::vector<std::string> key;
136 for (auto i : parameters_) key.push_back(i.first);
137 return key;
138 }
139
140 private:
142 std::map<std::string, std::any> parameters_;
143
144}; // Parameters
145} // namespace framework::config
146
147#endif // FRAMEWORK_PARAMETERS_H
Class encapsulating parameters for configuring a processor.
Definition Parameters.h:29
std::vector< std::string > keys() const
Get a list of the keys available.
Definition Parameters.h:134
void add(const std::string &name, const T &value)
Add a parameter to the parameter list.
Definition Parameters.h:42
const T & get(const std::string &name) const
Retrieve the parameter of the given name.
Definition Parameters.h:78
const T & get(const std::string &name, const T &def) const
Retrieve a parameter with a default specified.
Definition Parameters.h:114
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
python execution and parameter extraction
Definition Parameters.h:19