MIRA
StlCollections.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_STLCOLLECTIONS_H_
48 #define _MIRA_STLCOLLECTIONS_H_
49 
50 #include <utils/Foreach.h>
51 
53 #include <serialization/RecursiveMemberReflector.h> // for exception types
54 
56 
57 namespace mira {
58 
60 
61 namespace serialization { // our private namespace
62 
64 
65 // SEQUENCES: list, vector, deque
66 
79 template<typename Reflector, typename Container>
81 {
82  typedef typename Container::iterator iterator;
83  typedef typename Container::value_type type;
84 
85  static void reflect(Reflector& r, Container& c)
86  {
87  // store the size first
88  uint32 count = c.size();
89  static const std::string contextCount = typeName<Container>() + " ReflectCollectionCount";
90  MIRA_REFLECT_CALL(Reflector, r, contextCount.c_str(),
92 
93  static const std::string contextItems = typeName<Container>() + " ReflectCollectionItems";
94  MIRA_REFLECT_CALL(Reflector, r, contextItems.c_str(),
96  }
97 };
98 
112 template<typename Reflector, typename Container>
114 {
115  typedef typename Container::iterator iterator;
116  typedef typename Container::value_type type;
117 
118  static void reflect(Reflector& r, Container& c)
119  {
120  c.clear(); // clear previous content
121 
122  // restore the size first
123  uint32 count;
124  static const std::string contextCount = typeName<Container>() + " ReflectCollectionCount";
125  MIRA_REFLECT_CALL(Reflector, r, contextCount.c_str(),
127 
128  // reserve the space and create the elements
129  c.resize(count);
130 
131  static const std::string contextItems = typeName<Container>() + " ReflectCollectionItems";
132  MIRA_REFLECT_CALL(Reflector, r, contextItems.c_str(),
134  }
135 };
136 
137 template<typename Reflector, typename Container>
139 {
140  typedef typename Container::value_type type;
141 
142  static void reflect(Reflector& r, Container& c)
143  {
144  // store each item
145  int id=0;
146  foreach(const type& cv, c)
147  {
148  // deserializing references to non pointer-type keys will not work
149  // (because these cannot be deserialized 'in place',
150  // the serialization framework is unable to track the correct memory address),
151  // so we prevent creating references during serialization already
152  // by use of the REFLECT_CTRLFLAG_TEMP_TRACKING
153  type& nonconstv = const_cast<type&>(cv);
155  MIRA_MEMBER_WITH_ID(r, "item", "item["+toString(id)+"]", nonconstv, "");
156  } else {
157  MIRA_MEMBER_WITH_ID(r, "item", "item["+toString(id)+"]", nonconstv,
159  }
160 
161  ++id;
162  }
163  }
164 };
165 
166 // SET: set, multiset
179 template<typename Reflector, typename Container>
181 {
182  static void reflect(Reflector& r, Container& c)
183  {
184  // store the size first
185  uint32 count = c.size();
186  static const std::string contextCount = typeName<Container>() + " ReflectCollectionCount";
187  MIRA_REFLECT_CALL(Reflector, r, contextCount.c_str(),
189 
190  static const std::string contextItems = typeName<Container>() + " ReflectReadSetItems";
191  MIRA_REFLECT_CALL(Reflector, r, contextItems.c_str(),
193  }
194 };
195 
196 template<typename Reflector, typename Container>
198 {
199  typedef typename Container::value_type type;
200  typedef typename Container::iterator iterator;
201 
202  static void reflect(Reflector& r, Container& c, uint32 count)
203  {
204  iterator hint = c.begin();
205 
206  // restore each item and insert into the set
207  for(uint32 id=0; id<count; ++id)
208  {
209  // reflect the value
210  type v;
211 
213  MIRA_MEMBER_WITH_ID(r, "item", "item["+toString(id)+"]", v, "");
214  } else {
215  // we have to deserialize the key to a temp object, cannot restore references to it !!!
216  MIRA_MEMBER_WITH_ID(r, "item", "item["+toString(id)+"]", v, "", REFLECT_CTRLFLAG_TEMP_TRACKING);
217  }
218  // add the value to the container
219  hint = c.insert(hint, v);
220  }
221  }
222 };
223 
235 template<typename Reflector, typename Container>
237 {
238  static void reflect(Reflector& r, Container& c)
239  {
240  c.clear(); // clear previous content
241 
242  // restore the size first
243  uint32 count;
244  static const std::string contextCount = typeName<Container>() + " ReflectCollectionCount";
245  MIRA_REFLECT_CALL(Reflector, r, contextCount.c_str(),
247 
248  static const std::string contextItems = typeName<Container>() + " ReflectWriteSetItems";
249  MIRA_REFLECT_CALL(Reflector, r, contextItems.c_str(),
251  }
252 };
253 
254 // Helper classes and functions for map pair reflection
255 // In contrast to normal pairs, the 'first' element in a map pair has const-type
256 // and is not object trackable (with few exceptions)
257 template<typename Derived, typename key_type, typename value_type>
259 
260  typedef typename value_type::second_type mapped_type;
261 
262  ReadMapPairHelper(const value_type& p) : mP(p) {}
263 
264  void reflect(Derived& r) {
265  // deserializing references to non pointer-type keys will not work
266  // (because these cannot be deserialized 'in place',
267  // the serialization framework is unable to track the correct memory address),
268  // so we prevent creating references during serialization already
269  // by use of the REFLECT_CTRLFLAG_TEMP_TRACKING
270  key_type& nonconstkey = const_cast<key_type&>(mP.first);
272  r.member("First", nonconstkey, "");
273  else
274  r.member("First", nonconstkey, "", REFLECT_CTRLFLAG_TEMP_TRACKING);
275 
276  mapped_type& nonconstv = const_cast<mapped_type&>(mP.second);
277  r.property("Second", nonconstv, "");
278  }
279 
280  const value_type& mP;
281 };
282 
283 
284 template<typename Derived, typename Container>
285 void reflectReadMapPair(Derived& r, const char* itemName, uint32 id,
286  const typename Container::value_type& p)
287 {
288  typedef typename Container::key_type key_type;
289  typedef typename Container::value_type value_type;
290 
292  MIRA_MEMBER_WITH_ID(r, itemName, std::string(itemName)+"["+toString(id)+"]", rp, "");
293 }
294 
295 template<typename Derived, typename Container>
297 
298  typedef typename Container::key_type key_type;
299  typedef typename Container::mapped_type mapped_type;
300  typedef typename Container::value_type value_type;
301  typedef typename Container::iterator iterator;
302 
303  WriteMapPairHelper(Container& c, iterator& hint)
304  : mC(c), mHint(hint) {}
305 
306  void reflect(Derived& r) {
307  key_type key;
309  r.member("First", key, "");
310  else
311  r.member("First", key, "", REFLECT_CTRLFLAG_TEMP_TRACKING);
312 
313  // insert a dummy value with the key
314  iterator it = mC.insert(mHint, value_type(key, mapped_type()));
315  mHint = it; // the iterator will be insertion hint for next element
316  r.property("Second", it->second, "");
317  }
318 
319  Container& mC;
321 };
322 
323 template<typename Derived, typename Container>
324 void reflectWriteMapPair(Derived& r, const char* itemName, const char* keyName, uint32 id,
325  Container& c, typename Container::iterator& ioHint)
326 {
327  typedef typename Container::iterator iterator;
328  typedef typename Container::value_type value_type;
329  typedef typename Container::key_type key_type;
330  typedef typename Container::mapped_type mapped_type;
331 
332  std::string nameId = "["+toString(id)+"]";
333  std::string itemNameWithId = std::string(itemName)+nameId;
335  MIRA_MEMBER_WITH_ID(r, itemName, itemNameWithId, wp, "");
336 }
337 
338 template<typename Reflector, typename Container>
340 {
341  typedef typename Container::value_type value_type;
342 
343  static void reflect(Reflector& r, Container& c)
344  {
345  // store each item
346  int id=0;
347  foreach(const value_type& p, c)
348  {
349  reflectReadMapPair<Reflector, Container>(r, "item", id, p);
350  ++id;
351  }
352  }
353 };
354 
372 template<typename Reflector, typename Container>
374 {
375  static void reflect(Reflector& r, Container& c)
376  {
377  // store the size first
378  uint32 count = c.size();
379  static const std::string contextCount = typeName<Container>() + " ReflectCollectionCount";
380  MIRA_REFLECT_CALL(Reflector, r, contextCount.c_str(),
382 
383  static const std::string contextItems = typeName<Container>() + " ReflectReadMapItems";
384  MIRA_REFLECT_CALL(Reflector, r, contextItems.c_str(),
386  }
387 };
388 
389 
390 template<typename Reflector, typename Container>
392 {
393  typedef typename Container::iterator iterator;
394 
395  static void reflect(Reflector& r, Container& c, uint32 count)
396  {
397  iterator hint = c.begin();
398 
399  // restore each item and insert into the map
400  for(uint32 id=0; id<count; ++id)
401  reflectWriteMapPair(r, "item", "key", id, c, hint);
402  }
403 };
404 
422 template<typename Reflector, typename Container>
424 {
425  static void reflect(Reflector& r, Container& c)
426  {
427  c.clear(); // clear previous content
428 
429  // restore the size first
430  uint32 count;
431  static const std::string contextCount = typeName<Container>() + " ReflectCollectionCount";
432  MIRA_REFLECT_CALL(Reflector, r, contextCount.c_str(),
434 
435  static const std::string contextItems = typeName<Container>() + " ReflectWriteMapItems";
436  MIRA_REFLECT_CALL(Reflector, r, contextItems.c_str(),
438  }
439 };
440 
442 
443 } //namespace
444 
446 
447 } //namespace
448 
449 #endif
This object can use object tracking internally, but the object tracking system&#39;s state remains unchan...
Definition: ReflectControlFlags.h:82
Type trait that indicates whether a type is a pointer type or a shared pointer.
Definition: IsPointerOrSharedPointer.h:69
Macro for iterating over all elements in a container.
Provides type trait that indicates whether a type is a pointer type or a shared pointer.
Container::iterator iterator
Definition: StlCollections.h:393
Container::value_type type
Definition: StlCollections.h:199
reflects generic associative containers like map, multimap, hash_map
Definition: StlCollections.h:373
Definition: StlCollections.h:258
static void reflect(Reflector &r, Container &c)
Definition: StlCollections.h:142
void reflect(Derived &r)
Definition: StlCollections.h:264
Reflects generic set containers like set, multiset.
Definition: StlCollections.h:180
void reflectWriteMapPair(Derived &r, const char *itemName, const char *keyName, uint32 id, Container &c, typename Container::iterator &ioHint)
Definition: StlCollections.h:324
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
iterator & mHint
Definition: StlCollections.h:320
Container & mC
Definition: StlCollections.h:319
Container::value_type value_type
Definition: StlCollections.h:300
Deserializes generic set containers like set, multiset.
Definition: StlCollections.h:236
Definition: StlCollections.h:296
static void reflect(Reflector &r, Container &c)
Definition: StlCollections.h:85
static void reflect(Reflector &r, Container &c)
Definition: StlCollections.h:118
static void reflect(Reflector &r, Container &c)
Definition: StlCollections.h:182
Definition: StlCollections.h:339
const value_type & mP
Definition: StlCollections.h:280
static void reflect(Reflector &r, Container &c)
Definition: StlCollections.h:238
Reflects generic sequential containers like vectors, list, deque (Read Only / Serialization) ...
Definition: StlCollections.h:80
static void reflect(Reflector &r, Container &c, uint32 count)
Definition: StlCollections.h:202
std::string toString(const T &value, int precision=-1)
Converts any data type to string (the data type must support the stream << operator).
Definition: ToString.h:256
ReflectCollectionCount and ReflectCollectionItems to be specialized for certain Reflectors.
Container::value_type type
Definition: StlCollections.h:83
Reflects generic sequencial containers like vectors, list, deque (Write Only / Deserialization) ...
Definition: StlCollections.h:113
static void reflect(Reflector &r, uint32 &ioCount)
Definition: ReflectCollection.h:71
#define MIRA_REFLECT_CALL(ReflectorType, reflector, context, COMMAND)
Whenever a reflection function calls another function that is independently maintained, the call should be marked to the reflector.
Definition: ReflectorMacros.h:115
static void reflect(Reflector &r, Container &c)
Definition: StlCollections.h:343
static void reflect(Reflector &r, Container &c, uint32 count)
Definition: StlCollections.h:395
WriteMapPairHelper(Container &c, iterator &hint)
Definition: StlCollections.h:303
ReadMapPairHelper(const value_type &p)
Definition: StlCollections.h:262
Container::key_type key_type
Definition: StlCollections.h:298
Container::value_type value_type
Definition: StlCollections.h:341
Definition: StlCollections.h:197
value_type::second_type mapped_type
Definition: StlCollections.h:260
static void reflect(Reflector &r, Container &c)
Definition: ReflectCollection.h:89
void reflectReadMapPair(Derived &r, const char *itemName, uint32 id, const typename Container::value_type &p)
Definition: StlCollections.h:285
static void reflect(Reflector &r, Container &c)
Definition: StlCollections.h:425
Container::iterator iterator
Definition: StlCollections.h:82
Definition: StlCollections.h:138
Container::iterator iterator
Definition: StlCollections.h:301
Container::iterator iterator
Definition: StlCollections.h:115
#define MIRA_MEMBER_WITH_ID(reflector, name, id, var,...)
Macro that should be used to reflect a member if a manually specified ID is used. ...
Definition: ReflectorInterface.h:1047
void reflect(Derived &r)
Definition: StlCollections.h:306
static void reflect(Reflector &r, Container &c)
Definition: StlCollections.h:375
Container::mapped_type mapped_type
Definition: StlCollections.h:299
Container::iterator iterator
Definition: StlCollections.h:200
Container::value_type type
Definition: StlCollections.h:116
Definition: StlCollections.h:391
reflects generic associative containers like map, multimap, hash_map
Definition: StlCollections.h:423
Container::value_type type
Definition: StlCollections.h:140