Main MRPT website > C++ reference for MRPT 1.5.9
stl_serialization.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <mrpt/utils/TTypeName_impl.h> // TTypeName<> for STL templates, needed for serialization of STL templates
13 #include <mrpt/utils/CStream.h>
14 #include <vector>
15 #include <deque>
16 #include <set>
17 #include <map>
18 #include <list>
19 #include <algorithm> // for_each()
20 
21 namespace mrpt
22 {
23  namespace utils
24  {
25  /** \addtogroup stlext_grp
26  * @{ */
27 
28  #define MRPTSTL_SERIALIZABLE_SEQ_CONTAINER( CONTAINER ) \
29  /** Template method to serialize a sequential STL container */ \
30  template <class T,class _Ax> \
31  CStream& operator << (mrpt::utils::CStream& out, const CONTAINER<T,_Ax> &obj) \
32  { \
33  out << std::string(#CONTAINER) << mrpt::utils::TTypeName<T>::get(); \
34  out << static_cast<uint32_t>(obj.size()); \
35  std::for_each( obj.begin(), obj.end(), mrpt::utils::metaprogramming::ObjectWriteToStream(&out) ); \
36  return out; \
37  } \
38  /** Template method to deserialize a sequential STL container */ \
39  template <class T,class _Ax> \
40  CStream& operator >> (mrpt::utils::CStream& in, CONTAINER<T,_Ax> &obj) \
41  { \
42  obj.clear(); \
43  std::string pref,stored_T; \
44  in >> pref; \
45  if (pref!=#CONTAINER) THROW_EXCEPTION_FMT("Error: serialized container %s<%s>'s preambles is wrong: '%s'",#CONTAINER,TTypeName<T>::get().c_str(),pref.c_str() ) \
46  in >> stored_T; \
47  if (stored_T != mrpt::utils::TTypeName<T>::get() ) THROW_EXCEPTION_FMT("Error: serialized container %s< %s != %s >",#CONTAINER,stored_T.c_str(),TTypeName<T>::get().c_str() ) \
48  uint32_t n; \
49  in >> n; \
50  obj.resize(n); \
51  std::for_each( obj.begin(), obj.end(), mrpt::utils::metaprogramming::ObjectReadFromStream(&in) ); \
52  return in; \
53  }
54 
55 
56  #define MRPTSTL_SERIALIZABLE_ASSOC_CONTAINER( CONTAINER ) \
57  /** Template method to serialize an associative STL container */ \
58  template <class K,class V, class _Pr, class _Alloc> \
59  CStream& operator << (mrpt::utils::CStream& out, const CONTAINER<K,V,_Pr,_Alloc> &obj) \
60  { \
61  out << std::string(#CONTAINER) << TTypeName<K>::get() << TTypeName<V>::get(); \
62  out << static_cast<uint32_t>(obj.size()); \
63  for (typename CONTAINER<K,V,_Pr,_Alloc>::const_iterator it=obj.begin();it!=obj.end();++it) \
64  out << it->first << it->second; \
65  return out; \
66  } \
67  /** Template method to deserialize an associative STL container */ \
68  template <class K,class V, class _Pr, class _Alloc> \
69  CStream& operator >> (mrpt::utils::CStream& in, CONTAINER<K,V,_Pr,_Alloc> &obj) \
70  { \
71  obj.clear(); \
72  std::string pref,stored_K,stored_V; \
73  in >> pref; \
74  if (pref!=#CONTAINER) THROW_EXCEPTION(format("Error: serialized container %s<%s,%s>'s preamble is wrong: '%s'",#CONTAINER, TTypeName<K>::get().c_str(), TTypeName<V>::get().c_str() ,pref.c_str())) \
75  in >> stored_K; \
76  if (stored_K != TTypeName<K>::get()) THROW_EXCEPTION(format("Error: serialized container %s key type %s != %s",#CONTAINER,stored_K.c_str(), TTypeName<K>::get().c_str())) \
77  in >> stored_V; \
78  if (stored_V != TTypeName<V>::get()) THROW_EXCEPTION(format("Error: serialized container %s value type %s != %s",#CONTAINER,stored_V.c_str(), TTypeName<V>::get().c_str())) \
79  uint32_t n; \
80  in >> n; \
81  for (uint32_t i=0;i<n;i++) \
82  { \
83  K key_obj; \
84  in >> key_obj; \
85  /* Create an pair (Key, empty), then read directly into the ".second": */ \
86  typename CONTAINER<K,V,_Pr,_Alloc>::iterator it_new = obj.insert(obj.end(), std::make_pair(key_obj, V()) ); \
87  in >> it_new->second; \
88  } \
89  return in; \
90  }
91 
92  MRPTSTL_SERIALIZABLE_SEQ_CONTAINER(std::vector) // Serialization for std::vector
93  MRPTSTL_SERIALIZABLE_SEQ_CONTAINER(std::deque) // Serialization for std::deque
94  MRPTSTL_SERIALIZABLE_SEQ_CONTAINER(std::list) // Serialization for std::list
95 
96  MRPTSTL_SERIALIZABLE_ASSOC_CONTAINER(std::map) // Serialization for std::map
97  MRPTSTL_SERIALIZABLE_ASSOC_CONTAINER(std::multimap) // Serialization for std::multimap
98 
99 
100  #define MRPTSTL_SERIALIZABLE_SIMPLE_ASSOC_CONTAINER( CONTAINER ) \
101  /** Template method to serialize an associative STL container */ \
102  template <class K,class _Pr,class _Alloc> \
103  CStream& operator << (mrpt::utils::CStream& out, const CONTAINER<K,_Pr,_Alloc> &obj) \
104  { \
105  out << std::string(#CONTAINER) << TTypeName<K>::get(); \
106  out << static_cast<uint32_t>(obj.size()); \
107  for (typename CONTAINER<K,_Pr,_Alloc>::const_iterator it=obj.begin();it!=obj.end();++it) \
108  out << *it; \
109  return out; \
110  } \
111  /** Template method to deserialize an associative STL container */ \
112  template <class K,class _Pr,class _Alloc> \
113  CStream& operator >> (mrpt::utils::CStream& in, CONTAINER<K,_Pr,_Alloc> &obj) \
114  { \
115  obj.clear(); \
116  std::string pref,stored_K; \
117  in >> pref; \
118  if (pref!=#CONTAINER) THROW_EXCEPTION(format("Error: serialized container %s<%s>'s preamble is wrong: '%s'",#CONTAINER, TTypeName<K>::get().c_str(),pref.c_str())) \
119  in >> stored_K; \
120  if (stored_K != TTypeName<K>::get()) THROW_EXCEPTION(format("Error: serialized container %s key type %s != %s",#CONTAINER,stored_K.c_str(), TTypeName<K>::get().c_str())) \
121  uint32_t n; \
122  in >> n; \
123  for (uint32_t i=0;i<n;i++) \
124  { \
125  K key_obj; \
126  in >> key_obj; \
127  obj.insert(key_obj); \
128  } \
129  return in; \
130  }
131 
132  MRPTSTL_SERIALIZABLE_SIMPLE_ASSOC_CONTAINER(std::set) // Serialization for std::set
133  MRPTSTL_SERIALIZABLE_SIMPLE_ASSOC_CONTAINER(std::multiset) // Serialization for std::multiset
134 
135 
136  /** Template method to serialize a STL pair */
137  template <class T1,class T2>
138  CStream& operator << (mrpt::utils::CStream& out, const std::pair<T1,T2> &obj)
139  {
140  out << std::string("std::pair") << TTypeName<T1>::get() << TTypeName<T2>::get();
141  out << obj.first << obj.second;
142  return out;
143  }
144  /** Template method to deserialize a STL pair */
145  template <class T1,class T2>
147  {
148  std::string pref,stored_K,stored_V;
149  in >> pref;
150  if (pref!="std::pair") THROW_EXCEPTION(format("Error: serialized std::pair<%s,%s>'s preamble is wrong: '%s'", TTypeName<T1>::get().c_str(), TTypeName<T2>::get().c_str() ,pref.c_str()))
151  in >> stored_K;
152  if (stored_K != TTypeName<T1>::get()) THROW_EXCEPTION(format("Error: serialized std::pair first type %s != %s",stored_K.c_str(), TTypeName<T1>::get().c_str()))
153  in >> stored_V;
154  if (stored_V != TTypeName<T2>::get()) THROW_EXCEPTION(format("Error: serialized std::pair second type %s != %s",stored_V.c_str(), TTypeName<T2>::get().c_str()))
155  in >> obj.first >> obj.second;
156  return in;
157  }
158 
159  /** @} */ // end of grouping
160  } // End of namespace
161 } // End of namespace
A template to obtain the type of its argument as a string at compile time.
Definition: TTypeName.h:47
#define THROW_EXCEPTION(msg)
#define MRPTSTL_SERIALIZABLE_SIMPLE_ASSOC_CONTAINER(CONTAINER)
GLsizei GLsizei GLuint * obj
Definition: glext.h:3902
::mrpt::utils::CStream & operator>>(mrpt::utils::CStream &in, CImagePtr &pObj)
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:38
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:21
static std::string get()
Definition: TTypeName.h:49
GLsizei const GLchar ** string
Definition: glext.h:3919
MRPTSTL_SERIALIZABLE_SEQ_CONTAINER(std::vector) MRPTSTL_SERIALIZABLE_SEQ_CONTAINER(std
Template method to serialize a STL pair.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define MRPTSTL_SERIALIZABLE_ASSOC_CONTAINER(CONTAINER)
GLuint in
Definition: glext.h:6301



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020