19 #ifndef __TPIE_SERIALIZATION_H__
20 #define __TPIE_SERIALIZATION_H__
26 #include <tpie/config.h>
33 #include <boost/type_traits/is_fundamental.hpp>
34 #include <boost/type_traits/is_enum.hpp>
35 #include <boost/utility/enable_if.hpp>
36 #include <boost/cstdint.hpp>
44 template <
bool b1,
bool b2>
45 struct _disjunction:
public boost::true_type {};
48 struct _disjunction<false, false>:
public boost::false_type {};
54 template <
typename T1,
typename T2>
55 struct disjunction:
public _disjunction<T1::value, T2::value> {};
72 serializer(std::ostream & out,
bool typesafe=
false): m_out(out), m_typesafe(false) {
73 *
this <<
"TPIE Serialization"
76 m_typesafe = typesafe;
80 inline serializer & write(
const T * data,
size_t l) {
81 *
this << (boost::uint16_t)l;
82 for (
size_t i=0; i < l; ++i)
87 inline typename boost::enable_if<disjunction<boost::is_fundamental<T>, boost::is_enum<T> > ,
88 serializer &>::type operator << (
const T & x) {
90 m_out.write(reinterpret_cast<const char*>(&x),
sizeof(T));
94 template <
typename T1,
typename T2>
95 inline serializer & operator <<(const std::pair<T1, T2> & p) {
96 write_type<std::pair<T1,T2> >();
97 return *
this << p.first << p.second;
100 template <
typename T>
101 inline serializer & operator <<(const std::vector<T> & v) {
102 write_type<std::vector<T> >();
103 *
this << (boost::uint16_t)v.size();
104 for (
size_t i=0; i < v.size(); ++i)
109 inline serializer & operator <<(
const char * data) {write_type<std::string>();
return write(data, strlen(data));}
110 inline serializer & operator <<(
const std::string & s) {write_type<std::string>();
return write(s.c_str(), s.size());}
112 template <
typename T>
115 hash<const char *> h;
116 m_out << (uint8_t)(h(
typeid(T).name()) % 256);
120 std::ostream & m_out;
136 *
this <<
"TPIE Serialization"
137 << (boost::uint16_t)1;
143 template <
typename T>
154 if (y != x)
throw serialization_error(
"Verification failed");
158 template <
typename T>
162 if (x > size)
throw serialization_error(
"array too short");
164 for (
size_t i=0; i < size; ++i)
169 template <
typename T>
170 inline typename boost::enable_if<disjunction<boost::is_fundamental<T>, boost::is_enum<T> >,
unserializer &>::type operator >> (T & x) {
172 char * y =
reinterpret_cast<char*
>(&x);
173 m_in.read(y,
sizeof(T));
174 if (m_in.eof() || m_in.fail())
throw serialization_error(
"Unexpected end-of-file");
178 template <
typename T1,
typename T2>
179 inline unserializer & operator >>(std::pair<T1, T2> & p) {
180 check_type<std::pair<T1, T2> >();
181 return *
this >> p.first >> p.second;
184 template <
typename T>
185 inline unserializer & operator >> (std::vector<T> & v) {
186 check_type<std::vector<T> >();
187 boost::uint16_t size;
190 for (
size_t i=0; i < size; ++i) {
198 check_type<std::string>();
200 boost::uint16_t size;
202 for (
size_t i=0; i < size; ++i) {
211 template <
typename T>
213 if (!m_typesafe)
return;
214 hash<const char *> h;
215 uint8_t hash = h(
typeid(T).name()) % 256;
218 if (s_hash == hash)
return;
219 std::stringstream ss;
220 ss <<
"Serialization type error, input type did not match expected type: " <<
typeid(T).name();
221 throw serialization_error(ss.str());
Class to compute the disjunction between two boost true/false types.
unserializer(std::istream &in)
Construct a unserializer reading from the std::istream in.
This file contains a few deprecated definitions for legacy code.
Class providing binary serialization to a std::ostream.
Internal hash map with guaranteed memory requirements.
serializer(std::ostream &out, bool typesafe=false)
Construct a serializer writing to out.
Class for unserializing binary data serialized with the serializer Data can be unserialized using the...