1#ifndef BOOST_SHARED_PTR_132_HPP_INCLUDED
2#define BOOST_SHARED_PTR_132_HPP_INCLUDED
3
4//
5// shared_ptr.hpp
6//
7// (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
8// Copyright (c) 2001, 2002, 2003 Peter Dimov
9//
10// Distributed under the Boost Software License, Version 1.0. (See
11// accompanying file LICENSE_1_0.txt or copy at
12// http://www.boost.org/LICENSE_1_0.txt)
13//
14// See http://www.boost.org/libs/smart_ptr/shared_ptr.htm for documentation.
15//
16
17#include <boost/config.hpp> // for broken compiler workarounds
18
19#if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
20#include <boost/serialization/detail/shared_ptr_nmt_132.hpp>
21#else
22
23#include <boost/assert.hpp>
24#include <boost/checked_delete.hpp>
25#include <boost/serialization/throw_exception.hpp>
26#include <boost/detail/workaround.hpp>
27
28#include <boost/serialization/access.hpp>
29#include <boost/serialization/detail/shared_count_132.hpp>
30
31#include <memory> // for std::auto_ptr
32#include <algorithm> // for std::swap
33#include <functional> // for std::less
34#include <typeinfo> // for std::bad_cast
35#include <iosfwd> // for std::basic_ostream
36
37#ifdef BOOST_MSVC // moved here to work around VC++ compiler crash
38# pragma warning(push)
39# pragma warning(disable:4284) // odd return type for operator->
40#endif
41
42namespace boost_132 {
43
44template<class T> class weak_ptr;
45template<class T> class enable_shared_from_this;
46
47namespace detail
48{
49
50struct static_cast_tag {};
51struct const_cast_tag {};
52struct dynamic_cast_tag {};
53struct polymorphic_cast_tag {};
54
55template<class T> struct shared_ptr_traits
56{
57 typedef T & reference;
58};
59
60template<> struct shared_ptr_traits<void>
61{
62 typedef void reference;
63};
64
65#if !defined(BOOST_NO_CV_VOID_SPECIALIZATIONS)
66
67template<> struct shared_ptr_traits<void const>
68{
69 typedef void reference;
70};
71
72template<> struct shared_ptr_traits<void volatile>
73{
74 typedef void reference;
75};
76
77template<> struct shared_ptr_traits<void const volatile>
78{
79 typedef void reference;
80};
81
82#endif
83
84// enable_shared_from_this support
85
86template<class T, class Y> void sp_enable_shared_from_this( shared_count const & pn, enable_shared_from_this< T > const * pe, Y const * px )
87{
88 if(pe != 0) pe->_internal_weak_this._internal_assign(const_cast<Y*>(px), pn);
89}
90
91inline void sp_enable_shared_from_this( shared_count const & /*pn*/, ... )
92{
93}
94
95} // namespace detail
96
97
98//
99// shared_ptr
100//
101// An enhanced relative of scoped_ptr with reference counted copy semantics.
102// The object pointed to is deleted when the last shared_ptr pointing to it
103// is destroyed or reset.
104//
105
106template<class T> class shared_ptr
107{
108private:
109 // Borland 5.5.1 specific workaround
110 typedef shared_ptr< T > this_type;
111
112public:
113
114 typedef T element_type;
115 typedef T value_type;
116 typedef T * pointer;
117 typedef typename detail::shared_ptr_traits< T >::reference reference;
118
119 shared_ptr(): px(0), pn() // never throws in 1.30+
120 {
121 }
122
123 template<class Y>
124 explicit shared_ptr(Y * p): px(p), pn(p, boost::checked_deleter<Y>()) // Y must be complete
125 {
126 detail::sp_enable_shared_from_this( pn, p, p );
127 }
128
129 //
130 // Requirements: D's copy constructor must not throw
131 //
132 // shared_ptr will release p by calling d(p)
133 //
134
135 template<class Y, class D> shared_ptr(Y * p, D d): px(p), pn(p, d)
136 {
137 detail::sp_enable_shared_from_this( pn, p, p );
138 }
139
140// generated copy constructor, assignment, destructor are fine...
141
142// except that Borland C++ has a bug, and g++ with -Wsynth warns
143#if defined(__GNUC__)
144 shared_ptr & operator=(shared_ptr const & r) // never throws
145 {
146 px = r.px;
147 pn = r.pn; // shared_count::op= doesn't throw
148 return *this;
149 }
150#endif
151
152 template<class Y>
153 explicit shared_ptr(weak_ptr<Y> const & r): pn(r.pn) // may throw
154 {
155 // it is now safe to copy r.px, as pn(r.pn) did not throw
156 px = r.px;
157 }
158
159 template<class Y>
160 shared_ptr(shared_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
161 {
162 }
163
164 template<class Y>
165 shared_ptr(shared_ptr<Y> const & r, detail::static_cast_tag): px(static_cast<element_type *>(r.px)), pn(r.pn)
166 {
167 }
168
169 template<class Y>
170 shared_ptr(shared_ptr<Y> const & r, detail::const_cast_tag): px(const_cast<element_type *>(r.px)), pn(r.pn)
171 {
172 }
173
174 template<class Y>
175 shared_ptr(shared_ptr<Y> const & r, detail::dynamic_cast_tag): px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
176 {
177 if(px == 0) // need to allocate new counter -- the cast failed
178 {
179 pn = detail::shared_count();
180 }
181 }
182
183 template<class Y>
184 shared_ptr(shared_ptr<Y> const & r, detail::polymorphic_cast_tag): px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
185 {
186 if(px == 0)
187 {
188 boost::serialization::throw_exception(e: std::bad_cast());
189 }
190 }
191
192#ifndef BOOST_NO_AUTO_PTR
193
194 template<class Y>
195 explicit shared_ptr(std::auto_ptr<Y> & r): px(r.get()), pn()
196 {
197 Y * tmp = r.get();
198 pn = detail::shared_count(r);
199 detail::sp_enable_shared_from_this( pn, tmp, tmp );
200 }
201
202#endif
203
204#if !defined(BOOST_MSVC) || (BOOST_MSVC > 1200)
205
206 template<class Y>
207 shared_ptr & operator=(shared_ptr<Y> const & r) // never throws
208 {
209 px = r.px;
210 pn = r.pn; // shared_count::op= doesn't throw
211 return *this;
212 }
213
214#endif
215
216#ifndef BOOST_NO_AUTO_PTR
217
218 template<class Y>
219 shared_ptr & operator=(std::auto_ptr<Y> & r)
220 {
221 this_type(r).swap(*this);
222 return *this;
223 }
224
225#endif
226
227 void reset() // never throws in 1.30+
228 {
229 this_type().swap(*this);
230 }
231
232 template<class Y> void reset(Y * p) // Y must be complete
233 {
234 BOOST_ASSERT(p == 0 || p != px); // catch self-reset errors
235 this_type(p).swap(*this);
236 }
237
238 template<class Y, class D> void reset(Y * p, D d)
239 {
240 this_type(p, d).swap(*this);
241 }
242
243 reference operator* () const // never throws
244 {
245 BOOST_ASSERT(px != 0);
246 return *px;
247 }
248
249 T * operator-> () const // never throws
250 {
251 BOOST_ASSERT(px != 0);
252 return px;
253 }
254
255 T * get() const // never throws
256 {
257 return px;
258 }
259
260 // implicit conversion to "bool"
261
262#if defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x530)
263
264 operator bool () const
265 {
266 return px != 0;
267 }
268
269#elif defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
270 typedef T * (this_type::*unspecified_bool_type)() const;
271
272 operator unspecified_bool_type() const // never throws
273 {
274 return px == 0? 0: &this_type::get;
275 }
276
277#else
278
279 typedef T * this_type::*unspecified_bool_type;
280
281 operator unspecified_bool_type() const // never throws
282 {
283 return px == 0? 0: &this_type::px;
284 }
285
286#endif
287
288 // operator! is redundant, but some compilers need it
289
290 bool operator! () const // never throws
291 {
292 return px == 0;
293 }
294
295 bool unique() const // never throws
296 {
297 return pn.unique();
298 }
299
300 long use_count() const // never throws
301 {
302 return pn.use_count();
303 }
304
305 void swap(shared_ptr< T > & other) // never throws
306 {
307 std::swap(px, other.px);
308 pn.swap(r&: other.pn);
309 }
310
311 template<class Y> bool _internal_less(shared_ptr<Y> const & rhs) const
312 {
313 return pn < rhs.pn;
314 }
315
316 void * _internal_get_deleter(std::type_info const & ti) const
317 {
318 return pn.get_deleter(ti);
319 }
320
321// Tasteless as this may seem, making all members public allows member templates
322// to work in the absence of member template friends. (Matthew Langston)
323
324#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
325
326private:
327
328 template<class Y> friend class shared_ptr;
329 template<class Y> friend class weak_ptr;
330
331
332#endif
333public: // for serialization
334 T * px; // contained pointer
335 detail::shared_count pn; // reference counter
336
337}; // shared_ptr
338
339template<class T, class U> inline bool operator==(shared_ptr< T > const & a, shared_ptr<U> const & b)
340{
341 return a.get() == b.get();
342}
343
344template<class T, class U> inline bool operator!=(shared_ptr< T > const & a, shared_ptr<U> const & b)
345{
346 return a.get() != b.get();
347}
348
349template<class T, class U> inline bool operator<(shared_ptr< T > const & a, shared_ptr<U> const & b)
350{
351 return a._internal_less(b);
352}
353
354template<class T> inline void swap(shared_ptr< T > & a, shared_ptr< T > & b)
355{
356 a.swap(b);
357}
358
359template<class T, class U> shared_ptr< T > static_pointer_cast(shared_ptr<U> const & r)
360{
361 return shared_ptr< T >(r, detail::static_cast_tag());
362}
363
364template<class T, class U> shared_ptr< T > const_pointer_cast(shared_ptr<U> const & r)
365{
366 return shared_ptr< T >(r, detail::const_cast_tag());
367}
368
369template<class T, class U> shared_ptr< T > dynamic_pointer_cast(shared_ptr<U> const & r)
370{
371 return shared_ptr< T >(r, detail::dynamic_cast_tag());
372}
373
374// shared_*_cast names are deprecated. Use *_pointer_cast instead.
375
376template<class T, class U> shared_ptr< T > shared_static_cast(shared_ptr<U> const & r)
377{
378 return shared_ptr< T >(r, detail::static_cast_tag());
379}
380
381template<class T, class U> shared_ptr< T > shared_dynamic_cast(shared_ptr<U> const & r)
382{
383 return shared_ptr< T >(r, detail::dynamic_cast_tag());
384}
385
386template<class T, class U> shared_ptr< T > shared_polymorphic_cast(shared_ptr<U> const & r)
387{
388 return shared_ptr< T >(r, detail::polymorphic_cast_tag());
389}
390
391template<class T, class U> shared_ptr< T > shared_polymorphic_downcast(shared_ptr<U> const & r)
392{
393 BOOST_ASSERT(dynamic_cast<T *>(r.get()) == r.get());
394 return shared_static_cast< T >(r);
395}
396
397// get_pointer() enables boost::mem_fn to recognize shared_ptr
398
399template<class T> inline T * get_pointer(shared_ptr< T > const & p)
400{
401 return p.get();
402}
403
404// operator<<
405
406
407template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, shared_ptr<Y> const & p)
408{
409 os << p.get();
410 return os;
411}
412
413// get_deleter (experimental)
414
415#if defined(__EDG_VERSION__) && (__EDG_VERSION__ <= 238)
416
417// g++ 2.9x doesn't allow static_cast<X const *>(void *)
418// apparently EDG 2.38 also doesn't accept it
419
420template<class D, class T> D * get_deleter(shared_ptr< T > const & p)
421{
422 void const * q = p._internal_get_deleter(typeid(D));
423 return const_cast<D *>(static_cast<D const *>(q));
424}
425
426#else
427
428template<class D, class T> D * get_deleter(shared_ptr< T > const & p)
429{
430 return static_cast<D *>(p._internal_get_deleter(typeid(D)));
431}
432
433#endif
434
435} // namespace boost
436
437#ifdef BOOST_MSVC
438# pragma warning(pop)
439#endif
440
441#endif // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
442
443#endif // #ifndef BOOST_SHARED_PTR_132_HPP_INCLUDED
444

source code of boost/libs/serialization/include/boost/serialization/detail/shared_ptr_132.hpp