19 #ifndef __TPIE_SERIALIZATION_H__
20 #define __TPIE_SERIALIZATION_H__
26 #include <tpie/config.h>
33 #include <type_traits>
60 serializer(std::ostream & out,
bool typesafe=
false): m_out(out), m_typesafe(false) {
61 *
this <<
"TPIE Serialization"
64 m_typesafe = typesafe;
68 inline serializer & write(
const T * data,
size_t l) {
70 for (
size_t i=0; i < l; ++i)
75 inline typename std::enable_if<std::is_fundamental<T>::value || std::is_enum<T>::value,
76 serializer &>::type operator << (
const T & x) {
78 m_out.write(reinterpret_cast<const char*>(&x),
sizeof(T));
82 template <
typename T1,
typename T2>
83 inline serializer & operator <<(const std::pair<T1, T2> & p) {
84 write_type<std::pair<T1,T2> >();
85 return *
this << p.first << p.second;
89 inline serializer & operator <<(const std::vector<T> & v) {
90 write_type<std::vector<T> >();
91 *
this << (uint16_t)v.size();
92 for (
size_t i=0; i < v.size(); ++i)
97 inline serializer & operator <<(
const char * data) {write_type<std::string>();
return write(data, strlen(data));}
98 inline serializer & operator <<(
const std::string & s) {write_type<std::string>();
return write(s.c_str(), s.size());}
100 template <
typename T>
103 hash<const char *> h;
104 m_out << (uint8_t)(h(
typeid(T).name()) % 256);
108 std::ostream & m_out;
124 *
this <<
"TPIE Serialization"
131 template <
typename T>
142 if (y != x)
throw serialization_error(
"Verification failed");
146 template <
typename T>
150 if (x > size)
throw serialization_error(
"array too short");
152 for (
size_t i=0; i < size; ++i)
157 template <
typename T>
158 inline typename std::enable_if<std::is_fundamental<T>::value || std::is_enum<T>::value,
unserializer &>::type operator >> (T & x) {
160 char * y =
reinterpret_cast<char*
>(&x);
161 m_in.read(y,
sizeof(T));
162 if (m_in.eof() || m_in.fail())
throw serialization_error(
"Unexpected end-of-file");
166 template <
typename T1,
typename T2>
167 inline unserializer & operator >>(std::pair<T1, T2> & p) {
168 check_type<std::pair<T1, T2> >();
169 return *
this >> p.first >> p.second;
172 template <
typename T>
173 inline unserializer & operator >> (std::vector<T> & v) {
174 check_type<std::vector<T> >();
178 for (
size_t i=0; i < size; ++i) {
186 check_type<std::string>();
190 for (
size_t i=0; i < size; ++i) {
199 template <
typename T>
201 if (!m_typesafe)
return;
202 hash<const char *> h;
203 uint8_t hash = h(
typeid(T).name()) % 256;
206 if (s_hash == hash)
return;
207 std::stringstream ss;
208 ss <<
"Serialization type error, input type did not match expected type: " <<
typeid(T).name();
209 throw serialization_error(ss.str());
unserializer(std::istream &in)
Construct a unserializer reading from the std::istream in.
Class to compute the disjunction between two std true/false types.
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...