]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | #ifndef BOOST_SERIALIZATION_VARIANT_HPP |
2 | #define BOOST_SERIALIZATION_VARIANT_HPP | |
3 | ||
4 | // MS compatible compilers support #pragma once | |
5 | #if defined(_MSC_VER) | |
6 | # pragma once | |
7 | #endif | |
8 | ||
9 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 | |
10 | // variant.hpp - non-intrusive serialization of variant types | |
11 | // | |
12 | // copyright (c) 2005 | |
13 | // troy d. straszheim <troy@resophonic.com> | |
14 | // http://www.resophonic.com | |
15 | // | |
16 | // Use, modification and distribution is subject to the Boost Software | |
17 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at | |
18 | // http://www.boost.org/LICENSE_1_0.txt) | |
19 | // | |
20 | // See http://www.boost.org for updates, documentation, and revision history. | |
21 | // | |
22 | // thanks to Robert Ramey, Peter Dimov, and Richard Crossley. | |
23 | // | |
24 | ||
25 | #include <boost/mpl/front.hpp> | |
26 | #include <boost/mpl/pop_front.hpp> | |
27 | #include <boost/mpl/eval_if.hpp> | |
28 | #include <boost/mpl/identity.hpp> | |
29 | #include <boost/mpl/size.hpp> | |
30 | #include <boost/mpl/empty.hpp> | |
31 | ||
32 | #include <boost/serialization/throw_exception.hpp> | |
33 | ||
34 | #include <boost/variant.hpp> | |
35 | ||
36 | #include <boost/archive/archive_exception.hpp> | |
37 | ||
38 | #include <boost/serialization/split_free.hpp> | |
39 | #include <boost/serialization/serialization.hpp> | |
40 | #include <boost/serialization/nvp.hpp> | |
41 | ||
42 | namespace boost { | |
43 | namespace serialization { | |
44 | ||
45 | template<class Archive> | |
92f5a8d4 TL |
46 | struct variant_save_visitor : |
47 | boost::static_visitor<> | |
7c673cae FG |
48 | { |
49 | variant_save_visitor(Archive& ar) : | |
50 | m_ar(ar) | |
51 | {} | |
52 | template<class T> | |
53 | void operator()(T const & value) const | |
54 | { | |
55 | m_ar << BOOST_SERIALIZATION_NVP(value); | |
56 | } | |
57 | private: | |
58 | Archive & m_ar; | |
59 | }; | |
60 | ||
61 | template<class Archive, BOOST_VARIANT_ENUM_PARAMS(/* typename */ class T)> | |
62 | void save( | |
63 | Archive & ar, | |
64 | boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const & v, | |
65 | unsigned int /*version*/ | |
66 | ){ | |
67 | int which = v.which(); | |
68 | ar << BOOST_SERIALIZATION_NVP(which); | |
69 | variant_save_visitor<Archive> visitor(ar); | |
70 | v.apply_visitor(visitor); | |
71 | } | |
72 | ||
73 | template<class S> | |
74 | struct variant_impl { | |
75 | ||
76 | struct load_null { | |
77 | template<class Archive, class V> | |
78 | static void invoke( | |
79 | Archive & /*ar*/, | |
80 | int /*which*/, | |
81 | V & /*v*/, | |
82 | const unsigned int /*version*/ | |
83 | ){} | |
84 | }; | |
85 | ||
86 | struct load_impl { | |
87 | template<class Archive, class V> | |
88 | static void invoke( | |
89 | Archive & ar, | |
90 | int which, | |
91 | V & v, | |
92 | const unsigned int version | |
93 | ){ | |
94 | if(which == 0){ | |
95 | // note: A non-intrusive implementation (such as this one) | |
96 | // necessary has to copy the value. This wouldn't be necessary | |
97 | // with an implementation that de-serialized to the address of the | |
98 | // aligned storage included in the variant. | |
99 | typedef typename mpl::front<S>::type head_type; | |
100 | head_type value; | |
101 | ar >> BOOST_SERIALIZATION_NVP(value); | |
102 | v = value; | |
92f5a8d4 TL |
103 | head_type * new_address = & boost::get<head_type>(v); |
104 | ar.reset_object_address(new_address, & value); | |
7c673cae FG |
105 | return; |
106 | } | |
107 | typedef typename mpl::pop_front<S>::type type; | |
108 | variant_impl<type>::load(ar, which - 1, v, version); | |
109 | } | |
110 | }; | |
111 | ||
112 | template<class Archive, class V> | |
113 | static void load( | |
114 | Archive & ar, | |
115 | int which, | |
116 | V & v, | |
117 | const unsigned int version | |
118 | ){ | |
119 | typedef typename mpl::eval_if<mpl::empty<S>, | |
120 | mpl::identity<load_null>, | |
121 | mpl::identity<load_impl> | |
122 | >::type typex; | |
123 | typex::invoke(ar, which, v, version); | |
124 | } | |
125 | ||
126 | }; | |
127 | ||
128 | template<class Archive, BOOST_VARIANT_ENUM_PARAMS(/* typename */ class T)> | |
129 | void load( | |
130 | Archive & ar, | |
131 | boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>& v, | |
132 | const unsigned int version | |
133 | ){ | |
134 | int which; | |
135 | typedef typename boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>::types types; | |
136 | ar >> BOOST_SERIALIZATION_NVP(which); | |
137 | if(which >= mpl::size<types>::value) | |
138 | // this might happen if a type was removed from the list of variant types | |
139 | boost::serialization::throw_exception( | |
140 | boost::archive::archive_exception( | |
141 | boost::archive::archive_exception::unsupported_version | |
142 | ) | |
143 | ); | |
144 | variant_impl<types>::load(ar, which, v, version); | |
145 | } | |
146 | ||
147 | template<class Archive,BOOST_VARIANT_ENUM_PARAMS(/* typename */ class T)> | |
148 | inline void serialize( | |
149 | Archive & ar, | |
150 | boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> & v, | |
151 | const unsigned int file_version | |
152 | ){ | |
153 | split_free(ar,v,file_version); | |
154 | } | |
155 | ||
156 | } // namespace serialization | |
157 | } // namespace boost | |
158 | ||
92f5a8d4 TL |
159 | //template<typename T0_, BOOST_VARIANT_ENUM_SHIFTED_PARAMS(typename T)> |
160 | ||
161 | #include <boost/serialization/tracking.hpp> | |
162 | ||
163 | namespace boost { | |
164 | namespace serialization { | |
165 | ||
166 | template<BOOST_VARIANT_ENUM_PARAMS(/* typename */ class T)> | |
167 | struct tracking_level< | |
168 | variant<BOOST_VARIANT_ENUM_PARAMS(T)> | |
169 | >{ | |
170 | typedef mpl::integral_c_tag tag; | |
171 | typedef mpl::int_< ::boost::serialization::track_always> type; | |
172 | BOOST_STATIC_CONSTANT(int, value = type::value); | |
173 | }; | |
174 | ||
175 | } // namespace serialization | |
176 | } // namespace boost | |
177 | ||
7c673cae | 178 | #endif //BOOST_SERIALIZATION_VARIANT_HPP |