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 
45 namespace Opm {
47  namespace parameter {
50  class Parameter : public ParameterMapItem {
51  public:
54  virtual ~Parameter() {}
58  virtual std::string getTag() const {return ID_xmltag__param;}
62  Parameter(const std::string& value, const std::string& type)
63  : value_(value), type_(type) {}
67  std::string getValue() const {return value_;}
71  std::string getType() const {return type_;}
72  private:
73  std::string value_;
74  std::string type_;
75  };
76 
81  std::string correct_parameter_tag(const ParameterMapItem& item);
82  std::string correct_type(const Parameter& parameter,
83  const std::string& type);
84 
90  template<>
91  struct ParameterMapItemTrait<int> {
92  static int convert(const ParameterMapItem& item,
93  std::string& conversion_error,
94  const bool)
95  {
96  conversion_error = correct_parameter_tag(item);
97  if (conversion_error != "") {
98  return 0;
99  }
100  const Parameter& parameter = dynamic_cast<const Parameter&>(item);
101  conversion_error = correct_type(parameter, ID_param_type__int);
102  if (conversion_error != "") {
103  return 0;
104  }
105  std::stringstream stream;
106  stream << parameter.getValue();
107  int value;
108  stream >> value;
109  if (stream.fail()) {
110  conversion_error = "Conversion to '" +
112  "' failed. Data was '" +
113  parameter.getValue() + "'.\n";
114  return 0;
115  }
116  return value;
117  }
118  static std::string type() {return ID_param_type__int;}
119  };
120 
126  template<>
127  struct ParameterMapItemTrait<double> {
128  static double convert(const ParameterMapItem& item,
129  std::string& conversion_error,
130  const bool)
131  {
132  conversion_error = correct_parameter_tag(item);
133  if (conversion_error != "") {
134  return 0.0;
135  }
136  const Parameter& parameter = dynamic_cast<const Parameter&>(item);
137  conversion_error = correct_type(parameter, ID_param_type__float);
138  if (conversion_error != "") {
139  return 0.0;
140  }
141  std::stringstream stream;
142  stream << parameter.getValue();
143  double value;
144  stream >> value;
145  if (stream.fail()) {
146  conversion_error = "Conversion to '" +
148  "' failed. Data was '" +
149  parameter.getValue() + "'.\n";
150  return 0.0;
151  }
152  return value;
153  }
154  static std::string type() {return ID_param_type__float;}
155  };
156 
162  template<>
163  struct ParameterMapItemTrait<bool> {
164  static bool convert(const ParameterMapItem& item,
165  std::string& conversion_error,
166  const bool)
167  {
168  conversion_error = correct_parameter_tag(item);
169  if (conversion_error != "") {
170  return false;
171  }
172  const Parameter& parameter = dynamic_cast<const Parameter&>(item);
173  conversion_error = correct_type(parameter, ID_param_type__bool);
174  if (conversion_error != "") {
175  return false;
176  }
177  if (parameter.getValue() == ID_true) {
178  return true;
179  } else if (parameter.getValue() == ID_false) {
180  return false;
181  } else {
182  conversion_error = "Conversion failed. Data was '" +
183  parameter.getValue() +
184  "', but should be one of '" +
185  ID_true + "' or '" + ID_false + "'.\n";
186  return false;
187  }
188  }
189  static std::string type() {return ID_param_type__bool;}
190  };
191 
197  template<>
198  struct ParameterMapItemTrait<std::string> {
199  static std::string convert(const ParameterMapItem& item,
200  std::string& conversion_error,
201  const bool)
202  {
203  conversion_error = correct_parameter_tag(item);
204  if (conversion_error != "") {
205  return "";
206  }
207  const Parameter& parameter = dynamic_cast<const Parameter&>(item);
208  conversion_error = correct_type(parameter, ID_param_type__string);
209  if (conversion_error != "") {
210  return "";
211  }
212  return parameter.getValue();
213  }
214  static std::string type() {return ID_param_type__string;}
215  };
216  } // namespace parameter
217 } // namespace Opm
218 #endif // OPM_PARAMETER_HPP
static std::string type()
Definition: Parameter.hpp:118
Parameter(const std::string &value, const std::string &type)
Definition: Parameter.hpp:62
virtual ~Parameter()
Definition: Parameter.hpp:54
Definition: AnisotropicEikonal.hpp:43
std::string getValue() const
Definition: Parameter.hpp:67
virtual std::string getTag() const
Definition: Parameter.hpp:58
static int convert(const ParameterMapItem &item, std::string &conversion_error, const bool)
Definition: Parameter.hpp:92
std::string correct_type(const Parameter &parameter, const std::string &type)
static std::string type()
Definition: Parameter.hpp:214
Definition: Parameter.hpp:50
const std::string ID_param_type__int
Definition: ParameterStrings.hpp:56
const std::string ID_false
Definition: ParameterStrings.hpp:44
const std::string ID_param_type__bool
Definition: ParameterStrings.hpp:55
Definition: ParameterMapItem.hpp:48
const std::string ID_param_type__float
Definition: ParameterStrings.hpp:57
STL namespace.
std::string correct_parameter_tag(const ParameterMapItem &item)
static std::string type()
Definition: Parameter.hpp:154
static std::string convert(const ParameterMapItem &item, std::string &conversion_error, const bool)
Definition: Parameter.hpp:199
const std::string ID_xmltag__param
Definition: ParameterStrings.hpp:47
Definition: ParameterMapItem.hpp:65
static double convert(const ParameterMapItem &item, std::string &conversion_error, const bool)
Definition: Parameter.hpp:128
const std::string ID_true
Definition: ParameterStrings.hpp:43
std::string getType() const
Definition: Parameter.hpp:71
static std::string type()
Definition: Parameter.hpp:189
const std::string ID_param_type__string
Definition: ParameterStrings.hpp:58
static bool convert(const ParameterMapItem &item, std::string &conversion_error, const bool)
Definition: Parameter.hpp:164