parametersystem.hpp
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3/*
4 This file is part of the Open Porous Media project (OPM).
5
6 OPM is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
10
11 OPM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with OPM. If not, see <http://www.gnu.org/licenses/>.
18
19 Consult the COPYING file in the top-level source directory of this
20 module for the precise wording of the license and the list of
21 copyright holders.
22*/
32#ifndef OPM_PARAMETER_SYSTEM_HPP
33#define OPM_PARAMETER_SYSTEM_HPP
34
35#include <dune/common/classname.hh>
36
37#include <cstring>
38#include <functional>
39#include <set>
40#include <sstream>
41#include <string>
42#include <type_traits>
43#include <vector>
44
45namespace Opm::Parameters {
46
47namespace detail {
48
49template <typename, class = void>
50struct has_name : public std::false_type {};
51
52template <typename T>
53struct has_name<T, std::void_t<decltype(std::declval<T>().name)>>
54: public std::true_type {};
55
57template<class Parameter>
59{
60 if constexpr (has_name<Parameter>::value) {
61 return Parameter::name;
62 } else {
63 std::string paramName = Dune::className<Parameter>();
64 paramName.replace(0, std::strlen("Opm::Parameters::"), "");
65 const auto pos = paramName.find_first_of('<');
66 if (pos != std::string::npos) {
67 paramName.erase(pos);
68 }
69 return paramName;
70 }
71}
72
74template<class ParamType>
75ParamType Get_(const std::string& paramName, ParamType defaultValue,
76 bool errorIfNotRegistered);
77
79void Hide_(const std::string& paramName);
80
82bool IsSet_(const std::string& paramName, bool errorIfNotRegistered);
83
85void Register_(const std::string& paramName,
86 const std::string& paramTypeName,
87 const std::string& defaultValue,
88 const char* usageString);
89
91void SetDefault_(const std::string& paramName,
92 const std::string& paramValue);
93
94}
95
97
107void printUsage(const std::string& helpPreamble,
108 std::ostream& os,
109 const std::string& errorMsg = "",
110 const bool showAll = false);
111
113using PositionalArgumentCallback = std::function<int(std::function<void(const std::string&,
114 const std::string&)>,
115 std::set<std::string>&,
116 std::string&,
117 int,
118 const char**,
119 int,
120 int)>;
137std::string
139 const char **argv,
140 const PositionalArgumentCallback& posArgCallback,
141 const std::string& helpPreamble = "");
142
149bool parseParameterFile(const std::string& fileName, bool overwrite = true);
150
157void printValues(std::ostream& os);
158
167bool printUnused(std::ostream& os);
168
184template <class Param>
185auto Get(bool errorIfNotRegistered = true)
186{
187 using ParamType = std::conditional_t<std::is_same_v<decltype(Param::value),
188 const char* const>, std::string,
189 std::remove_const_t<decltype(Param::value)>>;
190 ParamType defaultValue = Param::value;
191 return detail::Get_(detail::getParamName<Param>(),
192 defaultValue, errorIfNotRegistered);
193}
194
209template <class Param>
210void SetDefault(decltype(Param::value) new_value)
211{
212 const std::string paramName = detail::getParamName<Param>();
213 std::ostringstream oss;
214 oss << new_value;
215 detail::SetDefault_(paramName, oss.str());
216}
217
222{
223 Parameter(const std::string& k, const std::string& v)
224 : key(k), value(v)
225 {}
226
227 friend std::ostream& operator<<(std::ostream& os, const Parameter& param)
228 {
229 os << param.key << "=\"" << param.value << '"';
230 return os;
231 }
232
233 bool operator==(const Parameter& setting) const
234 {
235 return setting.key == key
236 && setting.value == value;
237 }
238
239 bool operator !=(const Parameter& setting) const
240 {
241 return !(*this == setting);
242 }
243
244 std::string key, value;
245};
246
253void getLists(std::vector<Parameter>& usedParams,
254 std::vector<Parameter>& unusedParams);
255
259void reset();
260
267template <class Param>
268bool IsSet(bool errorIfNotRegistered = true)
269{
270 return detail::IsSet_(detail::getParamName<Param>(), errorIfNotRegistered);
271}
272
289template <class Param>
290void Register(const char* usageString)
291{
292 const std::string paramName = detail::getParamName<Param>();
293 const auto defaultValue = Param::value;
294 using ParamType = std::conditional_t<std::is_same_v<decltype(defaultValue),
295 const char* const>, std::string,
296 std::remove_const_t<decltype(defaultValue)>>;
297
298 std::ostringstream oss;
299 oss << defaultValue;
300 detail::Register_(paramName, Dune::className<ParamType>(), oss.str(), usageString);
301}
302
308template <class Param>
309void Hide()
310{
311 detail::Hide_(detail::getParamName<Param>());
312}
313
319
329
330} // namespace Opm::Parameters
331
332#endif // OPM_PARAMETER_SYSTEM_HPP
bool IsSet_(const std::string &paramName, bool errorIfNotRegistered)
Private implementation.
ParamType Get_(const std::string &paramName, ParamType defaultValue, bool errorIfNotRegistered)
Private implementation.
auto getParamName()
get the name data member of a parameter
Definition: parametersystem.hpp:58
void Register_(const std::string &paramName, const std::string &paramTypeName, const std::string &defaultValue, const char *usageString)
Private implementation.
void SetDefault_(const std::string &paramName, const std::string &paramValue)
Private implementation.
void Hide_(const std::string &paramName)
Private implementation.
Definition: blackoilnewtonmethodparams.hpp:31
std::function< int(std::function< void(const std::string &, const std::string &)>, std::set< std::string > &, std::string &, int, const char **, int, int)> PositionalArgumentCallback
Callback function for command line parsing.
Definition: parametersystem.hpp:120
std::string parseCommandLineOptions(int argc, const char **argv, const PositionalArgumentCallback &posArgCallback, const std::string &helpPreamble="")
Parse the parameters provided on the command line.
void printValues(std::ostream &os)
Print values of the run-time parameters.
void SetDefault(decltype(Param::value) new_value)
Set a runtime parameter.
Definition: parametersystem.hpp:210
bool IsSet(bool errorIfNotRegistered=true)
Returns true if a parameter has been specified at runtime, false otherwise.
Definition: parametersystem.hpp:268
void Register(const char *usageString)
Register a run-time parameter.
Definition: parametersystem.hpp:290
bool IsRegistrationOpen()
Query whether parameter registration is open or not.
void endRegistration()
Indicate that all parameters are registered for a given type tag.
bool parseParameterFile(const std::string &fileName, bool overwrite=true)
Read the parameters from an INI-style file.
void getLists(std::vector< Parameter > &usedParams, std::vector< Parameter > &unusedParams)
Retrieves the lists of parameters specified at runtime and their values.
void printUsage(const std::string &helpPreamble, std::ostream &os, const std::string &errorMsg="", const bool showAll=false)
Print a usage message for all run-time parameters.
void reset()
Reset parameter system.
void Hide()
Indicate that a given parameter should not be mentioned in the help message.
Definition: parametersystem.hpp:309
bool printUnused(std::ostream &os)
Print the list of unused run-time parameters.
auto Get(bool errorIfNotRegistered=true)
Retrieve a runtime parameter.
Definition: parametersystem.hpp:185
A struct holding the key-value pair for a parameter.
Definition: parametersystem.hpp:222
bool operator==(const Parameter &setting) const
Definition: parametersystem.hpp:233
std::string key
Definition: parametersystem.hpp:244
std::string value
Definition: parametersystem.hpp:244
Parameter(const std::string &k, const std::string &v)
Definition: parametersystem.hpp:223
bool operator!=(const Parameter &setting) const
Definition: parametersystem.hpp:239
friend std::ostream & operator<<(std::ostream &os, const Parameter &param)
Definition: parametersystem.hpp:227
Definition: parametersystem.hpp:50