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  \brief Header of ...
36 
37  \author Jean-Christophe FABRE <fabrejc@supagro.inra.fr>
38  */
39 
40 
41 #ifndef __MAPVALUE_HPP__
42 #define __MAPVALUE_HPP__
43 
44 
45 #include <map>
46 #include <boost/shared_ptr.hpp>
47 
55 #include <openfluid/dllexport.hpp>
56 
57 
58 namespace openfluid { namespace core {
59 
60 /**
61  MapValue is a container for a key => value map, 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  typedef std::map<std::string,boost::shared_ptr<Value> > Map_t;
138 
139 
140  private:
141 
142  Map_t m_Value;
143 
144  public:
145 
146  /**
147  Default constructor
148  */
150 
151  /**
152  Copy constructor
153  */
154  MapValue(const MapValue& Val);
155 
156  MapValue(const Map_t& Val) : CompoundValue(), m_Value(Val) {};
157 
158  Value& operator =(const Value& Other);
159 
160  ~MapValue();
161 
162  inline Type getType() const { return Value::MAP; };
163 
164  Value* clone() const { return new MapValue(*this); };
165 
166  void writeToStream(std::ostream& OutStm) const;
167 
168  /**
169  Sets a new value at the given key
170  @param[in] Key the key to add
171  @param[in] Element the element to add, must be derived from openfluid::core::Value
172  */
173  void set(const std::string& Key, Value* Element);
174 
175  /**
176  Sets a new double value at the given key
177  @param[in] Key the key to add
178  @param[in] Val the value to add
179  */
180  inline void setDouble(const std::string& Key, const double& Val)
181  { set(Key,new DoubleValue(Val)); };
182 
183  /**
184  Sets a new long value at the given key
185  @param[in] Key the key to add
186  @param[in] Val the value to add
187 
188  */
189  inline void setInteger(const std::string& Key, const long& Val)
190  { set(Key,new IntegerValue(Val)); };
191 
192  /**
193  Sets a new boolean value at the given key
194  @param[in] Key the key to add
195  @param[in] Val the value to add
196  */
197  inline void setBoolean(const std::string& Key, const bool& Val)
198  { set(Key,new BooleanValue(Val)); };
199 
200  /**
201  Sets a new string value at the given key
202  @param[in] Key the key to add
203  @param[in] Val the value to add
204  */
205  inline void setString(const std::string& Key, const std::string& Val)
206  { set(Key,new StringValue(Val)); };
207 
208  /**
209  Sets a new VectorValue value at the given key
210  @param[in] Key the key to add
211  @param[in] Val the value to add
212  */
213  inline void setVectorValue(const std::string& Key, const VectorValue& Val)
214  { set(Key,new VectorValue(Val)); };
215 
216  /**
217  Sets a new MatrixValue 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 setMatrixValue(const std::string& Key, const MatrixValue& Val)
222  { set(Key,new MatrixValue(Val)); };
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 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& get(const std::string& Key);
236 
237  /**
238  Returns the double 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  inline double getDouble(const std::string& Key) { return get(Key).asDoubleValue().get(); };
243 
244  /**
245  Returns the long 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 long getInteger(const std::string& Key) { return get(Key).asIntegerValue().get(); };
250 
251  /**
252  Returns the boolean value of the map at the given key
253  @param[in] Key the key of the requested value
254  @return the value at the given key
255  */
256  inline bool getBoolean(const std::string& Key) { return get(Key).asBooleanValue().get(); };
257 
258  /**
259  Returns the string value of the map at the given key
260  @param[in] Key the key of the requested value
261  @return the value at the given key
262  */
263  inline std::string getString(const std::string& Key) { return get(Key).asStringValue().get(); };
264 
265  /**
266  Returns the VectorValue value of the map at the given key
267  @param[in] Key the key of the requested value
268  @return the value at the given key
269  */
270  inline VectorValue getVectorValue(const std::string& Key) { return get(Key).asVectorValue(); };
271 
272  /**
273  Returns the MatrixValue 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 MatrixValue getMatrixValue(const std::string& Key) { return get(Key).asMatrixValue(); };
278 
279  /**
280  Removes the value corresponding to the given key
281  @param[in] Key the key to remove
282  */
283  bool remove(const std::string& Key);
284 
285  /**
286  Returns the size of the map
287  @return size of the map
288  */
289  inline unsigned long getSize() const { return m_Value.size(); };
290 
291  /**
292  Returns the size of the map
293  @return size of the map
294  */
295  unsigned long size() const { return m_Value.size(); };
296 
297  /**
298  Checks if the given key exists
299  @param[in] Key the key to check
300  @return true if the given key is present
301  */
302  inline bool isKeyExist(const std::string& Key) const { return (m_Value.find(Key) != m_Value.end()); };
303 
304  /**
305  Clears the map by removing all values
306  */
307  void clear();
308 
309 };
310 
311 
312 } } // namespaces
313 
314 
315 // =====================================================================
316 // =====================================================================
317 
318 
319 #endif /* __MAPVALUE_HPP__ */
double getDouble(const std::string &Key)
Definition: MapValue.hpp:242
Definition: Value.hpp:68
VectorValue getVectorValue(const std::string &Key)
Definition: MapValue.hpp:270
void setDouble(const std::string &Key, const double &Val)
Definition: MapValue.hpp:180
bool getBoolean(const std::string &Key)
Definition: MapValue.hpp:256
Header of ...
Definition: StringValue.hpp:91
Definition: CompoundValue.hpp:53
void setInteger(const std::string &Key, const long &Val)
Definition: MapValue.hpp:189
MapValue()
Definition: MapValue.hpp:149
bool isKeyExist(const std::string &Key) const
Definition: MapValue.hpp:302
Value * clone() const
Definition: MapValue.hpp:164
Type getType() const
Definition: MapValue.hpp:162
Definition: IntegerValue.hpp:106
Definition: DoubleValue.hpp:103
Definition: MapValue.hpp:134
unsigned long getSize() const
Definition: MapValue.hpp:289
MatrixValue getMatrixValue(const std::string &Key)
Definition: MapValue.hpp:277
Header of ...
void setBoolean(const std::string &Key, const bool &Val)
Definition: MapValue.hpp:197
Type
Definition: Value.hpp:68
void setString(const std::string &Key, const std::string &Val)
Definition: MapValue.hpp:205
Header of ...
Definition: MatrixValue.hpp:115
Definition: Value.hpp:64
unsigned long size() const
Definition: MapValue.hpp:295
void setMatrixValue(const std::string &Key, const MatrixValue &Val)
Definition: MapValue.hpp:221
MapValue(const Map_t &Val)
Definition: MapValue.hpp:156
std::string getString(const std::string &Key)
Definition: MapValue.hpp:263
Header of ...
Header of ...
long getInteger(const std::string &Key)
Definition: MapValue.hpp:249
Header of ...
Definition: VectorValue.hpp:119
void setVectorValue(const std::string &Key, const VectorValue &Val)
Definition: MapValue.hpp:213
Definition: BooleanValue.hpp:104
std::map< std::string, boost::shared_ptr< Value > > Map_t
Definition: MapValue.hpp:137
Header of ...
#define DLLEXPORT
Definition: dllexport.hpp:51