Chris@16
|
1 #ifndef BOOST_SERIALIZATION_SHARED_PTR_HPP
|
Chris@16
|
2 #define BOOST_SERIALIZATION_SHARED_PTR_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 2004 Robert Ramey and Martin Ecker
|
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 #include <cstddef> // NULL
|
Chris@101
|
20 #include <memory>
|
Chris@16
|
21
|
Chris@16
|
22 #include <boost/config.hpp>
|
Chris@16
|
23 #include <boost/mpl/integral_c.hpp>
|
Chris@16
|
24 #include <boost/mpl/integral_c_tag.hpp>
|
Chris@16
|
25
|
Chris@16
|
26 #include <boost/detail/workaround.hpp>
|
Chris@16
|
27 #include <boost/shared_ptr.hpp>
|
Chris@16
|
28
|
Chris@101
|
29 #include <boost/serialization/shared_ptr_helper.hpp>
|
Chris@16
|
30 #include <boost/serialization/split_free.hpp>
|
Chris@16
|
31 #include <boost/serialization/nvp.hpp>
|
Chris@16
|
32 #include <boost/serialization/version.hpp>
|
Chris@16
|
33 #include <boost/serialization/tracking.hpp>
|
Chris@16
|
34
|
Chris@16
|
35 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
|
Chris@101
|
36 // boost:: shared_ptr serialization traits
|
Chris@16
|
37 // version 1 to distinguish from boost 1.32 version. Note: we can only do this
|
Chris@16
|
38 // for a template when the compiler supports partial template specialization
|
Chris@16
|
39
|
Chris@16
|
40 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
|
Chris@16
|
41 namespace boost {
|
Chris@16
|
42 namespace serialization{
|
Chris@16
|
43 template<class T>
|
Chris@16
|
44 struct version< ::boost::shared_ptr< T > > {
|
Chris@16
|
45 typedef mpl::integral_c_tag tag;
|
Chris@16
|
46 #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3206))
|
Chris@101
|
47 typedef typename mpl::int_<1> type;
|
Chris@16
|
48 #else
|
Chris@16
|
49 typedef mpl::int_<1> type;
|
Chris@16
|
50 #endif
|
Chris@16
|
51 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x570))
|
Chris@16
|
52 BOOST_STATIC_CONSTANT(int, value = 1);
|
Chris@16
|
53 #else
|
Chris@16
|
54 BOOST_STATIC_CONSTANT(int, value = type::value);
|
Chris@16
|
55 #endif
|
Chris@16
|
56 };
|
Chris@16
|
57 // don't track shared pointers
|
Chris@16
|
58 template<class T>
|
Chris@16
|
59 struct tracking_level< ::boost::shared_ptr< T > > {
|
Chris@16
|
60 typedef mpl::integral_c_tag tag;
|
Chris@16
|
61 #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3206))
|
Chris@101
|
62 typedef typename mpl::int_< ::boost::serialization::track_never> type;
|
Chris@16
|
63 #else
|
Chris@16
|
64 typedef mpl::int_< ::boost::serialization::track_never> type;
|
Chris@16
|
65 #endif
|
Chris@16
|
66 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x570))
|
Chris@16
|
67 BOOST_STATIC_CONSTANT(int, value = ::boost::serialization::track_never);
|
Chris@16
|
68 #else
|
Chris@16
|
69 BOOST_STATIC_CONSTANT(int, value = type::value);
|
Chris@16
|
70 #endif
|
Chris@16
|
71 };
|
Chris@16
|
72 }}
|
Chris@16
|
73 #define BOOST_SERIALIZATION_SHARED_PTR(T)
|
Chris@16
|
74 #else
|
Chris@16
|
75 // define macro to let users of these compilers do this
|
Chris@16
|
76 #define BOOST_SERIALIZATION_SHARED_PTR(T) \
|
Chris@16
|
77 BOOST_CLASS_VERSION( \
|
Chris@16
|
78 ::boost::shared_ptr< T >, \
|
Chris@16
|
79 1 \
|
Chris@16
|
80 ) \
|
Chris@16
|
81 BOOST_CLASS_TRACKING( \
|
Chris@16
|
82 ::boost::shared_ptr< T >, \
|
Chris@16
|
83 ::boost::serialization::track_never \
|
Chris@16
|
84 ) \
|
Chris@16
|
85 /**/
|
Chris@16
|
86 #endif
|
Chris@16
|
87
|
Chris@16
|
88 namespace boost {
|
Chris@16
|
89 namespace serialization{
|
Chris@16
|
90
|
Chris@16
|
91 struct null_deleter {
|
Chris@16
|
92 void operator()(void const *) const {}
|
Chris@16
|
93 };
|
Chris@16
|
94
|
Chris@16
|
95 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
|
Chris@101
|
96 // serialization for boost::shared_ptr
|
Chris@101
|
97
|
Chris@101
|
98 // Using a constant means that all shared pointers are held in the same set.
|
Chris@101
|
99 // Thus we detect handle multiple pointers to the same value instances
|
Chris@101
|
100 // in the archive.
|
Chris@101
|
101 void * const shared_ptr_helper_id = 0;
|
Chris@16
|
102
|
Chris@16
|
103 template<class Archive, class T>
|
Chris@16
|
104 inline void save(
|
Chris@16
|
105 Archive & ar,
|
Chris@16
|
106 const boost::shared_ptr< T > &t,
|
Chris@16
|
107 const unsigned int /* file_version */
|
Chris@16
|
108 ){
|
Chris@16
|
109 // The most common cause of trapping here would be serializing
|
Chris@16
|
110 // something like shared_ptr<int>. This occurs because int
|
Chris@16
|
111 // is never tracked by default. Wrap int in a trackable type
|
Chris@16
|
112 BOOST_STATIC_ASSERT((tracking_level< T >::value != track_never));
|
Chris@16
|
113 const T * t_ptr = t.get();
|
Chris@16
|
114 ar << boost::serialization::make_nvp("px", t_ptr);
|
Chris@16
|
115 }
|
Chris@16
|
116
|
Chris@16
|
117 #ifdef BOOST_SERIALIZATION_SHARED_PTR_132_HPP
|
Chris@16
|
118 template<class Archive, class T>
|
Chris@16
|
119 inline void load(
|
Chris@16
|
120 Archive & ar,
|
Chris@16
|
121 boost::shared_ptr< T > &t,
|
Chris@16
|
122 const unsigned int file_version
|
Chris@16
|
123 ){
|
Chris@16
|
124 // something like shared_ptr<int>. This occurs because int
|
Chris@16
|
125 // is never tracked by default. Wrap int in a trackable type
|
Chris@16
|
126 BOOST_STATIC_ASSERT((tracking_level< T >::value != track_never));
|
Chris@16
|
127 T* r;
|
Chris@16
|
128 if(file_version < 1){
|
Chris@16
|
129 ar.register_type(static_cast<
|
Chris@16
|
130 boost_132::detail::sp_counted_base_impl<T *, null_deleter > *
|
Chris@16
|
131 >(NULL));
|
Chris@16
|
132 boost_132::shared_ptr< T > sp;
|
Chris@16
|
133 ar >> boost::serialization::make_nvp("px", sp.px);
|
Chris@16
|
134 ar >> boost::serialization::make_nvp("pn", sp.pn);
|
Chris@16
|
135 // got to keep the sps around so the sp.pns don't disappear
|
Chris@101
|
136 boost::serialization::shared_ptr_helper<boost::shared_ptr> & h =
|
Chris@101
|
137 ar.template get_helper< shared_ptr_helper<boost::shared_ptr> >(
|
Chris@101
|
138 shared_ptr_helper_id
|
Chris@101
|
139 );
|
Chris@101
|
140 h.append(sp);
|
Chris@16
|
141 r = sp.get();
|
Chris@16
|
142 }
|
Chris@16
|
143 else{
|
Chris@16
|
144 ar >> boost::serialization::make_nvp("px", r);
|
Chris@16
|
145 }
|
Chris@101
|
146 shared_ptr_helper<boost::shared_ptr> & h =
|
Chris@101
|
147 ar.template get_helper<shared_ptr_helper<boost::shared_ptr> >(
|
Chris@101
|
148 shared_ptr_helper_id
|
Chris@101
|
149 );
|
Chris@101
|
150 h.reset(t,r);
|
Chris@16
|
151 }
|
Chris@101
|
152 #else
|
Chris@16
|
153
|
Chris@16
|
154 template<class Archive, class T>
|
Chris@16
|
155 inline void load(
|
Chris@16
|
156 Archive & ar,
|
Chris@16
|
157 boost::shared_ptr< T > &t,
|
Chris@16
|
158 const unsigned int /*file_version*/
|
Chris@16
|
159 ){
|
Chris@16
|
160 // The most common cause of trapping here would be serializing
|
Chris@16
|
161 // something like shared_ptr<int>. This occurs because int
|
Chris@16
|
162 // is never tracked by default. Wrap int in a trackable type
|
Chris@16
|
163 BOOST_STATIC_ASSERT((tracking_level< T >::value != track_never));
|
Chris@16
|
164 T* r;
|
Chris@16
|
165 ar >> boost::serialization::make_nvp("px", r);
|
Chris@101
|
166
|
Chris@101
|
167 boost::serialization::shared_ptr_helper<boost::shared_ptr> & h =
|
Chris@101
|
168 ar.template get_helper<shared_ptr_helper<boost::shared_ptr> >(
|
Chris@101
|
169 shared_ptr_helper_id
|
Chris@101
|
170 );
|
Chris@101
|
171 h.reset(t,r);
|
Chris@16
|
172 }
|
Chris@16
|
173 #endif
|
Chris@16
|
174
|
Chris@16
|
175 template<class Archive, class T>
|
Chris@16
|
176 inline void serialize(
|
Chris@16
|
177 Archive & ar,
|
Chris@16
|
178 boost::shared_ptr< T > &t,
|
Chris@16
|
179 const unsigned int file_version
|
Chris@16
|
180 ){
|
Chris@16
|
181 // correct shared_ptr serialization depends upon object tracking
|
Chris@16
|
182 // being used.
|
Chris@16
|
183 BOOST_STATIC_ASSERT(
|
Chris@16
|
184 boost::serialization::tracking_level< T >::value
|
Chris@16
|
185 != boost::serialization::track_never
|
Chris@16
|
186 );
|
Chris@16
|
187 boost::serialization::split_free(ar, t, file_version);
|
Chris@16
|
188 }
|
Chris@16
|
189
|
Chris@16
|
190 } // namespace serialization
|
Chris@16
|
191 } // namespace boost
|
Chris@16
|
192
|
Chris@101
|
193 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
|
Chris@101
|
194 // std::shared_ptr serialization traits
|
Chris@101
|
195 // version 1 to distinguish from boost 1.32 version. Note: we can only do this
|
Chris@101
|
196 // for a template when the compiler supports partial template specialization
|
Chris@101
|
197
|
Chris@101
|
198 #ifndef BOOST_NO_CXX11_SMART_PTR
|
Chris@101
|
199 #include <boost/static_assert.hpp>
|
Chris@101
|
200
|
Chris@101
|
201 // note: we presume that any compiler/library which supports C++11
|
Chris@101
|
202 // std::pointers also supports template partial specialization
|
Chris@101
|
203 // trap here if such presumption were to turn out to wrong!!!
|
Chris@101
|
204 #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
|
Chris@101
|
205 BOOST_STATIC_ASSERT(false);
|
Chris@101
|
206 #endif
|
Chris@101
|
207
|
Chris@101
|
208 namespace boost {
|
Chris@101
|
209 namespace serialization{
|
Chris@101
|
210 template<class T>
|
Chris@101
|
211 struct version< ::std::shared_ptr< T > > {
|
Chris@101
|
212 typedef mpl::integral_c_tag tag;
|
Chris@101
|
213 typedef mpl::int_<1> type;
|
Chris@101
|
214 BOOST_STATIC_CONSTANT(int, value = type::value);
|
Chris@101
|
215 };
|
Chris@101
|
216 // don't track shared pointers
|
Chris@101
|
217 template<class T>
|
Chris@101
|
218 struct tracking_level< ::std::shared_ptr< T > > {
|
Chris@101
|
219 typedef mpl::integral_c_tag tag;
|
Chris@101
|
220 typedef mpl::int_< ::boost::serialization::track_never> type;
|
Chris@101
|
221 BOOST_STATIC_CONSTANT(int, value = type::value);
|
Chris@101
|
222 };
|
Chris@101
|
223 }}
|
Chris@101
|
224 // the following just keeps older programs from breaking
|
Chris@101
|
225 #define BOOST_SERIALIZATION_SHARED_PTR(T)
|
Chris@101
|
226
|
Chris@101
|
227 namespace boost {
|
Chris@101
|
228 namespace serialization{
|
Chris@101
|
229
|
Chris@101
|
230 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
|
Chris@101
|
231 // serialization for std::shared_ptr
|
Chris@101
|
232
|
Chris@101
|
233 template<class Archive, class T>
|
Chris@101
|
234 inline void save(
|
Chris@101
|
235 Archive & ar,
|
Chris@101
|
236 const std::shared_ptr< T > &t,
|
Chris@101
|
237 const unsigned int /* file_version */
|
Chris@101
|
238 ){
|
Chris@101
|
239 // The most common cause of trapping here would be serializing
|
Chris@101
|
240 // something like shared_ptr<int>. This occurs because int
|
Chris@101
|
241 // is never tracked by default. Wrap int in a trackable type
|
Chris@101
|
242 BOOST_STATIC_ASSERT((tracking_level< T >::value != track_never));
|
Chris@101
|
243 const T * t_ptr = t.get();
|
Chris@101
|
244 ar << boost::serialization::make_nvp("px", t_ptr);
|
Chris@101
|
245 }
|
Chris@101
|
246
|
Chris@101
|
247 template<class Archive, class T>
|
Chris@101
|
248 inline void load(
|
Chris@101
|
249 Archive & ar,
|
Chris@101
|
250 std::shared_ptr< T > &t,
|
Chris@101
|
251 const unsigned int /*file_version*/
|
Chris@101
|
252 ){
|
Chris@101
|
253 // The most common cause of trapping here would be serializing
|
Chris@101
|
254 // something like shared_ptr<int>. This occurs because int
|
Chris@101
|
255 // is never tracked by default. Wrap int in a trackable type
|
Chris@101
|
256 BOOST_STATIC_ASSERT((tracking_level< T >::value != track_never));
|
Chris@101
|
257 T* r;
|
Chris@101
|
258 ar >> boost::serialization::make_nvp("px", r);
|
Chris@101
|
259 //void (* const id)(Archive &, std::shared_ptr< T > &, const unsigned int) = & load;
|
Chris@101
|
260 boost::serialization::shared_ptr_helper<std::shared_ptr> & h =
|
Chris@101
|
261 ar.template get_helper<
|
Chris@101
|
262 shared_ptr_helper<std::shared_ptr>
|
Chris@101
|
263 >(
|
Chris@101
|
264 shared_ptr_helper_id
|
Chris@101
|
265 );
|
Chris@101
|
266 h.reset(t,r);
|
Chris@101
|
267 }
|
Chris@101
|
268
|
Chris@101
|
269 template<class Archive, class T>
|
Chris@101
|
270 inline void serialize(
|
Chris@101
|
271 Archive & ar,
|
Chris@101
|
272 std::shared_ptr< T > &t,
|
Chris@101
|
273 const unsigned int file_version
|
Chris@101
|
274 ){
|
Chris@101
|
275 // correct shared_ptr serialization depends upon object tracking
|
Chris@101
|
276 // being used.
|
Chris@101
|
277 BOOST_STATIC_ASSERT(
|
Chris@101
|
278 boost::serialization::tracking_level< T >::value
|
Chris@101
|
279 != boost::serialization::track_never
|
Chris@101
|
280 );
|
Chris@101
|
281 boost::serialization::split_free(ar, t, file_version);
|
Chris@101
|
282 }
|
Chris@101
|
283
|
Chris@101
|
284 } // namespace serialization
|
Chris@101
|
285 } // namespace boost
|
Chris@101
|
286
|
Chris@101
|
287 #endif // BOOST_NO_CXX11_SMART_PTR
|
Chris@101
|
288
|
Chris@16
|
289 #endif // BOOST_SERIALIZATION_SHARED_PTR_HPP
|