Parameter.hpp
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 
42 #include <opm/core/utility/parameters/ParameterMapItem.hpp>
43 #include <opm/core/utility/parameters/ParameterStrings.hpp>
44 
45 namespace Opm {
48  class Parameter : public ParameterMapItem {
49  public:
52  virtual ~Parameter() {}
56  virtual std::string getTag() const {return ID_xmltag__param;}
60  Parameter(const std::string& value, const std::string& type)
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 
79  std::string correct_parameter_tag(const ParameterMapItem& item);
80  std::string correct_type(const Parameter& parameter,
81  const std::string& type);
82 
88  template<>
89  struct ParameterMapItemTrait<int> {
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 '" +
109  ID_param_type__int +
110  "' failed. Data was '" +
111  parameter.getValue() + "'.\n";
112  return 0;
113  }
114  return value;
115  }
116  static std::string type() {return ID_param_type__int;}
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 '" +
145  ID_param_type__float +
146  "' failed. Data was '" +
147  parameter.getValue() + "'.\n";
148  return 0.0;
149  }
150  return value;
151  }
152  static std::string type() {return ID_param_type__float;}
153  };
154 
160  template<>
161  struct ParameterMapItemTrait<bool> {
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  }
187  static std::string type() {return ID_param_type__bool;}
188  };
189 
195  template<>
196  struct ParameterMapItemTrait<std::string> {
197  static std::string convert(const ParameterMapItem& item,
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  }
212  static std::string type() {return ID_param_type__string;}
213  };
214 } // namespace Opm
215 #endif // OPM_PARAMETER_HPP
virtual std::string getTag() const
Definition: Parameter.hpp:56
virtual ~Parameter()
Definition: Parameter.hpp:52
Definition: ParameterMapItem.hpp:64
The parameter handlig system is structured as a tree, where each node inhertis from ParameterMapItem...
Definition: ParameterMapItem.hpp:47
Definition: AnisotropicEikonal.cpp:446
std::string getType() const
Definition: Parameter.hpp:69
Definition: Parameter.hpp:48
std::string getValue() const
Definition: Parameter.hpp:65
Parameter(const std::string &value, const std::string &type)
Definition: Parameter.hpp:60