Parameters::Register split out parts of implementation

allows putting it in translation unit
This commit is contained in:
Arne Morten Kvarving 2024-09-02 14:19:33 +02:00
parent 48bcf6ac04
commit 98b33d582f
2 changed files with 36 additions and 22 deletions

View File

@ -160,6 +160,35 @@ void Hide_(const std::string& paramName)
paramInfo.isHidden = true; paramInfo.isHidden = true;
} }
void Register_(const std::string& paramName,
const std::string& paramTypeName,
const std::string& defaultValue,
const char* usageString)
{
if (!MetaData::registrationOpen()) {
throw std::logic_error("Parameter registration was already closed before "
"the parameter '" + paramName + "' was registered.");
}
ParamInfo paramInfo;
paramInfo.paramName = paramName;
paramInfo.paramTypeName = paramTypeName;
paramInfo.usageString = usageString;
paramInfo.defaultValue = defaultValue;
paramInfo.isHidden = false;
if (MetaData::registry().find(paramName) != MetaData::registry().end()) {
// allow to register a parameter twice, but only if the
// parameter name, type and usage string are exactly the same.
if (MetaData::registry().at(paramName) == paramInfo) {
return;
}
throw std::logic_error("Parameter " + paramName
+" registered twice with non-matching characteristics.");
}
MetaData::mutableRegistry()[paramName] = paramInfo;
}
} }
bool ParamInfo::operator==(const ParamInfo& other) const bool ParamInfo::operator==(const ParamInfo& other) const

View File

@ -87,6 +87,12 @@ auto getParamName()
//! \brief Private implementation. //! \brief Private implementation.
void Hide_(const std::string& paramName); void Hide_(const std::string& paramName);
//! \brief Private implementation.
void Register_(const std::string& paramName,
const std::string& paramTypeName,
const std::string& defaultValue,
const char* usageString);
} }
struct ParamInfo struct ParamInfo
@ -459,11 +465,6 @@ template <class Param>
void Register(const char* usageString) void Register(const char* usageString)
{ {
const std::string paramName = detail::getParamName<Param>(); const std::string paramName = detail::getParamName<Param>();
if (!MetaData::registrationOpen()) {
throw std::logic_error("Parameter registration was already closed before "
"the parameter '" + paramName + "' was registered.");
}
const auto defaultValue = Param::value; const auto defaultValue = Param::value;
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,
@ -471,25 +472,9 @@ void Register(const char* usageString)
MetaData::registrationFinalizers().push_back( MetaData::registrationFinalizers().push_back(
std::make_unique<ParamRegFinalizer_<Param>>()); std::make_unique<ParamRegFinalizer_<Param>>());
ParamInfo paramInfo;
paramInfo.paramName = paramName;
paramInfo.paramTypeName = Dune::className<ParamType>();
paramInfo.usageString = usageString;
std::ostringstream oss; std::ostringstream oss;
oss << defaultValue; oss << defaultValue;
paramInfo.defaultValue = oss.str(); detail::Register_(paramName, Dune::className<ParamType>(), oss.str(), usageString);
paramInfo.isHidden = false;
if (MetaData::registry().find(paramName) != MetaData::registry().end()) {
// allow to register a parameter twice, but only if the
// parameter name, type and usage string are exactly the same.
if (MetaData::registry().at(paramName) == paramInfo) {
return;
}
throw std::logic_error("Parameter " + paramName
+" registered twice with non-matching characteristics.");
}
MetaData::mutableRegistry()[paramName] = paramInfo;
} }
/*! /*!