Loading...
Searching...
No Matches
parametersystem.hpp
Go to the documentation of this file.
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// vi: set et ts=4 sw=4 sts=4:
/*
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
Consult the COPYING file in the top-level source directory of this
module for the precise wording of the license and the list of
copyright holders.
*/
#ifndef OPM_PARAMETER_SYSTEM_HPP
#define OPM_PARAMETER_SYSTEM_HPP
#include <dune/common/classname.hh>
#include <cstring>
#include <functional>
#include <set>
#include <sstream>
#include <string>
#include <type_traits>
#include <vector>
namespace Opm::Parameters {
namespace detail {
template <typename, class = void>
struct has_name : public std::false_type {};
template <typename T>
struct has_name<T, std::void_t<decltype(std::declval<T>().name)>>
: public std::true_type {};
template<class Parameter>
auto getParamName()
{
if constexpr (has_name<Parameter>::value) {
return Parameter::name;
} else {
std::string paramName = Dune::className<Parameter>();
paramName.replace(0, std::strlen("Opm::Parameters::"), "");
const auto pos = paramName.find_first_of('<');
if (pos != std::string::npos) {
paramName.erase(pos);
}
return paramName;
}
}
template<class ParamType>
ParamType Get_(const std::string& paramName, ParamType defaultValue,
bool errorIfNotRegistered);
void Hide_(const std::string& paramName);
bool IsSet_(const std::string& paramName, bool errorIfNotRegistered);
void Register_(const std::string& paramName,
const std::string& paramTypeName,
const std::string& defaultValue,
const char* usageString);
void SetDefault_(const std::string& paramName,
const std::string& paramValue);
}
void printUsage(const std::string& helpPreamble,
std::ostream& os,
const std::string& errorMsg = "",
const bool showAll = false);
using PositionalArgumentCallback = std::function<int(std::function<void(const std::string&,
const std::string&)>,
std::set<std::string>&,
std::string&,
int,
const char**,
int,
int)>;
std::string
parseCommandLineOptions(int argc,
const char **argv,
const PositionalArgumentCallback& posArgCallback,
const std::string& helpPreamble = "");
bool parseParameterFile(const std::string& fileName, bool overwrite = true);
void printValues(std::ostream& os);
bool printUnused(std::ostream& os);
template <class Param>
auto Get(bool errorIfNotRegistered = true)
{
using ParamType = std::conditional_t<std::is_same_v<decltype(Param::value),
const char* const>, std::string,
std::remove_const_t<decltype(Param::value)>>;
ParamType defaultValue = Param::value;
return detail::Get_(detail::getParamName<Param>(),
defaultValue, errorIfNotRegistered);
}
template <class Param>
void SetDefault(decltype(Param::value) new_value)
{
const std::string paramName = detail::getParamName<Param>();
std::ostringstream oss;
oss << new_value;
detail::SetDefault_(paramName, oss.str());
}
struct Parameter
{
Parameter(const std::string& k, const std::string& v)
: key(k), value(v)
{}
friend std::ostream& operator<<(std::ostream& os, const Parameter& param)
{
os << param.key << "=\"" << param.value << '"';
return os;
}
bool operator==(const Parameter& setting) const
{
return setting.key == key
&& setting.value == value;
}
bool operator !=(const Parameter& setting) const
{
return !(*this == setting);
}
std::string key, value;
};
void getLists(std::vector<Parameter>& usedParams,
std::vector<Parameter>& unusedParams);
void reset();
template <class Param>
bool IsSet(bool errorIfNotRegistered = true)
{
return detail::IsSet_(detail::getParamName<Param>(), errorIfNotRegistered);
}
template <class Param>
void Register(const char* usageString)
{
const std::string paramName = detail::getParamName<Param>();
const auto defaultValue = Param::value;
using ParamType = std::conditional_t<std::is_same_v<decltype(defaultValue),
const char* const>, std::string,
std::remove_const_t<decltype(defaultValue)>>;
std::ostringstream oss;
oss << defaultValue;
detail::Register_(paramName, Dune::className<ParamType>(), oss.str(), usageString);
}
template <class Param>
void Hide()
{
detail::Hide_(detail::getParamName<Param>());
}
bool IsRegistrationOpen();
void endRegistration();
} // namespace Opm::Parameters
#endif // OPM_PARAMETER_SYSTEM_HPP
Opm::Parameters::parseCommandLineOptions
std::string parseCommandLineOptions(int argc, const char **argv, const PositionalArgumentCallback &posArgCallback, const std::string &helpPreamble)
Parse the parameters provided on the command line.
Definition parametersystem.cpp:636
Opm::Parameters::printValues
void printValues(std::ostream &os)
Print values of the run-time parameters.
Definition parametersystem.cpp:741
Opm::Parameters::detail::IsSet_
bool IsSet_(const std::string &paramName, bool errorIfNotRegistered)
Private implementation.
Definition parametersystem.cpp:426
Opm::Parameters::parseParameterFile
bool parseParameterFile(const std::string &fileName, bool overwrite)
Read the parameters from an INI-style file.
Definition parametersystem.cpp:563
Opm::Parameters::detail::Get_
ParamType Get_(const std::string &paramName, ParamType defaultValue, bool errorIfNotRegistered)
Private implementation.
Definition parametersystem.cpp:363
Opm::Parameters::getLists
void getLists(std::vector< Parameter > &usedParams, std::vector< Parameter > &unusedParams)
Retrieves the lists of parameters specified at runtime and their values.
Definition parametersystem.cpp:505
Opm::Parameters::reset
void reset()
Reset parameter system.
Definition parametersystem.cpp:485
Opm::Parameters::detail::Register_
void Register_(const std::string &paramName, const std::string &paramTypeName, const std::string &defaultValue, const char *usageString)
Private implementation.
Definition parametersystem.cpp:444
Opm::Parameters::printUnused
bool printUnused(std::ostream &os)
Print the list of unused run-time parameters.
Definition parametersystem.cpp:790
Opm::Parameters::detail::SetDefault_
void SetDefault_(const std::string &paramName, const std::string &paramValue)
Private implementation.
Definition parametersystem.cpp:473
Opm::Parameters::detail::Hide_
void Hide_(const std::string &paramName)
Private implementation.
Definition parametersystem.cpp:409
Opm::Parameters::SetDefault
void SetDefault(decltype(Param::value) new_value)
Set a runtime parameter.
Definition parametersystem.hpp:212
Opm::Parameters::IsSet
bool IsSet(bool errorIfNotRegistered=true)
Returns true if a parameter has been specified at runtime, false otherwise.
Definition parametersystem.hpp:270
Opm::Parameters::Register
void Register(const char *usageString)
Register a run-time parameter.
Definition parametersystem.hpp:292
Opm::Parameters::PositionalArgumentCallback
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:114
Opm::Parameters::detail::getParamName
auto getParamName()
get the name data member of a parameter
Definition parametersystem.hpp:58
Opm::Parameters::Hide
void Hide()
Indicate that a given parameter should not be mentioned in the help message.
Definition parametersystem.hpp:311
Opm::Parameters::Get
auto Get(bool errorIfNotRegistered=true)
Retrieve a runtime parameter.
Definition parametersystem.hpp:187
Opm::Parameters::detail::has_name
Definition parametersystem.hpp:50