comparison DEPENDENCIES/generic/include/boost/serialization/variant.hpp @ 16:2665513ce2d3

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