MapValue.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 MapValue.hpp
35 
36  @author Jean-Christophe FABRE <jean-christophe.fabre@inra.fr>
37 */
38 
39 
40 #ifndef __OPENFLUID_CORE_MAPVALUE_HPP__
41 #define __OPENFLUID_CORE_MAPVALUE_HPP__
42 
43 
44 #include <map>
45 #include <memory>
46 
54 #include <openfluid/dllexport.hpp>
55 
56 
57 namespace openfluid { namespace core {
58 
59 /**
60  MapValue is a container for a key => value map,
61  where keys are strings and values can be any type derived from openfluid::core::Value.\n
62 
63 \see Value
64 
65 \n
66 
67 <I>Example : declaration</I>
68 @code
69  // declaration of a MapValue, empty by default
70  openfluid::core::MapValue Val1;
71 @endcode
72 
73 
74 <I>Example : setting the contained values</I>
75 @code
76  // using the generic set method (notice the new operator)
77  Val1.set("myvalue1",new openfluid::core::DoubleValue(18.05));
78 
79  // using a specific set method
80  Val1.setDoubleValue("myvalue2",openfluid::core::DoubleValue(0.005));
81 
82  // using a specific set method
83  Val1.setMatrixValue("myvalue3",openfluid::core::MatrixValue(3,3,1.99));
84 @endcode
85 
86 
87 <I>Example : getting the contained values</I>
88 @code
89  openfluid::core::DoubleValue Tmp1;
90  double DblTmp1;
91 
92  // using the generic get method
93  Tmp1 = Val1.get("myvalue1").asDoubleValue();
94 
95  // using specific get methods
96  Tmp1 = Val1.getDoubleValue("myvalue1");
97  DblTmp1 = Val1.getDouble("myvalue1");
98 
99  // or using the [] operator
100  Tmp1 = Val1["myvalue1"].asDoubleValue();
101 @endcode
102 
103 
104 <I>Example : testing the contained elements</I>
105 @code
106  // testing if a key exist
107  Val1.isKeyExist("myvalue1"); // true in this case;
108 
109  // testing if a key exist and the contained value type
110  Val1.isKeyExist("myvalue2") && Val1["myvalue2"].getType() == openfluid::core::Value::BOOLEAN; // false in this case
111 @endcode
112 
113 
114 <I>Example : conversion from string</I>
115 @code
116  openfluid::core::StringValue StringVal;
117  openfluid::core::MapValue Val2;
118 
119  // to MapValue, using a string values separator
120  StringVal.set("{\"myvalue1\":toto,\"myvalue2\"=12.56,\"myvalue3\"=17,\"myvalue3\"=false");
121  StringVal.toMapValue(Val2);
122 
123  // all values are stored as strings, that can be converted to other types
124  openfluid::core::IntegerValue TmpInt;
125  Val2.get("myvalue3").asStringValue().toIntegerValue(TmpInt);
126 @endcode
127 
128 
129 <I>Example : conversion to string</I>
130 @code
131  std::string StdStrVal = Val1.toString();
132 @endcode
133 */
135 {
136  public:
137 
138  typedef std::map<std::string,std::shared_ptr<Value> > Map_t;
139 
140  typedef Map_t::iterator iterator;
141 
142  typedef Map_t::const_iterator const_iterator;
143 
144 
145  private:
146 
147  Map_t m_Value;
148 
149 
150  public:
151 
152  /**
153  Default constructor
154  */
156  { }
157 
158  /**
159  Copy constructor
160  */
161  MapValue(const MapValue& Val);
162 
163  MapValue(const Map_t& Val) : CompoundValue(), m_Value(Val)
164  { }
165 
166  ~MapValue();
167 
168  Value& operator =(const Value& Other);
169 
170 
171  inline Type getType() const
172  {
173  return Value::MAP;
174  }
175 
176  Value* clone() const
177  {
178  return new MapValue(*this);
179  }
180 
181  void writeToStream(std::ostream& OutStm) const;
182 
183  void writeQuotedToStream(std::ostream& OutStm) const
184  {
185  writeToStream(OutStm);
186  }
187 
188  /**
189  Sets a new value at the given key
190  @param[in] Key the key to add
191  @param[in] Element the element to add, must be derived from openfluid::core::Value
192  */
193  void set(const std::string& Key, Value* Element);
194 
195  /**
196  Sets a new double value at the given key
197  @param[in] Key the key to add
198  @param[in] Val the value to add
199  */
200  inline void setDouble(const std::string& Key, const double& Val)
201  {
202  set(Key,new DoubleValue(Val));
203  }
204 
205  /**
206  Sets a new long value at the given key
207  @param[in] Key the key to add
208  @param[in] Val the value to add
209 
210  */
211  inline void setInteger(const std::string& Key, const long& Val)
212  {
213  set(Key,new IntegerValue(Val));
214  }
215 
216  /**
217  Sets a new boolean value at the given key
218  @param[in] Key the key to add
219  @param[in] Val the value to add
220  */
221  inline void setBoolean(const std::string& Key, const bool& Val)
222  {
223  set(Key,new BooleanValue(Val));
224  }
225 
226  /**
227  Sets a new string value at the given key
228  @param[in] Key the key to add
229  @param[in] Val the value to add
230  */
231  inline void setString(const std::string& Key, const std::string& Val)
232  {
233  set(Key,new StringValue(Val));
234  }
235 
236  /**
237  Sets a new VectorValue value at the given key
238  @param[in] Key the key to add
239  @param[in] Val the value to add
240  */
241  inline void setVectorValue(const std::string& Key, const VectorValue& Val)
242  {
243  set(Key,new VectorValue(Val));
244  }
245 
246  /**
247  Sets a new MatrixValue value at the given key
248  @param[in] Key the key to add
249  @param[in] Val the value to add
250  */
251  inline void setMatrixValue(const std::string& Key, const MatrixValue& Val)
252  {
253  set(Key,new MatrixValue(Val));
254  }
255 
256  /**
257  Sets a new MapValue value at the given key
258  @param[in] Key the key to add
259  @param[in] Val the value to add
260  */
261  inline void setMapValue(const std::string& Key, const MapValue& Val)
262  {
263  set(Key,new MapValue(Val));
264  }
265 
266  /**
267  Operator to get/set a value at a key given between []
268  @return the value at the given key
269  */
270  Value& operator[](const std::string& Key);
271 
272  /**
273  Returns a reference to the value of the map at the given key
274  @param[in] Key the key of the requested value
275  @return the value at the given key
276  */
277  Value& at(const std::string& Key);
278 
279  /**
280  Returns a const reference to the value of the map at the given key
281  @param[in] Key the key of the requested value
282  @return the value at the given key
283  */
284  const Value& at(const std::string& Key) const;
285 
286  /**
287  Returns the double value of the map at the given key
288  @param[in] Key the key of the requested value
289  @return the value at the given key
290  */
291  inline double getDouble(const std::string& Key) const
292  {
293  return at(Key).asDoubleValue().get();
294  }
295 
296  /**
297  Returns the long value of the map at the given key
298  @param[in] Key the key of the requested value
299  @return the value at the given key
300  */
301  inline long getInteger(const std::string& Key) const
302  {
303  return at(Key).asIntegerValue().get();
304  }
305 
306  /**
307  Returns the boolean value of the map at the given key
308  @param[in] Key the key of the requested value
309  @return the value at the given key
310  */
311  inline bool getBoolean(const std::string& Key) const
312  {
313  return at(Key).asBooleanValue().get();
314  }
315 
316  /**
317  Returns the string value of the map at the given key
318  @param[in] Key the key of the requested value
319  @return the value at the given key
320  */
321  inline std::string getString(const std::string& Key) const
322  {
323  return at(Key).asStringValue().get();
324  }
325 
326  /**
327  Returns the VectorValue value of the map at the given key
328  @param[in] Key the key of the requested value
329  @return the value at the given key
330  */
331  inline VectorValue getVectorValue(const std::string& Key) const
332  {
333  return at(Key).asVectorValue();
334  }
335 
336  /**
337  Returns the MatrixValue value of the map at the given key
338  @param[in] Key the key of the requested value
339  @return the value at the given key
340  */
341  inline MatrixValue getMatrixValue(const std::string& Key) const
342  {
343  return at(Key).asMatrixValue();
344  }
345 
346  /**
347  Returns the MapValue value of the map at the given key
348  @param[in] Key the key of the requested value
349  @return the value at the given key
350  */
351  inline MapValue getMapValue(const std::string& Key) const
352  {
353  return at(Key).asMapValue();
354  }
355 
356  /**
357  Removes the value corresponding to the given key
358  @param[in] Key the key to remove
359  */
360  bool remove(const std::string& Key);
361 
362  /**
363  Returns the size of the map
364  @return size of the map
365  */
366  inline unsigned long getSize() const
367  {
368  return m_Value.size();
369  }
370 
371  /**
372  Returns the size of the map
373  @return size of the map
374  */
375  unsigned long size() const
376  {
377  return m_Value.size();
378  }
379 
380  /**
381  Checks if the given key exists
382  @param[in] Key the key to check
383  @return true if the given key is present
384  */
385  inline bool isKeyExist(const std::string& Key) const
386  {
387  return (m_Value.find(Key) != m_Value.end());
388  }
389 
390  /**
391  Returns the list of keys of the map
392  @return a std::vector of std::string containing the keys of the map
393  */
394  std::vector<std::string> getKeys() const;
395 
396  /**
397  Clears the map by removing all values
398  */
399  void clear();
400 
401  /**
402  Returns an iterator referring to the first element in the map
403  @return an iterator to the first element in the map
404  */
405  inline iterator begin()
406  {
407  return m_Value.begin();
408  }
409 
410  /**
411  Returns a constant iterator referring to the first element in the map
412  @return a constant iterator to the first element in the map
413  */
414  inline const_iterator begin() const
415  {
416  return m_Value.begin();
417  }
418 
419  /**
420  Returns an iterator referring to the past-the-end element in the map
421  @return an iterator to the past-the-end element in the map
422  */
423  inline iterator end()
424  {
425  return m_Value.end();
426  }
427 
428  /**
429  Returns a constant iterator referring to the past-the-end element in the map
430  @return a constant iterator to the past-the-end element in the map
431  */
432  inline const_iterator end() const
433  {
434  return m_Value.end();
435  }
436 
437 };
438 
439 
440 } } // namespaces
441 
442 
443 // =====================================================================
444 // =====================================================================
445 
446 
447 #endif /* __OPENFLUID_CORE_MAPVALUE_HPP__ */
Definition: StringValue.hpp:88
Definition: CompoundValue.hpp:51
Definition: Value.hpp:64
VectorValue getVectorValue(const std::string &Key) const
Definition: MapValue.hpp:331
Definition: IntegerValue.hpp:103
long getInteger(const std::string &Key) const
Definition: MapValue.hpp:301
std::string get() const
Definition: StringValue.hpp:184
Type
Definition: Value.hpp:68
Definition: VectorValue.hpp:118
std::string getString(const std::string &Key) const
Definition: MapValue.hpp:321
const VectorValue & asVectorValue() const
iterator end()
Definition: MapValue.hpp:423
Definition: Value.hpp:68
MapValue getMapValue(const std::string &Key) const
Definition: MapValue.hpp:351
void setDouble(const std::string &Key, const double &Val)
Definition: MapValue.hpp:200
std::map< std::string, std::shared_ptr< Value > > Map_t
Definition: MapValue.hpp:138
void setVectorValue(const std::string &Key, const VectorValue &Val)
Definition: MapValue.hpp:241
Value * clone() const
Definition: MapValue.hpp:176
MatrixValue getMatrixValue(const std::string &Key) const
Definition: MapValue.hpp:341
unsigned long getSize() const
Definition: MapValue.hpp:366
const StringValue & asStringValue() const
void setInteger(const std::string &Key, const long &Val)
Definition: MapValue.hpp:211
unsigned long size() const
Definition: MapValue.hpp:375
iterator begin()
Definition: MapValue.hpp:405
const_iterator end() const
Definition: MapValue.hpp:432
void setString(const std::string &Key, const std::string &Val)
Definition: MapValue.hpp:231
bool isKeyExist(const std::string &Key) const
Definition: MapValue.hpp:385
Definition: ApplicationException.hpp:47
long get() const
Definition: IntegerValue.hpp:158
double getDouble(const std::string &Key) const
Definition: MapValue.hpp:291
bool get() const
Definition: BooleanValue.hpp:157
void setBoolean(const std::string &Key, const bool &Val)
Definition: MapValue.hpp:221
void setMapValue(const std::string &Key, const MapValue &Val)
Definition: MapValue.hpp:261
const MatrixValue & asMatrixValue() const
const IntegerValue & asIntegerValue() const
Definition: BooleanValue.hpp:104
Map_t::iterator iterator
Definition: MapValue.hpp:140
const_iterator begin() const
Definition: MapValue.hpp:414
const DoubleValue & asDoubleValue() const
const BooleanValue & asBooleanValue() const
Definition: DoubleValue.hpp:102
Type getType() const
Definition: MapValue.hpp:171
MapValue(const Map_t &Val)
Definition: MapValue.hpp:163
MapValue()
Definition: MapValue.hpp:155
const MapValue & asMapValue() const
Definition: MapValue.hpp:134
Map_t::const_iterator const_iterator
Definition: MapValue.hpp:142
#define OPENFLUID_API
Definition: dllexport.hpp:86
void setMatrixValue(const std::string &Key, const MatrixValue &Val)
Definition: MapValue.hpp:251
double get() const
Definition: DoubleValue.hpp:157
Definition: MatrixValue.hpp:115
void writeQuotedToStream(std::ostream &OutStm) const
Definition: MapValue.hpp:183
bool getBoolean(const std::string &Key) const
Definition: MapValue.hpp:311