7 #ifndef BIL_BINARYSERIALIZATION_HPP
8 #define BIL_BINARYSERIALIZATION_HPP
15 #include <boost/type_traits/is_pod.hpp>
16 #include <boost/utility/enable_if.hpp>
29 template <
typename T>
inline
30 typename boost::enable_if<boost::is_pod<T>,
void>::type
34 outputStream.write(reinterpret_cast<const char*>(&data),
sizeof(T));
45 template <
typename T>
inline
46 typename boost::enable_if<boost::is_pod<T>,
void>::type
47 writeBinary(
const std::vector<T>& data, std::ostream& outputStream)
50 size_t length = data.size();
51 outputStream.write(reinterpret_cast<const char*>(&length),
sizeof(length));
56 outputStream.write(reinterpret_cast<const char*>(&(data[0])),
sizeof(T) * length);
68 template <
typename T>
inline
69 typename boost::disable_if<boost::is_pod<T>,
void>::type
70 writeBinary(
const std::vector<T>& data, std::ostream& outputStream)
73 size_t length = data.size();
74 outputStream.write(reinterpret_cast<const char*>(&length),
sizeof(length));
75 if (!outputStream)
throw IOException();
77 for (
size_t i = 0; i < length; ++i)
89 template <
typename K,
typename V,
typename C,
typename A>
inline
90 void writeBinary(
const std::map<K, V, C, A>& data, std::ostream& outputStream)
93 size_t length = data.size();
94 outputStream.write(reinterpret_cast<const char*>(&length),
sizeof(length));
97 std::map<K, V, C, A>::const_iterator it = data.begin();
98 std::map<K, V, C, A>::const_iterator itEnd = data.end();
99 for (; it != itEnd; ++it)
113 void writeBinary(
const std::string& data, std::ostream& outputStream);
123 template <
typename T>
inline
124 typename boost::enable_if<boost::is_pod<T>,
void>::type
128 inputStream.read(reinterpret_cast<char*>(&data),
sizeof(T));
139 template <
typename T>
inline
140 typename boost::enable_if<boost::is_pod<T>,
void>::type
145 inputStream.read(reinterpret_cast<char*>(&length),
sizeof(length));
151 inputStream.read(reinterpret_cast<char*>(&(data[0])),
sizeof(T) * length);
163 template <
typename T>
inline
164 typename boost::disable_if<boost::is_pod<T>,
void>::type
165 readBinary(std::vector<T>& data, std::istream& inputStream)
169 inputStream.read(reinterpret_cast<char*>(&length),
sizeof(length));
170 if (!inputStream)
throw IOException();
173 for (
size_t i = 0; i < length; ++i)
185 template <
typename K,
typename V,
typename C,
typename A>
inline
186 void readBinary(std::map<K, V, C, A>& data, std::istream& inputStream)
189 size_t length = data.size();
190 inputStream.read(reinterpret_cast<char*>(&length),
sizeof(length));
194 for (
size_t i = 0; i < length; ++i)
200 std::pair<std::map<K, V>::iterator,
bool> ret =
201 data.insert(std::make_pair(key, V()));
202 V& value = (ret.first)->second;
214 void readBinary(std::string& data, std::istream& inputStream);