IPPL (Independent Parallel Particle Layer)
IPPL
Loading...
Searching...
No Matches
ParameterList.h
Go to the documentation of this file.
1//
2// Class ParameterList
3// Ippl related parameters (e.g. tolerance in case of iterative solvers).
4// Example:
5// ippl::ParameterList params;
6// params.add<double>("tolerance", 1.0e-8);
7// params.get<double>("tolerance");
8//
9//
10#ifndef IPPL_PARAMETER_LIST_H
11#define IPPL_PARAMETER_LIST_H
12
13#include <iomanip>
14#include <iostream>
15#include <map>
16#include <string>
17#include <utility>
18
19#include "Types/Variant.h"
20
22
23namespace ippl {
24
30 public:
31 // allowed parameter types
32 using variant_t =
33 std::variant<double, float, bool, std::string, unsigned int, int, ParameterList>;
34
35 ParameterList() = default;
36 ParameterList(const ParameterList&) = default;
37
43 template <typename T>
44 void add(const std::string& key, const T& value) {
45 if (params_m.contains(key)) {
46 throw IpplException("ParameterList::add()",
47 "Parameter '" + key + "' already exists.");
48 }
49 params_m[key] = value;
50 }
51
58 template <typename T>
59 T get(const std::string& key) const {
60 if (!params_m.contains(key)) {
61 throw IpplException("ParameterList::get()",
62 "Parameter '" + key + "' not contained.");
63 }
64 return std::get<T>(params_m.at(key));
65 }
66
74 template <typename T>
75 T get(const std::string& key, const T& defval) const {
76 if (!params_m.contains(key)) {
77 return defval;
78 }
79 return std::get<T>(params_m.at(key));
80 }
81
86 void merge(const ParameterList& p) noexcept {
87 for (const auto& [key, value] : p.params_m) {
88 params_m[key] = value;
89 }
90 }
91
97 void update(const ParameterList& p) noexcept {
98 for (const auto& [key, value] : p.params_m) {
99 if (params_m.contains(key)) {
100 params_m[key] = value;
101 }
102 }
103 }
104
110 template <typename T>
111 void update(const std::string& key, const T& value) {
112 if (!params_m.contains(key)) {
113 throw IpplException("ParameterList::update()",
114 "Parameter '" + key + "' does not exist.");
115 }
116 params_m[key] = value;
117 }
118
119 template <class Stream>
120 friend Stream& operator<<(Stream& stream, const ParameterList& sp) {
121 std::cout << "HI" << std::endl;
122 for (const auto& [key, value] : sp.params_m) {
123 const auto& keyLocal = key;
124 std::visit(
125 [&](auto&& arg) {
126 stream << std::make_pair(keyLocal, arg);
127 },
128 value);
129 }
130
131 return stream;
132 }
133
137 friend std::ostream& operator<<(std::ostream& os, const ParameterList& sp) {
138 static int indent = -4;
139
140 indent += 4;
141 if (indent > 0) {
142 os << '\n';
143 }
144 for (const auto& [key, value] : sp.params_m) {
145 const auto& keyLocal = key;
146 std::visit(
147 [&](auto&& arg) {
148 // 21 March 2021
149 // https://stackoverflow.com/questions/15884284/c-printing-spaces-or-tabs-given-a-user-input-integer
150 os << std::string(indent, ' ') << std::left << std::setw(20) << keyLocal
151 << " " << arg;
152 },
153 value);
154 // 21 March 2021
155 // https://stackoverflow.com/questions/289715/last-key-in-a-stdmap
156 if (key != std::prev(sp.params_m.end())->first) {
157 os << '\n';
158 }
159 }
160 indent -= 4;
161
162 return os;
163 }
165 if (this != &other) {
166 // Copy members from 'other' to 'this'
167 params_m = other.params_m;
168 }
169 return *this;
170 }
171
172 protected:
173 std::map<std::string, variant_t> params_m;
174 };
175} // namespace ippl
176
177#endif
KOKKOS_INLINE_FUNCTION auto & get(::ippl::Tuple< Ts... > &t)
Definition Tuple.h:362
Definition Archive.h:20
void update(const std::string &key, const T &value)
std::variant< double, float, bool, std::string, unsigned int, int, ParameterList > variant_t
friend Stream & operator<<(Stream &stream, const ParameterList &sp)
ParameterList()=default
T get(const std::string &key) const
ParameterList(const ParameterList &)=default
T get(const std::string &key, const T &defval) const
void merge(const ParameterList &p) noexcept
std::map< std::string, variant_t > params_m
void add(const std::string &key, const T &value)
ParameterList & operator=(const ParameterList &other)
friend std::ostream & operator<<(std::ostream &os, const ParameterList &sp)
void update(const ParameterList &p) noexcept