pflib v3.9.0-rc3-11-g2537d8f
Pretty Fine HGCROC Interaction Library
Loading...
Searching...
No Matches
Parameters.h
1#pragma once
2#ifndef PFLIB_CONFIG_PARAMETERS_H
3#define PFLIB_CONFIG_PARAMETERS_H
4
5/*~~~~~~~~~~~~~~~~*/
6/* C++ StdLib */
7/*~~~~~~~~~~~~~~~~*/
8#include <any>
9#include <boost/core/demangle.hpp>
10#include <iostream>
11#include <limits>
12#include <map>
13#include <string>
14#include <type_traits>
15#include <typeinfo>
16#include <vector>
17
18#include "pflib/Exception.h"
19
20namespace pflib {
21
32 public:
43 template <typename T>
44 void add(const std::string& name, const T& value, bool overlay = false) {
45 if (not overlay and exists(name)) {
46 PFEXCEPTION_RAISE("Config",
47 "The parameter " + name +
48 " already exists in the list of parameters.");
49 }
50
51 parameters_[name] = value;
52 }
53
60 bool exists(const std::string& name) const;
61
72 template <typename T>
73 const T& get(const std::string& name) const {
74 // Check if the variable exists in the map. If it doesn't,
75 // raise an exception.
76 if (not exists(name)) {
77 PFEXCEPTION_RAISE(
78 "Config",
79 "Parameter '" + name + "' does not exist in list of parameters.");
80 }
81
82 try {
83 return std::any_cast<const T&>(parameters_.at(name));
84 } catch (const std::bad_any_cast& e) {
85 PFEXCEPTION_RAISE(
86 "Config",
87 "Parameter '" + name + "' of type '" +
88 boost::core::demangle(parameters_.at(name).type().name()) +
89 "' is being cast to incorrect type '" +
90 boost::core::demangle(typeid(T).name()) + "'.");
91 }
92 }
93
104 template <typename T>
105 const T& get(const std::string& name, const T& def) const {
106 if (not exists(name)) return def;
107
108 // get here knowing that name exists in parameters_
109 return get<T>(name);
110 }
111
121
128 void from_yaml(const std::string& filepath, bool overlay = true);
129
130 private:
133
134}; // Parameters
135} // namespace pflib
136
137#endif // PFLIB_CONFIG_PARAMETERS_H
T at(T... args)
Class encapsulating parameters for generically holding configuration parameters.
Definition Parameters.h:31
const T & get(const std::string &name, const T &def) const
Retrieve a parameter with a default specified.
Definition Parameters.h:105
std::vector< std::string > keys() const
Get a list of the keys available.
Definition Parameters.cxx:104
bool exists(const std::string &name) const
Check to see if a parameter exists.
Definition Parameters.cxx:100
const T & get(const std::string &name) const
Retrieve the parameter of the given name.
Definition Parameters.h:73
void from_yaml(const std::string &filepath, bool overlay=true)
Load parameters from a YAML file.
Definition Parameters.cxx:110
void add(const std::string &name, const T &value, bool overlay=false)
Add a parameter to the parameter list.
Definition Parameters.h:44
std::map< std::string, std::any > parameters_
container holding parameter names and their values
Definition Parameters.h:132
This version of the fast control code interfaces with the CMS Fast control library which can be contr...
Definition Backend.cxx:3