pass property struct to Parameters::get

use this to obtain default value and parameter name
This commit is contained in:
Arne Morten Kvarving 2024-04-04 15:44:46 +02:00
parent 72322ec6ff
commit fe06454112

View File

@ -49,6 +49,7 @@
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include <tuple>
#include <type_traits> #include <type_traits>
#include <unordered_map> #include <unordered_map>
@ -71,14 +72,11 @@
* \endcode * \endcode
*/ */
#define EWOMS_GET_PARAM(TypeTag, ParamType, ParamName) \ #define EWOMS_GET_PARAM(TypeTag, ParamType, ParamName) \
(::Opm::Parameters::get<TypeTag, ParamType>(#ParamName, \ (::Opm::Parameters::get<TypeTag, Properties::ParamName>())
getPropValue<TypeTag, Properties::ParamName>()))
//!\cond SKIP_THIS //!\cond SKIP_THIS
#define EWOMS_GET_PARAM_(TypeTag, ParamType, ParamName) \ #define EWOMS_GET_PARAM_(TypeTag, ParamType, ParamName) \
(::Opm::Parameters::get<TypeTag, ParamType>(#ParamName, \ (::Opm::Parameters::get<TypeTag, Properties::ParamName>(false))
getPropValue<TypeTag, Properties::ParamName>(), \
/*errorIfNotRegistered=*/false))
namespace Opm { namespace Opm {
namespace Parameters { namespace Parameters {
@ -103,10 +101,8 @@ struct ParamInfo
}; };
// forward declaration // forward declaration
template <class TypeTag, class ParamType> template <class TypeTag, template<class,class> class Property>
const ParamType get(const char *paramName, auto get(bool errorIfNotRegistered = true);
const ParamType& defaultValue,
bool errorIfNotRegistered = true);
class ParamRegFinalizerBase_ class ParamRegFinalizerBase_
{ {
@ -116,28 +112,16 @@ public:
virtual void retrieve() = 0; virtual void retrieve() = 0;
}; };
template <class TypeTag, class ParamType> template <class TypeTag, template<class,class> class Property>
class ParamRegFinalizer_ : public ParamRegFinalizerBase_ class ParamRegFinalizer_ : public ParamRegFinalizerBase_
{ {
public: public:
ParamRegFinalizer_(const std::string& paramName, const ParamType& defaultValue) void retrieve() override
: paramName_(paramName)
, defaultValue_(defaultValue)
{}
virtual void retrieve() override
{ {
// retrieve the parameter once to make sure that its value does // retrieve the parameter once to make sure that its value does
// not contain a syntax error. // not contain a syntax error.
ParamType __attribute__((unused)) dummy = std::ignore = get<TypeTag, Property>(/*errorIfNotRegistered=*/true);
get<TypeTag, ParamType>(paramName_.data(),
defaultValue_,
/*errorIfNotRegistered=*/true);
} }
private:
std::string paramName_;
ParamType defaultValue_;
}; };
} // namespace Parameters } // namespace Parameters
@ -886,7 +870,7 @@ class Param
public: public:
template <class ParamType> template <class ParamType>
static ParamType get(const char* paramName, static ParamType get(const std::string& paramName,
const ParamType& defaultValue, const ParamType& defaultValue,
bool errorIfNotRegistered = true) bool errorIfNotRegistered = true)
{ {
@ -948,7 +932,7 @@ private:
}; };
static void check_(const std::string& paramTypeName, static void check_(const std::string& paramTypeName,
const char* paramName) const std::string& paramName)
{ {
using StaticData = std::unordered_map<std::string, Blubb>; using StaticData = std::unordered_map<std::string, Blubb>;
static StaticData staticData; static StaticData staticData;
@ -965,14 +949,14 @@ private:
b = &(it->second); b = &(it->second);
if (b->paramTypeName != paramTypeName) { if (b->paramTypeName != paramTypeName) {
throw std::logic_error("GET_*_PARAM for parameter '"+std::string(paramName) throw std::logic_error("GET_*_PARAM for parameter '" + paramName
+"' called with at least two different types (" +"' called with at least two different types ("
+b->paramTypeName+" and "+paramTypeName+")"); + b->paramTypeName + " and " + paramTypeName+")");
} }
} }
template <class ParamType> template <class ParamType>
static ParamType retrieve_(const char* paramName, static ParamType retrieve_(const std::string& paramName,
const ParamType& defaultValue, const ParamType& defaultValue,
bool errorIfNotRegistered = true) bool errorIfNotRegistered = true)
{ {
@ -989,7 +973,7 @@ private:
"been registered."); "been registered.");
if (ParamsMeta::registry().find(paramName) == ParamsMeta::registry().end()) if (ParamsMeta::registry().find(paramName) == ParamsMeta::registry().end())
throw std::runtime_error("Accessing parameter "+std::string(paramName) throw std::runtime_error("Accessing parameter " + paramName
+" without prior registration is not allowed."); +" without prior registration is not allowed.");
} }
@ -1006,9 +990,14 @@ private:
} }
}; };
template <class TypeTag, class ParamType> template <class TypeTag, template<class,class> class Property>
const ParamType get(const char* paramName, const ParamType& defaultValue, bool errorIfNotRegistered) auto get(bool errorIfNotRegistered)
{ {
const std::string paramName = getPropName<TypeTag,Property>();
const auto defaultValue = getPropValue<TypeTag, Property>();
using ParamType = std::conditional_t<std::is_same_v<decltype(defaultValue),
const char* const>, std::string,
std::remove_const_t<decltype(defaultValue)>>;
return Param<TypeTag>::template get<ParamType>(paramName, defaultValue, errorIfNotRegistered); return Param<TypeTag>::template get<ParamType>(paramName, defaultValue, errorIfNotRegistered);
} }
@ -1089,8 +1078,8 @@ void registerParam(const char* usageString)
using ParamType = std::conditional_t<std::is_same_v<decltype(defaultValue), using ParamType = std::conditional_t<std::is_same_v<decltype(defaultValue),
const char* const>, std::string, const char* const>, std::string,
std::remove_const_t<decltype(defaultValue)>>; std::remove_const_t<decltype(defaultValue)>>;
ParamsMeta::registrationFinalizers().emplace_back( ParamsMeta::registrationFinalizers().push_back(
new ParamRegFinalizer_<TypeTag, ParamType>(paramName, defaultValue)); std::make_unique<ParamRegFinalizer_<TypeTag, Param>>());
ParamInfo paramInfo; ParamInfo paramInfo;
paramInfo.paramName = paramName; paramInfo.paramName = paramName;