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 <iostream>
9#include <limits>
10#include <map>
11#include <string>
12#include <type_traits>
13#include <typeinfo>
14#include <vector>
15
16/*~~~~~~~~~~~~~~~*/
17/* Exception */
18/*~~~~~~~~~~~~~~~*/
19#include "Framework/Exception/Exception.h"
20
21namespace framework {
22namespace config {
23
28 public:
31
34
41 void setParameters(std::map<std::string, std::any> parameters) {
42 parameters_ = parameters;
43 }
44
54 template <typename T>
55 void addParameter(const std::string& name, const T& value) {
56 if (exists(name)) {
57 EXCEPTION_RAISE("ParameterExist",
58 "The parameter " + name +
59 " already exists in the list of parameters.");
60 }
61
62 parameters_[name] = value;
63 }
64
71 bool exists(const std::string& name) const {
72 return parameters_.find(name) != parameters_.end();
73 }
74
88 template <typename T>
89 T getParameter(const std::string& name) const {
90 // Check if the variable exists in the map. If it doesn't,
91 // raise an exception.
92 if (not exists(name)) {
93 EXCEPTION_RAISE(
94 "NonExistParam",
95 "Parameter '" + name + "' does not exist in list of parameters.");
96 }
97
98 try {
99 auto parameter = std::any_cast<T>(parameters_.at(name));
100 return parameter;
101 } catch (const std::bad_any_cast& e) {
102 EXCEPTION_RAISE("BadTypeParam",
103 "Parameter '" + name + "' of type '" +
104 parameters_.at(name).type().name() +
105 "' is being cast to incorrect type '" +
106 typeid(T).name() + "'.");
107 }
108 }
109
117 template <typename T>
118 T getParameter(const std::string& name, const T& def) const {
119 if (not exists(name)) return def;
120
121 // get here knowing that name exists in parameters_
122 return getParameter<T>(name);
123 }
124
130 std::vector<std::string> keys() const {
131 std::vector<std::string> key;
132 for (auto i : parameters_) key.push_back(i.first);
133 return key;
134 }
135
136 private:
138 std::map<std::string, std::any> parameters_;
139
140}; // Parameters
141} // namespace config
142} // namespace framework
143
144#endif // FRAMEWORK_PARAMETERS_H
Class encapsulating parameters for configuring a processor.
Definition Parameters.h:27
std::vector< std::string > keys() const
Get a list of the keys available.
Definition Parameters.h:130
T getParameter(const std::string &name) const
Retrieve the parameter of the given name.
Definition Parameters.h:89
void addParameter(const std::string &name, const T &value)
Add a parameter to the parameter list.
Definition Parameters.h:55
void setParameters(std::map< std::string, std::any > parameters)
Set the mapping of parameter names to value.
Definition Parameters.h:41
bool exists(const std::string &name) const
Check to see if a parameter exists.
Definition Parameters.h:71
T getParameter(const std::string &name, const T &def) const
Retrieve a parameter with a default specified.
Definition Parameters.h:118
std::map< std::string, std::any > parameters_
Parameters.
Definition Parameters.h:138
All classes in the ldmx-sw project use this namespace.
Definition PerfDict.cxx:45