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