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@supagro.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  { return Value::MAP; }
173 
174  Value* clone() const
175  { return new MapValue(*this); }
176 
177  void writeToStream(std::ostream& OutStm) const;
178 
179  void writeQuotedToStream(std::ostream& OutStm) const
180  { writeToStream(OutStm); }
181 
182  /**
183  Sets a new value at the given key
184  @param[in] Key the key to add
185  @param[in] Element the element to add, must be derived from openfluid::core::Value
186  */
187  void set(const std::string& Key, Value* Element);
188 
189  /**
190  Sets a new double value at the given key
191  @param[in] Key the key to add
192  @param[in] Val the value to add
193  */
194  inline void setDouble(const std::string& Key, const double& Val)
195  { set(Key,new DoubleValue(Val)); }
196 
197  /**
198  Sets a new long value at the given key
199  @param[in] Key the key to add
200  @param[in] Val the value to add
201 
202  */
203  inline void setInteger(const std::string& Key, const long& Val)
204  { set(Key,new IntegerValue(Val)); }
205 
206  /**
207  Sets a new boolean value at the given key
208  @param[in] Key the key to add
209  @param[in] Val the value to add
210  */
211  inline void setBoolean(const std::string& Key, const bool& Val)
212  { set(Key,new BooleanValue(Val)); }
213 
214  /**
215  Sets a new string value at the given key
216  @param[in] Key the key to add
217  @param[in] Val the value to add
218  */
219  inline void setString(const std::string& Key, const std::string& Val)
220  { set(Key,new StringValue(Val)); }
221 
222  /**
223  Sets a new VectorValue value at the given key
224  @param[in] Key the key to add
225  @param[in] Val the value to add
226  */
227  inline void setVectorValue(const std::string& Key, const VectorValue& Val)
228  { set(Key,new VectorValue(Val)); }
229 
230  /**
231  Sets a new MatrixValue value at the given key
232  @param[in] Key the key to add
233  @param[in] Val the value to add
234  */
235  inline void setMatrixValue(const std::string& Key, const MatrixValue& Val)
236  { set(Key,new MatrixValue(Val)); }
237 
238  /**
239  Sets a new MapValue value at the given key
240  @param[in] Key the key to add
241  @param[in] Val the value to add
242  */
243  inline void setMapValue(const std::string& Key, const MapValue& Val)
244  { set(Key,new MapValue(Val)); }
245 
246  /**
247  Operator to get/set a value at a key given between []
248  @return the value at the given key
249  */
250  Value& operator[](const std::string& Key);
251 
252  /**
253  Returns a reference to the value of the map at the given key
254  @param[in] Key the key of the requested value
255  @return the value at the given key
256  */
257  Value& at(const std::string& Key);
258 
259  /**
260  Returns a const reference to the value of the map at the given key
261  @param[in] Key the key of the requested value
262  @return the value at the given key
263  */
264  const Value& at(const std::string& Key) const;
265 
266  /**
267  Returns the double value of the map at the given key
268  @param[in] Key the key of the requested value
269  @return the value at the given key
270  */
271  inline double getDouble(const std::string& Key) const
272  { return at(Key).asDoubleValue().get(); }
273 
274  /**
275  Returns the long value of the map at the given key
276  @param[in] Key the key of the requested value
277  @return the value at the given key
278  */
279  inline long getInteger(const std::string& Key) const
280  { return at(Key).asIntegerValue().get(); }
281 
282  /**
283  Returns the boolean value of the map at the given key
284  @param[in] Key the key of the requested value
285  @return the value at the given key
286  */
287  inline bool getBoolean(const std::string& Key) const
288  { return at(Key).asBooleanValue().get(); }
289 
290  /**
291  Returns the string value of the map at the given key
292  @param[in] Key the key of the requested value
293  @return the value at the given key
294  */
295  inline std::string getString(const std::string& Key) const
296  { return at(Key).asStringValue().get(); }
297 
298  /**
299  Returns the VectorValue value of the map at the given key
300  @param[in] Key the key of the requested value
301  @return the value at the given key
302  */
303  inline VectorValue getVectorValue(const std::string& Key) const
304  { return at(Key).asVectorValue(); }
305 
306  /**
307  Returns the MatrixValue 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 MatrixValue getMatrixValue(const std::string& Key) const
312  { return at(Key).asMatrixValue(); }
313 
314  /**
315  Returns the MapValue value of the map at the given key
316  @param[in] Key the key of the requested value
317  @return the value at the given key
318  */
319  inline MapValue getMapValue(const std::string& Key) const
320  { return at(Key).asMapValue(); }
321 
322  /**
323  Removes the value corresponding to the given key
324  @param[in] Key the key to remove
325  */
326  bool remove(const std::string& Key);
327 
328  /**
329  Returns the size of the map
330  @return size of the map
331  */
332  inline unsigned long getSize() const
333  { return m_Value.size(); }
334 
335  /**
336  Returns the size of the map
337  @return size of the map
338  */
339  unsigned long size() const
340  { return m_Value.size(); }
341 
342  /**
343  Checks if the given key exists
344  @param[in] Key the key to check
345  @return true if the given key is present
346  */
347  inline bool isKeyExist(const std::string& Key) const
348  { return (m_Value.find(Key) != m_Value.end()); }
349 
350  /**
351  Returns the list of keys of the map
352  @return a std::vector of std::string containing the keys of the map
353  */
354  std::vector<std::string> getKeys() const;
355 
356  /**
357  Clears the map by removing all values
358  */
359  void clear();
360 
361  /**
362  Returns an iterator referring to the first element in the map
363  @return an iterator to the first element in the map
364  */
365  inline iterator begin()
366  { return m_Value.begin(); }
367 
368  /**
369  Returns a constant iterator referring to the first element in the map
370  @return a constant iterator to the first element in the map
371  */
372  inline const_iterator begin() const
373  { return m_Value.begin(); }
374 
375  /**
376  Returns an iterator referring to the past-the-end element in the map
377  @return an iterator to the past-the-end element in the map
378  */
379  inline iterator end()
380  { return m_Value.end(); }
381 
382  /**
383  Returns a constant iterator referring to the past-the-end element in the map
384  @return a constant iterator to the past-the-end element in the map
385  */
386  inline const_iterator end() const
387  { return m_Value.end(); }
388 
389 };
390 
391 
392 } } // namespaces
393 
394 
395 // =====================================================================
396 // =====================================================================
397 
398 
399 #endif /* __OPENFLUID_CORE_MAPVALUE_HPP__ */
const StringValue & asStringValue() const
Definition: Value.hpp:64
void writeQuotedToStream(std::ostream &OutStm) const
Definition: MapValue.hpp:179
void setVectorValue(const std::string &Key, const VectorValue &Val)
Definition: MapValue.hpp:227
Definition: BooleanValue.hpp:103
Value * clone() const
Definition: MapValue.hpp:174
MatrixValue getMatrixValue(const std::string &Key) const
Definition: MapValue.hpp:311
Definition: VectorValue.hpp:118
void setMapValue(const std::string &Key, const MapValue &Val)
Definition: MapValue.hpp:243
VectorValue getVectorValue(const std::string &Key) const
Definition: MapValue.hpp:303
MapValue getMapValue(const std::string &Key) const
Definition: MapValue.hpp:319
Definition: CompoundValue.hpp:50
bool isKeyExist(const std::string &Key) const
Definition: MapValue.hpp:347
std::string get() const
Definition: StringValue.hpp:175
const VectorValue & asVectorValue() const
Definition: Value.hpp:68
double getDouble(const std::string &Key) const
Definition: MapValue.hpp:271
Type getType() const
Definition: MapValue.hpp:171
const IntegerValue & asIntegerValue() const
Definition: MapValue.hpp:134
const DoubleValue & asDoubleValue() const
unsigned long getSize() const
Definition: MapValue.hpp:332
std::string getString(const std::string &Key) const
Definition: MapValue.hpp:295
Definition: IntegerValue.hpp:105
const_iterator end() const
Definition: MapValue.hpp:386
const BooleanValue & asBooleanValue() const
const_iterator begin() const
Definition: MapValue.hpp:372
Map_t::const_iterator const_iterator
Definition: MapValue.hpp:142
long get() const
Definition: IntegerValue.hpp:154
bool getBoolean(const std::string &Key) const
Definition: MapValue.hpp:287
Type
Definition: Value.hpp:68
long getInteger(const std::string &Key) const
Definition: MapValue.hpp:279
double get() const
Definition: DoubleValue.hpp:151
unsigned long size() const
Definition: MapValue.hpp:339
iterator begin()
Definition: MapValue.hpp:365
Definition: ApplicationException.hpp:47
bool get() const
Definition: BooleanValue.hpp:152
const MatrixValue & asMatrixValue() const
const MapValue & asMapValue() const
MapValue(const Map_t &Val)
Definition: MapValue.hpp:163
#define OPENFLUID_API
Definition: dllexport.hpp:87
void setBoolean(const std::string &Key, const bool &Val)
Definition: MapValue.hpp:211
void setMatrixValue(const std::string &Key, const MatrixValue &Val)
Definition: MapValue.hpp:235
void setDouble(const std::string &Key, const double &Val)
Definition: MapValue.hpp:194
void setString(const std::string &Key, const std::string &Val)
Definition: MapValue.hpp:219
Map_t::iterator iterator
Definition: MapValue.hpp:140
Definition: DoubleValue.hpp:102
Definition: MatrixValue.hpp:114
std::map< std::string, std::shared_ptr< Value > > Map_t
Definition: MapValue.hpp:138
Definition: StringValue.hpp:91
iterator end()
Definition: MapValue.hpp:379
MapValue()
Definition: MapValue.hpp:155
void setInteger(const std::string &Key, const long &Val)
Definition: MapValue.hpp:203