1 #ifndef BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED
2 #define BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED
7 // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
8 // Copyright (c) 2001-2008 Peter Dimov
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 // In order to avoid circular dependencies with Boost.TR1
20 // we make sure that our include of <memory> doesn't try to
21 // pull in the TR1 headers: that's why we use this header
22 // rather than including <memory> directly:
23 #include <boost/config/no_tr1/memory.hpp> // std::auto_ptr
25 #include <boost/assert.hpp>
26 #include <boost/checked_delete.hpp>
27 #include <boost/throw_exception.hpp>
28 #include <boost/smart_ptr/detail/shared_count.hpp>
29 #include <boost/detail/workaround.hpp>
30 #include <boost/smart_ptr/detail/sp_convertible.hpp>
31 #include <boost/smart_ptr/detail/sp_nullptr_t.hpp>
32 #include <boost/smart_ptr/detail/sp_disable_deprecated.hpp>
33 #include <boost/smart_ptr/detail/sp_noexcept.hpp>
35 #if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
36 #include <boost/smart_ptr/detail/spinlock_pool.hpp>
39 #include <algorithm> // for std::swap
40 #include <functional> // for std::less
41 #include <typeinfo> // for std::bad_cast
42 #include <cstddef> // for std::size_t
44 #if !defined(BOOST_NO_IOSTREAM)
45 #if !defined(BOOST_NO_IOSFWD)
46 #include <iosfwd> // for std::basic_ostream
52 #if defined( BOOST_SP_DISABLE_DEPRECATED )
53 #pragma GCC diagnostic push
54 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
60 template<class T> class shared_ptr;
61 template<class T> class weak_ptr;
62 template<class T> class enable_shared_from_this;
63 class enable_shared_from_raw;
68 template< class T, class D > class unique_ptr;
70 } // namespace movelib
75 // sp_element, element_type
77 template< class T > struct sp_element
82 #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
84 template< class T > struct sp_element< T[] >
89 #if !defined( __BORLANDC__ ) || !BOOST_WORKAROUND( __BORLANDC__, < 0x600 )
91 template< class T, std::size_t N > struct sp_element< T[N] >
98 #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
100 // sp_dereference, return type of operator*
102 template< class T > struct sp_dereference
107 template<> struct sp_dereference< void >
112 #if !defined(BOOST_NO_CV_VOID_SPECIALIZATIONS)
114 template<> struct sp_dereference< void const >
119 template<> struct sp_dereference< void volatile >
124 template<> struct sp_dereference< void const volatile >
129 #endif // !defined(BOOST_NO_CV_VOID_SPECIALIZATIONS)
131 #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
133 template< class T > struct sp_dereference< T[] >
138 #if !defined( __BORLANDC__ ) || !BOOST_WORKAROUND( __BORLANDC__, < 0x600 )
140 template< class T, std::size_t N > struct sp_dereference< T[N] >
147 #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
149 // sp_member_access, return type of operator->
151 template< class T > struct sp_member_access
156 #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
158 template< class T > struct sp_member_access< T[] >
163 #if !defined( __BORLANDC__ ) || !BOOST_WORKAROUND( __BORLANDC__, < 0x600 )
165 template< class T, std::size_t N > struct sp_member_access< T[N] >
172 #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
174 // sp_array_access, return type of operator[]
176 template< class T > struct sp_array_access
181 #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
183 template< class T > struct sp_array_access< T[] >
188 #if !defined( __BORLANDC__ ) || !BOOST_WORKAROUND( __BORLANDC__, < 0x600 )
190 template< class T, std::size_t N > struct sp_array_access< T[N] >
197 #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
199 // sp_extent, for operator[] index check
201 template< class T > struct sp_extent
203 enum _vt { value = 0 };
206 #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
208 template< class T, std::size_t N > struct sp_extent< T[N] >
210 enum _vt { value = N };
213 #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
215 // enable_shared_from_this support
217 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 )
221 pe->_internal_accept_owner( ppx, const_cast< Y* >( py ) );
225 template< class X, class Y > inline void sp_enable_shared_from_this( boost::shared_ptr<X> * ppx, Y const * py, boost::enable_shared_from_raw const * pe );
229 // Avoid C4793, ... causes native code generation
231 struct sp_any_pointer
233 template<class T> sp_any_pointer( T* ) {}
236 inline void sp_enable_shared_from_this( sp_any_pointer, sp_any_pointer, sp_any_pointer )
242 inline void sp_enable_shared_from_this( ... )
248 #if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) && !defined( BOOST_NO_AUTO_PTR )
250 // rvalue auto_ptr support based on a technique by Dave Abrahams
252 template< class T, class R > struct sp_enable_if_auto_ptr
256 template< class T, class R > struct sp_enable_if_auto_ptr< std::auto_ptr< T >, R >
263 // sp_assert_convertible
265 template< class Y, class T > inline void sp_assert_convertible()
267 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
269 // static_assert( sp_convertible< Y, T >::value );
270 typedef char tmp[ sp_convertible< Y, T >::value? 1: -1 ];
275 T* p = static_cast< Y* >( 0 );
281 // pointer constructor helper
283 template< class T, class Y > inline void sp_pointer_construct( boost::shared_ptr< T > * ppx, Y * p, boost::detail::shared_count & pn )
285 boost::detail::shared_count( p ).swap( pn );
286 boost::detail::sp_enable_shared_from_this( ppx, p, p );
289 #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
291 template< class T, class Y > inline void sp_pointer_construct( boost::shared_ptr< T[] > * /*ppx*/, Y * p, boost::detail::shared_count & pn )
293 sp_assert_convertible< Y[], T[] >();
294 boost::detail::shared_count( p, boost::checked_array_deleter< T >() ).swap( pn );
297 template< class T, std::size_t N, class Y > inline void sp_pointer_construct( boost::shared_ptr< T[N] > * /*ppx*/, Y * p, boost::detail::shared_count & pn )
299 sp_assert_convertible< Y[N], T[N] >();
300 boost::detail::shared_count( p, boost::checked_array_deleter< T >() ).swap( pn );
303 #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
305 // deleter constructor helper
307 template< class T, class Y > inline void sp_deleter_construct( boost::shared_ptr< T > * ppx, Y * p )
309 boost::detail::sp_enable_shared_from_this( ppx, p, p );
312 #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
314 template< class T, class Y > inline void sp_deleter_construct( boost::shared_ptr< T[] > * /*ppx*/, Y * /*p*/ )
316 sp_assert_convertible< Y[], T[] >();
319 template< class T, std::size_t N, class Y > inline void sp_deleter_construct( boost::shared_ptr< T[N] > * /*ppx*/, Y * /*p*/ )
321 sp_assert_convertible< Y[N], T[N] >();
324 #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
326 } // namespace detail
332 // An enhanced relative of scoped_ptr with reference counted copy semantics.
333 // The object pointed to is deleted when the last shared_ptr pointing to it
334 // is destroyed or reset.
337 template<class T> class shared_ptr
341 // Borland 5.5.1 specific workaround
342 typedef shared_ptr<T> this_type;
346 typedef typename boost::detail::sp_element< T >::type element_type;
348 shared_ptr() BOOST_SP_NOEXCEPT : px( 0 ), pn() // never throws in 1.30+
352 #if !defined( BOOST_NO_CXX11_NULLPTR )
354 shared_ptr( boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT : px( 0 ), pn() // never throws
361 explicit shared_ptr( Y * p ): px( p ), pn() // Y must be complete
363 boost::detail::sp_pointer_construct( this, p, pn );
367 // Requirements: D's copy constructor must not throw
369 // shared_ptr will release p by calling d(p)
372 template<class Y, class D> shared_ptr( Y * p, D d ): px( p ), pn( p, d )
374 boost::detail::sp_deleter_construct( this, p );
377 #if !defined( BOOST_NO_CXX11_NULLPTR )
379 template<class D> shared_ptr( boost::detail::sp_nullptr_t p, D d ): px( p ), pn( p, d )
385 // As above, but with allocator. A's copy constructor shall not throw.
387 template<class Y, class D, class A> shared_ptr( Y * p, D d, A a ): px( p ), pn( p, d, a )
389 boost::detail::sp_deleter_construct( this, p );
392 #if !defined( BOOST_NO_CXX11_NULLPTR )
394 template<class D, class A> shared_ptr( boost::detail::sp_nullptr_t p, D d, A a ): px( p ), pn( p, d, a )
400 // generated copy constructor, destructor are fine...
402 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
404 // ... except in C++0x, move disables the implicit copy
406 shared_ptr( shared_ptr const & r ) BOOST_SP_NOEXCEPT : px( r.px ), pn( r.pn )
413 explicit shared_ptr( weak_ptr<Y> const & r ): pn( r.pn ) // may throw
415 boost::detail::sp_assert_convertible< Y, T >();
417 // it is now safe to copy r.px, as pn(r.pn) did not throw
422 shared_ptr( weak_ptr<Y> const & r, boost::detail::sp_nothrow_tag )
423 BOOST_NOEXCEPT : px( 0 ), pn( r.pn, boost::detail::sp_nothrow_tag() )
432 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
434 shared_ptr( shared_ptr<Y> const & r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
438 shared_ptr( shared_ptr<Y> const & r )
441 BOOST_NOEXCEPT : px( r.px ), pn( r.pn )
443 boost::detail::sp_assert_convertible< Y, T >();
448 shared_ptr( shared_ptr<Y> const & r, element_type * p ) BOOST_NOEXCEPT : px( p ), pn( r.pn )
452 #ifndef BOOST_NO_AUTO_PTR
455 explicit shared_ptr( std::auto_ptr<Y> & r ): px(r.get()), pn()
457 boost::detail::sp_assert_convertible< Y, T >();
460 pn = boost::detail::shared_count( r );
462 boost::detail::sp_deleter_construct( this, tmp );
465 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
468 shared_ptr( std::auto_ptr<Y> && r ): px(r.get()), pn()
470 boost::detail::sp_assert_convertible< Y, T >();
473 pn = boost::detail::shared_count( r );
475 boost::detail::sp_deleter_construct( this, tmp );
478 #elif !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
481 explicit shared_ptr( Ap r, typename boost::detail::sp_enable_if_auto_ptr<Ap, int>::type = 0 ): px( r.get() ), pn()
483 typedef typename Ap::element_type Y;
485 boost::detail::sp_assert_convertible< Y, T >();
488 pn = boost::detail::shared_count( r );
490 boost::detail::sp_deleter_construct( this, tmp );
493 #endif // BOOST_NO_SFINAE, BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
495 #endif // BOOST_NO_AUTO_PTR
497 #if !defined( BOOST_NO_CXX11_SMART_PTR ) && !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
499 template< class Y, class D >
500 shared_ptr( std::unique_ptr< Y, D > && r ): px( r.get() ), pn()
502 boost::detail::sp_assert_convertible< Y, T >();
504 typename std::unique_ptr< Y, D >::pointer tmp = r.get();
505 pn = boost::detail::shared_count( r );
507 boost::detail::sp_deleter_construct( this, tmp );
512 template< class Y, class D >
513 shared_ptr( boost::movelib::unique_ptr< Y, D > r ): px( r.get() ), pn()
515 boost::detail::sp_assert_convertible< Y, T >();
517 typename boost::movelib::unique_ptr< Y, D >::pointer tmp = r.get();
518 pn = boost::detail::shared_count( r );
520 boost::detail::sp_deleter_construct( this, tmp );
525 shared_ptr & operator=( shared_ptr const & r ) BOOST_SP_NOEXCEPT
527 this_type(r).swap(*this);
531 #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1400)
534 shared_ptr & operator=(shared_ptr<Y> const & r) BOOST_NOEXCEPT
536 this_type(r).swap(*this);
542 #ifndef BOOST_NO_AUTO_PTR
545 shared_ptr & operator=( std::auto_ptr<Y> & r )
547 this_type( r ).swap( *this );
551 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
554 shared_ptr & operator=( std::auto_ptr<Y> && r )
556 this_type( static_cast< std::auto_ptr<Y> && >( r ) ).swap( *this );
560 #elif !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
563 typename boost::detail::sp_enable_if_auto_ptr< Ap, shared_ptr & >::type operator=( Ap r )
565 this_type( r ).swap( *this );
569 #endif // BOOST_NO_SFINAE, BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
571 #endif // BOOST_NO_AUTO_PTR
573 #if !defined( BOOST_NO_CXX11_SMART_PTR ) && !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
575 template<class Y, class D>
576 shared_ptr & operator=( std::unique_ptr<Y, D> && r )
578 this_type( static_cast< std::unique_ptr<Y, D> && >( r ) ).swap(*this);
584 template<class Y, class D>
585 shared_ptr & operator=( boost::movelib::unique_ptr<Y, D> r )
587 // this_type( static_cast< unique_ptr<Y, D> && >( r ) ).swap( *this );
589 boost::detail::sp_assert_convertible< Y, T >();
591 typename boost::movelib::unique_ptr< Y, D >::pointer p = r.get();
596 tmp.pn = boost::detail::shared_count( r );
598 boost::detail::sp_deleter_construct( &tmp, p );
607 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
609 shared_ptr( shared_ptr && r ) BOOST_SP_NOEXCEPT : px( r.px ), pn()
616 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
618 shared_ptr( shared_ptr<Y> && r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
622 shared_ptr( shared_ptr<Y> && r )
625 BOOST_NOEXCEPT : px( r.px ), pn()
627 boost::detail::sp_assert_convertible< Y, T >();
633 shared_ptr & operator=( shared_ptr && r ) BOOST_SP_NOEXCEPT
635 this_type( static_cast< shared_ptr && >( r ) ).swap( *this );
640 shared_ptr & operator=( shared_ptr<Y> && r ) BOOST_NOEXCEPT
642 this_type( static_cast< shared_ptr<Y> && >( r ) ).swap( *this );
648 shared_ptr( shared_ptr<Y> && r, element_type * p ) BOOST_NOEXCEPT : px( p ), pn()
656 #if !defined( BOOST_NO_CXX11_NULLPTR )
658 shared_ptr & operator=( boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT // never throws
660 this_type().swap(*this);
666 void reset() BOOST_NOEXCEPT // never throws in 1.30+
668 this_type().swap(*this);
671 template<class Y> void reset( Y * p ) // Y must be complete
673 BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors
674 this_type( p ).swap( *this );
677 template<class Y, class D> void reset( Y * p, D d )
679 this_type( p, d ).swap( *this );
682 template<class Y, class D, class A> void reset( Y * p, D d, A a )
684 this_type( p, d, a ).swap( *this );
687 template<class Y> void reset( shared_ptr<Y> const & r, element_type * p )
689 this_type( r, p ).swap( *this );
692 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
694 template<class Y> void reset( shared_ptr<Y> && r, element_type * p )
696 this_type( static_cast< shared_ptr<Y> && >( r ), p ).swap( *this );
701 // never throws (but has a BOOST_ASSERT in it, so not marked with BOOST_NOEXCEPT)
702 typename boost::detail::sp_dereference< T >::type operator* () const
704 BOOST_ASSERT( px != 0 );
708 // never throws (but has a BOOST_ASSERT in it, so not marked with BOOST_NOEXCEPT)
709 typename boost::detail::sp_member_access< T >::type operator-> () const
711 BOOST_ASSERT( px != 0 );
715 // never throws (but has a BOOST_ASSERT in it, so not marked with BOOST_NOEXCEPT)
716 typename boost::detail::sp_array_access< T >::type operator[] ( std::ptrdiff_t i ) const
718 BOOST_ASSERT( px != 0 );
719 BOOST_ASSERT( i >= 0 && ( i < boost::detail::sp_extent< T >::value || boost::detail::sp_extent< T >::value == 0 ) );
721 return static_cast< typename boost::detail::sp_array_access< T >::type >( px[ i ] );
724 element_type * get() const BOOST_NOEXCEPT
729 // implicit conversion to "bool"
730 #include <boost/smart_ptr/detail/operator_bool.hpp>
732 bool unique() const BOOST_NOEXCEPT
737 long use_count() const BOOST_NOEXCEPT
739 return pn.use_count();
742 void swap( shared_ptr & other ) BOOST_NOEXCEPT
744 std::swap(px, other.px);
748 template<class Y> bool owner_before( shared_ptr<Y> const & rhs ) const BOOST_NOEXCEPT
753 template<class Y> bool owner_before( weak_ptr<Y> const & rhs ) const BOOST_NOEXCEPT
758 void * _internal_get_deleter( boost::detail::sp_typeinfo const & ti ) const BOOST_NOEXCEPT
760 return pn.get_deleter( ti );
763 void * _internal_get_untyped_deleter() const BOOST_NOEXCEPT
765 return pn.get_untyped_deleter();
768 bool _internal_equiv( shared_ptr const & r ) const BOOST_NOEXCEPT
770 return px == r.px && pn == r.pn;
773 // Tasteless as this may seem, making all members public allows member templates
774 // to work in the absence of member template friends. (Matthew Langston)
776 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
780 template<class Y> friend class shared_ptr;
781 template<class Y> friend class weak_ptr;
786 element_type * px; // contained pointer
787 boost::detail::shared_count pn; // reference counter
791 template<class T, class U> inline bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b) BOOST_NOEXCEPT
793 return a.get() == b.get();
796 template<class T, class U> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<U> const & b) BOOST_NOEXCEPT
798 return a.get() != b.get();
801 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
803 // Resolve the ambiguity between our op!= and the one in rel_ops
805 template<class T> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<T> const & b) BOOST_NOEXCEPT
807 return a.get() != b.get();
812 #if !defined( BOOST_NO_CXX11_NULLPTR )
814 template<class T> inline bool operator==( shared_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT
819 template<class T> inline bool operator==( boost::detail::sp_nullptr_t, shared_ptr<T> const & p ) BOOST_NOEXCEPT
824 template<class T> inline bool operator!=( shared_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT
829 template<class T> inline bool operator!=( boost::detail::sp_nullptr_t, shared_ptr<T> const & p ) BOOST_NOEXCEPT
836 template<class T, class U> inline bool operator<(shared_ptr<T> const & a, shared_ptr<U> const & b) BOOST_NOEXCEPT
838 return a.owner_before( b );
841 template<class T> inline void swap(shared_ptr<T> & a, shared_ptr<T> & b) BOOST_NOEXCEPT
846 template<class T, class U> shared_ptr<T> static_pointer_cast( shared_ptr<U> const & r ) BOOST_NOEXCEPT
848 (void) static_cast< T* >( static_cast< U* >( 0 ) );
850 typedef typename shared_ptr<T>::element_type E;
852 E * p = static_cast< E* >( r.get() );
853 return shared_ptr<T>( r, p );
856 template<class T, class U> shared_ptr<T> const_pointer_cast( shared_ptr<U> const & r ) BOOST_NOEXCEPT
858 (void) const_cast< T* >( static_cast< U* >( 0 ) );
860 typedef typename shared_ptr<T>::element_type E;
862 E * p = const_cast< E* >( r.get() );
863 return shared_ptr<T>( r, p );
866 template<class T, class U> shared_ptr<T> dynamic_pointer_cast( shared_ptr<U> const & r ) BOOST_NOEXCEPT
868 (void) dynamic_cast< T* >( static_cast< U* >( 0 ) );
870 typedef typename shared_ptr<T>::element_type E;
872 E * p = dynamic_cast< E* >( r.get() );
873 return p? shared_ptr<T>( r, p ): shared_ptr<T>();
876 template<class T, class U> shared_ptr<T> reinterpret_pointer_cast( shared_ptr<U> const & r ) BOOST_NOEXCEPT
878 (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
880 typedef typename shared_ptr<T>::element_type E;
882 E * p = reinterpret_cast< E* >( r.get() );
883 return shared_ptr<T>( r, p );
886 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
888 template<class T, class U> shared_ptr<T> static_pointer_cast( shared_ptr<U> && r ) BOOST_NOEXCEPT
890 (void) static_cast< T* >( static_cast< U* >( 0 ) );
892 typedef typename shared_ptr<T>::element_type E;
894 E * p = static_cast< E* >( r.get() );
895 return shared_ptr<T>( std::move(r), p );
898 template<class T, class U> shared_ptr<T> const_pointer_cast( shared_ptr<U> && r ) BOOST_NOEXCEPT
900 (void) const_cast< T* >( static_cast< U* >( 0 ) );
902 typedef typename shared_ptr<T>::element_type E;
904 E * p = const_cast< E* >( r.get() );
905 return shared_ptr<T>( std::move(r), p );
908 template<class T, class U> shared_ptr<T> dynamic_pointer_cast( shared_ptr<U> && r ) BOOST_NOEXCEPT
910 (void) dynamic_cast< T* >( static_cast< U* >( 0 ) );
912 typedef typename shared_ptr<T>::element_type E;
914 E * p = dynamic_cast< E* >( r.get() );
915 return p? shared_ptr<T>( std::move(r), p ): shared_ptr<T>();
918 template<class T, class U> shared_ptr<T> reinterpret_pointer_cast( shared_ptr<U> && r ) BOOST_NOEXCEPT
920 (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
922 typedef typename shared_ptr<T>::element_type E;
924 E * p = reinterpret_cast< E* >( r.get() );
925 return shared_ptr<T>( std::move(r), p );
928 #endif // !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
930 // get_pointer() enables boost::mem_fn to recognize shared_ptr
932 template<class T> inline typename shared_ptr<T>::element_type * get_pointer(shared_ptr<T> const & p) BOOST_NOEXCEPT
939 #if !defined(BOOST_NO_IOSTREAM)
941 #if defined(BOOST_NO_TEMPLATED_IOSTREAMS) || ( defined(__GNUC__) && (__GNUC__ < 3) )
943 template<class Y> std::ostream & operator<< (std::ostream & os, shared_ptr<Y> const & p)
951 // in STLport's no-iostreams mode no iostream symbols can be used
952 #ifndef _STLP_NO_IOSTREAMS
954 # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300 && __SGI_STL_PORT)
955 // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
956 using std::basic_ostream;
957 template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, shared_ptr<Y> const & p)
959 template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, shared_ptr<Y> const & p)
966 #endif // _STLP_NO_IOSTREAMS
968 #endif // __GNUC__ < 3
970 #endif // !defined(BOOST_NO_IOSTREAM)
977 #if ( defined(__GNUC__) && BOOST_WORKAROUND(__GNUC__, < 3) ) || \
978 ( defined(__EDG_VERSION__) && BOOST_WORKAROUND(__EDG_VERSION__, <= 238) ) || \
979 ( defined(__HP_aCC) && BOOST_WORKAROUND(__HP_aCC, <= 33500) )
981 // g++ 2.9x doesn't allow static_cast<X const *>(void *)
982 // apparently EDG 2.38 and HP aCC A.03.35 also don't accept it
984 template<class D, class T> D * basic_get_deleter(shared_ptr<T> const & p)
986 void const * q = p._internal_get_deleter(BOOST_SP_TYPEID(D));
987 return const_cast<D *>(static_cast<D const *>(q));
992 template<class D, class T> D * basic_get_deleter( shared_ptr<T> const & p ) BOOST_NOEXCEPT
994 return static_cast<D *>( p._internal_get_deleter(BOOST_SP_TYPEID(D)) );
999 class esft2_deleter_wrapper
1003 shared_ptr<void const volatile> deleter_;
1007 esft2_deleter_wrapper()
1011 template< class T > void set_deleter( shared_ptr<T> const & deleter )
1016 template<typename D> D* get_deleter() const BOOST_NOEXCEPT
1018 return boost::detail::basic_get_deleter<D>( deleter_ );
1021 template< class T> void operator()( T* )
1023 BOOST_ASSERT( deleter_.use_count() <= 1 );
1028 } // namespace detail
1030 template<class D, class T> D * get_deleter( shared_ptr<T> const & p ) BOOST_NOEXCEPT
1032 D *del = boost::detail::basic_get_deleter<D>(p);
1036 boost::detail::esft2_deleter_wrapper *del_wrapper = boost::detail::basic_get_deleter<boost::detail::esft2_deleter_wrapper>(p);
1037 // The following get_deleter method call is fully qualified because
1038 // older versions of gcc (2.95, 3.2.3) fail to compile it when written del_wrapper->get_deleter<D>()
1039 if(del_wrapper) del = del_wrapper->::boost::detail::esft2_deleter_wrapper::get_deleter<D>();
1047 #if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
1049 template<class T> inline bool atomic_is_lock_free( shared_ptr<T> const * /*p*/ ) BOOST_NOEXCEPT
1054 template<class T> shared_ptr<T> atomic_load( shared_ptr<T> const * p )
1056 boost::detail::spinlock_pool<2>::scoped_lock lock( p );
1060 template<class T> inline shared_ptr<T> atomic_load_explicit( shared_ptr<T> const * p, /*memory_order mo*/ int )
1062 return atomic_load( p );
1065 template<class T> void atomic_store( shared_ptr<T> * p, shared_ptr<T> r )
1067 boost::detail::spinlock_pool<2>::scoped_lock lock( p );
1071 template<class T> inline void atomic_store_explicit( shared_ptr<T> * p, shared_ptr<T> r, /*memory_order mo*/ int )
1073 atomic_store( p, r ); // std::move( r )
1076 template<class T> shared_ptr<T> atomic_exchange( shared_ptr<T> * p, shared_ptr<T> r )
1078 boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( p );
1084 return r; // return std::move( r )
1087 template<class T> shared_ptr<T> atomic_exchange_explicit( shared_ptr<T> * p, shared_ptr<T> r, /*memory_order mo*/ int )
1089 return atomic_exchange( p, r ); // std::move( r )
1092 template<class T> bool atomic_compare_exchange( shared_ptr<T> * p, shared_ptr<T> * v, shared_ptr<T> w )
1094 boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( p );
1098 if( p->_internal_equiv( *v ) )
1108 shared_ptr<T> tmp( *p );
1117 template<class T> inline bool atomic_compare_exchange_explicit( shared_ptr<T> * p, shared_ptr<T> * v, shared_ptr<T> w, /*memory_order success*/ int, /*memory_order failure*/ int )
1119 return atomic_compare_exchange( p, v, w ); // std::move( w )
1122 #endif // !defined(BOOST_SP_NO_ATOMIC_ACCESS)
1126 template< class T > struct hash;
1128 template< class T > std::size_t hash_value( boost::shared_ptr<T> const & p ) BOOST_NOEXCEPT
1130 return boost::hash< typename boost::shared_ptr<T>::element_type* >()( p.get() );
1133 } // namespace boost
1135 #if defined( BOOST_SP_DISABLE_DEPRECATED )
1136 #pragma GCC diagnostic pop
1139 #endif // #ifndef BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED