BoostSerialization.h
Go to the documentation of this file.
1 // (C) Copyright Renaud Detry 2007-2015.
2 // Distributed under the GNU General Public License and under the
3 // BSD 3-Clause License (See accompanying file LICENSE.txt).
4 
5 /** @file */
6 
7 
8 #ifndef NUKLEI_BOOST_SERIALIZATION_H
9 #define NUKLEI_BOOST_SERIALIZATION_H
10 
11 #include <boost/config.hpp>
12 
13 #ifdef NUKLEI_USE_CEREAL
14 # define NUKLEI_SERIALIZATION_IMPL 1
15 #else
16 # define NUKLEI_SERIALIZATION_IMPL 0
17 #endif
18 
19 // -----------------------------------------------------------------------------
20 // -----------------------------------------------------------------------------
21 // -----------------------------------------------------------------------------
22 // -----------------------------------------------------------------------------
23 #if NUKLEI_SERIALIZATION_IMPL == 0 // Boost Serialization
24 
25 #include <boost/archive/archive_exception.hpp>
26 
27 #include <boost/archive/text_iarchive.hpp>
28 #include <boost/archive/text_oarchive.hpp>
29 #include <boost/archive/xml_iarchive.hpp>
30 #include <boost/archive/xml_oarchive.hpp>
31 #include <boost/archive/binary_iarchive.hpp>
32 #include <boost/archive/binary_oarchive.hpp>
33 
34 #include <boost/serialization/nvp.hpp>
35 #include <boost/serialization/utility.hpp>
36 #include <boost/serialization/vector.hpp>
37 #include <boost/serialization/list.hpp>
38 #include <boost/serialization/map.hpp>
39 #include <boost/serialization/shared_ptr.hpp>
40 #include <boost/serialization/string.hpp>
41 #include <boost/serialization/version.hpp>
42 #include <boost/serialization/optional.hpp>
43 
44 #include <boost/serialization/split_free.hpp>
45 
46 #if BOOST_VERSION < 104100
47 #error Nuklei requires Boost >= 1.41
48 #endif
49 
50 #define NUKLEI_SERIALIZATION_NVP(T) BOOST_SERIALIZATION_NVP(T)
51 #define NUKLEI_SERIALIZATION_FRIEND_CLASSNAME boost::serialization::access
52 #define NUKLEI_SERIALIZATION_MAKE_NVP boost::serialization::make_nvp
53 #define NUKLEI_SERIALIZATION_BASE(T) boost::serialization::base_object<T>( *this )
54 #define NUKLEI_SERIALIZATION_CLASS_VERSION BOOST_CLASS_VERSION
55 #define NUKLEI_SERIALIZATION_REGISTER_TYPE BOOST_CLASS_EXPORT_IMPLEMENT
56 #define NUKLEI_SERIALIZATION_DECLARE_TYPE_WITH_NAME BOOST_CLASS_EXPORT_KEY2
57 #define NUKLEI_SERIALIZATION_BINARY_IARCHIVE boost::archive::binary_iarchive
58 #define NUKLEI_SERIALIZATION_BINARY_OARCHIVE boost::archive::binary_oarchive
59 #define NUKLEI_SERIALIZATION_XML_IARCHIVE boost::archive::xml_iarchive
60 #define NUKLEI_SERIALIZATION_XML_OARCHIVE boost::archive::xml_oarchive
61 #define NUKLEI_SERIALIZATION_NAMESPACE_BEGIN namespace boost { namespace serialization {
62 #define NUKLEI_SERIALIZATION_NAMESPACE_END } }
63 
64 
65 #include <nuklei/Common.h>
66 
67 #if (NUKLEI_UNIQUE_PTR_IMPL == 0 && defined(BOOST_NO_CXX11_SMART_PTR)) || NUKLEI_UNIQUE_PTR_IMPL == 1
68 
69 // From boost_1_36_0/libs/serialization/example/demo_auto_ptr.cpp
70 // Modified to support XML archives.
71 // I don't know why this is not included in the installed headers.
72 
73 namespace boost {
74  namespace serialization {
75 
76  /////////////////////////////////////////////////////////////
77  // implement serialization for auto_ptr<T>
78  // note: this must be added to the boost namespace in order to
79  // be called by the library
80  template<class Archive, class T>
81  inline void save(
82  Archive & ar,
83  const std::auto_ptr<T> &t,
84  const unsigned int file_version
85  ){
86  // only the raw pointer has to be saved
87  // the ref count is rebuilt automatically on load
88  const T * const raw_ptr = t.get();
89  ar << NUKLEI_SERIALIZATION_NVP(raw_ptr);
90  }
91 
92  template<class Archive, class T>
93  inline void load(
94  Archive & ar,
95  std::auto_ptr<T> &t,
96  const unsigned int file_version
97  ){
98  T *raw_ptr;
99  ar >> NUKLEI_SERIALIZATION_NVP(raw_ptr);
100  t.reset(raw_ptr);
101  }
102 
103  // split non-intrusive serialization function member into separate
104  // non intrusive save/load member functions
105  template<class Archive, class T>
106  inline void serialize(
107  Archive & ar,
108  std::auto_ptr<T> &t,
109  const unsigned int file_version
110  ){
111  boost::serialization::split_free(ar, t, file_version);
112  }
113 
114  } // namespace serialization
115 } // namespace boost
116 
117 #else
118 
119 #include <boost/serialization/unique_ptr.hpp>
120 
121 #endif
122 
123 #include <boost/ptr_container/serialize_ptr_vector.hpp>
124 #include <boost/ptr_container/serialize_ptr_array.hpp>
125 
126 // Should come after archives.
127 #include <boost/serialization/export.hpp>
128 #include <boost/serialization/base_object.hpp>
129 
130 // -----------------------------------------------------------------------------
131 // -----------------------------------------------------------------------------
132 // -----------------------------------------------------------------------------
133 // -----------------------------------------------------------------------------
134 #elif NUKLEI_SERIALIZATION_IMPL == 1 // Cereal
135 
136 #include <nuklei/cereal/archives/binary.hpp>
137 #include <nuklei/cereal/archives/xml.hpp>
138 //#include <nuklei/cereal/archives/json.hpp>
139 #include <nuklei/cereal/types/polymorphic.hpp>
140 
141 #include <nuklei/cereal/types/vector.hpp>
142 #include <nuklei/cereal/types/list.hpp>
143 #include <nuklei/cereal/types/string.hpp>
144 #include <nuklei/cereal/types/utility.hpp>
145 #include <nuklei/cereal/types/map.hpp>
146 #include <nuklei/cereal/types/memory.hpp>
147 #include <nuklei/cereal/types/common.hpp>
148 
149 #if BOOST_VERSION < 104100
150 #error Nuklei requires Boost >= 1.41
151 #endif
152 
153 #include <boost/optional.hpp>
154 
155 namespace nuklei_cereal
156 {
157  //! Saving for boost::optional
158  template <class Archive, class Optioned> inline
159  void save(Archive & ar, ::boost::optional<Optioned> const & optional)
160  {
161  bool initFlag = (bool)optional;
162  if (initFlag)
163  {
164  ar(NUKLEI_CEREAL_NVP_("initialized", true));
165  ar(NUKLEI_CEREAL_NVP_("value", optional.get()));
166  }
167  else
168  {
169  ar(NUKLEI_CEREAL_NVP_("initialized", false));
170  }
171  }
172 
173  //! Loading for boost::optional
174  template <class Archive, class Optioned> inline
175  void load(Archive & ar, ::boost::optional<Optioned> & optional)
176  {
177 
178  bool initFlag;
179  ar(NUKLEI_CEREAL_NVP_("initialized", initFlag));
180  if (initFlag)
181  {
182  Optioned val;
183  ar(NUKLEI_CEREAL_NVP_("value", val));
184  optional = std::move(val);
185  }
186  else
187  optional = ::boost::none; // this is all we need to do to reset the internal flag and value
188 
189  }
190 } // namespace nuklei_cereal
191 
192 #define NUKLEI_SERIALIZATION_NVP(T) NUKLEI_CEREAL_NVP(T)
193 #define NUKLEI_SERIALIZATION_FRIEND_CLASSNAME nuklei_cereal::access
194 #define NUKLEI_SERIALIZATION_MAKE_NVP nuklei_cereal::make_nvp
195 #define NUKLEI_SERIALIZATION_BASE(T) nuklei_cereal::base_class<T>( this )
196 #define NUKLEI_SERIALIZATION_CLASS_VERSION NUKLEI_CEREAL_CLASS_VERSION
197 #define NUKLEI_SERIALIZATION_REGISTER_TYPE NUKLEI_CEREAL_REGISTER_TYPE
198 #define NUKLEI_SERIALIZATION_DECLARE_TYPE_WITH_NAME(T, U)
199 #define NUKLEI_SERIALIZATION_BINARY_IARCHIVE nuklei_cereal::BinaryInputArchive
200 #define NUKLEI_SERIALIZATION_BINARY_OARCHIVE nuklei_cereal::BinaryOutputArchive
201 #define NUKLEI_SERIALIZATION_XML_IARCHIVE nuklei_cereal::XMLInputArchive
202 #define NUKLEI_SERIALIZATION_XML_OARCHIVE nuklei_cereal::XMLOutputArchive
203 #define NUKLEI_SERIALIZATION_NAMESPACE_BEGIN namespace nuklei_cereal {
204 #define NUKLEI_SERIALIZATION_NAMESPACE_END }
205 
206 #include <nuklei/Common.h>
207 
208 #if (NUKLEI_UNIQUE_PTR_IMPL == 0 && defined(BOOST_NO_CXX11_SMART_PTR)) || NUKLEI_UNIQUE_PTR_IMPL == 1
209 #error Cereal requires C++ 11
210 #endif
211 
212 #include <nuklei/cereal/types/base_class.hpp>
213 
214 #endif
215 
216 #endif
217 
© Copyright 2007-2013 Renaud Detry.
Distributed under the terms of the GNU General Public License (GPL).
(See accompanying file LICENSE.txt or copy at http://www.gnu.org/copyleft/gpl.html.)
Revised Sun Sep 13 2020 19:10:06.