fire v0.19.0
Framework for sImulation and Reconstruction of Events
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#include <boost/core/demangle.hpp>
16#include "fire/exception/Exception.h"
17
18namespace fire::config {
19
29 public:
40 template <typename T>
41 void add(const std::string& name, const T& value) {
42 if (exists(name)) {
43 throw Exception("Config",
44 "The parameter " + name + " already exists in the list of parameters.");
45 }
46
47 parameters_[name] = value;
48 }
49
50 template <typename T>
51 void addParameter(const std::string& name, const T& value) {
52 add<T>(name,value);
53 }
54
61 bool exists(const std::string& name) const {
62 return parameters_.find(name) != parameters_.end();
63 }
64
75 template <typename T>
76 const T& get(const std::string& name) const {
77 // Check if the variable exists in the map. If it doesn't,
78 // raise an exception.
79 if (not exists(name)) {
80 throw Exception("Config","Parameter '" + name + "' 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 throw Exception("Config","Parameter '" + name + "' of type '" +
87 boost::core::demangle(parameters_.at(name).type().name()) +
88 "' is being cast to incorrect type '" +
89 boost::core::demangle(typeid(T).name()) + "'.");
90 }
91 }
92
93 template <typename T>
94 const T& getParameter(const std::string& name) const {
95 return get<T>(name);
96 }
97
108 template <typename T>
109 const T& get(const std::string& name, const T& def) const {
110 if (not exists(name)) return def;
111
112 // get here knowing that name exists in parameters_
113 return get<T>(name);
114 }
115
116 template <typename T>
117 const T& getParameter(const std::string& name, const T& def) const {
118 return get<T>(name,def);
119 }
120
131 for (auto i : parameters_) key.push_back(i.first);
132 return key;
133 }
134
135 private:
138
139}; // Parameters
140} // namespace fire::config
141
142#endif // FRAMEWORK_PARAMETERS_H
Standard base exception class with some useful output information.
Definition: Exception.h:18
Class encapsulating parameters for configuring a processor.
Definition: Parameters.h:28
const T & get(const std::string &name, const T &def) const
Retrieve a parameter with a default specified.
Definition: Parameters.h:109
bool exists(const std::string &name) const
Check to see if a parameter exists.
Definition: Parameters.h:61
std::vector< std::string > keys() const
Get a list of the keys available.
Definition: Parameters.h:129
std::map< std::string, std::any > parameters_
container holding parameter names and their values
Definition: Parameters.h:137
const T & get(const std::string &name) const
Retrieve the parameter of the given name.
Definition: Parameters.h:76
void add(const std::string &name, const T &value)
Add a parameter to the parameter list.
Definition: Parameters.h:41
python execution and parameter extraction
Definition: Parameters.h:18
T push_back(T... args)