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 <limits>
40#include <set>
41#include <sstream>
42#include <string>
43#include <string_view>
44#include <type_traits>
45#include <vector>
46
47namespace Opm::Parameters {
48
49namespace detail {
50
51template <typename, class = void>
52struct has_name : public std::false_type {};
53
54template <typename T>
55struct has_name<T, std::void_t<decltype(std::declval<T>().name)>>
56: public std::true_type {};
57
59template<class Parameter>
61{
62 if constexpr (has_name<Parameter>::value) {
63 return Parameter::name;
64 } else {
65 std::string paramName = Dune::className<Parameter>();
66 // Dune::className() gives the compiler's rendering of the type, which
67 // need not start with the namespace.
68 constexpr std::string_view nsQualifier = "Opm::Parameters::";
69 if (const auto nsPos = paramName.find(nsQualifier); nsPos != std::string::npos) {
70 paramName.erase(0, nsPos + nsQualifier.size());
71 }
72 const auto pos = paramName.find_first_of('<');
73 if (pos != std::string::npos) {
74 paramName.erase(pos);
75 }
76 return paramName;
77 }
78}
79
81template<class ParamType>
82ParamType Get_(const std::string& paramName, ParamType defaultValue,
83 bool errorIfNotRegistered);
84
86void Hide_(const std::string& paramName);
87
89bool HideIfRegistered_(const std::string& paramName);
90
92bool IsSet_(const std::string& paramName, bool errorIfNotRegistered);
93
95void Register_(const std::string& paramName,
96 const std::string& paramTypeName,
97 const std::string& defaultValue,
98 const char* usageString);
99
101void SetDefault_(const std::string& paramName,
102 const std::string& paramValue);
103
104}
105
107
119void printUsage(const std::string& helpPreamble,
120 std::ostream& os,
121 const std::string& errorMsg = "",
122 const bool showAll = false);
123
125using PositionalArgumentCallback = std::function<int(std::function<void(const std::string&,
126 const std::string&)>,
127 std::set<std::string>&,
128 std::string&,
129 int,
130 const char**,
131 int,
132 int)>;
150std::string
152 const char **argv,
153 const PositionalArgumentCallback& posArgCallback,
154 const std::string& helpPreamble = "");
155
162bool parseParameterFile(const std::string& fileName, bool overwrite = true);
163
170void printValues(std::ostream& os);
171
180bool printUnused(std::ostream& os);
181
197template <class Param>
198auto Get(bool errorIfNotRegistered = true)
199{
200 using ParamType = std::conditional_t<std::is_same_v<decltype(Param::value),
201 const char* const>, std::string,
202 std::remove_const_t<decltype(Param::value)>>;
203 ParamType defaultValue = Param::value;
204 return detail::Get_(detail::getParamName<Param>(),
205 defaultValue, errorIfNotRegistered);
206}
207
222template <class Param>
223void SetDefault(decltype(Param::value) new_value)
224{
225 const std::string paramName = detail::getParamName<Param>();
226 std::ostringstream oss;
227 // full precision: the default stream setting of 6 significant digits
228 // silently corrupts values on the text round trip
229 if constexpr (std::is_floating_point_v<decltype(Param::value)>) {
230 oss.precision(std::numeric_limits<decltype(Param::value)>::max_digits10);
231 }
232 oss << new_value;
233 detail::SetDefault_(paramName, oss.str());
234}
235
240{
241 Parameter(const std::string& k, const std::string& v)
242 : key(k), value(v)
243 {}
244
245 friend std::ostream& operator<<(std::ostream& os, const Parameter& param)
246 {
247 os << param.key << "=\"" << param.value << '"';
248 return os;
249 }
250
251 bool operator==(const Parameter& setting) const
252 {
253 return setting.key == key
254 && setting.value == value;
255 }
256
257 bool operator !=(const Parameter& setting) const
258 {
259 return !(*this == setting);
260 }
261
262 std::string key, value;
263};
264
271void getLists(std::vector<Parameter>& usedParams,
272 std::vector<Parameter>& unusedParams);
273
277void reset();
278
285template <class Param>
286bool IsSet(bool errorIfNotRegistered = true)
287{
288 return detail::IsSet_(detail::getParamName<Param>(), errorIfNotRegistered);
289}
290
307template <class Param>
308void Register(const char* usageString)
309{
310 const std::string paramName = detail::getParamName<Param>();
311 const auto defaultValue = Param::value;
312 using ParamType = std::conditional_t<std::is_same_v<decltype(defaultValue),
313 const char* const>, std::string,
314 std::remove_const_t<decltype(defaultValue)>>;
315
316 std::ostringstream oss;
317 oss << defaultValue;
318 detail::Register_(paramName, Dune::className<ParamType>(), oss.str(), usageString);
319}
320
326template <class Param>
327void Hide()
328{
329 detail::Hide_(detail::getParamName<Param>());
330}
331
337template <class Param>
339{
340 return detail::HideIfRegistered_(detail::getParamName<Param>());
341}
342
348
358
359} // namespace Opm::Parameters
360
361#endif // OPM_PARAMETER_SYSTEM_HPP
bool IsSet_(const std::string &paramName, bool errorIfNotRegistered)
Private implementation.
bool HideIfRegistered_(const std::string &paramName)
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:60
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:132
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:223
bool IsSet(bool errorIfNotRegistered=true)
Returns true if a parameter has been specified at runtime, false otherwise.
Definition: parametersystem.hpp:286
bool HideIfRegistered()
Indicate that a given parameter should not be mentioned in the help message if present.
Definition: parametersystem.hpp:338
void Register(const char *usageString)
Register a run-time parameter.
Definition: parametersystem.hpp:308
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:327
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:198
A struct holding the key-value pair for a parameter.
Definition: parametersystem.hpp:240
bool operator==(const Parameter &setting) const
Definition: parametersystem.hpp:251
std::string key
Definition: parametersystem.hpp:262
std::string value
Definition: parametersystem.hpp:262
Parameter(const std::string &k, const std::string &v)
Definition: parametersystem.hpp:241
bool operator!=(const Parameter &setting) const
Definition: parametersystem.hpp:257
friend std::ostream & operator<<(std::ostream &os, const Parameter &param)
Definition: parametersystem.hpp:245
Definition: parametersystem.hpp:52