Parameters::registerParam: grab name from struct

optionally allow a member to override
This commit is contained in:
Arne Morten Kvarving 2024-03-22 15:51:45 +01:00
parent 069161bd84
commit 22a5aab10e
2 changed files with 35 additions and 7 deletions

View File

@ -73,8 +73,7 @@
* \endcode
*/
#define EWOMS_REGISTER_PARAM(TypeTag, ParamType, ParamName, Description) \
::Opm::Parameters::registerParam<TypeTag, Properties::ParamName>( \
#ParamName, Description)
::Opm::Parameters::registerParam<TypeTag, Properties::ParamName>(Description)
/*!
* \ingroup Parameter
@ -1105,12 +1104,14 @@ bool isSet(const char* paramName, bool errorIfNotRegistered = true)
}
template <class TypeTag, template<class,class> class Param>
void registerParam(const char* paramName, const char* usageString)
void registerParam(const char* usageString)
{
using ParamsMeta = GetProp<TypeTag, Properties::ParameterMetaData>;
if (!ParamsMeta::registrationOpen())
const std::string paramName = getPropName<TypeTag,Param>();
if (!ParamsMeta::registrationOpen()) {
throw std::logic_error("Parameter registration was already closed before "
"the parameter '"+std::string(paramName)+"' was registered.");
"the parameter '" + paramName + "' was registered.");
}
const auto defaultValue = getPropValue<TypeTag, Param>();
using ParamType = std::conditional_t<std::is_same_v<decltype(defaultValue),
@ -1134,7 +1135,7 @@ void registerParam(const char* paramName, const char* usageString)
// parameter name, type and usage string are exactly the same.
if (ParamsMeta::registry().at(paramName) == paramInfo)
return;
throw std::logic_error("Parameter "+std::string(paramName)
throw std::logic_error("Parameter " + paramName
+" registered twice with non-matching characteristics.");
}

View File

@ -26,9 +26,12 @@
#ifndef OPM_PROPERTY_SYSTEM_HH
#define OPM_PROPERTY_SYSTEM_HH
#include <dune/common/classname.hh>
#include <cstring>
#include <ostream>
#include <tuple>
#include <type_traits>
#include <ostream>
namespace Opm {
namespace Properties {
@ -215,6 +218,13 @@ struct GetSplicePropImpl
static_assert(!std::is_same<type, std::tuple<>>::value, "Splice is undefined!");
};
template <typename, class = void>
struct has_name : public std::false_type {};
template <typename T>
struct has_name<T, decltype((void)T::name, void())>
: public std::true_type {};
} // end namespace Detail
} // end namespace Property
@ -237,6 +247,23 @@ using GetSplicePropType = typename Properties::Detail::GetSplicePropImpl<TypeTag
//! get the value data member of a property
template<class TypeTag, template<class,class> class Property>
constexpr auto getPropValue() { return Properties::Detail::GetPropImpl<TypeTag, Property>::type::value; }
//! get the name data member of a property
template<class TypeTag, template<class,class> class Property>
auto getPropName()
{
using type = typename Properties::Detail::GetPropImpl<TypeTag,Property>::type;
if constexpr (Properties::Detail::has_name<type>::value) {
return Properties::Detail::GetPropImpl<TypeTag, Property>::type::name;
} else {
std::string paramName = Dune::className<type>();
paramName.replace(0, std::strlen("Opm::Properties::"), "");
const auto pos = paramName.find_first_of('<');
paramName.erase(pos);
return paramName;
}
}
#ifdef __clang__
#pragma clang diagnostic pop
#endif