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 ar.reset_object_address(& boost::get<head_type>(v), & value);
106 typedef typename mpl::pop_front<S>::type type;
107 variant_impl<type>::load(ar, which - 1, v, version);
111 template<class Archive, class V>
116 const unsigned int version
118 typedef typename mpl::eval_if<mpl::empty<S>,
119 mpl::identity<load_null>,
120 mpl::identity<load_impl>
122 typex::invoke(ar, which, v, version);
127 template<class Archive, BOOST_VARIANT_ENUM_PARAMS(/* typename */ class T)>
130 boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>& v,
131 const unsigned int version
134 typedef typename boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>::types types;
135 ar >> BOOST_SERIALIZATION_NVP(which);
136 if(which >= mpl::size<types>::value)
137 // this might happen if a type was removed from the list of variant types
138 boost::serialization::throw_exception(
139 boost::archive::archive_exception(
140 boost::archive::archive_exception::unsupported_version
143 variant_impl<types>::load(ar, which, v, version);
146 template<class Archive,BOOST_VARIANT_ENUM_PARAMS(/* typename */ class T)>
147 inline void serialize(
149 boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> & v,
150 const unsigned int file_version
152 split_free(ar,v,file_version);
155 } // namespace serialization
158 #endif //BOOST_SERIALIZATION_VARIANT_HPP