cepgen is hosted by Hepforge, IPPP Durham
CepGen N/A
Central exclusive processes event generator
ParametersList.h
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2018-2025 Laurent Forthomme
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifndef CepGen_Core_ParametersList_h
20#define CepGen_Core_ParametersList_h
21
22#include <map>
23#include <string>
24#include <unordered_map>
25
26#include "CepGen/Utils/Limits.h"
27
34#define REGISTER_CONTENT_TYPE \
35 __TYPE_ENUM(bool, bool_values_, "bool") \
36 __TYPE_ENUM(int, int_values_, "int") \
37 __TYPE_ENUM(unsigned long long, ulong_values_, "ulong") \
38 __TYPE_ENUM(double, dbl_values_, "float") \
39 __TYPE_ENUM(std::string, str_values_, "str") \
40 __TYPE_ENUM(Limits, lim_values_, "Limits") \
41 __TYPE_ENUM(ParametersList, param_values_, "Params") \
42 __TYPE_ENUM(std::vector<int>, vec_int_values_, "vint") \
43 __TYPE_ENUM(std::vector<double>, vec_dbl_values_, "vfloat") \
44 __TYPE_ENUM(std::vector<std::string>, vec_str_values_, "vstr") \
45 __TYPE_ENUM(std::vector<Limits>, vec_lim_values_, "VLimits") \
46 __TYPE_ENUM(std::vector<ParametersList>, vec_param_values_, "VParams") \
47 __TYPE_ENUM(std::vector<std::vector<double> >, vec_vec_dbl_values_, "vvfloat")
48
49namespace cepgen {
50 const auto MODULE_NAME = "mod_name";
54 template <typename T>
55 struct default_arg {
56 static T get() { return T(); }
57 };
58
59 public:
60 ParametersList() = default;
62 ~ParametersList() = default; // required for unique_ptr initialisation! avoids cleaning all individual objects
64
65 bool hasName() const;
66 std::string name(const std::string& default_value = "") const;
67 ParametersList& setName(const std::string&);
68
69 bool operator==(const ParametersList&) const;
70 bool operator!=(const ParametersList& oth) const { return !operator==(oth); }
72
73 ParametersList& feed(const std::string&);
74
77 template <typename T>
78 bool has(const std::string& key) const;
81 size_t erase(const std::string&);
84 template <typename T>
85 size_t erase(const std::string&);
86
90 template <typename T>
91 const ParametersList& fill(const std::string& key, T& value) const {
92 if (has<T>(key))
93 value = get<T>(key);
94 return *this;
95 }
99 template <typename T>
100 T get(const std::string& key, const T& def = default_arg<T>::get()) const;
106 template <typename T, typename U>
107 U getAs(const std::string& key, const U& def = default_arg<U>::get()) const {
108 return static_cast<U>(get<T>(key, static_cast<T>(def)));
109 }
112 template <typename T>
113 T& operator[](const std::string& key);
114 template <typename T>
115 ParametersList& set(const std::string&, const T&);
121 template <typename T, typename U>
122 ParametersList& setAs(const std::string& key, const U& value) {
123 return set<T>(key, static_cast<T>(value));
124 }
125 ParametersList& rename(const std::string&, const std::string&);
128 bool empty() const;
129
130 template <typename T>
131 std::vector<std::string> keysOf() const;
134 std::vector<std::string> keys(bool name_key = true) const;
138 std::string getString(const std::string& key, bool wrap = false) const;
141 inline std::string getNameString(bool wrap = false) const { return getString(MODULE_NAME, wrap); }
142 std::string serialise() const;
143
144 friend std::ostream& operator<<(std::ostream&,
145 const ParametersList&);
146 const ParametersList& print(std::ostream&) const;
147 std::string print(bool compact = false) const;
148
149 private:
150 std::map<std::string, ParametersList> param_values_;
151 std::unordered_map<std::string, bool> bool_values_;
152 std::unordered_map<std::string, int> int_values_;
153 std::unordered_map<std::string, unsigned long long> ulong_values_;
154 std::unordered_map<std::string, double> dbl_values_;
155 std::unordered_map<std::string, std::string> str_values_;
156 std::unordered_map<std::string, Limits> lim_values_;
157 std::unordered_map<std::string, std::vector<int> > vec_int_values_;
158 std::unordered_map<std::string, std::vector<double> > vec_dbl_values_;
159 std::unordered_map<std::string, std::vector<std::string> > vec_str_values_;
160 std::unordered_map<std::string, std::vector<Limits> > vec_lim_values_;
161 std::unordered_map<std::string, std::vector<ParametersList> > vec_param_values_;
162 std::unordered_map<std::string, std::vector<std::vector<double> > > vec_vec_dbl_values_;
163 };
164
166#define __TYPE_ENUM(type, map, name) \
167 template <> \
168 bool ParametersList::has<type>(const std::string&) const; \
169 template <> \
170 type ParametersList::get<type>(const std::string&, const type&) const; \
171 template <> \
172 type& ParametersList::operator[]<type>(const std::string&); \
173 template <> \
174 ParametersList& ParametersList::set<type>(const std::string&, const type&); \
175 template <> \
176 std::vector<std::string> ParametersList::keysOf<type>() const; \
177 template <> \
178 size_t ParametersList::erase<type>(const std::string&);
180#undef __TYPE_ENUM
181} // namespace cepgen
182
183#endif
#define REGISTER_CONTENT_TYPE
Looper over the list of parameter containers handled by the ParametersList object.
bool has(const std::string &key) const
Check if a given parameter is handled in this list.
std::string serialise() const
Serialise a parameter collection into a parseable string.
bool hasName() const
Does the parameters' list have a name key?
bool operator==(const ParametersList &) const
Equality operator.
ParametersList & operator+=(const ParametersList &oth)
Concatenate two parameters containers.
const ParametersList & print(std::ostream &) const
Debugging-like printout of a parameter container.
U getAs(const std::string &key, const U &def=default_arg< U >::get()) const
Get a recast parameter value.
ParametersList & feed(const std::string &)
Feed a control string to the list of parameters.
ParametersList & setName(const std::string &)
Set the module name.
ParametersList diff(const ParametersList &) const
Diff with another parameters list ('mine' + 'theirs' keys)
size_t erase(const std::string &)
Erase a typed parameter with the key.
std::vector< std::string > keys(bool name_key=true) const
bool empty() const
Is the list empty?
ParametersList & setAs(const std::string &key, const U &value)
size_t erase(const std::string &)
Erase a parameter with key.
T get(const std::string &key, const T &def=default_arg< T >::get()) const
Get a parameter value.
ParametersList & operator=(const ParametersList &)=default
Assignment operator.
std::string getNameString(bool wrap=false) const
Get a string-converted version of the module name if any.
ParametersList & set(const std::string &, const T &)
Set a parameter value Set a recast parameter value.
std::string name(const std::string &default_value="") const
Retrieve the module name if any.
T & operator[](const std::string &key)
Reference to a parameter value.
std::vector< std::string > keysOf() const
List of keys for one type in this list of parameters List of keys handled in this list of parameters.
std::string print(bool compact=false) const
Normal printout of a parameter container.
bool operator!=(const ParametersList &oth) const
Inequality operator.
friend std::ostream & operator<<(std::ostream &, const ParametersList &)
Human-readable version of a parameter container.
ParametersList & rename(const std::string &, const std::string &)
Rename the key to a parameter value.
ParametersList operator+(const ParametersList &other) const
Concatenation of two-parameter containers.
ParametersList(const ParametersList &)
Copy constructor.
const ParametersList & fill(const std::string &key, T &value) const
Fill a variable with the key content if exists.
std::string getString(const std::string &key, bool wrap=false) const
Get a string-converted version of a value.
Common namespace for this Monte Carlo generator.
Definition Handler.h:26
const auto MODULE_NAME
Indexing key for the module name Parameters container.