1 #ifndef BOOST_SERIALIZATION_SHARED_PTR_132_HPP
2 #define BOOST_SERIALIZATION_SHARED_PTR_132_HPP
4 // MS compatible compilers support #pragma once
5 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
9 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
10 // shared_ptr.hpp: serialization for boost shared pointer
12 // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
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 // note: totally unadvised hack to gain access to private variables
20 // in shared_ptr and shared_count. Unfortunately its the only way to
21 // do this without changing shared_ptr and shared_count
22 // the best we can do is to detect a conflict here
23 #include <boost/config.hpp>
26 #include <cstddef> // NULL
28 #include <boost/serialization/assume_abstract.hpp>
29 #include <boost/serialization/split_free.hpp>
30 #include <boost/serialization/nvp.hpp>
31 #include <boost/serialization/tracking.hpp>
32 #include <boost/serialization/void_cast.hpp>
34 // mark base class as an (uncreatable) base class
35 #include <boost/serialization/detail/shared_ptr_132.hpp>
37 /////////////////////////////////////////////////////////////
38 // Maintain a couple of lists of loaded shared pointers of the old previous
42 namespace serialization
{
46 void operator()(void const *) const {}
50 } // namespace serialization
51 } // namespace boost_132
53 /////////////////////////////////////////////////////////////
54 // sp_counted_base_impl serialization
57 namespace serialization
{
59 template<class Archive
, class P
, class D
>
60 inline void serialize(
62 boost_132::detail::sp_counted_base_impl
<P
, D
> & /* t */,
63 const unsigned int /*file_version*/
65 // register the relationship between each derived class
66 // its polymorphic base
67 boost::serialization::void_cast_register
<
68 boost_132::detail::sp_counted_base_impl
<P
, D
>,
69 boost_132::detail::sp_counted_base
71 static_cast<boost_132::detail::sp_counted_base_impl
<P
, D
> *>(NULL
),
72 static_cast<boost_132::detail::sp_counted_base
*>(NULL
)
76 template<class Archive
, class P
, class D
>
77 inline void save_construct_data(
80 boost_132::detail::sp_counted_base_impl
<P
, D
> *t
,
81 const BOOST_PFTO
unsigned int /* file_version */
83 // variables used for construction
84 ar
<< boost::serialization::make_nvp("ptr", t
->ptr
);
87 template<class Archive
, class P
, class D
>
88 inline void load_construct_data(
90 boost_132::detail::sp_counted_base_impl
<P
, D
> * t
,
91 const unsigned int /* file_version */
94 ar
>> boost::serialization::make_nvp("ptr", ptr_
);
95 // ::new(t)boost_132::detail::sp_counted_base_impl<P, D>(ptr_, D());
97 // note: the original ::new... above is replaced by the one here. This one
98 // creates all new objects with a null_deleter so that after the archive
99 // is finished loading and the shared_ptrs are destroyed - the underlying
100 // raw pointers are NOT deleted. This is necessary as they are used by the
101 // new system as well.
102 ::new(t
)boost_132::detail::sp_counted_base_impl
<
104 boost_132::serialization::detail::null_deleter
106 ptr_
, boost_132::serialization::detail::null_deleter()
108 // compensate for that fact that a new shared count always is
109 // initialized with one. the add_ref_copy below will increment it
110 // every time its serialized so without this adjustment
111 // the use and weak counts will be off by one.
118 /////////////////////////////////////////////////////////////
119 // shared_count serialization
122 namespace serialization
{
124 template<class Archive
>
127 const boost_132::detail::shared_count
&t
,
128 const unsigned int /* file_version */
130 ar
<< boost::serialization::make_nvp("pi", t
.pi_
);
133 template<class Archive
>
136 boost_132::detail::shared_count
&t
,
137 const unsigned int /* file_version */
139 ar
>> boost::serialization::make_nvp("pi", t
.pi_
);
141 t
.pi_
->add_ref_copy();
147 BOOST_SERIALIZATION_SPLIT_FREE(boost_132::detail::shared_count
)
149 /////////////////////////////////////////////////////////////
150 // implement serialization for shared_ptr<T>
153 namespace serialization
{
155 template<class Archive
, class T
>
158 const boost_132::shared_ptr
<T
> &t
,
159 const unsigned int /* file_version */
161 // only the raw pointer has to be saved
162 // the ref count is maintained automatically as shared pointers are loaded
163 ar
.register_type(static_cast<
164 boost_132::detail::sp_counted_base_impl
<T
*, boost::checked_deleter
<T
> > *
166 ar
<< boost::serialization::make_nvp("px", t
.px
);
167 ar
<< boost::serialization::make_nvp("pn", t
.pn
);
170 template<class Archive
, class T
>
173 boost_132::shared_ptr
<T
> &t
,
174 const unsigned int /* file_version */
176 // only the raw pointer has to be saved
177 // the ref count is maintained automatically as shared pointers are loaded
178 ar
.register_type(static_cast<
179 boost_132::detail::sp_counted_base_impl
<T
*, boost::checked_deleter
<T
> > *
181 ar
>> boost::serialization::make_nvp("px", t
.px
);
182 ar
>> boost::serialization::make_nvp("pn", t
.pn
);
185 template<class Archive
, class T
>
186 inline void serialize(
188 boost_132::shared_ptr
<T
> &t
,
189 const unsigned int file_version
191 // correct shared_ptr serialization depends upon object tracking
194 boost::serialization::tracking_level
<T
>::value
195 != boost::serialization::track_never
197 boost::serialization::split_free(ar
, t
, file_version
);
203 // note: change below uses null_deleter
204 // This macro is used to export GUIDS for shared pointers to allow
205 // the serialization system to export them properly. David Tonge
206 #define BOOST_SHARED_POINTER_EXPORT_GUID(T, K) \
207 typedef boost_132::detail::sp_counted_base_impl< \
209 boost::checked_deleter< T > \
210 > __shared_ptr_ ## T; \
211 BOOST_CLASS_EXPORT_GUID(__shared_ptr_ ## T, "__shared_ptr_" K) \
212 BOOST_CLASS_EXPORT_GUID(T, K) \
215 #define BOOST_SHARED_POINTER_EXPORT(T) \
216 BOOST_SHARED_POINTER_EXPORT_GUID( \
218 BOOST_PP_STRINGIZE(T) \
222 #endif // BOOST_SERIALIZATION_SHARED_PTR_132_HPP