Parameter.hpp
Go to the documentation of this file.
1//===========================================================================
2//
3// File: Parameter.hpp
4//
5// Created: Tue Jun 2 16:00:21 2009
6//
7// Author(s): Bård Skaflestad <bard.skaflestad@sintef.no>
8// Atgeirr F Rasmussen <atgeirr@sintef.no>
9//
10// $Date$
11//
12// $Revision$
13//
14//===========================================================================
15
16/*
17 Copyright 2009, 2010 SINTEF ICT, Applied Mathematics.
18 Copyright 2009, 2010 Statoil ASA.
19
20 This file is part of the Open Porous Media project (OPM).
21
22 OPM is free software: you can redistribute it and/or modify
23 it under the terms of the GNU General Public License as published by
24 the Free Software Foundation, either version 3 of the License, or
25 (at your option) any later version.
26
27 OPM is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
31
32 You should have received a copy of the GNU General Public License
33 along with OPM. If not, see <http://www.gnu.org/licenses/>.
34*/
35
36#ifndef OPM_PARAMETER_HEADER
37#define OPM_PARAMETER_HEADER
38
39#include <string>
40#include <sstream>
41
44
45namespace Opm {
48 class Parameter : public ParameterMapItem {
49 public:
52 virtual ~Parameter() {}
56 virtual std::string getTag() const {return ID_xmltag__param;}
61 : value_(value), type_(type) {}
65 std::string getValue() const {return value_;}
69 std::string getType() const {return type_;}
70 private:
71 std::string value_;
72 std::string type_;
73 };
74
81 const std::string& type);
82
88 template<>
90 static int convert(const ParameterMapItem& item,
91 std::string& conversion_error,
92 const bool)
93 {
94 conversion_error = correct_parameter_tag(item);
95 if (conversion_error != "") {
96 return 0;
97 }
98 const Parameter& parameter = dynamic_cast<const Parameter&>(item);
99 conversion_error = correct_type(parameter, ID_param_type__int);
100 if (conversion_error != "") {
101 return 0;
102 }
103 std::stringstream stream;
104 stream << parameter.getValue();
105 int value;
106 stream >> value;
107 if (stream.fail()) {
108 conversion_error = "Conversion to '" +
110 "' failed. Data was '" +
111 parameter.getValue() + "'.\n";
112 return 0;
113 }
114 return value;
115 }
117 };
118
124 template<>
125 struct ParameterMapItemTrait<double> {
126 static double convert(const ParameterMapItem& item,
127 std::string& conversion_error,
128 const bool)
129 {
130 conversion_error = correct_parameter_tag(item);
131 if (conversion_error != "") {
132 return 0.0;
133 }
134 const Parameter& parameter = dynamic_cast<const Parameter&>(item);
135 conversion_error = correct_type(parameter, ID_param_type__float);
136 if (conversion_error != "") {
137 return 0.0;
138 }
139 std::stringstream stream;
140 stream << parameter.getValue();
141 double value;
142 stream >> value;
143 if (stream.fail()) {
144 conversion_error = "Conversion to '" +
146 "' failed. Data was '" +
147 parameter.getValue() + "'.\n";
148 return 0.0;
149 }
150 return value;
151 }
153 };
154
160 template<>
162 static bool convert(const ParameterMapItem& item,
163 std::string& conversion_error,
164 const bool)
165 {
166 conversion_error = correct_parameter_tag(item);
167 if (conversion_error != "") {
168 return false;
169 }
170 const Parameter& parameter = dynamic_cast<const Parameter&>(item);
171 conversion_error = correct_type(parameter, ID_param_type__bool);
172 if (conversion_error != "") {
173 return false;
174 }
175 if (parameter.getValue() == ID_true) {
176 return true;
177 } else if (parameter.getValue() == ID_false) {
178 return false;
179 } else {
180 conversion_error = "Conversion failed. Data was '" +
181 parameter.getValue() +
182 "', but should be one of '" +
183 ID_true + "' or '" + ID_false + "'.\n";
184 return false;
185 }
186 }
188 };
189
195 template<>
198 std::string& conversion_error,
199 const bool)
200 {
201 conversion_error = correct_parameter_tag(item);
202 if (conversion_error != "") {
203 return "";
204 }
205 const Parameter& parameter = dynamic_cast<const Parameter&>(item);
206 conversion_error = correct_type(parameter, ID_param_type__string);
207 if (conversion_error != "") {
208 return "";
209 }
210 return parameter.getValue();
211 }
213 };
214} // namespace Opm
215#endif // OPM_PARAMETER_HPP
cJSON * item
Definition: cJSON.h:218
const char *const string
Definition: cJSON.h:170
Definition: Parameter.hpp:48
virtual ~Parameter()
Definition: Parameter.hpp:52
std::string getValue() const
Definition: Parameter.hpp:65
virtual std::string getTag() const
Definition: Parameter.hpp:56
Parameter(const std::string &value, const std::string &type)
Definition: Parameter.hpp:60
std::string getType() const
Definition: Parameter.hpp:69
char bool
Definition: msvc_stdbool.h:17
Definition: A.hpp:4
std::string correct_parameter_tag(const ParameterMapItem &item)
const std::string ID_param_type__string
Definition: ParameterStrings.hpp:51
const std::string ID_param_type__int
Definition: ParameterStrings.hpp:49
const std::string ID_true
Definition: ParameterStrings.hpp:42
const std::string ID_xmltag__param
Definition: ParameterStrings.hpp:46
const std::string ID_param_type__float
Definition: ParameterStrings.hpp:50
const std::string ID_false
Definition: ParameterStrings.hpp:43
std::string correct_type(const Parameter &parameter, const std::string &type)
const std::string ID_param_type__bool
Definition: ParameterStrings.hpp:48
T value(details::expression_node< T > *n)
Definition: exprtk.hpp:12955
Definition: ParameterMapItem.hpp:47
static bool convert(const ParameterMapItem &item, std::string &conversion_error, const bool)
Definition: Parameter.hpp:162
static std::string type()
Definition: Parameter.hpp:187
static std::string type()
Definition: Parameter.hpp:152
static double convert(const ParameterMapItem &item, std::string &conversion_error, const bool)
Definition: Parameter.hpp:126
static std::string type()
Definition: Parameter.hpp:116
static int convert(const ParameterMapItem &item, std::string &conversion_error, const bool)
Definition: Parameter.hpp:90
static std::string type()
Definition: Parameter.hpp:212
static std::string convert(const ParameterMapItem &item, std::string &conversion_error, const bool)
Definition: Parameter.hpp:197
Definition: ParameterMapItem.hpp:64