Parameter: move more code to compile unit
in particular this allows encapsulating sstream
This commit is contained in:
parent
e05de9a741
commit
f987a3794c
@ -37,7 +37,6 @@
|
||||
#define OPM_PARAMETER_HEADER
|
||||
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
#include <opm/common/utility/parameters/ParameterMapItem.hpp>
|
||||
#include <opm/common/utility/parameters/ParameterStrings.hpp>
|
||||
@ -89,30 +88,8 @@ namespace Opm {
|
||||
struct ParameterMapItemTrait<int> {
|
||||
static int convert(const ParameterMapItem& item,
|
||||
std::string& conversion_error,
|
||||
const bool)
|
||||
{
|
||||
conversion_error = correct_parameter_tag(item);
|
||||
if (conversion_error != "") {
|
||||
return 0;
|
||||
}
|
||||
const Parameter& parameter = dynamic_cast<const Parameter&>(item);
|
||||
conversion_error = correct_type(parameter, ID_param_type__int);
|
||||
if (conversion_error != "") {
|
||||
return 0;
|
||||
}
|
||||
std::stringstream stream;
|
||||
stream << parameter.getValue();
|
||||
int value;
|
||||
stream >> value;
|
||||
if (stream.fail()) {
|
||||
conversion_error = "Conversion to '" +
|
||||
ID_param_type__int +
|
||||
"' failed. Data was '" +
|
||||
parameter.getValue() + "'.\n";
|
||||
return 0;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
const bool);
|
||||
|
||||
static std::string type() {return ID_param_type__int;}
|
||||
};
|
||||
|
||||
@ -125,30 +102,8 @@ namespace Opm {
|
||||
struct ParameterMapItemTrait<double> {
|
||||
static double convert(const ParameterMapItem& item,
|
||||
std::string& conversion_error,
|
||||
const bool)
|
||||
{
|
||||
conversion_error = correct_parameter_tag(item);
|
||||
if (conversion_error != "") {
|
||||
return 0.0;
|
||||
}
|
||||
const Parameter& parameter = dynamic_cast<const Parameter&>(item);
|
||||
conversion_error = correct_type(parameter, ID_param_type__float);
|
||||
if (conversion_error != "") {
|
||||
return 0.0;
|
||||
}
|
||||
std::stringstream stream;
|
||||
stream << parameter.getValue();
|
||||
double value;
|
||||
stream >> value;
|
||||
if (stream.fail()) {
|
||||
conversion_error = "Conversion to '" +
|
||||
ID_param_type__float +
|
||||
"' failed. Data was '" +
|
||||
parameter.getValue() + "'.\n";
|
||||
return 0.0;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
const bool);
|
||||
|
||||
static std::string type() {return ID_param_type__float;}
|
||||
};
|
||||
|
||||
@ -161,29 +116,8 @@ namespace Opm {
|
||||
struct ParameterMapItemTrait<bool> {
|
||||
static bool convert(const ParameterMapItem& item,
|
||||
std::string& conversion_error,
|
||||
const bool)
|
||||
{
|
||||
conversion_error = correct_parameter_tag(item);
|
||||
if (conversion_error != "") {
|
||||
return false;
|
||||
}
|
||||
const Parameter& parameter = dynamic_cast<const Parameter&>(item);
|
||||
conversion_error = correct_type(parameter, ID_param_type__bool);
|
||||
if (conversion_error != "") {
|
||||
return false;
|
||||
}
|
||||
if (parameter.getValue() == ID_true) {
|
||||
return true;
|
||||
} else if (parameter.getValue() == ID_false) {
|
||||
return false;
|
||||
} else {
|
||||
conversion_error = "Conversion failed. Data was '" +
|
||||
parameter.getValue() +
|
||||
"', but should be one of '" +
|
||||
ID_true + "' or '" + ID_false + "'.\n";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
const bool);
|
||||
|
||||
static std::string type() {return ID_param_type__bool;}
|
||||
};
|
||||
|
||||
@ -196,19 +130,8 @@ namespace Opm {
|
||||
struct ParameterMapItemTrait<std::string> {
|
||||
static std::string convert(const ParameterMapItem& item,
|
||||
std::string& conversion_error,
|
||||
const bool)
|
||||
{
|
||||
conversion_error = correct_parameter_tag(item);
|
||||
if (conversion_error != "") {
|
||||
return "";
|
||||
}
|
||||
const Parameter& parameter = dynamic_cast<const Parameter&>(item);
|
||||
conversion_error = correct_type(parameter, ID_param_type__string);
|
||||
if (conversion_error != "") {
|
||||
return "";
|
||||
}
|
||||
return parameter.getValue();
|
||||
}
|
||||
const bool);
|
||||
|
||||
static std::string type() {return ID_param_type__string;}
|
||||
};
|
||||
} // namespace Opm
|
||||
|
@ -36,37 +36,140 @@
|
||||
#if HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
#include <string>
|
||||
#include <opm/common/utility/parameters/Parameter.hpp>
|
||||
|
||||
namespace Opm {
|
||||
std::string
|
||||
correct_parameter_tag(const ParameterMapItem& item)
|
||||
{
|
||||
std::string tag = item.getTag();
|
||||
if (tag != ID_xmltag__param) {
|
||||
std::string error = "The XML tag was '" +
|
||||
tag + "' but should be '" +
|
||||
ID_xmltag__param + "'.\n";
|
||||
return error;
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
namespace Opm {
|
||||
|
||||
std::string
|
||||
correct_parameter_tag(const ParameterMapItem& item)
|
||||
{
|
||||
std::string tag = item.getTag();
|
||||
if (tag != ID_xmltag__param) {
|
||||
std::string error = "The XML tag was '" +
|
||||
tag + "' but should be '" +
|
||||
ID_xmltag__param + "'.\n";
|
||||
return error;
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
std::string
|
||||
correct_type(const Parameter& parameter,
|
||||
const std::string& param_type)
|
||||
{
|
||||
std::string type = parameter.getType();
|
||||
if (type != param_type && type != ID_param_type__cmdline) {
|
||||
std::string error = "The data was of type '" + type +
|
||||
"' but should be of type '" +
|
||||
param_type + "'.\n";
|
||||
return error;
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
int ParameterMapItemTrait<int>::
|
||||
convert(const ParameterMapItem& item,
|
||||
std::string& conversion_error,
|
||||
const bool)
|
||||
{
|
||||
conversion_error = correct_parameter_tag(item);
|
||||
if (!conversion_error.empty()) {
|
||||
return 0;
|
||||
}
|
||||
const Parameter& parameter = dynamic_cast<const Parameter&>(item);
|
||||
conversion_error = correct_type(parameter, ID_param_type__int);
|
||||
if (!conversion_error.empty()) {
|
||||
return 0;
|
||||
}
|
||||
std::stringstream stream;
|
||||
stream << parameter.getValue();
|
||||
int value;
|
||||
stream >> value;
|
||||
if (stream.fail()) {
|
||||
conversion_error = "Conversion to '" +
|
||||
ID_param_type__int +
|
||||
"' failed. Data was '" +
|
||||
parameter.getValue() + "'.\n";
|
||||
return 0;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
double ParameterMapItemTrait<double>::
|
||||
convert(const ParameterMapItem& item,
|
||||
std::string& conversion_error,
|
||||
const bool)
|
||||
{
|
||||
conversion_error = correct_parameter_tag(item);
|
||||
if (!conversion_error.empty()) {
|
||||
return 0.0;
|
||||
}
|
||||
const Parameter& parameter = dynamic_cast<const Parameter&>(item);
|
||||
conversion_error = correct_type(parameter, ID_param_type__float);
|
||||
if (!conversion_error.empty()) {
|
||||
return 0.0;
|
||||
}
|
||||
std::stringstream stream;
|
||||
stream << parameter.getValue();
|
||||
double value;
|
||||
stream >> value;
|
||||
if (stream.fail()) {
|
||||
conversion_error = "Conversion to '" +
|
||||
ID_param_type__float +
|
||||
"' failed. Data was '" +
|
||||
parameter.getValue() + "'.\n";
|
||||
return 0.0;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
bool ParameterMapItemTrait<bool>::
|
||||
convert(const ParameterMapItem& item,
|
||||
std::string& conversion_error,
|
||||
const bool)
|
||||
{
|
||||
conversion_error = correct_parameter_tag(item);
|
||||
if (!conversion_error.empty()) {
|
||||
return false;
|
||||
}
|
||||
const Parameter& parameter = dynamic_cast<const Parameter&>(item);
|
||||
conversion_error = correct_type(parameter, ID_param_type__bool);
|
||||
if (!conversion_error.empty()) {
|
||||
return false;
|
||||
}
|
||||
if (parameter.getValue() == ID_true) {
|
||||
return true;
|
||||
} else if (parameter.getValue() == ID_false) {
|
||||
return false;
|
||||
} else {
|
||||
conversion_error = "Conversion failed. Data was '" +
|
||||
parameter.getValue() +
|
||||
"', but should be one of '" +
|
||||
ID_true + "' or '" + ID_false + "'.\n";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
std::string ParameterMapItemTrait<std::string>::
|
||||
convert(const ParameterMapItem& item,
|
||||
std::string& conversion_error,
|
||||
const bool)
|
||||
{
|
||||
conversion_error = correct_parameter_tag(item);
|
||||
if (!conversion_error.empty()) {
|
||||
return "";
|
||||
}
|
||||
const Parameter& parameter = dynamic_cast<const Parameter&>(item);
|
||||
conversion_error = correct_type(parameter, ID_param_type__string);
|
||||
if (!conversion_error.empty()) {
|
||||
return "";
|
||||
}
|
||||
return parameter.getValue();
|
||||
}
|
||||
|
||||
std::string
|
||||
correct_type(const Parameter& parameter,
|
||||
const std::string& param_type)
|
||||
{
|
||||
std::string type = parameter.getType();
|
||||
if ( (type != param_type) &&
|
||||
(type != ID_param_type__cmdline) ) {
|
||||
std::string error = "The data was of type '" + type +
|
||||
"' but should be of type '" +
|
||||
param_type + "'.\n";
|
||||
return error;
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
} // namespace Opm
|
||||
|
Loading…
Reference in New Issue
Block a user