| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
#pragma once |
| 43 |
#include <vector> |
| 44 |
#include <deque> |
| 45 |
#include <list> |
| 46 |
#include <set> |
| 47 |
#include <unordered_set> |
| 48 |
#include <string> |
| 49 |
#include <boost/type_traits/is_integral.hpp> |
| 50 |
#include <boost/type_traits/integral_constant.hpp> |
| 51 |
#include <boost/mpl/bool.hpp> |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
template <class T> |
| 58 |
struct is_blob_type { typedef boost::false_type type; }; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
template <class Archive, class T> |
| 69 |
inline std::enable_if_t<is_blob_type<T>::type::value, bool> do_serialize(Archive &ar, T &v) |
| 70 |
{ |
| 71 |
ar.serialize_blob(&v, sizeof(v)); |
| 72 |
return true; |
| 73 |
} |
| 74 |
template <class Archive, class T> |
| 75 |
inline std::enable_if_t<boost::is_integral<T>::value, bool> do_serialize(Archive &ar, T &v) |
| 76 |
{ |
| 77 |
ar.serialize_int(v); |
| 78 |
return true; |
| 79 |
} |
| 80 |
template <class Archive, class T> |
| 81 |
inline auto do_serialize(Archive &ar, T &v) -> decltype(v.member_do_serialize(ar), true) |
| 82 |
{ |
| 83 |
return v.member_do_serialize(ar); |
| 84 |
} |
| 85 |
template <class Archive> |
| 86 |
inline bool do_serialize(Archive &ar, bool &v) |
| 87 |
{ |
| 88 |
ar.serialize_blob(&v, sizeof(v)); |
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
#define BLOB_SERIALIZER(T) \ |
| 99 |
template<> \ |
| 100 |
struct is_blob_type<T> { \ |
| 101 |
typedef boost::true_type type; \ |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
#define VARIANT_TAG(Archive, Type, Tag) \ |
| 109 |
template <bool W> \ |
| 110 |
struct variant_serialization_traits<Archive<W>, Type> { \ |
| 111 |
static inline typename Archive<W>::variant_tag_type get_tag() { \ |
| 112 |
return Tag; \ |
| 113 |
} \ |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
#define BEGIN_SERIALIZE() \ |
| 123 |
template <bool W, template <bool> class Archive> \ |
| 124 |
bool member_do_serialize(Archive<W> &ar) { |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
#define BEGIN_SERIALIZE_OBJECT() \ |
| 132 |
template <bool W, template <bool> class Archive> \ |
| 133 |
bool member_do_serialize(Archive<W> &ar) { \ |
| 134 |
ar.begin_object(); \ |
| 135 |
bool r = do_serialize_object(ar); \ |
| 136 |
ar.end_object(); \ |
| 137 |
return r; \ |
| 138 |
} \ |
| 139 |
template <bool W, template <bool> class Archive> \ |
| 140 |
bool do_serialize_object(Archive<W> &ar){ |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
#define PREPARE_CUSTOM_VECTOR_SERIALIZATION(size, vec) \ |
| 145 |
::serialization::detail::prepare_custom_vector_serialization(size, vec, typename Archive<W>::is_saving()) |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
#define END_SERIALIZE() \ |
| 151 |
return ar.good(); \ |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
#define FIELD_N(t, f) \ |
| 159 |
do { \ |
| 160 |
ar.tag(t); \ |
| 161 |
bool r = do_serialize(ar, f); \ |
| 162 |
if (!r || !ar.good()) return false; \ |
| 163 |
} while(0); |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
#define FIELD(f) \ |
| 170 |
do { \ |
| 171 |
ar.tag(#f); \ |
| 172 |
bool r = do_serialize(ar, f); \ |
| 173 |
if (!r || !ar.good()) return false; \ |
| 174 |
} while(0); |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
#define FIELDS(f) \ |
| 181 |
do { \ |
| 182 |
bool r = do_serialize(ar, f); \ |
| 183 |
if (!r || !ar.good()) return false; \ |
| 184 |
} while(0); |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
#define VARINT_FIELD(f) \ |
| 190 |
do { \ |
| 191 |
ar.tag(#f); \ |
| 192 |
ar.serialize_varint(f); \ |
| 193 |
if (!ar.good()) return false; \ |
| 194 |
} while(0); |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
#define VARINT_FIELD_N(t, f) \ |
| 201 |
do { \ |
| 202 |
ar.tag(t); \ |
| 203 |
ar.serialize_varint(f); \ |
| 204 |
if (!ar.good()) return false; \ |
| 205 |
} while(0); |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
#define MAGIC_FIELD(m) \ |
| 210 |
std::string magic = m; \ |
| 211 |
do { \ |
| 212 |
ar.tag("magic"); \ |
| 213 |
ar.serialize_blob((void*)magic.data(), magic.size()); \ |
| 214 |
if (!ar.good()) return false; \ |
| 215 |
if (magic != m) return false; \ |
| 216 |
} while(0); |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
#define VERSION_FIELD(v) \ |
| 221 |
uint32_t version = v; \ |
| 222 |
do { \ |
| 223 |
ar.tag("version"); \ |
| 224 |
ar.serialize_varint(version); \ |
| 225 |
if (!ar.good()) return false; \ |
| 226 |
} while(0); |
| 227 |
|
| 228 |
|
| 229 |
namespace serialization { |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
namespace detail |
| 236 |
{ |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
template <typename T> |
| 242 |
void prepare_custom_vector_serialization(size_t size, std::vector<T>& vec, const boost::mpl::bool_<true>& ) |
| 243 |
{ |
| 244 |
} |
| 245 |
|
| 246 |
template <typename T> |
| 247 |
void prepare_custom_vector_serialization(size_t size, std::vector<T>& vec, const boost::mpl::bool_<false>& ) |
| 248 |
{ |
| 249 |
vec.resize(size); |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
template<class Archive> |
| 257 |
bool do_check_stream_state(Archive& ar, boost::mpl::bool_<true>, bool noeof) |
| 258 |
{ |
| 259 |
return ar.good(); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
template<class Archive> |
| 268 |
bool do_check_stream_state(Archive& ar, boost::mpl::bool_<false>, bool noeof) |
| 269 |
{ |
| 270 |
bool result = false; |
| 271 |
if (ar.good()) |
| 272 |
{ |
| 273 |
result = noeof || ar.eof(); |
| 274 |
} |
| 275 |
return result; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
template<class Archive> |
| 284 |
bool check_stream_state(Archive& ar, bool noeof = false) |
| 285 |
{ |
| 286 |
return detail::do_check_stream_state(ar, typename Archive::is_saving(), noeof); |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
template <class Archive, class T> |
| 294 |
inline bool serialize(Archive &ar, T &v) |
| 295 |
{ |
| 296 |
bool r = do_serialize(ar, v); |
| 297 |
return r && check_stream_state(ar, false); |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
template <class Archive, class T> |
| 305 |
inline bool serialize_noeof(Archive &ar, T &v) |
| 306 |
{ |
| 307 |
bool r = do_serialize(ar, v); |
| 308 |
return r && check_stream_state(ar, true); |
| 309 |
} |
| 310 |
} |