Main MRPT website > C++ reference for 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
23 {
24 namespace serialization
25 {
26 /** \addtogroup stlext_grp
27  * @{ */
28 
29 #define MRPTSTL_SERIALIZABLE_SEQ_CONTAINER(CONTAINER) \
30  /** Template method to serialize a sequential STL container */ \
31  template <class T, class _Ax> \
32  CArchive& operator<<(CArchive& out, const CONTAINER<T, _Ax>& obj) \
33  { \
34  out << std::string(#CONTAINER) << mrpt::typemeta::TTypeName<T>::get(); \
35  out << static_cast<uint32_t>(obj.size()); \
36  std::for_each( \
37  obj.begin(), obj.end(), \
38  metaprogramming::ObjectWriteToStream(&out)); \
39  return out; \
40  } \
41  /** Template method to deserialize a sequential STL container */ \
42  template <class T, class _Ax> \
43  CArchive& operator>>(CArchive& in, CONTAINER<T, _Ax>& obj) \
44  { \
45  obj.clear(); \
46  std::string pref, stored_T; \
47  in >> pref; \
48  if (pref != #CONTAINER) \
49  THROW_EXCEPTION_FMT( \
50  "Error: serialized container %s<%s>'s preambles is wrong: " \
51  "'%s'", \
52  #CONTAINER, mrpt::typemeta::TTypeName<T>::get().c_str(), \
53  pref.c_str()) \
54  in >> stored_T; \
55  if (stored_T != \
56  std::string(mrpt::typemeta::TTypeName<T>::get().c_str())) \
57  THROW_EXCEPTION_FMT( \
58  "Error: serialized container %s< %s != %s >", #CONTAINER, \
59  stored_T.c_str(), mrpt::typemeta::TTypeName<T>::get().c_str()) \
60  uint32_t n; \
61  in >> n; \
62  obj.resize(n); \
63  std::for_each( \
64  obj.begin(), obj.end(), \
65  metaprogramming::ObjectReadFromStream(&in)); \
66  return in; \
67  }
68 
69 #define MRPTSTL_SERIALIZABLE_ASSOC_CONTAINER(CONTAINER) \
70  /** Template method to serialize an associative STL container */ \
71  template <class K, class V, class _Pr, class _Alloc> \
72  CArchive& operator<<( \
73  CArchive& out, const CONTAINER<K, V, _Pr, _Alloc>& obj) \
74  { \
75  out << std::string(#CONTAINER) << mrpt::typemeta::TTypeName<K>::get() \
76  << mrpt::typemeta::TTypeName<V>::get(); \
77  out << static_cast<uint32_t>(obj.size()); \
78  for (typename CONTAINER<K, V, _Pr, _Alloc>::const_iterator it = \
79  obj.begin(); \
80  it != obj.end(); ++it) \
81  out << it->first << it->second; \
82  return out; \
83  } \
84  /** Template method to deserialize an associative STL container */ \
85  template <class K, class V, class _Pr, class _Alloc> \
86  CArchive& operator>>(CArchive& in, CONTAINER<K, V, _Pr, _Alloc>& obj) \
87  { \
88  obj.clear(); \
89  std::string pref, stored_K, stored_V; \
90  in >> pref; \
91  if (pref != #CONTAINER) \
92  THROW_EXCEPTION( \
93  format( \
94  "Error: serialized container %s<%s,%s>'s preamble is " \
95  "wrong: '%s'", \
96  #CONTAINER, mrpt::typemeta::TTypeName<K>::get().c_str(), \
97  mrpt::typemeta::TTypeName<V>::get().c_str(), \
98  pref.c_str())) \
99  in >> stored_K; \
100  if (stored_K != \
101  std::string(mrpt::typemeta::TTypeName<K>::get().c_str())) \
102  THROW_EXCEPTION( \
103  format( \
104  "Error: serialized container %s key type %s != %s", \
105  #CONTAINER, stored_K.c_str(), \
106  mrpt::typemeta::TTypeName<K>::get().c_str())) \
107  in >> stored_V; \
108  if (stored_V != \
109  std::string(mrpt::typemeta::TTypeName<V>::get().c_str())) \
110  THROW_EXCEPTION( \
111  format( \
112  "Error: serialized container %s value type %s != %s", \
113  #CONTAINER, stored_V.c_str(), \
114  mrpt::typemeta::TTypeName<V>::get().c_str())) \
115  uint32_t n; \
116  in >> n; \
117  for (uint32_t i = 0; i < n; i++) \
118  { \
119  K key_obj; \
120  in >> key_obj; \
121  /* Create an pair (Key, empty), then read directly into the \
122  * ".second": */ \
123  typename CONTAINER<K, V, _Pr, _Alloc>::iterator it_new = \
124  obj.insert(obj.end(), std::make_pair(key_obj, V())); \
125  in >> it_new->second; \
126  } \
127  return in; \
128  }
129 
130 #define MRPTSTL_SERIALIZABLE_SIMPLE_ASSOC_CONTAINER(CONTAINER) \
131  /** Template method to serialize an associative STL container */ \
132  template <class K, class _Pr, class _Alloc> \
133  CArchive& operator<<(CArchive& out, const CONTAINER<K, _Pr, _Alloc>& obj) \
134  { \
135  out << std::string(#CONTAINER) << mrpt::typemeta::TTypeName<K>::get(); \
136  out << static_cast<uint32_t>(obj.size()); \
137  for (typename CONTAINER<K, _Pr, _Alloc>::const_iterator it = \
138  obj.begin(); \
139  it != obj.end(); ++it) \
140  out << *it; \
141  return out; \
142  } \
143  /** Template method to deserialize an associative STL container */ \
144  template <class K, class _Pr, class _Alloc> \
145  CArchive& operator>>(CArchive& in, CONTAINER<K, _Pr, _Alloc>& obj) \
146  { \
147  obj.clear(); \
148  std::string pref, stored_K; \
149  in >> pref; \
150  if (pref != #CONTAINER) \
151  THROW_EXCEPTION( \
152  format( \
153  "Error: serialized container %s<%s>'s preamble is wrong: " \
154  "'%s'", \
155  #CONTAINER, mrpt::typemeta::TTypeName<K>::get().c_str(), \
156  pref.c_str())) \
157  in >> stored_K; \
158  if (stored_K != \
159  std::string(mrpt::typemeta::TTypeName<K>::get().c_str())) \
160  THROW_EXCEPTION( \
161  format( \
162  "Error: serialized container %s key type %s != %s", \
163  #CONTAINER, stored_K.c_str(), \
164  mrpt::typemeta::TTypeName<K>::get().c_str())) \
165  uint32_t n; \
166  in >> n; \
167  for (uint32_t i = 0; i < n; i++) \
168  { \
169  K key_obj; \
170  in >> key_obj; \
171  obj.insert(key_obj); \
172  } \
173  return in; \
174  }
175 
179 
182 
185 
186 /** Template method to serialize a std::array<T,N> */
187 template <class T, size_t N>
188 CArchive& operator<<(CArchive& out, const std::array<T, N>& obj)
189 {
190  out << std::string("std::array") << static_cast<uint32_t>(N)
192  std::for_each(
193  obj.begin(), obj.end(), metaprogramming::ObjectWriteToStream(&out));
194  return out;
195 }
196 
197 /** Template method to deserialize a std::array<T,N> */
198 template <class T, size_t N>
199 CArchive& operator>>(CArchive& in, std::array<T, N>& obj)
200 {
201  std::string pref, stored_T;
202  uint32_t stored_N;
203  in >> pref >> stored_N;
204  if (pref != "std::array" || stored_N != N)
206  "Error: serialized container %s's preambles is wrong: "
207  "'%s'",
208  mrpt::typemeta::TTypeName<std::array<T, N>>::get().c_str(),
209  pref.c_str());
210  in >> stored_T;
211  if (stored_T != std::string(mrpt::typemeta::TTypeName<T>::get().c_str()))
213  "Error: serialized container std::array< %s != %s >",
214  stored_T.c_str(), mrpt::typemeta::TTypeName<T>::get().c_str());
215  std::for_each(
216  obj.begin(), obj.end(), metaprogramming::ObjectReadFromStream(&in));
217  return in;
218 }
219 
220 /** Template method to serialize a STL pair */
221 template <class T1, class T2>
222 CArchive& operator<<(CArchive& out, const std::pair<T1, T2>& obj)
223 {
224  out << std::string("std::pair") << mrpt::typemeta::TTypeName<T1>::get()
226  out << obj.first << obj.second;
227  return out;
228 }
229 /** Template method to deserialize a STL pair */
230 template <class T1, class T2>
231 CArchive& operator>>(CArchive& in, std::pair<T1, T2>& obj)
232 {
233  std::string pref, stored_K, stored_V;
234  in >> pref;
235  if (pref != "std::pair")
237  format(
238  "Error: serialized std::pair<%s,%s>'s preamble is wrong: '%s'",
240  mrpt::typemeta::TTypeName<T2>::get().c_str(), pref.c_str()))
241  in >> stored_K;
242  if (stored_K != std::string(mrpt::typemeta::TTypeName<T1>::get().c_str()))
244  format(
245  "Error: serialized std::pair first type %s != %s",
246  stored_K.c_str(), mrpt::typemeta::TTypeName<T1>::get().c_str()))
247  in >> stored_V;
248  if (stored_V != std::string(mrpt::typemeta::TTypeName<T2>::get().c_str()))
250  format(
251  "Error: serialized std::pair second type %s != %s",
252  stored_V.c_str(), mrpt::typemeta::TTypeName<T2>::get().c_str()))
253  in >> obj.first >> obj.second;
254  return in;
255 }
256 
257 /** @} */ // end of grouping
258 } // End of namespace
259 } // End of namespace
MRPTSTL_SERIALIZABLE_ASSOC_CONTAINER
#define MRPTSTL_SERIALIZABLE_ASSOC_CONTAINER(CONTAINER)
Definition: stl_serialization.h:69
MRPTSTL_SERIALIZABLE_SIMPLE_ASSOC_CONTAINER
#define MRPTSTL_SERIALIZABLE_SIMPLE_ASSOC_CONTAINER(CONTAINER)
Definition: stl_serialization.h:129
mrpt::serialization::metaprogramming::ObjectReadFromStream
An object for reading objects from a stream, intended for being used in STL algorithms.
Definition: metaprogramming_serialization.h:24
obj
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
MRPTSTL_SERIALIZABLE_SEQ_CONTAINER
#define MRPTSTL_SERIALIZABLE_SEQ_CONTAINER(CONTAINER)
Definition: stl_serialization.h:29
mrpt::typemeta::TTypeName
A template to obtain the type of its argument as a string at compile time.
Definition: TTypeName.h:65
THROW_EXCEPTION_FMT
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:43
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::serialization::operator>>
CArchive & operator>>(CArchive &s, mrpt::aligned_std_vector< float > &a)
Definition: CArchive.cpp:291
THROW_EXCEPTION
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
mrpt::serialization::CArchive
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:48
mrpt::format
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
mrpt::typemeta::TTypeName::get
constexpr static auto get()
Definition: TTypeName.h:67
metaprogramming_serialization.h
mrpt::serialization::operator<<
CArchive & operator<<(CArchive &s, const mrpt::aligned_std_vector< float > &a)
Definition: CArchive.cpp:240
mrpt::serialization::metaprogramming::ObjectWriteToStream
An object for writing objects to a stream, intended for being used in STL algorithms.
Definition: metaprogramming_serialization.h:65
in
GLuint in
Definition: glext.h:7274
string
GLsizei const GLchar ** string
Definition: glext.h:4101
CArchive.h
uint32_t
unsigned __int32 uint32_t
Definition: rptypes.h:47
TTypeName_stl.h



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST