Skip to content
Snippets Groups Projects
Commit bca0bd28 authored by Dmitri Naumov's avatar Dmitri Naumov
Browse files

[PL] Extract findParameterByName().

parent ca1fb11c
No related branches found
No related tags found
No related merge requests found
...@@ -105,4 +105,24 @@ std::vector<std::reference_wrapper<ProcessVariable>> findProcessVariables( ...@@ -105,4 +105,24 @@ std::vector<std::reference_wrapper<ProcessVariable>> findProcessVariables(
return vars; return vars;
} }
ParameterBase* findParameterByName(
std::string const& parameter_name,
std::vector<std::unique_ptr<ParameterBase>> const& parameters)
{
// Find corresponding parameter by name.
auto const it = std::find_if(
parameters.cbegin(), parameters.cend(),
[&parameter_name](std::unique_ptr<ParameterBase> const& p) {
return p->name == parameter_name;
});
if (it == parameters.end())
{
return nullptr;
}
DBUG("Found parameter `%s'.", (*it)->name.c_str());
return it->get();
}
} // namespace ProcessLib } // namespace ProcessLib
...@@ -47,6 +47,15 @@ std::vector<std::reference_wrapper<ProcessVariable>> findProcessVariables( ...@@ -47,6 +47,15 @@ std::vector<std::reference_wrapper<ProcessVariable>> findProcessVariables(
BaseLib::ConfigTree const& pv_config, BaseLib::ConfigTree const& pv_config,
std::string const& tag); std::string const& tag);
/// Find an optional parameter of specific type for a given name.
///
/// \tparam ParameterDataType the data type of the parameter
/// \param parameter_name name of the requested parameter
/// \param parameters list of parameters in which it will be searched
ParameterBase* findParameterByName(
std::string const& parameter_name,
std::vector<std::unique_ptr<ParameterBase>> const& parameters);
/// Find an optional parameter of specific type for a given name. /// Find an optional parameter of specific type for a given name.
/// ///
/// \tparam ParameterDataType the data type of the parameter /// \tparam ParameterDataType the data type of the parameter
...@@ -63,22 +72,16 @@ Parameter<ParameterDataType>* findParameterOptional( ...@@ -63,22 +72,16 @@ Parameter<ParameterDataType>* findParameterOptional(
int const num_components) int const num_components)
{ {
// Find corresponding parameter by name. // Find corresponding parameter by name.
auto const parameter_it = std::find_if( ParameterBase* parameter_ptr =
parameters.cbegin(), parameters.cend(), findParameterByName(parameter_name, parameters);
[&parameter_name](std::unique_ptr<ParameterBase> const& p) { if (parameter_ptr == nullptr)
return p->name == parameter_name;
});
if (parameter_it == parameters.end())
{ {
return nullptr; return nullptr;
} }
DBUG("Found parameter `%s'.", (*parameter_it)->name.c_str());
// Check the type correctness of the found parameter. // Check the type correctness of the found parameter.
auto* const parameter = auto* const parameter =
dynamic_cast<Parameter<ParameterDataType>*>(parameter_it->get()); dynamic_cast<Parameter<ParameterDataType>*>(parameter_ptr);
if (!parameter) { if (!parameter) {
OGS_FATAL("The read parameter `%s' is of incompatible type.", OGS_FATAL("The read parameter `%s' is of incompatible type.",
parameter_name.c_str()); parameter_name.c_str());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment