| 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 |
#pragma once |
| 38 |
|
| 39 |
#include <boost/variant/variant.hpp> |
| 40 |
#include <boost/variant/apply_visitor.hpp> |
| 41 |
#include <boost/variant/static_visitor.hpp> |
| 42 |
#include <boost/mpl/empty.hpp> |
| 43 |
#include <boost/mpl/if.hpp> |
| 44 |
#include <boost/mpl/front.hpp> |
| 45 |
#include <boost/mpl/pop_front.hpp> |
| 46 |
#include "serialization.h" |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
template <class Archive, class T> |
| 55 |
struct variant_serialization_traits |
| 56 |
{ |
| 57 |
}; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
template <class Archive, class Variant, class TBegin, class TEnd> |
| 64 |
struct variant_reader |
| 65 |
{ |
| 66 |
typedef typename Archive::variant_tag_type variant_tag_type; |
| 67 |
typedef typename boost::mpl::next<TBegin>::type TNext; |
| 68 |
typedef typename boost::mpl::deref<TBegin>::type current_type; |
| 69 |
|
| 70 |
|
| 71 |
static inline bool read(Archive &ar, Variant &v, variant_tag_type t) |
| 72 |
{ |
| 73 |
if(variant_serialization_traits<Archive, current_type>::get_tag() == t) { |
| 74 |
current_type x; |
| 75 |
if(!do_serialize(ar, x)) |
| 76 |
{ |
| 77 |
ar.set_fail(); |
| 78 |
return false; |
| 79 |
} |
| 80 |
v = x; |
| 81 |
} else { |
| 82 |
|
| 83 |
return variant_reader<Archive, Variant, TNext, TEnd>::read(ar, v, t); |
| 84 |
} |
| 85 |
return true; |
| 86 |
} |
| 87 |
}; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
template <class Archive, class Variant, class TBegin> |
| 92 |
struct variant_reader<Archive, Variant, TBegin, TBegin> |
| 93 |
{ |
| 94 |
typedef typename Archive::variant_tag_type variant_tag_type; |
| 95 |
|
| 96 |
static inline bool read(Archive &ar, Variant &v, variant_tag_type t) |
| 97 |
{ |
| 98 |
ar.set_fail(); |
| 99 |
return false; |
| 100 |
} |
| 101 |
}; |
| 102 |
|
| 103 |
template <template <bool> class Archive, typename... T> |
| 104 |
static bool do_serialize(Archive<false> &ar, boost::variant<T...> &v) { |
| 105 |
using types = typename boost::variant<T...>::types; |
| 106 |
typename Archive<false>::variant_tag_type t; |
| 107 |
ar.begin_variant(); |
| 108 |
ar.read_variant_tag(t); |
| 109 |
if(!variant_reader<Archive<false>, boost::variant<T...>, |
| 110 |
typename boost::mpl::begin<types>::type, |
| 111 |
typename boost::mpl::end<types>::type>::read(ar, v, t)) |
| 112 |
{ |
| 113 |
ar.set_fail(); |
| 114 |
return false; |
| 115 |
} |
| 116 |
ar.end_variant(); |
| 117 |
return true; |
| 118 |
} |
| 119 |
|
| 120 |
template <template <bool> class Archive> |
| 121 |
struct variant_write_visitor : public boost::static_visitor<bool> |
| 122 |
{ |
| 123 |
Archive<true> &ar; |
| 124 |
|
| 125 |
variant_write_visitor(Archive<true> &a) : ar(a) { } |
| 126 |
|
| 127 |
template <class T> |
| 128 |
bool operator ()(T &rv) const |
| 129 |
{ |
| 130 |
ar.begin_variant(); |
| 131 |
ar.write_variant_tag(variant_serialization_traits<Archive<true>, T>::get_tag()); |
| 132 |
if(!do_serialize(ar, rv)) |
| 133 |
{ |
| 134 |
ar.set_fail(); |
| 135 |
return false; |
| 136 |
} |
| 137 |
ar.end_variant(); |
| 138 |
return true; |
| 139 |
} |
| 140 |
}; |
| 141 |
|
| 142 |
template <template <bool> class Archive, typename... T> |
| 143 |
static bool do_serialize(Archive<true> &ar, boost::variant<T...> &v) |
| 144 |
{ |
| 145 |
return boost::apply_visitor(variant_write_visitor<Archive>(ar), v); |
| 146 |
} |