1 #ifndef BOOST_SERIALIZATION_SHARED_PTR_HPP
2 #define BOOST_SERIALIZATION_SHARED_PTR_HPP
4 // MS compatible compilers support #pragma once
9 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
10 // shared_ptr.hpp: serialization for boost shared pointer
12 // (C) Copyright 2004 Robert Ramey and Martin Ecker
13 // Use, modification and distribution is subject to the Boost Software
14 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
15 // http://www.boost.org/LICENSE_1_0.txt)
17 // See http://www.boost.org for updates, documentation, and revision history.
19 #include <cstddef> // NULL
22 #include <boost/config.hpp>
23 #include <boost/mpl/integral_c.hpp>
24 #include <boost/mpl/integral_c_tag.hpp>
26 #include <boost/detail/workaround.hpp>
27 #include <boost/shared_ptr.hpp>
29 #include <boost/serialization/shared_ptr_helper.hpp>
30 #include <boost/serialization/split_free.hpp>
31 #include <boost/serialization/nvp.hpp>
32 #include <boost/serialization/version.hpp>
33 #include <boost/serialization/tracking.hpp>
35 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
36 // boost:: shared_ptr serialization traits
37 // version 1 to distinguish from boost 1.32 version. Note: we can only do this
38 // for a template when the compiler supports partial template specialization
40 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
42 namespace serialization{
44 struct version< ::boost::shared_ptr< T > > {
45 typedef mpl::integral_c_tag tag;
46 #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3206))
47 typedef typename mpl::int_<1> type;
49 typedef mpl::int_<1> type;
51 BOOST_STATIC_CONSTANT(int, value = type::value);
53 // don't track shared pointers
55 struct tracking_level< ::boost::shared_ptr< T > > {
56 typedef mpl::integral_c_tag tag;
57 #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3206))
58 typedef typename mpl::int_< ::boost::serialization::track_never> type;
60 typedef mpl::int_< ::boost::serialization::track_never> type;
62 BOOST_STATIC_CONSTANT(int, value = type::value);
65 #define BOOST_SERIALIZATION_SHARED_PTR(T)
67 // define macro to let users of these compilers do this
68 #define BOOST_SERIALIZATION_SHARED_PTR(T) \
69 BOOST_CLASS_VERSION( \
70 ::boost::shared_ptr< T >, \
73 BOOST_CLASS_TRACKING( \
74 ::boost::shared_ptr< T >, \
75 ::boost::serialization::track_never \
81 namespace serialization{
84 void operator()(void const *) const {}
87 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
88 // serialization for boost::shared_ptr
90 // Using a constant means that all shared pointers are held in the same set.
91 // Thus we detect handle multiple pointers to the same value instances
93 void * const shared_ptr_helper_id = 0;
95 template<class Archive, class T>
98 const boost::shared_ptr< T > &t,
99 const unsigned int /* file_version */
101 // The most common cause of trapping here would be serializing
102 // something like shared_ptr<int>. This occurs because int
103 // is never tracked by default. Wrap int in a trackable type
104 BOOST_STATIC_ASSERT((tracking_level< T >::value != track_never));
105 const T * t_ptr = t.get();
106 ar << boost::serialization::make_nvp("px", t_ptr);
109 #ifdef BOOST_SERIALIZATION_SHARED_PTR_132_HPP
110 template<class Archive, class T>
113 boost::shared_ptr< T > &t,
114 const unsigned int file_version
116 // something like shared_ptr<int>. This occurs because int
117 // is never tracked by default. Wrap int in a trackable type
118 BOOST_STATIC_ASSERT((tracking_level< T >::value != track_never));
120 if(file_version < 1){
121 ar.register_type(static_cast<
122 boost_132::detail::sp_counted_base_impl<T *, null_deleter > *
124 boost_132::shared_ptr< T > sp;
125 ar >> boost::serialization::make_nvp("px", sp.px);
126 ar >> boost::serialization::make_nvp("pn", sp.pn);
127 // got to keep the sps around so the sp.pns don't disappear
128 boost::serialization::shared_ptr_helper<boost::shared_ptr> & h =
129 ar.template get_helper< shared_ptr_helper<boost::shared_ptr> >(
136 ar >> boost::serialization::make_nvp("px", r);
138 shared_ptr_helper<boost::shared_ptr> & h =
139 ar.template get_helper<shared_ptr_helper<boost::shared_ptr> >(
146 template<class Archive, class T>
149 boost::shared_ptr< T > &t,
150 const unsigned int /*file_version*/
152 // The most common cause of trapping here would be serializing
153 // something like shared_ptr<int>. This occurs because int
154 // is never tracked by default. Wrap int in a trackable type
155 BOOST_STATIC_ASSERT((tracking_level< T >::value != track_never));
157 ar >> boost::serialization::make_nvp("px", r);
159 boost::serialization::shared_ptr_helper<boost::shared_ptr> & h =
160 ar.template get_helper<shared_ptr_helper<boost::shared_ptr> >(
167 template<class Archive, class T>
168 inline void serialize(
170 boost::shared_ptr< T > &t,
171 const unsigned int file_version
173 // correct shared_ptr serialization depends upon object tracking
176 boost::serialization::tracking_level< T >::value
177 != boost::serialization::track_never
179 boost::serialization::split_free(ar, t, file_version);
182 } // namespace serialization
185 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
186 // std::shared_ptr serialization traits
187 // version 1 to distinguish from boost 1.32 version. Note: we can only do this
188 // for a template when the compiler supports partial template specialization
190 #ifndef BOOST_NO_CXX11_SMART_PTR
191 #include <boost/static_assert.hpp>
193 // note: we presume that any compiler/library which supports C++11
194 // std::pointers also supports template partial specialization
195 // trap here if such presumption were to turn out to wrong!!!
196 #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
197 BOOST_STATIC_ASSERT(false);
201 namespace serialization{
203 struct version< ::std::shared_ptr< T > > {
204 typedef mpl::integral_c_tag tag;
205 typedef mpl::int_<1> type;
206 BOOST_STATIC_CONSTANT(int, value = type::value);
208 // don't track shared pointers
210 struct tracking_level< ::std::shared_ptr< T > > {
211 typedef mpl::integral_c_tag tag;
212 typedef mpl::int_< ::boost::serialization::track_never> type;
213 BOOST_STATIC_CONSTANT(int, value = type::value);
216 // the following just keeps older programs from breaking
217 #define BOOST_SERIALIZATION_SHARED_PTR(T)
220 namespace serialization{
222 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
223 // serialization for std::shared_ptr
225 template<class Archive, class T>
228 const std::shared_ptr< T > &t,
229 const unsigned int /* file_version */
231 // The most common cause of trapping here would be serializing
232 // something like shared_ptr<int>. This occurs because int
233 // is never tracked by default. Wrap int in a trackable type
234 BOOST_STATIC_ASSERT((tracking_level< T >::value != track_never));
235 const T * t_ptr = t.get();
236 ar << boost::serialization::make_nvp("px", t_ptr);
239 template<class Archive, class T>
242 std::shared_ptr< T > &t,
243 const unsigned int /*file_version*/
245 // The most common cause of trapping here would be serializing
246 // something like shared_ptr<int>. This occurs because int
247 // is never tracked by default. Wrap int in a trackable type
248 BOOST_STATIC_ASSERT((tracking_level< T >::value != track_never));
250 ar >> boost::serialization::make_nvp("px", r);
251 //void (* const id)(Archive &, std::shared_ptr< T > &, const unsigned int) = & load;
252 boost::serialization::shared_ptr_helper<std::shared_ptr> & h =
253 ar.template get_helper<
254 shared_ptr_helper<std::shared_ptr>
261 template<class Archive, class T>
262 inline void serialize(
264 std::shared_ptr< T > &t,
265 const unsigned int file_version
267 // correct shared_ptr serialization depends upon object tracking
270 boost::serialization::tracking_level< T >::value
271 != boost::serialization::track_never
273 boost::serialization::split_free(ar, t, file_version);
276 } // namespace serialization
279 #endif // BOOST_NO_CXX11_SMART_PTR
281 #endif // BOOST_SERIALIZATION_SHARED_PTR_HPP