cereal
A C++11 library for serialization
 All Classes Files Functions Variables Typedefs Enumerations Groups
boost_variant.hpp
1 /*
2  Copyright (c) 2013, Randolph Voorhies, Shane Grant
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of cereal nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 #ifndef CEREAL_TYPES_BOOST_VARIANT_HPP_
28 #define CEREAL_TYPES_BOOST_VARIANT_HPP_
29 
30 #include <cereal/cereal.hpp>
31 #include <boost/variant.hpp>
32 #include <boost/mpl/size.hpp>
33 
34 namespace cereal
35 {
36  namespace binary_detail
37  {
38  template <class Archive>
39  struct variant_save_visitor : boost::static_visitor<>
40  {
41  variant_save_visitor(Archive & ar) : ar(ar) {}
42 
43  template<class T>
44  void operator()(T const & value) const
45  {
46  ar( _CEREAL_NVP("data", value) );
47  }
48 
49  Archive & ar;
50  };
51 
52  template<int N, class Variant, class ... Args, class Archive>
53  typename std::enable_if<N == boost::mpl::size<typename Variant::types>::value, void>::type
54  load_variant(Archive & ar, int target, Variant & variant)
55  {
56  throw ::cereal::Exception("Error traversing variant during load");
57  }
58 
59  template<int N, class Variant, class H, class ... T, class Archive>
60  typename std::enable_if<N < boost::mpl::size<typename Variant::types>::value, void>::type
61  load_variant(Archive & ar, int target, Variant & variant)
62  {
63  if(N == target)
64  {
65  H value;
66  ar( value );
67  variant = value;
68  }
69  else
70  load_variant<N+1, Variant, T...>(ar, target, variant);
71  }
72 
73  } // namespace binary_detail
74 
76  template <class Archive, typename... VariantTypes> inline
77  void save( Archive & ar, boost::variant<VariantTypes...> const & variant )
78  {
79  int32_t which = variant.which();
80  ar( _CEREAL_NVP("which", which) );
81  binary_detail::variant_save_visitor<Archive> visitor(ar);
82  variant.apply_visitor(visitor);
83  }
84 
86  template <class Archive, typename... VariantTypes> inline
87  void load( Archive & ar, boost::variant<VariantTypes...> & variant )
88  {
89  typedef typename boost::variant<VariantTypes...>::types types;
90 
91  int32_t which;
92  ar( which );
93  if(which >= boost::mpl::size<types>::value)
94  throw Exception("Invalid 'which' selector when deserializing boost::variant");
95 
96  binary_detail::load_variant<0, boost::variant<VariantTypes...>, VariantTypes...>(ar, which, variant);
97  }
98 } // namespace cereal
99 
100 #endif // CEREAL_TYPES_BOOST_VARIANT_HPP_