Ticket #974: shared_ptr.hpp

File shared_ptr.hpp, 13.6 KB (added by Paul A. Bristow, 15 years ago)

shared_ptr including warning supression

Line 
1#ifndef BOOST_SHARED_PTR_HPP_INCLUDED
2#define BOOST_SHARED_PTR_HPP_INCLUDED
3
4//
5// shared_ptr.hpp
6//
7// (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
8// Copyright (c) 2001-2006 Peter Dimov
9// Copyright Paul A. Bristow, 2007 - implemented warning supression from P Dimov May 2007
10//
11// Distributed under the Boost Software License, Version 1.0. (See
12// accompanying file LICENSE_1_0.txt or copy at
13// http://www.boost.org/LICENSE_1_0.txt)
14//
15// See http://www.boost.org/libs/smart_ptr/shared_ptr.htm for documentation.
16//
17
18#include <boost/config.hpp> // for broken compiler workarounds
19
20#if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
21#include <boost/detail/shared_ptr_nmt.hpp>
22#else
23
24#include <memory> // for std::auto_ptr
25
26#include <boost/assert.hpp>
27#include <boost/checked_delete.hpp>
28#include <boost/throw_exception.hpp>
29#include <boost/detail/shared_count.hpp>
30#include <boost/detail/workaround.hpp>
31
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
43{
44
45template<class T> class weak_ptr;
46template<class T> class enable_shared_from_this;
47
48namespace detail
49{
50
51struct static_cast_tag {};
52struct const_cast_tag {};
53struct dynamic_cast_tag {};
54struct polymorphic_cast_tag {};
55
56template<class T> struct shared_ptr_traits
57{
58 typedef T & reference;
59};
60
61template<> struct shared_ptr_traits<void>
62{
63 typedef void reference;
64};
65
66#if !defined(BOOST_NO_CV_VOID_SPECIALIZATIONS)
67
68template<> struct shared_ptr_traits<void const>
69{
70 typedef void reference;
71};
72
73template<> struct shared_ptr_traits<void volatile>
74{
75 typedef void reference;
76};
77
78template<> struct shared_ptr_traits<void const volatile>
79{
80 typedef void reference;
81};
82
83#endif
84
85// enable_shared_from_this support
86
87template<class T, class Y> void sp_enable_shared_from_this( shared_count const & pn, boost::enable_shared_from_this<T> const * pe, Y const * px )
88{
89 if(pe != 0) pe->_internal_weak_this._internal_assign(const_cast<Y*>(px), pn);
90}
91
92
93#if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) && !defined( BOOST_NO_AUTO_PTR )
94
95// rvalue auto_ptr support based on a technique by Dave Abrahams
96
97template< class T, class R > struct sp_enable_if_auto_ptr
98{
99};
100
101template< class T, class R > struct sp_enable_if_auto_ptr< std::auto_ptr< T >, R >
102{
103 typedef R type;
104};
105
106#endif
107
108} // namespace detail
109
110
111//
112// shared_ptr
113//
114// An enhanced relative of scoped_ptr with reference counted copy semantics.
115// The object pointed to is deleted when the last shared_ptr pointing to it
116// is destroyed or reset.
117//
118
119template<class T> class shared_ptr
120{
121private:
122
123 // Borland 5.5.1 specific workaround
124 typedef shared_ptr<T> this_type;
125
126public:
127
128 typedef T element_type;
129 typedef T value_type;
130 typedef T * pointer;
131 typedef typename boost::detail::shared_ptr_traits<T>::reference reference;
132
133 shared_ptr(): px(0), pn() // never throws in 1.30+
134 {
135 }
136
137 template<class Y>
138 explicit shared_ptr( Y * p ): px( p ), pn( p ) // Y must be complete
139 {
140 boost::detail::sp_enable_shared_from_this( pn, p, p );
141 }
142
143 //
144 // Requirements: D's copy constructor must not throw
145 //
146 // shared_ptr will release p by calling d(p)
147 //
148
149 template<class Y, class D> shared_ptr(Y * p, D d): px(p), pn(p, d)
150 {
151 boost::detail::sp_enable_shared_from_this( pn, p, p );
152 }
153
154 // As above, but with allocator. A's copy constructor shall not throw.
155
156 template<class Y, class D, class A> shared_ptr( Y * p, D d, A a ): px( p ), pn( p, d, a )
157 {
158 boost::detail::sp_enable_shared_from_this( pn, p, p );
159 }
160
161// generated copy constructor, assignment, destructor are fine...
162
163// except that Borland C++ has a bug, and g++ with -Wsynth warns
164#if defined(__BORLANDC__) || defined(__GNUC__)
165
166 shared_ptr & operator=(shared_ptr const & r) // never throws
167 {
168 px = r.px;
169 pn = r.pn; // shared_count::op= doesn't throw
170 return *this;
171 }
172
173#endif
174
175 template<class Y>
176 explicit shared_ptr(weak_ptr<Y> const & r): pn(r.pn) // may throw
177 {
178 // it is now safe to copy r.px, as pn(r.pn) did not throw
179 px = r.px;
180 }
181
182 template<class Y>
183 shared_ptr(shared_ptr<Y> const & r): px(r.px), pn(r.pn) // never throws
184 {
185 }
186
187 template<class Y>
188 shared_ptr(shared_ptr<Y> const & r, boost::detail::static_cast_tag): px(static_cast<element_type *>(r.px)), pn(r.pn)
189 {
190 }
191
192 template<class Y>
193 shared_ptr(shared_ptr<Y> const & r, boost::detail::const_cast_tag): px(const_cast<element_type *>(r.px)), pn(r.pn)
194 {
195 }
196
197 template<class Y>
198 shared_ptr(shared_ptr<Y> const & r, boost::detail::dynamic_cast_tag): px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
199 {
200 if(px == 0) // need to allocate new counter -- the cast failed
201 {
202 pn = boost::detail::shared_count();
203 }
204 }
205
206 template<class Y>
207 shared_ptr(shared_ptr<Y> const & r, boost::detail::polymorphic_cast_tag): px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
208 {
209 if(px == 0)
210 {
211 boost::throw_exception(std::bad_cast());
212 }
213 }
214
215#ifndef BOOST_NO_AUTO_PTR
216
217 template<class Y>
218 explicit shared_ptr(std::auto_ptr<Y> & r): px(r.get()), pn()
219 {
220 Y * tmp = r.get();
221 pn = boost::detail::shared_count(r);
222 boost::detail::sp_enable_shared_from_this( pn, tmp, tmp );
223 }
224
225#if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
226
227 template<class Ap>
228 shared_ptr( Ap r, typename boost::detail::sp_enable_if_auto_ptr<Ap, int>::type = 0 ): px( r.get() ), pn()
229 {
230 typename Ap::element_type * tmp = r.get();
231 pn = boost::detail::shared_count( r );
232 boost::detail::sp_enable_shared_from_this( pn, tmp, tmp );
233 }
234
235
236#endif // BOOST_NO_SFINAE, BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
237
238#endif // BOOST_NO_AUTO_PTR
239
240#if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1300)
241
242 template<class Y>
243 shared_ptr & operator=(shared_ptr<Y> const & r) // never throws
244 {
245 px = r.px;
246 pn = r.pn; // shared_count::op= doesn't throw
247 return *this;
248 }
249
250#endif
251
252#ifndef BOOST_NO_AUTO_PTR
253
254 template<class Y>
255 shared_ptr & operator=( std::auto_ptr<Y> & r )
256 {
257 this_type(r).swap(*this);
258 return *this;
259 }
260
261#if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
262
263 template<class Ap>
264 typename boost::detail::sp_enable_if_auto_ptr< Ap, shared_ptr & >::type operator=( Ap r )
265 {
266 this_type( r ).swap( *this );
267 return *this;
268 }
269
270
271#endif // BOOST_NO_SFINAE, BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
272
273#endif // BOOST_NO_AUTO_PTR
274
275 void reset() // never throws in 1.30+
276 {
277 this_type().swap(*this);
278 }
279
280 template<class Y> void reset(Y * p) // Y must be complete
281 {
282 BOOST_ASSERT(p == 0 || p != px); // catch self-reset errors
283 this_type(p).swap(*this);
284 }
285
286 template<class Y, class D> void reset( Y * p, D d )
287 {
288 this_type( p, d ).swap( *this );
289 }
290
291 template<class Y, class D, class A> void reset( Y * p, D d, A a )
292 {
293 this_type( p, d, a ).swap( *this );
294 }
295
296 reference operator* () const // never throws
297 {
298 BOOST_ASSERT(px != 0);
299 return *px;
300 }
301
302 T * operator-> () const // never throws
303 {
304 BOOST_ASSERT(px != 0);
305 return px;
306 }
307
308 T * get() const // never throws
309 {
310 return px;
311 }
312
313 // implicit conversion to "bool"
314
315#if defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x530)
316
317 operator bool () const
318 {
319 return px != 0;
320 }
321
322#elif \
323 ( defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__, < 0x3200) ) || \
324 ( defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ < 304) )
325
326 typedef T * (this_type::*unspecified_bool_type)() const;
327
328 operator unspecified_bool_type() const // never throws
329 {
330 return px == 0? 0: &this_type::get;
331 }
332
333#else
334
335 typedef T * this_type::*unspecified_bool_type;
336
337 operator unspecified_bool_type() const // never throws
338 {
339 return px == 0? 0: &this_type::px;
340 }
341
342#endif
343
344 // operator! is redundant, but some compilers need it
345
346 bool operator! () const // never throws
347 {
348 return px == 0;
349 }
350
351 bool unique() const // never throws
352 {
353 return pn.unique();
354 }
355
356 long use_count() const // never throws
357 {
358 return pn.use_count();
359 }
360
361 void swap(shared_ptr<T> & other) // never throws
362 {
363 std::swap(px, other.px);
364 pn.swap(other.pn);
365 }
366
367 template<class Y> bool _internal_less(shared_ptr<Y> const & rhs) const
368 {
369 return pn < rhs.pn;
370 }
371
372 void * _internal_get_deleter(std::type_info const & ti) const
373 {
374 return pn.get_deleter(ti);
375 }
376
377// Tasteless as this may seem, making all members public allows member templates
378// to work in the absence of member template friends. (Matthew Langston)
379
380#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
381
382private:
383
384 template<class Y> friend class shared_ptr;
385 template<class Y> friend class weak_ptr;
386
387
388#endif
389
390 T * px; // contained pointer
391 boost::detail::shared_count pn; // reference counter
392
393}; // shared_ptr
394
395template<class T, class U> inline bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b)
396{
397 return a.get() == b.get();
398}
399
400template<class T, class U> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<U> const & b)
401{
402 return a.get() != b.get();
403}
404
405#if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
406
407// Resolve the ambiguity between our op!= and the one in rel_ops
408
409template<class T> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<T> const & b)
410{
411 return a.get() != b.get();
412}
413
414#endif
415
416template<class T, class U> inline bool operator<(shared_ptr<T> const & a, shared_ptr<U> const & b)
417{
418 return a._internal_less(b);
419}
420
421template<class T> inline void swap(shared_ptr<T> & a, shared_ptr<T> & b)
422{
423 a.swap(b);
424}
425
426template<class T, class U> shared_ptr<T> static_pointer_cast(shared_ptr<U> const & r)
427{
428 return shared_ptr<T>(r, boost::detail::static_cast_tag());
429}
430
431template<class T, class U> shared_ptr<T> const_pointer_cast(shared_ptr<U> const & r)
432{
433 return shared_ptr<T>(r, boost::detail::const_cast_tag());
434}
435
436template<class T, class U> shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const & r)
437{
438 return shared_ptr<T>(r, boost::detail::dynamic_cast_tag());
439}
440
441// shared_*_cast names are deprecated. Use *_pointer_cast instead.
442
443template<class T, class U> shared_ptr<T> shared_static_cast(shared_ptr<U> const & r)
444{
445 return shared_ptr<T>(r, boost::detail::static_cast_tag());
446}
447
448template<class T, class U> shared_ptr<T> shared_dynamic_cast(shared_ptr<U> const & r)
449{
450 return shared_ptr<T>(r, boost::detail::dynamic_cast_tag());
451}
452
453template<class T, class U> shared_ptr<T> shared_polymorphic_cast(shared_ptr<U> const & r)
454{
455 return shared_ptr<T>(r, boost::detail::polymorphic_cast_tag());
456}
457
458template<class T, class U> shared_ptr<T> shared_polymorphic_downcast(shared_ptr<U> const & r)
459{
460 BOOST_ASSERT(dynamic_cast<T *>(r.get()) == r.get());
461 return shared_static_cast<T>(r);
462}
463
464// get_pointer() enables boost::mem_fn to recognize shared_ptr
465
466template<class T> inline T * get_pointer(shared_ptr<T> const & p)
467{
468 return p.get();
469}
470
471// operator<<
472
473#if defined(__GNUC__) && (__GNUC__ < 3)
474
475template<class Y> std::ostream & operator<< (std::ostream & os, shared_ptr<Y> const & p)
476{
477 os << p.get();
478 return os;
479}
480
481#else
482
483# if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300 && __SGI_STL_PORT)
484// MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
485using std::basic_ostream;
486template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, shared_ptr<Y> const & p)
487# else
488template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, shared_ptr<Y> const & p)
489# endif
490{
491 os << p.get();
492 return os;
493}
494
495#endif
496
497// get_deleter (experimental)
498
499#if ( defined(__GNUC__) && BOOST_WORKAROUND(__GNUC__, < 3) ) || \
500 ( defined(__EDG_VERSION__) && BOOST_WORKAROUND(__EDG_VERSION__, <= 238) ) || \
501 ( defined(__HP_aCC) && BOOST_WORKAROUND(__HP_aCC, <= 33500) )
502
503// g++ 2.9x doesn't allow static_cast<X const *>(void *)
504// apparently EDG 2.38 and HP aCC A.03.35 also don't accept it
505
506template<class D, class T> D * get_deleter(shared_ptr<T> const & p)
507{
508 void const * q = p._internal_get_deleter(typeid(D));
509 return const_cast<D *>(static_cast<D const *>(q));
510}
511
512#else
513
514template<class D, class T> D * get_deleter(shared_ptr<T> const & p)
515{
516 return static_cast<D *>(p._internal_get_deleter(typeid(D)));
517}
518
519#endif
520
521} // namespace boost
522
523#ifdef BOOST_MSVC
524# pragma warning(pop)
525#endif
526
527#endif // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
528
529#endif // #ifndef BOOST_SHARED_PTR_HPP_INCLUDED