1 #ifndef BOOST_SERIALIZATION_VARIANT_HPP
2 #define BOOST_SERIALIZATION_VARIANT_HPP
4 // MS compatible compilers support #pragma once
9 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
10 // variant.hpp - non-intrusive serialization of variant types
13 // troy d. straszheim <troy@resophonic.com>
14 // http://www.resophonic.com
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)
20 // See http://www.boost.org for updates, documentation, and revision history.
22 // thanks to Robert Ramey, Peter Dimov, and Richard Crossley.
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>
32 #include <boost/serialization/throw_exception.hpp>
34 #include <boost/variant.hpp>
36 #include <boost/archive/archive_exception.hpp>
38 #include <boost/serialization/split_free.hpp>
39 #include <boost/serialization/serialization.hpp>
40 #include <boost/serialization/nvp.hpp>
43 namespace serialization {
45 template<class Archive>
46 struct variant_save_visitor :
47 boost::static_visitor<>
49 variant_save_visitor(Archive& ar) :
53 void operator()(T const & value) const
55 m_ar << BOOST_SERIALIZATION_NVP(value);
61 template<class Archive, BOOST_VARIANT_ENUM_PARAMS(/* typename */ class T)>
64 boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const & v,
65 unsigned int /*version*/
67 int which = v.which();
68 ar << BOOST_SERIALIZATION_NVP(which);
69 variant_save_visitor<Archive> visitor(ar);
70 v.apply_visitor(visitor);
77 template<class Archive, class V>
82 const unsigned int /*version*/
87 template<class Archive, class V>
92 const unsigned int version
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;
101 ar >> BOOST_SERIALIZATION_NVP(value);
103 head_type * new_address = & boost::get<head_type>(v);
104 ar.reset_object_address(new_address, & value);
107 typedef typename mpl::pop_front<S>::type type;
108 variant_impl<type>::load(ar, which - 1, v, version);
112 template<class Archive, class V>
117 const unsigned int version
119 typedef typename mpl::eval_if<mpl::empty<S>,
120 mpl::identity<load_null>,
121 mpl::identity<load_impl>
123 typex::invoke(ar, which, v, version);
128 template<class Archive, BOOST_VARIANT_ENUM_PARAMS(/* typename */ class T)>
131 boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>& v,
132 const unsigned int version
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
144 variant_impl<types>::load(ar, which, v, version);
147 template<class Archive,BOOST_VARIANT_ENUM_PARAMS(/* typename */ class T)>
148 inline void serialize(
150 boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> & v,
151 const unsigned int file_version
153 split_free(ar,v,file_version);
156 } // namespace serialization
159 //template<typename T0_, BOOST_VARIANT_ENUM_SHIFTED_PARAMS(typename T)>
161 #include <boost/serialization/tracking.hpp>
164 namespace serialization {
166 template<BOOST_VARIANT_ENUM_PARAMS(/* typename */ class T)>
167 struct tracking_level<
168 variant<BOOST_VARIANT_ENUM_PARAMS(T)>
170 typedef mpl::integral_c_tag tag;
171 typedef mpl::int_< ::boost::serialization::track_always> type;
172 BOOST_STATIC_CONSTANT(int, value = type::value);
175 } // namespace serialization
178 #endif //BOOST_SERIALIZATION_VARIANT_HPP