add Parameters::Parameter struct

this holds a key-value pair for a parameter with some utility functions
for comparison and printing.

use this to move Parameters::getLists to the translation unit
This commit is contained in:
Arne Morten Kvarving 2024-09-04 15:39:02 +02:00
parent ae8dd62fe3
commit 410039206d
4 changed files with 63 additions and 59 deletions

View File

@ -427,6 +427,33 @@ void printParamUsage(std::ostream& os, const ParamInfo& paramInfo)
os << paramMessage;
}
void getLists(std::vector<Parameter>& usedParams,
std::vector<Parameter>& unusedParams)
{
usedParams.clear();
unusedParams.clear();
if (MetaData::registrationOpen()) {
throw std::runtime_error("Parameter lists can only retrieved after _all_ of them have "
"been registered.");
}
// get all parameter keys
std::list<std::string> allKeysList;
getFlattenedKeyList(allKeysList, MetaData::tree());
for (const auto& key : allKeysList) {
if (MetaData::registry().find(key) == MetaData::registry().end()) {
// key was not registered
unusedParams.emplace_back(key, MetaData::tree()[key]);
}
else {
// key was registered
usedParams.emplace_back(key, MetaData::tree()[key]);
}
}
}
void getFlattenedKeyList(std::list<std::string>& dest,
const Dune::ParameterTree& tree,
const std::string& prefix)

View File

@ -50,8 +50,10 @@
#include <sstream>
#include <stdexcept>
#include <string>
#include <utility>
#include <tuple>
#include <type_traits>
#include <vector>
#include <unistd.h>
#include <sys/ioctl.h>
@ -318,38 +320,43 @@ void SetDefault(decltype(Param::value) new_value)
detail::SetDefault_(paramName, oss.str());
}
/*!
* \brief A struct holding the key-value pair for a parameter.
*/
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;
};
/*!
* \brief Retrieves the lists of parameters specified at runtime and their values.
*
* The two arguments besides the TypeTag are assumed to be STL containers which store
* std::pair<std::string, std::string>.
*/
template <class Container>
void getLists(Container& usedParams, Container& unusedParams)
{
usedParams.clear();
unusedParams.clear();
if (MetaData::registrationOpen()) {
throw std::runtime_error("Parameter lists can only retrieved after _all_ of them have "
"been registered.");
}
// get all parameter keys
std::list<std::string> allKeysList;
getFlattenedKeyList(allKeysList, MetaData::tree());
for (const auto& key : allKeysList) {
if (MetaData::registry().find(key) == MetaData::registry().end()) {
// key was not registered
unusedParams.emplace_back(key, MetaData::tree()[key]);
}
else {
// key was registered
usedParams.emplace_back(key, MetaData::tree()[key]);
}
}
}
void getLists(std::vector<Parameter>& usedParams,
std::vector<Parameter>& unusedParams);
/*!
* \brief Reset parameter system.

View File

@ -171,8 +171,7 @@ static inline int setupParameters_(int argc,
}
// make sure that no unknown parameters are encountered
using KeyValuePair = std::pair<std::string, std::string>;
using ParamList = std::list<KeyValuePair>;
using ParamList = std::vector<Parameters::Parameter>;
ParamList usedParams;
ParamList unusedParams;
@ -188,7 +187,7 @@ static inline int setupParameters_(int argc,
std::cerr << "\n";
for (const auto& keyValue : unusedParams)
std::cerr << " " << keyValue.first << "=\"" << keyValue.second << "\"\n";
std::cerr << " " << keyValue << "\n";
std::cerr << "\n";
std::cerr << "Use\n"

View File

@ -41,35 +41,6 @@ struct SimpleParamBoolN2
namespace {
class Setting
{
public:
Setting(const std::string& k, const std::string& v)
: key(k), value(v)
{}
friend std::ostream& operator<<(std::ostream& os, const Setting& setting)
{
os << setting.key << "=" << setting.value << '\n';
return os;
}
bool operator==(const Setting& setting) const
{
return setting.key == key
&& setting.value == value;
}
bool operator !=(const Setting& setting) const
{
return !(*this == setting);
}
private:
std::string key;
std::string value;
};
struct Fixture
{
Fixture()
@ -117,7 +88,7 @@ BOOST_FIXTURE_TEST_CASE(GetLists, Fixture)
BOOST_CHECK_EQUAL(Opm::Parameters::IsSet<Opm::Parameters::SimpleParamDouble>(), false);
BOOST_CHECK_EQUAL(Opm::Parameters::IsSet<Opm::Parameters::SimpleParamInt>(), false);
using SettingMap = std::vector<Setting>;
using SettingMap = std::vector<Opm::Parameters::Parameter>;
const SettingMap set_ref = {
{"SimpleParamBool", "true"},