MRPT  1.9.9
stl_serialization.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
13 #include <mrpt/typemeta/TTypeName_stl.h> // TTypeName<> for STL templates, needed for serialization of STL templates
14 #include <algorithm> // for_each()
15 #include <array>
16 #include <deque>
17 #include <list>
18 #include <map>
19 #include <set>
20 #include <vector>
21 
22 namespace mrpt::serialization
23 {
24 /** \addtogroup stlext_grp
25  * @{ */
26 
27 #define MRPTSTL_SERIALIZABLE_SEQ_CONTAINER(CONTAINER) \
28  /** Template method to serialize a sequential STL container */ \
29  template <class T, class _Ax> \
30  CArchive& operator<<(CArchive& out, const CONTAINER<T, _Ax>& obj) \
31  { \
32  out << std::string(#CONTAINER) << mrpt::typemeta::TTypeName<T>::get(); \
33  out << static_cast<uint32_t>(obj.size()); \
34  std::for_each( \
35  obj.begin(), obj.end(), \
36  metaprogramming::ObjectWriteToStream(&out)); \
37  return out; \
38  } \
39  /** Template method to deserialize a sequential STL container */ \
40  template <class T, class _Ax> \
41  CArchive& operator>>(CArchive& in, CONTAINER<T, _Ax>& obj) \
42  { \
43  obj.clear(); \
44  std::string pref, stored_T; \
45  in >> pref; \
46  if (pref != #CONTAINER) \
47  THROW_EXCEPTION_FMT( \
48  "Error: serialized container %s<%s>'s preambles is wrong: " \
49  "'%s'", \
50  #CONTAINER, mrpt::typemeta::TTypeName<T>::get().c_str(), \
51  pref.c_str()); \
52  in >> stored_T; \
53  if (stored_T != \
54  std::string(mrpt::typemeta::TTypeName<T>::get().c_str())) \
55  THROW_EXCEPTION_FMT( \
56  "Error: serialized container %s< %s != %s >", #CONTAINER, \
57  stored_T.c_str(), \
58  mrpt::typemeta::TTypeName<T>::get().c_str()); \
59  uint32_t n; \
60  in >> n; \
61  obj.resize(n); \
62  std::for_each( \
63  obj.begin(), obj.end(), \
64  metaprogramming::ObjectReadFromStream(&in)); \
65  return in; \
66  }
67 
68 template <typename T>
69 using is_map_like = std::is_same<
70  typename T::value_type,
71  std::pair<const typename T::key_type, typename T::mapped_type>>;
72 
73 template <typename T>
74 using is_map = std::is_same<
75  T, typename std::map<
76  typename T::key_type, typename T::mapped_type,
77  typename T::key_compare, typename T::allocator_type>>;
78 
79 template <typename T>
80 using is_multimap = std::is_same<
81  T, typename std::multimap<
82  typename T::key_type, typename T::mapped_type,
83  typename T::key_compare, typename T::allocator_type>>;
84 
87 {
88  return "std::map";
89 }
92 {
93  return "std::multimap";
94 }
95 
96 /** Template method to serialize an associative STL container */
98 CArchive& operator<<(CArchive& out, const T& obj)
99 {
100  out << containerName<T>()
103  std::decay_t<typename T::mapped_type>>::get();
104  out << static_cast<uint32_t>(obj.size());
105  for (typename T::const_iterator it = obj.begin(); it != obj.end(); ++it)
106  out << it->first << it->second;
107  return out;
108 }
109 /** Template method to deserialize an associative STL container */
111 CArchive& operator>>(CArchive& in, T& obj)
112 {
113  obj.clear();
114  std::string pref, stored_K, stored_V;
115  in >> pref;
116  if (pref != containerName<T>())
118  "Error: serialized container %s<%s,%s>'s preamble is "
119  "wrong: '%s'",
120  containerName<T>().c_str(),
123  pref.c_str()));
124  in >> stored_K;
125  if (stored_K !=
126  std::string(
129  "Error: serialized container %s key type %s != %s",
130  containerName<T>().c_str(), stored_K.c_str(),
132  in >> stored_V;
133  if (stored_V !=
134  std::string(
137  "Error: serialized container %s value type %s != %s",
138  containerName<T>().c_str(), stored_V.c_str(),
140  uint32_t n;
141  in >> n;
142  for (uint32_t i = 0; i < n; i++)
143  {
144  typename T::key_type key_obj;
145  in >> key_obj;
146  /* Create an pair (Key, empty), then read directly into the
147  * ".second": */
148  typename T::iterator it_new = obj.insert(
149  obj.end(), std::make_pair(key_obj, typename T::mapped_type()));
150  in >> it_new->second;
151  }
152  return in;
153 }
154 
155 #define MRPTSTL_SERIALIZABLE_SIMPLE_ASSOC_CONTAINER(CONTAINER) \
156  /** Template method to serialize an associative STL container */ \
157  template <class K, class _Pr, class _Alloc> \
158  CArchive& operator<<(CArchive& out, const CONTAINER<K, _Pr, _Alloc>& obj) \
159  { \
160  out << std::string(#CONTAINER) << mrpt::typemeta::TTypeName<K>::get(); \
161  out << static_cast<uint32_t>(obj.size()); \
162  for (typename CONTAINER<K, _Pr, _Alloc>::const_iterator it = \
163  obj.begin(); \
164  it != obj.end(); ++it) \
165  out << *it; \
166  return out; \
167  } \
168  /** Template method to deserialize an associative STL container */ \
169  template <class K, class _Pr, class _Alloc> \
170  CArchive& operator>>(CArchive& in, CONTAINER<K, _Pr, _Alloc>& obj) \
171  { \
172  obj.clear(); \
173  std::string pref, stored_K; \
174  in >> pref; \
175  if (pref != #CONTAINER) \
176  THROW_EXCEPTION(format( \
177  "Error: serialized container %s<%s>'s preamble is wrong: " \
178  "'%s'", \
179  #CONTAINER, mrpt::typemeta::TTypeName<K>::get().c_str(), \
180  pref.c_str())); \
181  in >> stored_K; \
182  if (stored_K != \
183  std::string(mrpt::typemeta::TTypeName<K>::get().c_str())) \
184  THROW_EXCEPTION(format( \
185  "Error: serialized container %s key type %s != %s", \
186  #CONTAINER, stored_K.c_str(), \
187  mrpt::typemeta::TTypeName<K>::get().c_str())); \
188  uint32_t n; \
189  in >> n; \
190  for (uint32_t i = 0; i < n; i++) \
191  { \
192  K key_obj; \
193  in >> key_obj; \
194  obj.insert(key_obj); \
195  } \
196  return in; \
197  }
198 
202 
205 
206 /** Template method to serialize a std::array<T,N> */
207 template <class T, size_t N>
208 CArchive& operator<<(CArchive& out, const std::array<T, N>& obj)
209 {
210  out << std::string("std::array") << static_cast<uint32_t>(N)
212  std::for_each(
213  obj.begin(), obj.end(), metaprogramming::ObjectWriteToStream(&out));
214  return out;
215 }
216 
217 /** Template method to deserialize a std::array<T,N> */
218 template <class T, size_t N>
219 CArchive& operator>>(CArchive& in, std::array<T, N>& obj)
220 {
221  std::string pref, stored_T;
222  uint32_t stored_N;
223  in >> pref >> stored_N;
224  if (pref != "std::array" || stored_N != N)
226  "Error: serialized container %s's preambles is wrong: "
227  "'%s'",
228  mrpt::typemeta::TTypeName<std::array<T, N>>::get().c_str(),
229  pref.c_str());
230  in >> stored_T;
231  if (stored_T != std::string(mrpt::typemeta::TTypeName<T>::get().c_str()))
233  "Error: serialized container std::array< %s != %s >",
234  stored_T.c_str(), mrpt::typemeta::TTypeName<T>::get().c_str());
235  std::for_each(
236  obj.begin(), obj.end(), metaprogramming::ObjectReadFromStream(&in));
237  return in;
238 }
239 
240 /** Template method to serialize a STL pair */
241 template <class T1, class T2>
242 CArchive& operator<<(CArchive& out, const std::pair<T1, T2>& obj)
243 {
244  out << std::string("std::pair") << mrpt::typemeta::TTypeName<T1>::get()
246  out << obj.first << obj.second;
247  return out;
248 }
249 /** Template method to deserialize a STL pair */
250 template <class T1, class T2>
251 CArchive& operator>>(CArchive& in, std::pair<T1, T2>& obj)
252 {
253  std::string pref, stored_K, stored_V;
254  in >> pref;
255  if (pref != "std::pair")
257  "Error: serialized std::pair<%s,%s>'s preamble is wrong: '%s'",
259  mrpt::typemeta::TTypeName<T2>::get().c_str(), pref.c_str()));
260  in >> stored_K;
261  if (stored_K != std::string(mrpt::typemeta::TTypeName<T1>::get().c_str()))
263  "Error: serialized std::pair first type %s != %s", stored_K.c_str(),
265  in >> stored_V;
266  if (stored_V != std::string(mrpt::typemeta::TTypeName<T2>::get().c_str()))
268  "Error: serialized std::pair second type %s != %s",
269  stored_V.c_str(), mrpt::typemeta::TTypeName<T2>::get().c_str()));
270  in >> obj.first >> obj.second;
271  return in;
272 }
273 
274 /** @} */ // end of grouping
275 } // namespace mrpt::serialization
std::string containerName()
An object for reading objects from a stream, intended for being used in STL algorithms.
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
GLenum GLsizei n
Definition: glext.h:5136
GLsizei GLsizei GLuint * obj
Definition: glext.h:4085
std::is_same< T, typename std::multimap< typename T::key_type, typename T::mapped_type, typename T::key_compare, typename T::allocator_type > > is_multimap
A template to obtain the type of its argument as a string at compile time.
Definition: TTypeName.h:69
std::is_same< typename T::value_type, std::pair< const typename T::key_type, typename T::mapped_type > > is_map_like
#define MRPTSTL_SERIALIZABLE_SEQ_CONTAINER(CONTAINER)
GLsizei const GLchar ** string
Definition: glext.h:4116
CArchive & operator>>(CArchive &s, mrpt::aligned_std_vector< float > &a)
Definition: CArchive.cpp:250
std::is_same< T, typename std::map< typename T::key_type, typename T::mapped_type, typename T::key_compare, typename T::allocator_type > > is_map
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:53
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
GLuint in
Definition: glext.h:7391
CArchive & operator<<(CArchive &s, const mrpt::aligned_std_vector< float > &a)
Definition: CArchive.cpp:199
GLsizei const GLfloat * value
Definition: glext.h:4134
#define MRPTSTL_SERIALIZABLE_SIMPLE_ASSOC_CONTAINER(CONTAINER)
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:69
unsigned __int32 uint32_t
Definition: rptypes.h:50
static constexpr auto get()
Definition: TTypeName.h:71
An object for writing objects to a stream, intended for being used in STL algorithms.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019