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
|
#define OPM_PARAMETER_HEADER
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
#include <opm/common/utility/parameters/ParameterMapItem.hpp>
|
#include <opm/common/utility/parameters/ParameterMapItem.hpp>
|
||||||
#include <opm/common/utility/parameters/ParameterStrings.hpp>
|
#include <opm/common/utility/parameters/ParameterStrings.hpp>
|
||||||
@ -89,30 +88,8 @@ namespace Opm {
|
|||||||
struct ParameterMapItemTrait<int> {
|
struct ParameterMapItemTrait<int> {
|
||||||
static int convert(const ParameterMapItem& item,
|
static int convert(const ParameterMapItem& item,
|
||||||
std::string& conversion_error,
|
std::string& conversion_error,
|
||||||
const bool)
|
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;
|
|
||||||
}
|
|
||||||
static std::string type() {return ID_param_type__int;}
|
static std::string type() {return ID_param_type__int;}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -125,30 +102,8 @@ namespace Opm {
|
|||||||
struct ParameterMapItemTrait<double> {
|
struct ParameterMapItemTrait<double> {
|
||||||
static double convert(const ParameterMapItem& item,
|
static double convert(const ParameterMapItem& item,
|
||||||
std::string& conversion_error,
|
std::string& conversion_error,
|
||||||
const bool)
|
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;
|
|
||||||
}
|
|
||||||
static std::string type() {return ID_param_type__float;}
|
static std::string type() {return ID_param_type__float;}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -161,29 +116,8 @@ namespace Opm {
|
|||||||
struct ParameterMapItemTrait<bool> {
|
struct ParameterMapItemTrait<bool> {
|
||||||
static bool convert(const ParameterMapItem& item,
|
static bool convert(const ParameterMapItem& item,
|
||||||
std::string& conversion_error,
|
std::string& conversion_error,
|
||||||
const bool)
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
static std::string type() {return ID_param_type__bool;}
|
static std::string type() {return ID_param_type__bool;}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -196,19 +130,8 @@ namespace Opm {
|
|||||||
struct ParameterMapItemTrait<std::string> {
|
struct ParameterMapItemTrait<std::string> {
|
||||||
static std::string convert(const ParameterMapItem& item,
|
static std::string convert(const ParameterMapItem& item,
|
||||||
std::string& conversion_error,
|
std::string& conversion_error,
|
||||||
const bool)
|
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();
|
|
||||||
}
|
|
||||||
static std::string type() {return ID_param_type__string;}
|
static std::string type() {return ID_param_type__string;}
|
||||||
};
|
};
|
||||||
} // namespace Opm
|
} // namespace Opm
|
||||||
|
@ -36,37 +36,140 @@
|
|||||||
#if HAVE_CONFIG_H
|
#if HAVE_CONFIG_H
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#endif
|
#endif
|
||||||
#include <string>
|
|
||||||
#include <opm/common/utility/parameters/Parameter.hpp>
|
#include <opm/common/utility/parameters/Parameter.hpp>
|
||||||
|
|
||||||
namespace Opm {
|
#include <sstream>
|
||||||
std::string
|
#include <string>
|
||||||
correct_parameter_tag(const ParameterMapItem& item)
|
|
||||||
{
|
namespace Opm {
|
||||||
std::string tag = item.getTag();
|
|
||||||
if (tag != ID_xmltag__param) {
|
std::string
|
||||||
std::string error = "The XML tag was '" +
|
correct_parameter_tag(const ParameterMapItem& item)
|
||||||
tag + "' but should be '" +
|
{
|
||||||
ID_xmltag__param + "'.\n";
|
std::string tag = item.getTag();
|
||||||
return error;
|
if (tag != ID_xmltag__param) {
|
||||||
} else {
|
std::string error = "The XML tag was '" +
|
||||||
return "";
|
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
|
} // namespace Opm
|
||||||
|
Loading…
Reference in New Issue
Block a user