Chris@16
|
1 #ifndef BOOST_SERIALIZATION_SHARED_PTR_132_HPP
|
Chris@16
|
2 #define BOOST_SERIALIZATION_SHARED_PTR_132_HPP
|
Chris@16
|
3
|
Chris@16
|
4 // MS compatible compilers support #pragma once
|
Chris@101
|
5 #if defined(_MSC_VER)
|
Chris@16
|
6 # pragma once
|
Chris@16
|
7 #endif
|
Chris@16
|
8
|
Chris@16
|
9 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
|
Chris@16
|
10 // shared_ptr.hpp: serialization for boost shared pointer
|
Chris@16
|
11
|
Chris@16
|
12 // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
|
Chris@16
|
13 // Use, modification and distribution is subject to the Boost Software
|
Chris@16
|
14 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
15 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
16
|
Chris@16
|
17 // See http://www.boost.org for updates, documentation, and revision history.
|
Chris@16
|
18
|
Chris@16
|
19 // note: totally unadvised hack to gain access to private variables
|
Chris@16
|
20 // in shared_ptr and shared_count. Unfortunately its the only way to
|
Chris@16
|
21 // do this without changing shared_ptr and shared_count
|
Chris@16
|
22 // the best we can do is to detect a conflict here
|
Chris@16
|
23 #include <boost/config.hpp>
|
Chris@16
|
24
|
Chris@16
|
25 #include <list>
|
Chris@16
|
26 #include <cstddef> // NULL
|
Chris@16
|
27
|
Chris@16
|
28 #include <boost/serialization/assume_abstract.hpp>
|
Chris@16
|
29 #include <boost/serialization/split_free.hpp>
|
Chris@16
|
30 #include <boost/serialization/nvp.hpp>
|
Chris@16
|
31 #include <boost/serialization/tracking.hpp>
|
Chris@16
|
32 #include <boost/serialization/void_cast.hpp>
|
Chris@16
|
33
|
Chris@16
|
34 // mark base class as an (uncreatable) base class
|
Chris@16
|
35 #include <boost/serialization/detail/shared_ptr_132.hpp>
|
Chris@16
|
36
|
Chris@16
|
37 /////////////////////////////////////////////////////////////
|
Chris@16
|
38 // Maintain a couple of lists of loaded shared pointers of the old previous
|
Chris@16
|
39 // version (1.32)
|
Chris@16
|
40
|
Chris@16
|
41 namespace boost_132 {
|
Chris@16
|
42 namespace serialization {
|
Chris@16
|
43 namespace detail {
|
Chris@16
|
44
|
Chris@16
|
45 struct null_deleter {
|
Chris@16
|
46 void operator()(void const *) const {}
|
Chris@16
|
47 };
|
Chris@16
|
48
|
Chris@16
|
49 } // namespace detail
|
Chris@16
|
50 } // namespace serialization
|
Chris@16
|
51 } // namespace boost_132
|
Chris@16
|
52
|
Chris@16
|
53 /////////////////////////////////////////////////////////////
|
Chris@16
|
54 // sp_counted_base_impl serialization
|
Chris@16
|
55
|
Chris@16
|
56 namespace boost {
|
Chris@16
|
57 namespace serialization {
|
Chris@16
|
58
|
Chris@16
|
59 template<class Archive, class P, class D>
|
Chris@16
|
60 inline void serialize(
|
Chris@16
|
61 Archive & /* ar */,
|
Chris@16
|
62 boost_132::detail::sp_counted_base_impl<P, D> & /* t */,
|
Chris@16
|
63 const unsigned int /*file_version*/
|
Chris@16
|
64 ){
|
Chris@16
|
65 // register the relationship between each derived class
|
Chris@16
|
66 // its polymorphic base
|
Chris@16
|
67 boost::serialization::void_cast_register<
|
Chris@16
|
68 boost_132::detail::sp_counted_base_impl<P, D>,
|
Chris@16
|
69 boost_132::detail::sp_counted_base
|
Chris@16
|
70 >(
|
Chris@16
|
71 static_cast<boost_132::detail::sp_counted_base_impl<P, D> *>(NULL),
|
Chris@16
|
72 static_cast<boost_132::detail::sp_counted_base *>(NULL)
|
Chris@16
|
73 );
|
Chris@16
|
74 }
|
Chris@16
|
75
|
Chris@16
|
76 template<class Archive, class P, class D>
|
Chris@16
|
77 inline void save_construct_data(
|
Chris@16
|
78 Archive & ar,
|
Chris@16
|
79 const
|
Chris@16
|
80 boost_132::detail::sp_counted_base_impl<P, D> *t,
|
Chris@16
|
81 const BOOST_PFTO unsigned int /* file_version */
|
Chris@16
|
82 ){
|
Chris@16
|
83 // variables used for construction
|
Chris@16
|
84 ar << boost::serialization::make_nvp("ptr", t->ptr);
|
Chris@16
|
85 }
|
Chris@16
|
86
|
Chris@16
|
87 template<class Archive, class P, class D>
|
Chris@16
|
88 inline void load_construct_data(
|
Chris@16
|
89 Archive & ar,
|
Chris@16
|
90 boost_132::detail::sp_counted_base_impl<P, D> * t,
|
Chris@16
|
91 const unsigned int /* file_version */
|
Chris@16
|
92 ){
|
Chris@16
|
93 P ptr_;
|
Chris@16
|
94 ar >> boost::serialization::make_nvp("ptr", ptr_);
|
Chris@16
|
95 // ::new(t)boost_132::detail::sp_counted_base_impl<P, D>(ptr_, D());
|
Chris@16
|
96 // placement
|
Chris@16
|
97 // note: the original ::new... above is replaced by the one here. This one
|
Chris@16
|
98 // creates all new objects with a null_deleter so that after the archive
|
Chris@16
|
99 // is finished loading and the shared_ptrs are destroyed - the underlying
|
Chris@16
|
100 // raw pointers are NOT deleted. This is necessary as they are used by the
|
Chris@16
|
101 // new system as well.
|
Chris@16
|
102 ::new(t)boost_132::detail::sp_counted_base_impl<
|
Chris@16
|
103 P,
|
Chris@16
|
104 boost_132::serialization::detail::null_deleter
|
Chris@16
|
105 >(
|
Chris@16
|
106 ptr_, boost_132::serialization::detail::null_deleter()
|
Chris@16
|
107 ); // placement new
|
Chris@16
|
108 // compensate for that fact that a new shared count always is
|
Chris@16
|
109 // initialized with one. the add_ref_copy below will increment it
|
Chris@16
|
110 // every time its serialized so without this adjustment
|
Chris@16
|
111 // the use and weak counts will be off by one.
|
Chris@16
|
112 t->use_count_ = 0;
|
Chris@16
|
113 }
|
Chris@16
|
114
|
Chris@16
|
115 } // serialization
|
Chris@16
|
116 } // namespace boost
|
Chris@16
|
117
|
Chris@16
|
118 /////////////////////////////////////////////////////////////
|
Chris@16
|
119 // shared_count serialization
|
Chris@16
|
120
|
Chris@16
|
121 namespace boost {
|
Chris@16
|
122 namespace serialization {
|
Chris@16
|
123
|
Chris@16
|
124 template<class Archive>
|
Chris@16
|
125 inline void save(
|
Chris@16
|
126 Archive & ar,
|
Chris@16
|
127 const boost_132::detail::shared_count &t,
|
Chris@16
|
128 const unsigned int /* file_version */
|
Chris@16
|
129 ){
|
Chris@16
|
130 ar << boost::serialization::make_nvp("pi", t.pi_);
|
Chris@16
|
131 }
|
Chris@16
|
132
|
Chris@16
|
133 template<class Archive>
|
Chris@16
|
134 inline void load(
|
Chris@16
|
135 Archive & ar,
|
Chris@16
|
136 boost_132::detail::shared_count &t,
|
Chris@16
|
137 const unsigned int /* file_version */
|
Chris@16
|
138 ){
|
Chris@16
|
139 ar >> boost::serialization::make_nvp("pi", t.pi_);
|
Chris@16
|
140 if(NULL != t.pi_)
|
Chris@16
|
141 t.pi_->add_ref_copy();
|
Chris@16
|
142 }
|
Chris@16
|
143
|
Chris@16
|
144 } // serialization
|
Chris@16
|
145 } // namespace boost
|
Chris@16
|
146
|
Chris@16
|
147 BOOST_SERIALIZATION_SPLIT_FREE(boost_132::detail::shared_count)
|
Chris@16
|
148
|
Chris@16
|
149 /////////////////////////////////////////////////////////////
|
Chris@16
|
150 // implement serialization for shared_ptr< T >
|
Chris@16
|
151
|
Chris@16
|
152 namespace boost {
|
Chris@16
|
153 namespace serialization {
|
Chris@16
|
154
|
Chris@16
|
155 template<class Archive, class T>
|
Chris@16
|
156 inline void save(
|
Chris@16
|
157 Archive & ar,
|
Chris@16
|
158 const boost_132::shared_ptr< T > &t,
|
Chris@16
|
159 const unsigned int /* file_version */
|
Chris@16
|
160 ){
|
Chris@16
|
161 // only the raw pointer has to be saved
|
Chris@16
|
162 // the ref count is maintained automatically as shared pointers are loaded
|
Chris@16
|
163 ar.register_type(static_cast<
|
Chris@16
|
164 boost_132::detail::sp_counted_base_impl<T *, boost::checked_deleter< T > > *
|
Chris@16
|
165 >(NULL));
|
Chris@16
|
166 ar << boost::serialization::make_nvp("px", t.px);
|
Chris@16
|
167 ar << boost::serialization::make_nvp("pn", t.pn);
|
Chris@16
|
168 }
|
Chris@16
|
169
|
Chris@16
|
170 template<class Archive, class T>
|
Chris@16
|
171 inline void load(
|
Chris@16
|
172 Archive & ar,
|
Chris@16
|
173 boost_132::shared_ptr< T > &t,
|
Chris@16
|
174 const unsigned int /* file_version */
|
Chris@16
|
175 ){
|
Chris@16
|
176 // only the raw pointer has to be saved
|
Chris@16
|
177 // the ref count is maintained automatically as shared pointers are loaded
|
Chris@16
|
178 ar.register_type(static_cast<
|
Chris@16
|
179 boost_132::detail::sp_counted_base_impl<T *, boost::checked_deleter< T > > *
|
Chris@16
|
180 >(NULL));
|
Chris@16
|
181 ar >> boost::serialization::make_nvp("px", t.px);
|
Chris@16
|
182 ar >> boost::serialization::make_nvp("pn", t.pn);
|
Chris@16
|
183 }
|
Chris@16
|
184
|
Chris@16
|
185 template<class Archive, class T>
|
Chris@16
|
186 inline void serialize(
|
Chris@16
|
187 Archive & ar,
|
Chris@16
|
188 boost_132::shared_ptr< T > &t,
|
Chris@16
|
189 const unsigned int file_version
|
Chris@16
|
190 ){
|
Chris@16
|
191 // correct shared_ptr serialization depends upon object tracking
|
Chris@16
|
192 // being used.
|
Chris@16
|
193 BOOST_STATIC_ASSERT(
|
Chris@16
|
194 boost::serialization::tracking_level< T >::value
|
Chris@16
|
195 != boost::serialization::track_never
|
Chris@16
|
196 );
|
Chris@16
|
197 boost::serialization::split_free(ar, t, file_version);
|
Chris@16
|
198 }
|
Chris@16
|
199
|
Chris@16
|
200 } // serialization
|
Chris@16
|
201 } // namespace boost
|
Chris@16
|
202
|
Chris@16
|
203 // note: change below uses null_deleter
|
Chris@16
|
204 // This macro is used to export GUIDS for shared pointers to allow
|
Chris@16
|
205 // the serialization system to export them properly. David Tonge
|
Chris@16
|
206 #define BOOST_SHARED_POINTER_EXPORT_GUID(T, K) \
|
Chris@16
|
207 typedef boost_132::detail::sp_counted_base_impl< \
|
Chris@16
|
208 T *, \
|
Chris@16
|
209 boost::checked_deleter< T > \
|
Chris@16
|
210 > __shared_ptr_ ## T; \
|
Chris@16
|
211 BOOST_CLASS_EXPORT_GUID(__shared_ptr_ ## T, "__shared_ptr_" K) \
|
Chris@16
|
212 BOOST_CLASS_EXPORT_GUID(T, K) \
|
Chris@16
|
213 /**/
|
Chris@16
|
214
|
Chris@16
|
215 #define BOOST_SHARED_POINTER_EXPORT(T) \
|
Chris@16
|
216 BOOST_SHARED_POINTER_EXPORT_GUID( \
|
Chris@16
|
217 T, \
|
Chris@16
|
218 BOOST_PP_STRINGIZE(T) \
|
Chris@16
|
219 ) \
|
Chris@16
|
220 /**/
|
Chris@16
|
221
|
Chris@16
|
222 #endif // BOOST_SERIALIZATION_SHARED_PTR_132_HPP
|