Documentation for OpenFLUID 2.2.2
Loading...
Searching...
No Matches
MapValue.hpp
Go to the documentation of this file.
1/*
2
3 This file is part of OpenFLUID software
4 Copyright(c) 2021-2026, INRAE
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
55
56
57namespace 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" : ["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 /**
122 Move constructor
123 */
125
126 MapValue(const Map_t& Val) : CompoundValue(), m_Value(Val)
127 { }
128
129 MapValue& operator=(const Value& Other) override;
130
131 MapValue& operator=(Value&& Other) override;
132
133 MapValue& operator=(const MapValue& Other) = default;
134
135 MapValue& operator=(MapValue&& Other) = default;
136
138
139 inline Type getType() const override
140 {
141 return Value::MAP;
142 }
143
144 Value* clone() const override
145 {
146 return new MapValue(*this);
147 }
148
149 void writeToStream(std::ostream& OutStm) const override;
150
151 void writeQuotedToStream(std::ostream& OutStm) const override
152 {
153 writeToStream(OutStm);
154 }
155
156 /**
157 Sets a new value at the given key
158 @param[in] Key the key to add
159 @param[in] Element the element to add, must be derived from openfluid::core::Value
160 */
161 void set(const std::string& Key, Value* Element);
162
163 /**
164 Sets a new double value at the given key
165 @param[in] Key the key to add
166 @param[in] Val the value to add
167 */
168 inline void setDouble(const std::string& Key, const double& Val)
169 {
170 set(Key,new DoubleValue(Val));
171 }
172
173 /**
174 Sets a new long value at the given key
175 @param[in] Key the key to add
176 @param[in] Val the value to add
177
178 */
179 inline void setInteger(const std::string& Key, const long& Val)
180 {
181 set(Key,new IntegerValue(Val));
182 }
183
184 /**
185 Sets a new boolean 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 setBoolean(const std::string& Key, const bool& Val)
190 {
191 set(Key,new BooleanValue(Val));
192 }
193
194 /**
195 Sets a new string 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 setString(const std::string& Key, const std::string& Val)
200 {
201 set(Key,new StringValue(Val));
202 }
203
204 /**
205 Sets a new VectorValue 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 setVectorValue(const std::string& Key, const VectorValue& Val)
210 {
211 set(Key,new VectorValue(Val));
212 }
213
214 /**
215 Sets a new MatrixValue 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 setMatrixValue(const std::string& Key, const MatrixValue& Val)
220 {
221 set(Key,new MatrixValue(Val));
222 }
223
224 /**
225 Sets a new MapValue value at the given key
226 @param[in] Key the key to add
227 @param[in] Val the value to add
228 */
229 inline void setMapValue(const std::string& Key, const MapValue& Val)
230 {
231 set(Key,new MapValue(Val));
232 }
233
234 /**
235 Operator to get/set a value at a key given between []
236 @return the value at the given key
237 */
238 Value& operator[](const std::string& Key);
239
240 /**
241 Returns a reference to the value of the map at the given key
242 @param[in] Key the key of the requested value
243 @return the value at the given key
244 */
245 Value& at(const std::string& Key);
246
247 /**
248 Returns a const reference to the value of the map at the given key
249 @param[in] Key the key of the requested value
250 @return the value at the given key
251 */
252 const Value& at(const std::string& Key) const;
253
254 /**
255 Returns the double 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 double getDouble(const std::string& Key) const
260 {
261 return at(Key).asDoubleValue().get();
262 }
263
264 /**
265 Returns the long 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 long getInteger(const std::string& Key) const
270 {
271 return at(Key).asIntegerValue().get();
272 }
273
274 /**
275 Returns the boolean 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 bool getBoolean(const std::string& Key) const
280 {
281 return at(Key).asBooleanValue().get();
282 }
283
284 /**
285 Returns the string 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 std::string getString(const std::string& Key) const
290 {
291 return at(Key).asStringValue().get();
292 }
293
294 /**
295 Returns the VectorValue 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 VectorValue getVectorValue(const std::string& Key) const
300 {
301 return at(Key).asVectorValue();
302 }
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 {
311 return at(Key).asMatrixValue();
312 }
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 {
321 return at(Key).asMapValue();
322 }
323
324 /**
325 Removes the value corresponding to the given key
326 @param[in] Key the key to remove
327 */
328 bool remove(const std::string& Key);
329
330 /**
331 Returns the size of the map
332 @return size of the map
333 */
334 inline unsigned long getSize() const
335 {
336 return m_Value.size();
337 }
338
339 /**
340 Returns the size of the map
341 @return size of the map
342 */
343 unsigned long size() const
344 {
345 return m_Value.size();
346 }
347
348 /**
349 Checks if the given key exists
350 @param[in] Key the key to check
351 @return true if the given key is present
352 */
353 inline bool isKeyExist(const std::string& Key) const
354 {
355 return (m_Value.find(Key) != m_Value.end());
356 }
357
358 /**
359 Returns the list of keys of the map
360 @return a std::vector of std::string containing the keys of the map
361 */
362 std::vector<std::string> getKeys() const;
363
364 /**
365 Clears the map by removing all values
366 */
367 void clear();
368
369 /**
370 Returns an iterator referring to the first element in the map
371 @return an iterator to the first element in the map
372 */
374 {
375 return m_Value.begin();
376 }
377
378 /**
379 Returns a constant iterator referring to the first element in the map
380 @return a constant iterator to the first element in the map
381 */
382 inline const_iterator begin() const
383 {
384 return m_Value.begin();
385 }
386
387 /**
388 Returns an iterator referring to the past-the-end element in the map
389 @return an iterator to the past-the-end element in the map
390 */
391 inline iterator end()
392 {
393 return m_Value.end();
394 }
395
396 /**
397 Returns a constant iterator referring to the past-the-end element in the map
398 @return a constant iterator to the past-the-end element in the map
399 */
400 inline const_iterator end() const
401 {
402 return m_Value.end();
403 }
404
405};
406
407
408} } // namespaces
409
410
411// =====================================================================
412// =====================================================================
413
414
415#endif /* __OPENFLUID_CORE_MAPVALUE_HPP__ */
Definition BooleanValue.hpp:81
Definition CompoundValue.hpp:52
Definition DoubleValue.hpp:81
Definition IntegerValue.hpp:80
Definition MapValue.hpp:93
void writeQuotedToStream(std::ostream &OutStm) const override
Definition MapValue.hpp:151
std::string getString(const std::string &Key) const
Definition MapValue.hpp:289
void setBoolean(const std::string &Key, const bool &Val)
Definition MapValue.hpp:189
void setMapValue(const std::string &Key, const MapValue &Val)
Definition MapValue.hpp:229
std::map< std::string, std::shared_ptr< Value > > Map_t
Definition MapValue.hpp:96
bool getBoolean(const std::string &Key) const
Definition MapValue.hpp:279
MapValue & operator=(MapValue &&Other)=default
MapValue(MapValue &&Val)
unsigned long getSize() const
Definition MapValue.hpp:334
void set(const std::string &Key, Value *Element)
MapValue(const Map_t &Val)
Definition MapValue.hpp:126
iterator begin()
Definition MapValue.hpp:373
void setString(const std::string &Key, const std::string &Val)
Definition MapValue.hpp:199
std::vector< std::string > getKeys() const
Value & at(const std::string &Key)
void setMatrixValue(const std::string &Key, const MatrixValue &Val)
Definition MapValue.hpp:219
bool remove(const std::string &Key)
MapValue & operator=(const Value &Other) override
void setDouble(const std::string &Key, const double &Val)
Definition MapValue.hpp:168
MapValue(const MapValue &Val)
MatrixValue getMatrixValue(const std::string &Key) const
Definition MapValue.hpp:309
unsigned long size() const
Definition MapValue.hpp:343
Map_t::iterator iterator
Definition MapValue.hpp:98
iterator end()
Definition MapValue.hpp:391
void setVectorValue(const std::string &Key, const VectorValue &Val)
Definition MapValue.hpp:209
long getInteger(const std::string &Key) const
Definition MapValue.hpp:269
MapValue & operator=(const MapValue &Other)=default
MapValue & operator=(Value &&Other) override
const_iterator end() const
Definition MapValue.hpp:400
void setInteger(const std::string &Key, const long &Val)
Definition MapValue.hpp:179
Value * clone() const override
Definition MapValue.hpp:144
MapValue()
Definition MapValue.hpp:113
Map_t::const_iterator const_iterator
Definition MapValue.hpp:100
const Value & at(const std::string &Key) const
Value & operator[](const std::string &Key)
VectorValue getVectorValue(const std::string &Key) const
Definition MapValue.hpp:299
Type getType() const override
Definition MapValue.hpp:139
const_iterator begin() const
Definition MapValue.hpp:382
bool isKeyExist(const std::string &Key) const
Definition MapValue.hpp:353
double getDouble(const std::string &Key) const
Definition MapValue.hpp:259
MapValue getMapValue(const std::string &Key) const
Definition MapValue.hpp:319
void writeToStream(std::ostream &OutStm) const override
Definition MatrixValue.hpp:85
Definition StringValue.hpp:83
Definition Value.hpp:63
const MapValue & asMapValue() const
const MatrixValue & asMatrixValue() const
Type
Definition Value.hpp:66
const VectorValue & asVectorValue() const
Definition VectorValue.hpp:85
#define OPENFLUID_API
Definition dllexport.hpp:86
Definition ApplicationException.hpp:47