Value.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2007, INRA - Montpellier SupAgro
5 
6 
7  == GNU General Public License Usage ==
8 
9  OpenFLUID is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  OpenFLUID is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21 
22 
23  == Other Usage ==
24 
25  Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26  license, and requires a written agreement between You and INRA.
27  Licensees for Other Usage of OpenFLUID may use this file in accordance
28  with the terms contained in the written agreement between You and INRA.
29 
30 */
31 
32 
33 /**
34  @file Value.hpp
35 
36  @author Jean-Christophe FABRE <jean-christophe.fabre@inra.fr>
37  */
38 
39 
40 #ifndef __OPENFLUID_CORE_VALUE_HPP__
41 #define __OPENFLUID_CORE_VALUE_HPP__
42 
43 
44 #include <iostream>
45 
46 #include <openfluid/dllexport.hpp>
48 
49 
50 namespace openfluid { namespace core {
51 
52 class NullValue;
53 class BooleanValue;
54 class DoubleValue;
55 class IntegerValue;
56 class StringValue;
57 
58 class VectorValue;
59 class MatrixValue;
60 class MapValue;
61 class TreeValue;
62 
63 
65 {
66  public:
67 
68  enum Type { NONE, BOOLEAN, INTEGER, DOUBLE, STRING, VECTOR, MATRIX, MAP, TREE, NULLL };
69 
70  Value() = default;
71 
72  virtual ~Value()
73  { }
74 
75  /**
76  Assignment operator
77  */
78  virtual Value& operator =(const Value& /*Other*/)
79  {
80  return *this;
81  }
82 
83  virtual Type getType() const = 0;
84 
85  virtual Value* clone() const
86  {
87  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Value is not cloneable");
88  }
89 
90  virtual bool convert(Value& /*Val*/) const
91  {
92  return false;
93  }
94 
95  virtual bool isSimple() const = 0;
96 
97  virtual bool isCompound() const = 0;
98 
99  virtual void writeToStream(std::ostream& OutStm) const = 0;
100 
101  virtual void writeQuotedToStream(std::ostream& OutStm) const = 0;
102 
103  friend std::ostream& operator<<(std::ostream& OutStm, const Value& Val)
104  {
105  Val.writeToStream(OutStm); return OutStm;
106  }
107 
108  /**
109  Returns true if the Value is a DoubleValue
110  */
111  inline bool isDoubleValue() const
112  {
113  return getType() == Value::DOUBLE;
114  }
115 
116  /**
117  Returns the value as a constant DoubleValue if the value is of the DoubleValue type
118  @return the value as a constant DoubleValue
119  @throw openfluid::base::FrameworkException if the value is not of the DoubleValue type
120  */
121  const DoubleValue& asDoubleValue() const;
122 
123  /**
124  Returns the value as a DoubleValue if the value is of the DoubleValue type
125  @return the value as a DoubleValue
126  @throw openfluid::base::FrameworkException if the value is not of the DoubleValue type
127  */
128  DoubleValue& asDoubleValue();
129 
130  /**
131  Returns true if the value is an IntegerValue
132  */
133  inline bool isIntegerValue() const
134  {
135  return getType() == Value::INTEGER;
136  }
137 
138  /**
139  Returns the value as a constant IntegerValue if the value is of the IntegerValue type
140  @return the value as a constant IntegerValue
141  @throw openfluid::base::FrameworkException if the value is not of the IntegerValue type
142  */
143  const IntegerValue& asIntegerValue() const;
144 
145  /**
146  Returns the value as a IntegerValue if the value is of the IntegerValue type
147  @return the value as a IntegerValue
148  @throw openfluid::base::FrameworkException if the value is not of the IntegerValue type
149  */
150  IntegerValue& asIntegerValue();
151 
152  /**
153  Returns true if the value is a BooleanValue
154  */
155  inline bool isBooleanValue() const
156  {
157  return getType() == Value::BOOLEAN;
158  }
159 
160  /**
161  Returns the value as a constant BooleanValue if the value is of the BooleanValue type
162  @return the value as a constant BooleanValue
163  @throw openfluid::base::FrameworkException if the value is not of the BooleanValue type
164  */
165  const BooleanValue& asBooleanValue() const;
166 
167  /**
168  Returns the value as a BooleanValue if the value is of the BooleanValue type
169  @return the value as a BooleanValue
170  @throw openfluid::base::FrameworkException if the value is not of the BooleanValue type
171  */
172  BooleanValue& asBooleanValue();
173 
174  /**
175  Returns true if the value is a StringValue
176  */
177  inline bool isStringValue() const
178  {
179  return getType() == Value::STRING;
180  }
181 
182  /**
183  Returns the value as a constant StringValue if the value is of the StringValue type
184  @return the value as a constant StringValue
185  @throw openfluid::base::FrameworkException if the value is not of the StringValue type
186  */
187  const StringValue& asStringValue() const;
188 
189  /**
190  Returns the value as a StringValue if the value is of the StringValue type
191  @return the value as a StringValue
192  @throw openfluid::base::FrameworkException if the value is not of the StringValue type
193  */
194  StringValue& asStringValue();
195 
196  /**
197  Returns true if the value is a NullValue
198  */
199  inline bool isNullValue() const
200  {
201  return getType() == Value::NULLL;
202  }
203 
204  /**
205  Returns the value as a constant NullValue if the value is of the NullValue type
206  @return the value as a constant NullValue
207  @throw openfluid::base::FrameworkException if the value is not of the NullValue type
208  */
209  const NullValue& asNullValue() const;
210 
211  /**
212  Returns the value as a NullValue if the value is of the NullValue type
213  @return the value as a NullValue
214  @throw openfluid::base::FrameworkException if the value is not of the NullValue type
215  */
216  NullValue& asNullValue();
217 
218  /**
219  Returns true if the value is a VectorValue
220  */
221  inline bool isVectorValue() const
222  {
223  return getType() == Value::VECTOR;
224  }
225 
226  /**
227  Returns the value as a constant VectorValue if the value is of the VectorValue type
228  @return the value as a constant VectorValue
229  @throw openfluid::base::FrameworkException if the value is not of the VectorValue type
230  */
231  const VectorValue& asVectorValue() const;
232 
233  /**
234  Returns the value as a VectorValue if the value is of the VectorValue type
235  @return the value as a VectorValue
236  @throw openfluid::base::FrameworkException if the value is not of the VectorValue type
237  */
238  VectorValue& asVectorValue();
239 
240  /**
241  Returns true if the value is a MatrixValue
242  */
243  inline bool isMatrixValue() const
244  {
245  return getType() == Value::MATRIX;
246  }
247 
248  /**
249  Returns the value as a constant MatrixValue if the value is of the MatrixValue type
250  @return the value as a constant MatrixValue
251  @throw openfluid::base::FrameworkException if the value is not of the MatrixValue type
252  */
253  const MatrixValue& asMatrixValue() const;
254 
255  /**
256  Returns the value as a MatrixValue if the value is of the MatrixValue type
257  @return the value as a MatrixValue
258  @throw openfluid::base::FrameworkException if the value is not of the MatrixValue type
259  */
260  MatrixValue& asMatrixValue();
261 
262  /**
263  Returns true if the value is a MapValue
264  */
265  inline bool isMapValue() const
266  {
267  return getType() == Value::MAP;
268  }
269 
270  /**
271  Returns the value as a constant MapValue if the value is of the MapValue type
272  @return the value as a constant MapValue
273  @throw openfluid::base::FrameworkException if the value is not of the MapValue type
274  */
275  const MapValue& asMapValue() const;
276 
277  /**
278  Returns the value as a MapValue if the value is of the MapValue type
279  @return the value as a MapValue
280  @throw openfluid::base::FrameworkException if the value is not of the MapValue type
281  */
282  MapValue& asMapValue();
283 
284  /**
285  Returns true if the value is a TreeValue
286  */
287  inline bool isTreeValue() const
288  {
289  return getType() == Value::TREE;
290  }
291 
292  /**
293  Returns the value as a constant TreeValue if the value is of the TreeValue type
294  @return the value as a constant TreeValue
295  @throw openfluid::base::FrameworkException if the value is not of the TreeValue type
296  */
297  const TreeValue& asTreeValue() const;
298 
299  /**
300  Returns the value as a TreeValue if the value is of the TreeValue type
301  @return the value as a TreeValue
302  @throw openfluid::base::FrameworkException if the value is not of the TreeValue type
303  */
304  TreeValue& asTreeValue();
305 
306  /**
307  Returns the value as a string
308  */
309  std::string toString() const;
310 
311  /**
312  Gets the value type ciorresponding to the name of the type
313  @param[in] ValueTypeString The type name as a string
314  @param[out] ValueType The value type
315  @return false if the type name does not exist or is misspelled, true otherwise
316  */
317  static bool getValueTypeFromString(const std::string& ValueTypeString, Value::Type& ValueType);
318 
319  /**
320  Returns the name of the type corresponding to the given type
321  @param[in] ValueType The value type
322  @return the type name
323  */
324  static std::string getStringFromValueType(const Value::Type ValueType);
325 
326 };
327 
328 
329 } } // namespaces
330 
331 
332 #endif /* __OPENFLUID_CORE_VALUE_HPP__ */
Definition: StringValue.hpp:88
bool isDoubleValue() const
Definition: Value.hpp:111
Definition: Value.hpp:64
bool isTreeValue() const
Definition: Value.hpp:287
bool isStringValue() const
Definition: Value.hpp:177
Definition: IntegerValue.hpp:103
virtual bool convert(Value &) const
Definition: Value.hpp:90
Definition: Value.hpp:68
Type
Definition: Value.hpp:68
Definition: VectorValue.hpp:118
virtual ~Value()
Definition: Value.hpp:72
bool isVectorValue() const
Definition: Value.hpp:221
bool isNullValue() const
Definition: Value.hpp:199
Definition: Value.hpp:68
Definition: TreeValue.hpp:54
bool isMapValue() const
Definition: Value.hpp:265
virtual Value * clone() const
Definition: Value.hpp:85
Definition: NullValue.hpp:56
Definition: Value.hpp:68
bool isIntegerValue() const
Definition: Value.hpp:133
bool isBooleanValue() const
Definition: Value.hpp:155
Definition: Value.hpp:68
Definition: FrameworkException.hpp:50
Definition: ApplicationException.hpp:47
virtual void writeToStream(std::ostream &OutStm) const =0
Definition: BooleanValue.hpp:104
Definition: Value.hpp:68
Definition: Value.hpp:68
Definition: DoubleValue.hpp:102
Definition: MapValue.hpp:134
Definition: Value.hpp:68
#define OPENFLUID_API
Definition: dllexport.hpp:86
bool isMatrixValue() const
Definition: Value.hpp:243
Definition: Value.hpp:68
Definition: MatrixValue.hpp:115
Definition: Value.hpp:68
friend std::ostream & operator<<(std::ostream &OutStm, const Value &Val)
Definition: Value.hpp:103