TPIE

v1.1rc1-6-g0c97303
serialization2.h
Go to the documentation of this file.
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 // Copyright 2010, The TPIE development team
4 //
5 // This file is part of TPIE.
6 //
7 // TPIE is free software: you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with TPIE. If not, see <http://www.gnu.org/licenses/>
19 
20 #ifndef TPIE_SERIALIZATION2_H
21 #define TPIE_SERIALIZATION2_H
22 
31 
32 #include <tpie/config.h>
33 #include <tpie/portability.h>
34 #include <typeinfo>
35 #include <boost/type_traits/is_pod.hpp>
36 #include <boost/type_traits/is_pointer.hpp>
37 #include <boost/utility/enable_if.hpp>
38 #include <tpie/is_simple_iterator.h>
39 
40 namespace tpie {
41 
42 #ifdef DOXYGEN
43 
45 // The following two declarations are for documentation purposes only.
47 
64 template <typename D>
65 void serialize(D & dst, const foo & v);
66 
80 template <typename S>
81 void unserialize(S & src, foo & v);
82 
83 #endif // DOXYGEN
84 // Library implementations of tpie::serialize and tpie::unserialize.
87 
88 template <bool> struct is_trivially_serializable_enable_if { };
89 template <> struct is_trivially_serializable_enable_if<true> {typedef void type;};
90 
91 template <typename T>
93 private:
94  template <typename TT>
95  static char magic(TT *, typename is_simple_iterator_enable_if<TT::is_trivially_serializable>::type *_=0);
96 
97  template <typename TT>
98  static long magic(...);
99 public:
100  static bool const value=
101  boost::is_pod<T>::value || sizeof(magic<T>((T*)0))==sizeof(char);
102 };
103 
107 template <typename D, typename T>
108 void serialize(D & dst, const T & v,
109  typename boost::enable_if<is_trivially_serializable<T> >::type * = 0,
110  typename boost::disable_if<boost::is_pointer<T> >::type * = 0) {
111  dst.write((const char *)&v, sizeof(T));
112 }
113 
117 template <typename S, typename T>
118 void unserialize(S & src, T & v,
119  typename boost::enable_if<is_trivially_serializable<T> >::type * = 0,
120  typename boost::disable_if<boost::is_pointer<T> >::type * = 0) {
121  src.read((char *)&v, sizeof(T));
122 }
123 
124 namespace bits {
125 
130 template <typename D, typename T,
131  bool is_simple_itr=tpie::is_simple_iterator<T>::value,
132  bool is_pod=boost::is_pod<typename std::iterator_traits<T>::value_type>::value,
133  bool is_pointer=boost::is_pointer<typename std::iterator_traits<T>::value_type>::value>
135  void operator()(D & dst, T start, T end) {
136  using tpie::serialize;
137  for (T i=start; i != end; ++i) serialize(dst, *i);
138  }
139 };
140 
141 template <typename D, typename T>
142 struct array_encode_magic<D, T, true, true, false> {
143  void operator()(D & d, T start, T end) {
144  const char * from = reinterpret_cast<const char *>(&*start);
145  const char * to = reinterpret_cast<const char *>(&*end);
146  d.write(from, to-from);
147  }
148 };
149 
154 template <typename D, typename T,
155  bool is_simple_itr=tpie::is_simple_iterator<T>::value,
156  bool is_pod=boost::is_pod<typename std::iterator_traits<T>::value_type>::value,
157  bool is_pointer=boost::is_pointer<typename std::iterator_traits<T>::value_type>::value>
159  void operator()(D & dst, T start, T end) {
160  using tpie::unserialize;
161  for (T i=start; i != end; ++i) unserialize(dst, *i);
162  }
163 };
164 
165 template <typename D, typename T>
166 struct array_decode_magic<D, T, true, true, false> {
167  void operator()(D & d, T start, T end) {
168  char * from = reinterpret_cast<char *>(&*start);
169  char * to = reinterpret_cast<char *>(&*end);
170  d.read(from, to-from);
171  }
172 };
173 
177 struct counter {
178  size_t size;
179  counter(): size(0) {}
180  void write(const void *, size_t s) {size += s;}
181 };
182 
183 } // namespace bits
184 
191 template <typename D, typename T>
192 void serialize(D & dst, T start, T end) {
194  magic(dst, start, end);
195 }
196 
203 template <typename D, typename T>
204 void unserialize(D & dst, T start, T end) {
206  magic(dst, start, end);
207 }
208 
212 template <typename D, typename T, typename alloc_t>
213 void serialize(D & dst, const std::vector<T, alloc_t> & v) {
214  using tpie::serialize;
215  serialize(dst, v.size());
216  serialize(dst, v.begin(), v.end());
217 }
218 
222 template <typename S, typename T, typename alloc_t>
223 void unserialize(S & src, std::vector<T, alloc_t> & v) {
224  typename std::vector<T>::size_type s;
225  using tpie::unserialize;
226  unserialize(src, s);
227  v.resize(s);
228  unserialize(src, v.begin(), v.end());
229 }
230 
235 template <typename D, typename T>
236 void serialize(D & dst, const std::basic_string<T> & v) {
237  using tpie::serialize;
238  serialize(dst, v.size());
239  serialize(dst, v.begin(), v.end());
240 }
241 
246 template <typename S, typename T>
247 void unserialize(S & src, std::basic_string<T> & v) {
248  typename std::basic_string<T>::size_type s;
249  using tpie::unserialize;
250  unserialize(src, s);
251  v.resize(s);
252  unserialize(src, v.begin(), v.end());
253 }
254 
258 template <typename T>
259 size_t serialized_size(const T & v) {
260  using tpie::serialize;
261  bits::counter c;
262  serialize(c, v);
263  return c.size;
264 }
265 
266 } // namespace tpie
267 
268 #endif // TPIE_SERIALIZATION2_H
Helper to facilitate fast serialization of trivially copyable arrays.
size_t serialized_size(const T &v)
Given a serializable, serialize it and measure its serialized size.
Checks if an iterator is simple.
This file contains a few deprecated definitions for legacy code.
void unserialize(S &src, foo &v)
Sample tpie::unserialize prototype.
Helper to facilitate fast unserialization of trivially copyable arrays.
Helper to count the serialized size of objects.
void serialize(D &dst, const foo &v)
Sample tpie::serialize prototype.