NDDEM
boost_variant.hpp
Go to the documentation of this file.
1 
4 /*
5  Copyright (c) 2014, Randolph Voorhies, Shane Grant
6  All rights reserved.
7 
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions are met:
10  * Redistributions of source code must retain the above copyright
11  notice, this list of conditions and the following disclaimer.
12  * Redistributions in binary form must reproduce the above copyright
13  notice, this list of conditions and the following disclaimer in the
14  documentation and/or other materials provided with the distribution.
15  * Neither the name of the copyright holder nor the
16  names of its contributors may be used to endorse or promote products
17  derived from this software without specific prior written permission.
18 
19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
23  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 #ifndef CEREAL_TYPES_BOOST_VARIANT_HPP_
31 #define CEREAL_TYPES_BOOST_VARIANT_HPP_
32 
34 #if defined(_MSC_VER) && _MSC_VER < 1911
35 #define CEREAL_CONSTEXPR_LAMBDA
36 #else // MSVC 2017 or newer, all other compilers
37 #define CEREAL_CONSTEXPR_LAMBDA constexpr
38 #endif
39 
40 #include "cereal/cereal.hpp"
41 #include <boost/variant/variant_fwd.hpp>
42 #include <boost/variant/static_visitor.hpp>
43 
44 namespace cereal
45 {
46  namespace boost_variant_detail
47  {
49  template <class Archive>
50  struct variant_save_visitor : boost::static_visitor<>
51  {
52  variant_save_visitor(Archive & ar_) : ar(ar_) {}
53 
54  template<class T>
55  void operator()(T const & value) const
56  {
57  ar( CEREAL_NVP_("data", value) );
58  }
59 
60  Archive & ar;
61  };
62 
64  template <class Archive, class T>
66  {
67  using ST = typename std::aligned_storage<sizeof(T), CEREAL_ALIGNOF(T)>::type;
68 
70  construct( reinterpret_cast<T *>( &st ) )
71  { }
72 
74  {
75  if (construct.itsValid)
76  {
77  construct->~T();
78  }
79  }
80 
81  void CEREAL_SERIALIZE_FUNCTION_NAME( Archive & ar )
82  {
84  }
85 
86  ST st;
88  };
89 
91  template <class T> struct load_variant_wrapper;
92 
94 
95  template <>
96  struct load_variant_wrapper<boost::detail::variant::void_>
97  {
98  template <class Variant, class Archive>
99  static void load_variant( Archive &, Variant & )
100  { }
101  };
102 
104  template <class T>
106  {
107  // default constructible
108  template <class Archive, class Variant>
109  static void load_variant_impl( Archive & ar, Variant & variant, std::true_type )
110  {
111  T value;
112  ar( CEREAL_NVP_("data", value) );
113  variant = std::move(value);
114  }
115 
116  // not default constructible
117  template<class Variant, class Archive>
118  static void load_variant_impl(Archive & ar, Variant & variant, std::false_type )
119  {
121 
122  ar( CEREAL_NVP_("data", loadWrapper) );
123  variant = std::move(*loadWrapper.construct.ptr());
124  }
125 
127  template<class Variant, class Archive>
128  static void load_variant(Archive & ar, Variant & variant)
129  {
131  }
132  };
133  } // namespace boost_variant_detail
134 
136  template <class Archive, typename ... VariantTypes> inline
137  void CEREAL_SAVE_FUNCTION_NAME( Archive & ar, boost::variant<VariantTypes...> const & variant )
138  {
139  int32_t which = variant.which();
140  ar( CEREAL_NVP_("which", which) );
142  variant.apply_visitor(visitor);
143  }
144 
146  template <class Archive, typename ... VariantTypes> inline
147  void CEREAL_LOAD_FUNCTION_NAME( Archive & ar, boost::variant<VariantTypes...> & variant )
148  {
149  int32_t which;
150  ar( CEREAL_NVP_("which", which) );
151 
152  using LoadFuncType = void(*)(Archive &, boost::variant<VariantTypes...> &);
154 
155  if(which >= int32_t(sizeof(loadFuncArray)/sizeof(loadFuncArray[0])))
156  throw Exception("Invalid 'which' selector when deserializing boost::variant");
157 
158  loadFuncArray[which](ar, variant);
159  }
160 } // namespace cereal
161 
162 #undef CEREAL_CONSTEXPR_LAMBDA
163 
164 #endif // CEREAL_TYPES_BOOST_VARIANT_HPP_
#define CEREAL_CONSTEXPR_LAMBDA
Definition: boost_variant.hpp:37
Main cereal functionality.
Used to construct types with no default constructor.
Definition: access.hpp:165
bool itsValid
Definition: access.hpp:218
#define CEREAL_NVP_(name, value)
Convenience for creating a templated NVP.
Definition: helpers.hpp:201
#define CEREAL_ALIGNOF
Checks if C++14 is available.
Definition: macros.hpp:153
type
The type the bitset is encoded with.
Definition: bitset.hpp:44
in certain simple scenarios. They should probably not be used if maximizing performance is the main o...
Definition: access.hpp:42
std::enable_if< std::is_arithmetic< T >::value, void >::type CEREAL_LOAD_FUNCTION_NAME(BinaryInputArchive &ar, T &t)
Loading for POD types from binary.
Definition: binary.hpp:126
std::enable_if< std::is_arithmetic< T >::value, void >::type CEREAL_SAVE_FUNCTION_NAME(BinaryOutputArchive &ar, T const &t)
Saving for POD types to binary.
Definition: binary.hpp:118
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1282
signed int int32_t
Definition: stdint.h:123
An exception class thrown when things go wrong at runtime.
Definition: helpers.hpp:49
typename std::aligned_storage< sizeof(T), CEREAL_ALIGNOF(T)>::type ST
Definition: boost_variant.hpp:67
::cereal::construct< T > construct
Definition: boost_variant.hpp:87
void CEREAL_SERIALIZE_FUNCTION_NAME(Archive &ar)
Definition: boost_variant.hpp:81
~LoadAndConstructLoadWrapper()
Definition: boost_variant.hpp:73
LoadAndConstructLoadWrapper()
Definition: boost_variant.hpp:69
static void load_variant(Archive &, Variant &)
Definition: boost_variant.hpp:99
static void load_variant_impl(Archive &ar, Variant &variant, std::true_type)
Definition: boost_variant.hpp:109
static void load_variant_impl(Archive &ar, Variant &variant, std::false_type)
Definition: boost_variant.hpp:118
static void load_variant(Archive &ar, Variant &variant)
Definition: boost_variant.hpp:128
variant_save_visitor(Archive &ar_)
Definition: boost_variant.hpp:52
Archive & ar
Definition: boost_variant.hpp:60
void operator()(T const &value) const
Definition: boost_variant.hpp:55
static T * load_andor_construct(A &, construct< T > &)
Definition: traits.hpp:1344