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-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<<( \
32  mrpt::utils::CStream& out, const CONTAINER<T, _Ax>& obj) \
33  { \
34  out << std::string(#CONTAINER) << mrpt::utils::TTypeName<T>::get(); \
35  out << static_cast<uint32_t>(obj.size()); \
36  std::for_each( \
37  obj.begin(), obj.end(), \
38  mrpt::utils::metaprogramming::ObjectWriteToStream(&out)); \
39  return out; \
40  } \
41  /** Template method to deserialize a sequential STL container */ \
42  template <class T, class _Ax> \
43  CStream& operator>>(mrpt::utils::CStream& 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, TTypeName<T>::get().c_str(), pref.c_str()) \
53  in >> stored_T; \
54  if (stored_T != mrpt::utils::TTypeName<T>::get()) \
55  THROW_EXCEPTION_FMT( \
56  "Error: serialized container %s< %s != %s >", #CONTAINER, \
57  stored_T.c_str(), 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  mrpt::utils::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  CStream& operator<<( \
71  mrpt::utils::CStream& out, const CONTAINER<K, V, _Pr, _Alloc>& obj) \
72  { \
73  out << std::string(#CONTAINER) << TTypeName<K>::get() \
74  << 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  CStream& operator>>( \
85  mrpt::utils::CStream& in, CONTAINER<K, V, _Pr, _Alloc>& obj) \
86  { \
87  obj.clear(); \
88  std::string pref, stored_K, stored_V; \
89  in >> pref; \
90  if (pref != #CONTAINER) \
91  THROW_EXCEPTION( \
92  format( \
93  "Error: serialized container %s<%s,%s>'s preamble is " \
94  "wrong: '%s'", \
95  #CONTAINER, TTypeName<K>::get().c_str(), \
96  TTypeName<V>::get().c_str(), pref.c_str())) \
97  in >> stored_K; \
98  if (stored_K != TTypeName<K>::get()) \
99  THROW_EXCEPTION( \
100  format( \
101  "Error: serialized container %s key type %s != %s", \
102  #CONTAINER, stored_K.c_str(), \
103  TTypeName<K>::get().c_str())) \
104  in >> stored_V; \
105  if (stored_V != TTypeName<V>::get()) \
106  THROW_EXCEPTION( \
107  format( \
108  "Error: serialized container %s value type %s != %s", \
109  #CONTAINER, stored_V.c_str(), \
110  TTypeName<V>::get().c_str())) \
111  uint32_t n; \
112  in >> n; \
113  for (uint32_t i = 0; i < n; i++) \
114  { \
115  K key_obj; \
116  in >> key_obj; \
117  /* Create an pair (Key, empty), then read directly into the \
118  * ".second": */ \
119  typename CONTAINER<K, V, _Pr, _Alloc>::iterator it_new = \
120  obj.insert(obj.end(), std::make_pair(key_obj, V())); \
121  in >> it_new->second; \
122  } \
123  return in; \
124  }
125 
127  std::vector) // Serialization for std::vector
128 MRPTSTL_SERIALIZABLE_SEQ_CONTAINER(std::deque) // Serialization for std::deque
129 MRPTSTL_SERIALIZABLE_SEQ_CONTAINER(std::list) // Serialization for std::list
130 
131 MRPTSTL_SERIALIZABLE_ASSOC_CONTAINER(std::map) // Serialization for std::map
133  std::multimap) // Serialization for std::multimap
134 
135 #define MRPTSTL_SERIALIZABLE_SIMPLE_ASSOC_CONTAINER(CONTAINER) \
136  /** Template method to serialize an associative STL container */ \
137  template <class K, class _Pr, class _Alloc> \
138  CStream& operator<<( \
139  mrpt::utils::CStream& out, const CONTAINER<K, _Pr, _Alloc>& obj) \
140  { \
141  out << std::string(#CONTAINER) << TTypeName<K>::get(); \
142  out << static_cast<uint32_t>(obj.size()); \
143  for (typename CONTAINER<K, _Pr, _Alloc>::const_iterator it = \
144  obj.begin(); \
145  it != obj.end(); ++it) \
146  out << *it; \
147  return out; \
148  } \
149  /** Template method to deserialize an associative STL container */ \
150  template <class K, class _Pr, class _Alloc> \
151  CStream& operator>>( \
152  mrpt::utils::CStream& in, CONTAINER<K, _Pr, _Alloc>& obj) \
153  { \
154  obj.clear(); \
155  std::string pref, stored_K; \
156  in >> pref; \
157  if (pref != #CONTAINER) \
158  THROW_EXCEPTION( \
159  format( \
160  "Error: serialized container %s<%s>'s preamble is wrong: " \
161  "'%s'", \
162  #CONTAINER, TTypeName<K>::get().c_str(), pref.c_str())) \
163  in >> stored_K; \
164  if (stored_K != TTypeName<K>::get()) \
165  THROW_EXCEPTION( \
166  format( \
167  "Error: serialized container %s key type %s != %s", \
168  #CONTAINER, stored_K.c_str(), \
169  TTypeName<K>::get().c_str())) \
170  uint32_t n; \
171  in >> n; \
172  for (uint32_t i = 0; i < n; i++) \
173  { \
174  K key_obj; \
175  in >> key_obj; \
176  obj.insert(key_obj); \
177  } \
178  return in; \
179  }
180 
182  std::set) // Serialization for std::set
184  std::multiset) // Serialization for std::multiset
185 
186 /** Template method to serialize a STL pair */
187 template <class T1, class T2>
188 CStream& operator<<(mrpt::utils::CStream& out, const std::pair<T1, T2>& obj)
189 {
190  out << std::string("std::pair") << TTypeName<T1>::get()
191  << TTypeName<T2>::get();
192  out << obj.first << obj.second;
193  return out;
194 }
195 /** Template method to deserialize a STL pair */
196 template <class T1, class T2>
198 {
199  std::string pref, stored_K, stored_V;
200  in >> pref;
201  if (pref != "std::pair")
203  format(
204  "Error: serialized std::pair<%s,%s>'s preamble is wrong: '%s'",
205  TTypeName<T1>::get().c_str(), TTypeName<T2>::get().c_str(),
206  pref.c_str()))
207  in >> stored_K;
208  if (stored_K != TTypeName<T1>::get())
210  format(
211  "Error: serialized std::pair first type %s != %s",
212  stored_K.c_str(), TTypeName<T1>::get().c_str()))
213  in >> stored_V;
214  if (stored_V != TTypeName<T2>::get())
216  format(
217  "Error: serialized std::pair second type %s != %s",
218  stored_V.c_str(), TTypeName<T2>::get().c_str()))
219  in >> obj.first >> obj.second;
220  return in;
221 }
222 
223 /** @} */ // end of grouping
224 } // End of namespace
225 } // End of namespace
A template to obtain the type of its argument as a string at compile time.
Definition: TTypeName.h:55
#define THROW_EXCEPTION(msg)
#define MRPTSTL_SERIALIZABLE_SIMPLE_ASSOC_CONTAINER(CONTAINER)
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:19
static std::string get()
Definition: TTypeName.h:57
GLsizei const GLchar ** string
Definition: glext.h:4101
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:7274
CStream & operator>>(mrpt::utils::CStream &in, char *a)
Definition: CStream.cpp:407



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019