fix doc example typo
[boost.git] / boost / smart_ptr / shared_ptr.hpp
blob1b367f0f4da8da05719b8df75b52cea3f1299db8
1 #ifndef BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED
2 #define BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED
4 //
5 // shared_ptr.hpp
6 //
7 // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
8 // Copyright (c) 2001-2008 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)
14 // See http://www.boost.org/libs/smart_ptr/shared_ptr.htm for documentation.
17 #include <boost/config.hpp> // for broken compiler workarounds
19 #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
20 #include <boost/smart_ptr/detail/shared_ptr_nmt.hpp>
21 #else
23 // In order to avoid circular dependencies with Boost.TR1
24 // we make sure that our include of <memory> doesn't try to
25 // pull in the TR1 headers: that's why we use this header
26 // rather than including <memory> directly:
27 #include <boost/config/no_tr1/memory.hpp> // std::auto_ptr
29 #include <boost/assert.hpp>
30 #include <boost/checked_delete.hpp>
31 #include <boost/throw_exception.hpp>
32 #include <boost/smart_ptr/detail/shared_count.hpp>
33 #include <boost/detail/workaround.hpp>
34 #include <boost/smart_ptr/detail/sp_convertible.hpp>
36 #if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
37 #include <boost/smart_ptr/detail/spinlock_pool.hpp>
38 #include <boost/memory_order.hpp>
39 #endif
41 #include <algorithm> // for std::swap
42 #include <functional> // for std::less
43 #include <typeinfo> // for std::bad_cast
45 #if !defined(BOOST_NO_IOSTREAM)
46 #if !defined(BOOST_NO_IOSFWD)
47 #include <iosfwd> // for std::basic_ostream
48 #else
49 #include <ostream>
50 #endif
51 #endif
53 #ifdef BOOST_MSVC // moved here to work around VC++ compiler crash
54 # pragma warning(push)
55 # pragma warning(disable:4284) // odd return type for operator->
56 #endif
58 namespace boost
61 template<class T> class shared_ptr;
62 template<class T> class weak_ptr;
63 template<class T> class enable_shared_from_this;
64 template<class T> class enable_shared_from_this2;
66 namespace detail
69 struct static_cast_tag {};
70 struct const_cast_tag {};
71 struct dynamic_cast_tag {};
72 struct polymorphic_cast_tag {};
74 template<class T> struct shared_ptr_traits
76 typedef T & reference;
79 template<> struct shared_ptr_traits<void>
81 typedef void reference;
84 #if !defined(BOOST_NO_CV_VOID_SPECIALIZATIONS)
86 template<> struct shared_ptr_traits<void const>
88 typedef void reference;
91 template<> struct shared_ptr_traits<void volatile>
93 typedef void reference;
96 template<> struct shared_ptr_traits<void const volatile>
98 typedef void reference;
101 #endif
103 // enable_shared_from_this support
105 template< class X, class Y, class T > inline void sp_enable_shared_from_this( boost::shared_ptr<X> const * ppx, Y const * py, boost::enable_shared_from_this< T > const * pe )
107 if( pe != 0 )
109 pe->_internal_accept_owner( ppx, const_cast< Y* >( py ) );
113 template< class X, class Y, class T > inline void sp_enable_shared_from_this( boost::shared_ptr<X> * ppx, Y const * py, boost::enable_shared_from_this2< T > const * pe )
115 if( pe != 0 )
117 pe->_internal_accept_owner( ppx, const_cast< Y* >( py ) );
121 #ifdef _MANAGED
123 // Avoid C4793, ... causes native code generation
125 struct sp_any_pointer
127 template<class T> sp_any_pointer( T* ) {}
130 inline void sp_enable_shared_from_this( sp_any_pointer, sp_any_pointer, sp_any_pointer )
134 #else // _MANAGED
136 inline void sp_enable_shared_from_this( ... )
140 #endif // _MANAGED
142 #if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) && !defined( BOOST_NO_AUTO_PTR )
144 // rvalue auto_ptr support based on a technique by Dave Abrahams
146 template< class T, class R > struct sp_enable_if_auto_ptr
150 template< class T, class R > struct sp_enable_if_auto_ptr< std::auto_ptr< T >, R >
152 typedef R type;
155 #endif
157 } // namespace detail
161 // shared_ptr
163 // An enhanced relative of scoped_ptr with reference counted copy semantics.
164 // The object pointed to is deleted when the last shared_ptr pointing to it
165 // is destroyed or reset.
168 template<class T> class shared_ptr
170 private:
172 // Borland 5.5.1 specific workaround
173 typedef shared_ptr<T> this_type;
175 public:
177 typedef T element_type;
178 typedef T value_type;
179 typedef T * pointer;
180 typedef typename boost::detail::shared_ptr_traits<T>::reference reference;
182 shared_ptr(): px(0), pn() // never throws in 1.30+
186 template<class Y>
187 explicit shared_ptr( Y * p ): px( p ), pn( p ) // Y must be complete
189 boost::detail::sp_enable_shared_from_this( this, p, p );
193 // Requirements: D's copy constructor must not throw
195 // shared_ptr will release p by calling d(p)
198 template<class Y, class D> shared_ptr(Y * p, D d): px(p), pn(p, d)
200 boost::detail::sp_enable_shared_from_this( this, p, p );
203 // As above, but with allocator. A's copy constructor shall not throw.
205 template<class Y, class D, class A> shared_ptr( Y * p, D d, A a ): px( p ), pn( p, d, a )
207 boost::detail::sp_enable_shared_from_this( this, p, p );
210 // generated copy constructor, destructor are fine
212 template<class Y>
213 explicit shared_ptr(weak_ptr<Y> const & r): pn(r.pn) // may throw
215 // it is now safe to copy r.px, as pn(r.pn) did not throw
216 px = r.px;
219 template<class Y>
220 shared_ptr( weak_ptr<Y> const & r, boost::detail::sp_nothrow_tag ): px( 0 ), pn( r.pn, boost::detail::sp_nothrow_tag() ) // never throws
222 if( !pn.empty() )
224 px = r.px;
228 template<class Y>
229 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
231 shared_ptr( shared_ptr<Y> const & r, typename detail::sp_enable_if_convertible<Y,T>::type = detail::sp_empty() )
233 #else
235 shared_ptr( shared_ptr<Y> const & r )
237 #endif
238 : px( r.px ), pn( r.pn ) // never throws
242 // aliasing
243 template< class Y >
244 shared_ptr( shared_ptr<Y> const & r, T * p ): px( p ), pn( r.pn ) // never throws
248 template<class Y>
249 shared_ptr(shared_ptr<Y> const & r, boost::detail::static_cast_tag): px(static_cast<element_type *>(r.px)), pn(r.pn)
253 template<class Y>
254 shared_ptr(shared_ptr<Y> const & r, boost::detail::const_cast_tag): px(const_cast<element_type *>(r.px)), pn(r.pn)
258 template<class Y>
259 shared_ptr(shared_ptr<Y> const & r, boost::detail::dynamic_cast_tag): px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
261 if(px == 0) // need to allocate new counter -- the cast failed
263 pn = boost::detail::shared_count();
267 template<class Y>
268 shared_ptr(shared_ptr<Y> const & r, boost::detail::polymorphic_cast_tag): px(dynamic_cast<element_type *>(r.px)), pn(r.pn)
270 if(px == 0)
272 boost::throw_exception(std::bad_cast());
276 #ifndef BOOST_NO_AUTO_PTR
278 template<class Y>
279 explicit shared_ptr(std::auto_ptr<Y> & r): px(r.get()), pn()
281 Y * tmp = r.get();
282 pn = boost::detail::shared_count(r);
283 boost::detail::sp_enable_shared_from_this( this, tmp, tmp );
286 #if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
288 template<class Ap>
289 explicit shared_ptr( Ap r, typename boost::detail::sp_enable_if_auto_ptr<Ap, int>::type = 0 ): px( r.get() ), pn()
291 typename Ap::element_type * tmp = r.get();
292 pn = boost::detail::shared_count( r );
293 boost::detail::sp_enable_shared_from_this( this, tmp, tmp );
297 #endif // BOOST_NO_SFINAE, BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
299 #endif // BOOST_NO_AUTO_PTR
301 // assignment
303 shared_ptr & operator=( shared_ptr const & r ) // never throws
305 this_type(r).swap(*this);
306 return *this;
309 #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1400)
311 template<class Y>
312 shared_ptr & operator=(shared_ptr<Y> const & r) // never throws
314 this_type(r).swap(*this);
315 return *this;
318 #endif
320 #ifndef BOOST_NO_AUTO_PTR
322 template<class Y>
323 shared_ptr & operator=( std::auto_ptr<Y> & r )
325 this_type(r).swap(*this);
326 return *this;
329 #if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
331 template<class Ap>
332 typename boost::detail::sp_enable_if_auto_ptr< Ap, shared_ptr & >::type operator=( Ap r )
334 this_type( r ).swap( *this );
335 return *this;
339 #endif // BOOST_NO_SFINAE, BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
341 #endif // BOOST_NO_AUTO_PTR
343 // Move support
345 #if defined( BOOST_HAS_RVALUE_REFS )
347 shared_ptr( shared_ptr && r ): px( r.px ), pn() // never throws
349 pn.swap( r.pn );
350 r.px = 0;
353 template<class Y>
354 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
356 shared_ptr( shared_ptr<Y> && r, typename detail::sp_enable_if_convertible<Y,T>::type = detail::sp_empty() )
358 #else
360 shared_ptr( shared_ptr<Y> && r )
362 #endif
363 : px( r.px ), pn() // never throws
365 pn.swap( r.pn );
366 r.px = 0;
369 shared_ptr & operator=( shared_ptr && r ) // never throws
371 this_type( std::move( r ) ).swap( *this );
372 return *this;
375 template<class Y>
376 shared_ptr & operator=( shared_ptr<Y> && r ) // never throws
378 this_type( std::move( r ) ).swap( *this );
379 return *this;
382 #endif
384 void reset() // never throws in 1.30+
386 this_type().swap(*this);
389 template<class Y> void reset(Y * p) // Y must be complete
391 BOOST_ASSERT(p == 0 || p != px); // catch self-reset errors
392 this_type(p).swap(*this);
395 template<class Y, class D> void reset( Y * p, D d )
397 this_type( p, d ).swap( *this );
400 template<class Y, class D, class A> void reset( Y * p, D d, A a )
402 this_type( p, d, a ).swap( *this );
405 template<class Y> void reset( shared_ptr<Y> const & r, T * p )
407 this_type( r, p ).swap( *this );
410 reference operator* () const // never throws
412 BOOST_ASSERT(px != 0);
413 return *px;
416 T * operator-> () const // never throws
418 BOOST_ASSERT(px != 0);
419 return px;
422 T * get() const // never throws
424 return px;
427 // implicit conversion to "bool"
428 #include <boost/smart_ptr/detail/operator_bool.hpp>
430 bool unique() const // never throws
432 return pn.unique();
435 long use_count() const // never throws
437 return pn.use_count();
440 void swap(shared_ptr<T> & other) // never throws
442 std::swap(px, other.px);
443 pn.swap(other.pn);
446 template<class Y> bool _internal_less(shared_ptr<Y> const & rhs) const
448 return pn < rhs.pn;
451 void * _internal_get_deleter( detail::sp_typeinfo const & ti ) const
453 return pn.get_deleter( ti );
456 bool _internal_equiv( shared_ptr const & r ) const
458 return px == r.px && pn == r.pn;
461 // Tasteless as this may seem, making all members public allows member templates
462 // to work in the absence of member template friends. (Matthew Langston)
464 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
466 private:
468 template<class Y> friend class shared_ptr;
469 template<class Y> friend class weak_ptr;
472 #endif
474 T * px; // contained pointer
475 boost::detail::shared_count pn; // reference counter
477 }; // shared_ptr
479 template<class T, class U> inline bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b)
481 return a.get() == b.get();
484 template<class T, class U> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<U> const & b)
486 return a.get() != b.get();
489 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
491 // Resolve the ambiguity between our op!= and the one in rel_ops
493 template<class T> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<T> const & b)
495 return a.get() != b.get();
498 #endif
500 template<class T, class U> inline bool operator<(shared_ptr<T> const & a, shared_ptr<U> const & b)
502 return a._internal_less(b);
505 template<class T> inline void swap(shared_ptr<T> & a, shared_ptr<T> & b)
507 a.swap(b);
510 template<class T, class U> shared_ptr<T> static_pointer_cast(shared_ptr<U> const & r)
512 return shared_ptr<T>(r, boost::detail::static_cast_tag());
515 template<class T, class U> shared_ptr<T> const_pointer_cast(shared_ptr<U> const & r)
517 return shared_ptr<T>(r, boost::detail::const_cast_tag());
520 template<class T, class U> shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const & r)
522 return shared_ptr<T>(r, boost::detail::dynamic_cast_tag());
525 // shared_*_cast names are deprecated. Use *_pointer_cast instead.
527 template<class T, class U> shared_ptr<T> shared_static_cast(shared_ptr<U> const & r)
529 return shared_ptr<T>(r, boost::detail::static_cast_tag());
532 template<class T, class U> shared_ptr<T> shared_dynamic_cast(shared_ptr<U> const & r)
534 return shared_ptr<T>(r, boost::detail::dynamic_cast_tag());
537 template<class T, class U> shared_ptr<T> shared_polymorphic_cast(shared_ptr<U> const & r)
539 return shared_ptr<T>(r, boost::detail::polymorphic_cast_tag());
542 template<class T, class U> shared_ptr<T> shared_polymorphic_downcast(shared_ptr<U> const & r)
544 BOOST_ASSERT(dynamic_cast<T *>(r.get()) == r.get());
545 return shared_static_cast<T>(r);
548 // get_pointer() enables boost::mem_fn to recognize shared_ptr
550 template<class T> inline T * get_pointer(shared_ptr<T> const & p)
552 return p.get();
555 // operator<<
557 #if !defined(BOOST_NO_IOSTREAM)
559 #if defined(BOOST_NO_TEMPLATED_IOSTREAMS) || ( defined(__GNUC__) && (__GNUC__ < 3) )
561 template<class Y> std::ostream & operator<< (std::ostream & os, shared_ptr<Y> const & p)
563 os << p.get();
564 return os;
567 #else
569 // in STLport's no-iostreams mode no iostream symbols can be used
570 #ifndef _STLP_NO_IOSTREAMS
572 # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300 && __SGI_STL_PORT)
573 // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
574 using std::basic_ostream;
575 template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, shared_ptr<Y> const & p)
576 # else
577 template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, shared_ptr<Y> const & p)
578 # endif
580 os << p.get();
581 return os;
584 #endif // _STLP_NO_IOSTREAMS
586 #endif // __GNUC__ < 3
588 #endif // !defined(BOOST_NO_IOSTREAM)
590 // get_deleter
592 #if ( defined(__GNUC__) && BOOST_WORKAROUND(__GNUC__, < 3) ) || \
593 ( defined(__EDG_VERSION__) && BOOST_WORKAROUND(__EDG_VERSION__, <= 238) ) || \
594 ( defined(__HP_aCC) && BOOST_WORKAROUND(__HP_aCC, <= 33500) )
596 // g++ 2.9x doesn't allow static_cast<X const *>(void *)
597 // apparently EDG 2.38 and HP aCC A.03.35 also don't accept it
599 template<class D, class T> D * get_deleter(shared_ptr<T> const & p)
601 void const * q = p._internal_get_deleter(BOOST_SP_TYPEID(D));
602 return const_cast<D *>(static_cast<D const *>(q));
605 #else
607 template<class D, class T> D * get_deleter(shared_ptr<T> const & p)
609 return static_cast<D *>(p._internal_get_deleter(BOOST_SP_TYPEID(D)));
612 #endif
614 // atomic access
616 #if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
618 template<class T> inline bool atomic_is_lock_free( shared_ptr<T> const * /*p*/ )
620 return false;
623 template<class T> shared_ptr<T> atomic_load( shared_ptr<T> const * p )
625 boost::detail::spinlock_pool<2>::scoped_lock lock( p );
626 return *p;
629 template<class T> inline shared_ptr<T> atomic_load_explicit( shared_ptr<T> const * p, memory_order /*mo*/ )
631 return atomic_load( p );
634 template<class T> void atomic_store( shared_ptr<T> * p, shared_ptr<T> r )
636 boost::detail::spinlock_pool<2>::scoped_lock lock( p );
637 p->swap( r );
640 template<class T> inline void atomic_store_explicit( shared_ptr<T> * p, shared_ptr<T> r, memory_order /*mo*/ )
642 atomic_store( p, r ); // std::move( r )
645 template<class T> shared_ptr<T> atomic_exchange( shared_ptr<T> * p, shared_ptr<T> r )
647 boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( p );
649 sp.lock();
650 p->swap( r );
651 sp.unlock();
653 return r; // return std::move( r )
656 template<class T> shared_ptr<T> atomic_exchange_explicit( shared_ptr<T> * p, shared_ptr<T> r, memory_order /*mo*/ )
658 return atomic_exchange( p, r ); // std::move( r )
661 template<class T> bool atomic_compare_exchange( shared_ptr<T> * p, shared_ptr<T> * v, shared_ptr<T> w )
663 boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( p );
665 sp.lock();
667 if( p->_internal_equiv( *v ) )
669 p->swap( w );
671 sp.unlock();
673 return true;
675 else
677 shared_ptr<T> tmp( *p );
679 sp.unlock();
681 tmp.swap( *v );
682 return false;
686 template<class T> inline bool atomic_compare_exchange_explicit( shared_ptr<T> * p, shared_ptr<T> * v, shared_ptr<T> w, memory_order /*success*/, memory_order /*failure*/ )
688 return atomic_compare_exchange( p, v, w ); // std::move( w )
691 #endif
693 } // namespace boost
695 #ifdef BOOST_MSVC
696 # pragma warning(pop)
697 #endif
699 #endif // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
701 #endif // #ifndef BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED