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  <I>Example : declaration</I>
66  @snippet misc/values.cpp map_decl
67 
68  <I>Example : setting the contained values</I>
69  @snippet misc/values.cpp map_set
70 
71  <I>Example : getting the contained values</I>
72  @snippet misc/values.cpp map_get
73 
74  <I>Example : testing the contained elements</I>
75  @snippet misc/values.cpp map_test
76 
77  <I>Example : conversion from string</I>
78  @snippet misc/values.cpp map_fromstr
79 
80  <I>Example : conversion to string</I>
81  @snippet misc/values.cpp map_tostr
82 
83  @cond OpenFLUID:completion
84  {
85  "contexts" : ["ANYWARE"],
86  "menupath" : ["Compute code", "Types", "Values"],
87  "title" : "MapValue",
88  "text" : "openfluid::core::MapValue %%SEL_START%%Val%%SEL_END%%"
89  }
90  @endcond
91 */
93 {
94  public:
95 
96  typedef std::map<std::string,std::shared_ptr<Value> > Map_t;
97 
98  typedef Map_t::iterator iterator;
99 
100  typedef Map_t::const_iterator const_iterator;
101 
102 
103  private:
104 
105  Map_t m_Value;
106 
107 
108  public:
109 
110  /**
111  Default constructor
112  */
114  { }
115 
116  /**
117  Copy constructor
118  */
119  MapValue(const MapValue& Val);
120 
121  MapValue(const Map_t& Val) : CompoundValue(), m_Value(Val)
122  { }
123 
124  ~MapValue();
125 
126  Value& operator =(const Value& Other);
127 
128 
129  inline Type getType() const
130  {
131  return Value::MAP;
132  }
133 
134  Value* clone() const
135  {
136  return new MapValue(*this);
137  }
138 
139  void writeToStream(std::ostream& OutStm) const;
140 
141  void writeQuotedToStream(std::ostream& OutStm) const
142  {
143  writeToStream(OutStm);
144  }
145 
146  /**
147  Sets a new value at the given key
148  @param[in] Key the key to add
149  @param[in] Element the element to add, must be derived from openfluid::core::Value
150  */
151  void set(const std::string& Key, Value* Element);
152 
153  /**
154  Sets a new double value at the given key
155  @param[in] Key the key to add
156  @param[in] Val the value to add
157  */
158  inline void setDouble(const std::string& Key, const double& Val)
159  {
160  set(Key,new DoubleValue(Val));
161  }
162 
163  /**
164  Sets a new long value at the given key
165  @param[in] Key the key to add
166  @param[in] Val the value to add
167 
168  */
169  inline void setInteger(const std::string& Key, const long& Val)
170  {
171  set(Key,new IntegerValue(Val));
172  }
173 
174  /**
175  Sets a new boolean value at the given key
176  @param[in] Key the key to add
177  @param[in] Val the value to add
178  */
179  inline void setBoolean(const std::string& Key, const bool& Val)
180  {
181  set(Key,new BooleanValue(Val));
182  }
183 
184  /**
185  Sets a new string value at the given key
186  @param[in] Key the key to add
187  @param[in] Val the value to add
188  */
189  inline void setString(const std::string& Key, const std::string& Val)
190  {
191  set(Key,new StringValue(Val));
192  }
193 
194  /**
195  Sets a new VectorValue value at the given key
196  @param[in] Key the key to add
197  @param[in] Val the value to add
198  */
199  inline void setVectorValue(const std::string& Key, const VectorValue& Val)
200  {
201  set(Key,new VectorValue(Val));
202  }
203 
204  /**
205  Sets a new MatrixValue 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 setMatrixValue(const std::string& Key, const MatrixValue& Val)
210  {
211  set(Key,new MatrixValue(Val));
212  }
213 
214  /**
215  Sets a new MapValue 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 setMapValue(const std::string& Key, const MapValue& Val)
220  {
221  set(Key,new MapValue(Val));
222  }
223 
224  /**
225  Operator to get/set a value at a key given between []
226  @return the value at the given key
227  */
228  Value& operator[](const std::string& Key);
229 
230  /**
231  Returns a reference to the value of the map at the given key
232  @param[in] Key the key of the requested value
233  @return the value at the given key
234  */
235  Value& at(const std::string& Key);
236 
237  /**
238  Returns a const reference to the value of the map at the given key
239  @param[in] Key the key of the requested value
240  @return the value at the given key
241  */
242  const Value& at(const std::string& Key) const;
243 
244  /**
245  Returns the double value of the map at the given key
246  @param[in] Key the key of the requested value
247  @return the value at the given key
248  */
249  inline double getDouble(const std::string& Key) const
250  {
251  return at(Key).asDoubleValue().get();
252  }
253 
254  /**
255  Returns the long value of the map at the given key
256  @param[in] Key the key of the requested value
257  @return the value at the given key
258  */
259  inline long getInteger(const std::string& Key) const
260  {
261  return at(Key).asIntegerValue().get();
262  }
263 
264  /**
265  Returns the boolean 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 bool getBoolean(const std::string& Key) const
270  {
271  return at(Key).asBooleanValue().get();
272  }
273 
274  /**
275  Returns the string 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 std::string getString(const std::string& Key) const
280  {
281  return at(Key).asStringValue().get();
282  }
283 
284  /**
285  Returns the VectorValue value of the map at the given key
286  @param[in] Key the key of the requested value
287  @return the value at the given key
288  */
289  inline VectorValue getVectorValue(const std::string& Key) const
290  {
291  return at(Key).asVectorValue();
292  }
293 
294  /**
295  Returns the MatrixValue value of the map at the given key
296  @param[in] Key the key of the requested value
297  @return the value at the given key
298  */
299  inline MatrixValue getMatrixValue(const std::string& Key) const
300  {
301  return at(Key).asMatrixValue();
302  }
303 
304  /**
305  Returns the MapValue 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 MapValue getMapValue(const std::string& Key) const
310  {
311  return at(Key).asMapValue();
312  }
313 
314  /**
315  Removes the value corresponding to the given key
316  @param[in] Key the key to remove
317  */
318  bool remove(const std::string& Key);
319 
320  /**
321  Returns the size of the map
322  @return size of the map
323  */
324  inline unsigned long getSize() const
325  {
326  return m_Value.size();
327  }
328 
329  /**
330  Returns the size of the map
331  @return size of the map
332  */
333  unsigned long size() const
334  {
335  return m_Value.size();
336  }
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
344  {
345  return (m_Value.find(Key) != m_Value.end());
346  }
347 
348  /**
349  Returns the list of keys of the map
350  @return a std::vector of std::string containing the keys of the map
351  */
352  std::vector<std::string> getKeys() const;
353 
354  /**
355  Clears the map by removing all values
356  */
357  void clear();
358 
359  /**
360  Returns an iterator referring to the first element in the map
361  @return an iterator to the first element in the map
362  */
363  inline iterator begin()
364  {
365  return m_Value.begin();
366  }
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  {
374  return m_Value.begin();
375  }
376 
377  /**
378  Returns an iterator referring to the past-the-end element in the map
379  @return an iterator to the past-the-end element in the map
380  */
381  inline iterator end()
382  {
383  return m_Value.end();
384  }
385 
386  /**
387  Returns a constant iterator referring to the past-the-end element in the map
388  @return a constant iterator to the past-the-end element in the map
389  */
390  inline const_iterator end() const
391  {
392  return m_Value.end();
393  }
394 
395 };
396 
397 
398 } } // namespaces
399 
400 
401 // =====================================================================
402 // =====================================================================
403 
404 
405 #endif /* __OPENFLUID_CORE_MAPVALUE_HPP__ */
void setBoolean(const std::string &Key, const bool &Val)
Definition: MapValue.hpp:179
const BooleanValue & asBooleanValue() const
bool get() const
Definition: BooleanValue.hpp:133
iterator end()
Definition: MapValue.hpp:381
MatrixValue getMatrixValue(const std::string &Key) const
Definition: MapValue.hpp:299
Definition: IntegerValue.hpp:79
const MatrixValue & asMatrixValue() const
bool isKeyExist(const std::string &Key) const
Definition: MapValue.hpp:343
long get() const
Definition: IntegerValue.hpp:134
std::string getString(const std::string &Key) const
Definition: MapValue.hpp:279
MapValue(const Map_t &Val)
Definition: MapValue.hpp:121
Definition: Value.hpp:64
Definition: CompoundValue.hpp:51
const IntegerValue & asIntegerValue() const
Definition: StringValue.hpp:76
void setMatrixValue(const std::string &Key, const MatrixValue &Val)
Definition: MapValue.hpp:209
bool getBoolean(const std::string &Key) const
Definition: MapValue.hpp:269
void setVectorValue(const std::string &Key, const VectorValue &Val)
Definition: MapValue.hpp:199
Definition: VectorValue.hpp:84
double get() const
Definition: DoubleValue.hpp:135
VectorValue getVectorValue(const std::string &Key) const
Definition: MapValue.hpp:289
const_iterator begin() const
Definition: MapValue.hpp:372
Definition: BooleanValue.hpp:80
const MapValue & asMapValue() const
std::string get() const
Definition: StringValue.hpp:172
iterator begin()
Definition: MapValue.hpp:363
Definition: Value.hpp:68
MapValue getMapValue(const std::string &Key) const
Definition: MapValue.hpp:309
Definition: DoubleValue.hpp:80
void writeQuotedToStream(std::ostream &OutStm) const
Definition: MapValue.hpp:141
Value * clone() const
Definition: MapValue.hpp:134
long getInteger(const std::string &Key) const
Definition: MapValue.hpp:259
void setString(const std::string &Key, const std::string &Val)
Definition: MapValue.hpp:189
const DoubleValue & asDoubleValue() const
Definition: MapValue.hpp:92
Map_t::const_iterator const_iterator
Definition: MapValue.hpp:100
Definition: ApplicationException.hpp:47
Type
Definition: Value.hpp:68
Definition: MatrixValue.hpp:84
double getDouble(const std::string &Key) const
Definition: MapValue.hpp:249
const StringValue & asStringValue() const
#define OPENFLUID_API
Definition: dllexport.hpp:86
Map_t::iterator iterator
Definition: MapValue.hpp:98
const VectorValue & asVectorValue() const
Type getType() const
Definition: MapValue.hpp:129
std::map< std::string, std::shared_ptr< Value > > Map_t
Definition: MapValue.hpp:96
unsigned long getSize() const
Definition: MapValue.hpp:324
MapValue()
Definition: MapValue.hpp:113
void setMapValue(const std::string &Key, const MapValue &Val)
Definition: MapValue.hpp:219
const_iterator end() const
Definition: MapValue.hpp:390
unsigned long size() const
Definition: MapValue.hpp:333
void setDouble(const std::string &Key, const double &Val)
Definition: MapValue.hpp:158
void setInteger(const std::string &Key, const long &Val)
Definition: MapValue.hpp:169