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
42namespace boost {
43namespace serialization {
44
45template<class Archive>
46struct variant_save_visitor :
47 boost::static_visitor<>
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 }
57private:
58 Archive & m_ar;
59};
60
61template<class Archive, BOOST_VARIANT_ENUM_PARAMS(/* typename */ class T)>
62void 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
73template<class S>
74struct 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;
103 ar.reset_object_address(& boost::get<head_type>(v), & value);
104 return;
105 }
106 typedef typename mpl::pop_front<S>::type type;
107 variant_impl<type>::load(ar, which - 1, v, version);
108 }
109 };
110
111 template<class Archive, class V>
112 static void load(
113 Archive & ar,
114 int which,
115 V & v,
116 const unsigned int version
117 ){
118 typedef typename mpl::eval_if<mpl::empty<S>,
119 mpl::identity<load_null>,
120 mpl::identity<load_impl>
121 >::type typex;
122 typex::invoke(ar, which, v, version);
123 }
124
125};
126
127template<class Archive, BOOST_VARIANT_ENUM_PARAMS(/* typename */ class T)>
128void load(
129 Archive & ar,
130 boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>& v,
131 const unsigned int version
132){
133 int which;
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 e: boost::archive::archive_exception(
140 boost::archive::archive_exception::unsupported_version
141 )
142 );
143 variant_impl<types>::load(ar, which, v, version);
144}
145
146template<class Archive,BOOST_VARIANT_ENUM_PARAMS(/* typename */ class T)>
147inline void serialize(
148 Archive & ar,
149 boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> & v,
150 const unsigned int file_version
151){
152 split_free(ar,v,file_version);
153}
154
155} // namespace serialization
156} // namespace boost
157
158#endif //BOOST_SERIALIZATION_VARIANT_HPP
159

source code of boost/boost/serialization/variant.hpp