MRPT  1.9.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-2018, 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/typemeta/TTypeName_stl.h> // TTypeName<> for STL templates, needed for serialization of STL templates
14 #include <vector>
15 #include <array>
16 #include <deque>
17 #include <set>
18 #include <map>
19 #include <list>
20 #include <algorithm> // for_each()
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(), mrpt::typemeta::TTypeName<T>::get().c_str()) \
58  uint32_t n; \
59  in >> n; \
60  obj.resize(n); \
61  std::for_each( \
62  obj.begin(), obj.end(), \
63  metaprogramming::ObjectReadFromStream(&in)); \
64  return in; \
65  }
66 
67 #define MRPTSTL_SERIALIZABLE_ASSOC_CONTAINER(CONTAINER) \
68  /** Template method to serialize an associative STL container */ \
69  template <class K, class V, class _Pr, class _Alloc> \
70  CArchive& operator<<( \
71  CArchive& out, const CONTAINER<K, V, _Pr, _Alloc>& obj) \
72  { \
73  out << std::string(#CONTAINER) << mrpt::typemeta::TTypeName<K>::get() \
74  << mrpt::typemeta::TTypeName<V>::get(); \
75  out << static_cast<uint32_t>(obj.size()); \
76  for (typename CONTAINER<K, V, _Pr, _Alloc>::const_iterator it = \
77  obj.begin(); \
78  it != obj.end(); ++it) \
79  out << it->first << it->second; \
80  return out; \
81  } \
82  /** Template method to deserialize an associative STL container */ \
83  template <class K, class V, class _Pr, class _Alloc> \
84  CArchive& operator>>(CArchive& in, CONTAINER<K, V, _Pr, _Alloc>& obj) \
85  { \
86  obj.clear(); \
87  std::string pref, stored_K, stored_V; \
88  in >> pref; \
89  if (pref != #CONTAINER) \
90  THROW_EXCEPTION( \
91  format( \
92  "Error: serialized container %s<%s,%s>'s preamble is " \
93  "wrong: '%s'", \
94  #CONTAINER, mrpt::typemeta::TTypeName<K>::get().c_str(), \
95  mrpt::typemeta::TTypeName<V>::get().c_str(), \
96  pref.c_str())) \
97  in >> stored_K; \
98  if (stored_K != \
99  std::string(mrpt::typemeta::TTypeName<K>::get().c_str())) \
100  THROW_EXCEPTION( \
101  format( \
102  "Error: serialized container %s key type %s != %s", \
103  #CONTAINER, stored_K.c_str(), \
104  mrpt::typemeta::TTypeName<K>::get().c_str())) \
105  in >> stored_V; \
106  if (stored_V != \
107  std::string(mrpt::typemeta::TTypeName<V>::get().c_str())) \
108  THROW_EXCEPTION( \
109  format( \
110  "Error: serialized container %s value type %s != %s", \
111  #CONTAINER, stored_V.c_str(), \
112  mrpt::typemeta::TTypeName<V>::get().c_str())) \
113  uint32_t n; \
114  in >> n; \
115  for (uint32_t i = 0; i < n; i++) \
116  { \
117  K key_obj; \
118  in >> key_obj; \
119  /* Create an pair (Key, empty), then read directly into the \
120  * ".second": */ \
121  typename CONTAINER<K, V, _Pr, _Alloc>::iterator it_new = \
122  obj.insert(obj.end(), std::make_pair(key_obj, V())); \
123  in >> it_new->second; \
124  } \
125  return in; \
126  }
127 
128 #define MRPTSTL_SERIALIZABLE_SIMPLE_ASSOC_CONTAINER(CONTAINER) \
129  /** Template method to serialize an associative STL container */ \
130  template <class K, class _Pr, class _Alloc> \
131  CArchive& operator<<(CArchive& out, const CONTAINER<K, _Pr, _Alloc>& obj) \
132  { \
133  out << std::string(#CONTAINER) << mrpt::typemeta::TTypeName<K>::get(); \
134  out << static_cast<uint32_t>(obj.size()); \
135  for (typename CONTAINER<K, _Pr, _Alloc>::const_iterator it = \
136  obj.begin(); \
137  it != obj.end(); ++it) \
138  out << *it; \
139  return out; \
140  } \
141  /** Template method to deserialize an associative STL container */ \
142  template <class K, class _Pr, class _Alloc> \
143  CArchive& operator>>(CArchive& in, CONTAINER<K, _Pr, _Alloc>& obj) \
144  { \
145  obj.clear(); \
146  std::string pref, stored_K; \
147  in >> pref; \
148  if (pref != #CONTAINER) \
149  THROW_EXCEPTION( \
150  format( \
151  "Error: serialized container %s<%s>'s preamble is wrong: " \
152  "'%s'", \
153  #CONTAINER, mrpt::typemeta::TTypeName<K>::get().c_str(), \
154  pref.c_str())) \
155  in >> stored_K; \
156  if (stored_K != \
157  std::string(mrpt::typemeta::TTypeName<K>::get().c_str())) \
158  THROW_EXCEPTION( \
159  format( \
160  "Error: serialized container %s key type %s != %s", \
161  #CONTAINER, stored_K.c_str(), \
162  mrpt::typemeta::TTypeName<K>::get().c_str())) \
163  uint32_t n; \
164  in >> n; \
165  for (uint32_t i = 0; i < n; i++) \
166  { \
167  K key_obj; \
168  in >> key_obj; \
169  obj.insert(key_obj); \
170  } \
171  return in; \
172  }
173 
177 
180 
183 
184 /** Template method to serialize a std::array<T,N> */
185 template <class T, size_t N>
186 CArchive& operator<<(CArchive& out, const std::array<T, N>& obj)
187 {
188  out << std::string("std::array") << static_cast<uint32_t>(N)
190  std::for_each(
191  obj.begin(), obj.end(), metaprogramming::ObjectWriteToStream(&out));
192  return out;
193 }
194 
195 /** Template method to deserialize a std::array<T,N> */
196 template <class T, size_t N>
197 CArchive& operator>>(CArchive& in, std::array<T, N>& obj)
198 {
199  std::string pref, stored_T;
200  uint32_t stored_N;
201  in >> pref >> stored_N;
202  if (pref != "std::array" || stored_N != N)
204  "Error: serialized container %s's preambles is wrong: "
205  "'%s'",
206  mrpt::typemeta::TTypeName<std::array<T, N>>::get().c_str(),
207  pref.c_str());
208  in >> stored_T;
209  if (stored_T != std::string(mrpt::typemeta::TTypeName<T>::get().c_str()))
211  "Error: serialized container std::array< %s != %s >",
212  stored_T.c_str(), mrpt::typemeta::TTypeName<T>::get().c_str());
213  std::for_each(
214  obj.begin(), obj.end(), metaprogramming::ObjectReadFromStream(&in));
215  return in;
216 }
217 
218 /** Template method to serialize a STL pair */
219 template <class T1, class T2>
220 CArchive& operator<<(CArchive& out, const std::pair<T1, T2>& obj)
221 {
222  out << std::string("std::pair") << mrpt::typemeta::TTypeName<T1>::get()
224  out << obj.first << obj.second;
225  return out;
226 }
227 /** Template method to deserialize a STL pair */
228 template <class T1, class T2>
229 CArchive& operator>>(CArchive& in, std::pair<T1, T2>& obj)
230 {
231  std::string pref, stored_K, stored_V;
232  in >> pref;
233  if (pref != "std::pair")
235  format(
236  "Error: serialized std::pair<%s,%s>'s preamble is wrong: '%s'",
238  mrpt::typemeta::TTypeName<T2>::get().c_str(), pref.c_str()))
239  in >> stored_K;
240  if (stored_K != std::string(mrpt::typemeta::TTypeName<T1>::get().c_str()))
242  format(
243  "Error: serialized std::pair first type %s != %s",
244  stored_K.c_str(), mrpt::typemeta::TTypeName<T1>::get().c_str()))
245  in >> stored_V;
246  if (stored_V != std::string(mrpt::typemeta::TTypeName<T2>::get().c_str()))
248  format(
249  "Error: serialized std::pair second type %s != %s",
250  stored_V.c_str(), mrpt::typemeta::TTypeName<T2>::get().c_str()))
251  in >> obj.first >> obj.second;
252  return in;
253 }
254 
255 /** @} */ // end of grouping
256 }
257 
An object for reading objects from a stream, intended for being used in STL algorithms.
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
A template to obtain the type of its argument as a string at compile time.
Definition: TTypeName.h:65
#define MRPTSTL_SERIALIZABLE_SEQ_CONTAINER(CONTAINER)
GLsizei const GLchar ** string
Definition: glext.h:4101
CArchive & operator>>(CArchive &s, mrpt::aligned_std_vector< float > &a)
Definition: CArchive.cpp:303
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:52
#define MRPTSTL_SERIALIZABLE_ASSOC_CONTAINER(CONTAINER)
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:7274
#define MRPTSTL_SERIALIZABLE_SIMPLE_ASSOC_CONTAINER(CONTAINER)
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:43
unsigned __int32 uint32_t
Definition: rptypes.h:47
static constexpr auto get()
Definition: TTypeName.h:67
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: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020