cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.3
A generic central exclusive processes event generator
Loading...
Searching...
No Matches
ParametersList.h
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2018-2024 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 char* const MODULE_NAME = "mod_name";
53 private:
55 template <typename T>
56 struct default_arg {
57 static inline T get() { return T(); }
58 };
59
60 public:
61 ParametersList() = default;
63 ~ParametersList() {} // required for unique_ptr initialisation! avoids cleaning all individual objects
65
66 bool hasName() const;
67 std::string name(const std::string& def = "") const;
68 ParametersList& setName(const std::string&);
69
70 bool operator==(const ParametersList&) const;
71 bool operator!=(const ParametersList& oth) const { return !operator==(oth); }
72 ParametersList diff(const ParametersList&) const;
73
74 ParametersList& feed(const std::string&);
75
78 template <typename T>
79 bool has(const std::string& key) const;
82 size_t erase(const std::string&);
85 template <typename T>
86 size_t erase(const std::string&);
87
91 template <typename T>
92 inline const ParametersList& fill(const std::string& key, T& value) const {
93 if (has<T>(key))
94 value = get<T>(key);
95 return *this;
96 }
100 template <typename T>
101 T get(const std::string& key, const T& def = default_arg<T>::get()) const;
107 template <typename T, typename U>
108 inline U getAs(const std::string& key, const U& def = default_arg<U>::get()) const {
109 return static_cast<U>(get<T>(key, static_cast<T>(def)));
110 }
113 template <typename T>
114 T& operator[](const std::string& key);
115 template <typename T>
116 ParametersList& set(const std::string&, const T&);
122 template <typename T, typename U>
123 inline ParametersList& setAs(const std::string& key, const U& value) {
124 return set<T>(key, static_cast<T>(value));
125 }
126 ParametersList& rename(const std::string&, const std::string&);
128 ParametersList operator+(const ParametersList& oth) const;
129 bool empty() const;
130
131 template <typename T>
132 std::vector<std::string> keysOf() const;
135 std::vector<std::string> keys(bool name_key = true) const;
139 std::string getString(const std::string& key, bool wrap = false) const;
142 inline std::string getNameString(bool wrap = false) const { return getString(MODULE_NAME, wrap); }
143 std::string serialise() const;
144
145 friend std::ostream& operator<<(std::ostream&,
146 const ParametersList&);
147 const ParametersList& print(std::ostream&) const;
148 std::string print(bool compact = false) const;
149
150 private:
151 std::map<std::string, ParametersList> param_values_;
152 std::unordered_map<std::string, bool> bool_values_;
153 std::unordered_map<std::string, int> int_values_;
154 std::unordered_map<std::string, unsigned long long> ulong_values_;
155 std::unordered_map<std::string, double> dbl_values_;
156 std::unordered_map<std::string, std::string> str_values_;
157 std::unordered_map<std::string, Limits> lim_values_;
158 std::unordered_map<std::string, std::vector<int> > vec_int_values_;
159 std::unordered_map<std::string, std::vector<double> > vec_dbl_values_;
160 std::unordered_map<std::string, std::vector<std::string> > vec_str_values_;
161 std::unordered_map<std::string, std::vector<Limits> > vec_lim_values_;
162 std::unordered_map<std::string, std::vector<ParametersList> > vec_param_values_;
163 std::unordered_map<std::string, std::vector<std::vector<double> > > vec_vec_dbl_values_;
164 };
165
167#define __TYPE_ENUM(type, map, name) \
168 template <> \
169 bool ParametersList::has<type>(const std::string&) const; \
170 template <> \
171 type ParametersList::get<type>(const std::string&, const type&) const; \
172 template <> \
173 type& ParametersList::operator[]<type>(const std::string&); \
174 template <> \
175 ParametersList& ParametersList::set<type>(const std::string&, const type&); \
176 template <> \
177 std::vector<std::string> ParametersList::keysOf<type>() const; \
178 template <> \
179 size_t ParametersList::erase<type>(const std::string&);
181#undef __TYPE_ENUM
182} // namespace cepgen
183
184#endif
#define REGISTER_CONTENT_TYPE
Looper over the list of parameters 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 parameters collection into a parseable string.
bool hasName() const
Does the parameters list have a name key?
bool operator==(const ParametersList &) const
Equality operator.
std::string name(const std::string &def="") const
Retrieve the module name if any.
ParametersList & operator+=(const ParametersList &oth)
Concatenate two parameters containers.
const ParametersList & print(std::ostream &) const
Debugging-like printout of a parameters 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 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.
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.
bool operator!=(const ParametersList &oth) const
Inequality operator.
friend std::ostream & operator<<(std::ostream &, const ParametersList &)
Human-readable version of a parameters container.
ParametersList & rename(const std::string &, const std::string &)
Rename the key to a parameter value.
ParametersList operator+(const ParametersList &oth) const
Concatenation of two parameters containers.
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.
const char *const MODULE_NAME
Indexing key for the module name Parameters container.