DOLFIN
DOLFIN C++ interface
Parameter.h
1 // Copyright (C) 2009 Anders Logg
2 //
3 // This file is part of DOLFIN.
4 //
5 // DOLFIN is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // DOLFIN is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17 
18 #ifndef __PARAMETER_H
19 #define __PARAMETER_H
20 
21 #include <array>
22 #include <set>
23 #include <sstream>
24 #include <string>
25 #include <boost/blank.hpp>
26 #include <boost/variant.hpp>
27 
28 namespace dolfin
29 {
30 
32 
33  class Parameter
34  {
35  public:
36 
40  template<typename T>
41  Parameter(std::string key, T x) : _value(x), _access_count(0),
42  _change_count(0), _is_set(true), _key(key),
43  _description("missing description") { check_key(key); }
44 
51  Parameter(std::string key, const char* x);
52 
54  enum class Type { Bool, Int, Float, String };
55 
61  Parameter(std::string key, Type ptype);
62 
65  template<typename T>
66  Parameter(std::string key, T min, T max) : _value(T(0)),
67  _range(std::array<T, 2>({{min, max}})), _access_count(0), _change_count(0),
68  _is_set(false), _key(key), _description("missing description")
69  { check_key(key); }
70 
72  Parameter(std::string key, std::set<std::string> range) : _value(std::string("")),
73  _range(range), _access_count(0), _change_count(0), _is_set(false),
74  _key(key), _description("missing description") { check_key(key); }
75 
77  Parameter(const Parameter&) = default;
78 
80  Parameter(Parameter&&) = default;
81 
83  virtual ~Parameter();
84 
86  Parameter& operator= (const Parameter &) = default;
87 
90  std::string key() const;
91 
94  std::string description() const;
95 
98  bool is_set() const;
99 
101  void reset();
102 
105  std::size_t access_count() const;
106 
109  std::size_t change_count() const;
110 
115  void set_range(int min_value, int max_value);
116 
121  void set_range(double min_value, double max_value);
122 
126  void set_range(std::set<std::string> range);
127 
131  void get_range(int& min_value, int& max_value) const;
132 
136  void get_range(double& min_value, double& max_value) const;
137 
140  void get_range(std::set<std::string>& range) const;
141 
144  const Parameter& operator= (int value);
145 
148  const Parameter& operator= (double value);
149 
152  const Parameter& operator= (std::string value);
153 
156  const Parameter& operator= (const char* value);
157 
160  const Parameter& operator= (bool value);
161 
163  boost::variant<boost::blank, bool, int, double, std::string> value() const;
164 
166  operator int() const;
167 
169  operator std::size_t() const;
170 
172  operator double() const;
173 
175  operator std::string() const;
176 
178  operator bool() const;
179 
181  std::string type_str() const;
182 
184  std::string value_str() const;
185 
187  std::string range_str() const;
188 
190  std::string str() const;
191 
193  static void check_key(std::string key);
194 
195  protected:
196 
197  // Value (0: blank, 1: bool, 2: int, 3: double, 4: std::string)
198  boost::variant<boost::blank, bool, int, double, std::string> _value;
199 
200  // Ranges (0: blank, 1: int, 2: double, 3: std::string)
201  boost::variant<boost::blank, std::array<int, 2>, std::array<double, 2>,
202  std::set<std::string>> _range;
203 
204  // Access count
205  mutable std::size_t _access_count;
206 
207  // Change count
208  std::size_t _change_count;
209 
210  // Whether or not parameter has been set
211  bool _is_set;
212 
213  // Parameter key
214  std::string _key;
215 
216  // Parameter description
217  std::string _description;
218 
219  };
220 }
221 #endif
std::string type_str() const
Return value type string.
Definition: Parameter.cpp:342
Type
Enum for the parameter type.
Definition: Parameter.h:54
std::string range_str() const
Return range string.
Definition: Parameter.cpp:382
Parameter(std::string key, T x)
Definition: Parameter.h:41
Definition: adapt.h:29
std::string str() const
Return short string description.
Definition: Parameter.cpp:420
boost::variant< boost::blank, bool, int, double, std::string > value() const
Return parameter value.
Definition: Parameter.cpp:245
bool is_set() const
Definition: Parameter.cpp:70
std::string value_str() const
Return value string.
Definition: Parameter.cpp:362
void set_range(int min_value, int max_value)
Definition: Parameter.cpp:90
Base class for parameters.
Definition: Parameter.h:33
Parameter & operator=(const Parameter &)=default
Assignment operator.
static void check_key(std::string key)
Check that key name is allowed.
Definition: Parameter.cpp:330
std::string description() const
Definition: Parameter.cpp:65
void get_range(int &min_value, int &max_value) const
Definition: Parameter.cpp:129
Parameter(std::string key, T min, T max)
Definition: Parameter.h:66
std::size_t change_count() const
Definition: Parameter.cpp:85
virtual ~Parameter()
Destructor.
Definition: Parameter.cpp:55
std::string key() const
Definition: Parameter.cpp:60
void reset()
Reset the parameter to empty, so that is_set() returns false.
Definition: Parameter.cpp:75
Parameter(std::string key, std::set< std::string > range)
Create and unset string parameter with set of allowable strings.
Definition: Parameter.h:72
std::size_t access_count() const
Definition: Parameter.cpp:80