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/ 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() BOOST_SP_NOEXCEPT
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 struct sp_internal_constructor_tag
330 } // namespace detail
336 // An enhanced relative of scoped_ptr with reference counted copy semantics.
337 // The object pointed to is deleted when the last shared_ptr pointing to it
338 // is destroyed or reset.
341 template<class T> class shared_ptr
345 // Borland 5.5.1 specific workaround
346 typedef shared_ptr<T> this_type;
350 typedef typename boost::detail::sp_element< T >::type element_type;
352 BOOST_CONSTEXPR shared_ptr() BOOST_SP_NOEXCEPT : px( 0 ), pn()
356 #if !defined( BOOST_NO_CXX11_NULLPTR )
358 BOOST_CONSTEXPR shared_ptr( boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT : px( 0 ), pn()
364 BOOST_CONSTEXPR shared_ptr( boost::detail::sp_internal_constructor_tag, element_type * px_, boost::detail::shared_count const & pn_ ) BOOST_SP_NOEXCEPT : px( px_ ), pn( pn_ )
368 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
370 BOOST_CONSTEXPR shared_ptr( boost::detail::sp_internal_constructor_tag, element_type * px_, boost::detail::shared_count && pn_ ) BOOST_SP_NOEXCEPT : px( px_ ), pn( std::move( pn_ ) )
377 explicit shared_ptr( Y * p ): px( p ), pn() // Y must be complete
379 boost::detail::sp_pointer_construct( this, p, pn );
383 // Requirements: D's copy constructor must not throw
385 // shared_ptr will release p by calling d(p)
388 template<class Y, class D> shared_ptr( Y * p, D d ): px( p ), pn( p, d )
390 boost::detail::sp_deleter_construct( this, p );
393 #if !defined( BOOST_NO_CXX11_NULLPTR )
395 template<class D> shared_ptr( boost::detail::sp_nullptr_t p, D d ): px( p ), pn( p, d )
401 // As above, but with allocator. A's copy constructor shall not throw.
403 template<class Y, class D, class A> shared_ptr( Y * p, D d, A a ): px( p ), pn( p, d, a )
405 boost::detail::sp_deleter_construct( this, p );
408 #if !defined( BOOST_NO_CXX11_NULLPTR )
410 template<class D, class A> shared_ptr( boost::detail::sp_nullptr_t p, D d, A a ): px( p ), pn( p, d, a )
416 // generated copy constructor, destructor are fine...
418 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
420 // ... except in C++0x, move disables the implicit copy
422 shared_ptr( shared_ptr const & r ) BOOST_SP_NOEXCEPT : px( r.px ), pn( r.pn )
429 explicit shared_ptr( weak_ptr<Y> const & r ): pn( r.pn ) // may throw
431 boost::detail::sp_assert_convertible< Y, T >();
433 // it is now safe to copy r.px, as pn(r.pn) did not throw
438 shared_ptr( weak_ptr<Y> const & r, boost::detail::sp_nothrow_tag )
439 BOOST_SP_NOEXCEPT : px( 0 ), pn( r.pn, boost::detail::sp_nothrow_tag() )
448 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
450 shared_ptr( shared_ptr<Y> const & r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
454 shared_ptr( shared_ptr<Y> const & r )
457 BOOST_SP_NOEXCEPT : px( r.px ), pn( r.pn )
459 boost::detail::sp_assert_convertible< Y, T >();
464 shared_ptr( shared_ptr<Y> const & r, element_type * p ) BOOST_SP_NOEXCEPT : px( p ), pn( r.pn )
468 #ifndef BOOST_NO_AUTO_PTR
471 explicit shared_ptr( std::auto_ptr<Y> & r ): px(r.get()), pn()
473 boost::detail::sp_assert_convertible< Y, T >();
476 pn = boost::detail::shared_count( r );
478 boost::detail::sp_deleter_construct( this, tmp );
481 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
484 shared_ptr( std::auto_ptr<Y> && r ): px(r.get()), pn()
486 boost::detail::sp_assert_convertible< Y, T >();
489 pn = boost::detail::shared_count( r );
491 boost::detail::sp_deleter_construct( this, tmp );
494 #elif !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
497 explicit shared_ptr( Ap r, typename boost::detail::sp_enable_if_auto_ptr<Ap, int>::type = 0 ): px( r.get() ), pn()
499 typedef typename Ap::element_type Y;
501 boost::detail::sp_assert_convertible< Y, T >();
504 pn = boost::detail::shared_count( r );
506 boost::detail::sp_deleter_construct( this, tmp );
509 #endif // BOOST_NO_SFINAE, BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
511 #endif // BOOST_NO_AUTO_PTR
513 #if !defined( BOOST_NO_CXX11_SMART_PTR ) && !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
515 template< class Y, class D >
516 shared_ptr( std::unique_ptr< Y, D > && r ): px( r.get() ), pn()
518 boost::detail::sp_assert_convertible< Y, T >();
520 typename std::unique_ptr< Y, D >::pointer tmp = r.get();
524 pn = boost::detail::shared_count( r );
525 boost::detail::sp_deleter_construct( this, tmp );
531 template< class Y, class D >
532 shared_ptr( boost::movelib::unique_ptr< Y, D > r ): px( r.get() ), pn()
534 boost::detail::sp_assert_convertible< Y, T >();
536 typename boost::movelib::unique_ptr< Y, D >::pointer tmp = r.get();
540 pn = boost::detail::shared_count( r );
541 boost::detail::sp_deleter_construct( this, tmp );
547 shared_ptr & operator=( shared_ptr const & r ) BOOST_SP_NOEXCEPT
549 this_type(r).swap(*this);
553 #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1400)
556 shared_ptr & operator=(shared_ptr<Y> const & r) BOOST_SP_NOEXCEPT
558 this_type(r).swap(*this);
564 #ifndef BOOST_NO_AUTO_PTR
567 shared_ptr & operator=( std::auto_ptr<Y> & r )
569 this_type( r ).swap( *this );
573 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
576 shared_ptr & operator=( std::auto_ptr<Y> && r )
578 this_type( static_cast< std::auto_ptr<Y> && >( r ) ).swap( *this );
582 #elif !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
585 typename boost::detail::sp_enable_if_auto_ptr< Ap, shared_ptr & >::type operator=( Ap r )
587 this_type( r ).swap( *this );
591 #endif // BOOST_NO_SFINAE, BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
593 #endif // BOOST_NO_AUTO_PTR
595 #if !defined( BOOST_NO_CXX11_SMART_PTR ) && !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
597 template<class Y, class D>
598 shared_ptr & operator=( std::unique_ptr<Y, D> && r )
600 this_type( static_cast< std::unique_ptr<Y, D> && >( r ) ).swap(*this);
606 template<class Y, class D>
607 shared_ptr & operator=( boost::movelib::unique_ptr<Y, D> r )
609 // this_type( static_cast< unique_ptr<Y, D> && >( r ) ).swap( *this );
611 boost::detail::sp_assert_convertible< Y, T >();
613 typename boost::movelib::unique_ptr< Y, D >::pointer p = r.get();
620 tmp.pn = boost::detail::shared_count( r );
622 boost::detail::sp_deleter_construct( &tmp, p );
632 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
634 shared_ptr( shared_ptr && r ) BOOST_SP_NOEXCEPT : px( r.px ), pn()
641 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
643 shared_ptr( shared_ptr<Y> && r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
647 shared_ptr( shared_ptr<Y> && r )
650 BOOST_SP_NOEXCEPT : px( r.px ), pn()
652 boost::detail::sp_assert_convertible< Y, T >();
658 shared_ptr & operator=( shared_ptr && r ) BOOST_SP_NOEXCEPT
660 this_type( static_cast< shared_ptr && >( r ) ).swap( *this );
665 shared_ptr & operator=( shared_ptr<Y> && r ) BOOST_SP_NOEXCEPT
667 this_type( static_cast< shared_ptr<Y> && >( r ) ).swap( *this );
673 shared_ptr( shared_ptr<Y> && r, element_type * p ) BOOST_SP_NOEXCEPT : px( p ), pn()
681 #if !defined( BOOST_NO_CXX11_NULLPTR )
683 shared_ptr & operator=( boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT
685 this_type().swap(*this);
691 void reset() BOOST_SP_NOEXCEPT
693 this_type().swap(*this);
696 template<class Y> void reset( Y * p ) // Y must be complete
698 BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors
699 this_type( p ).swap( *this );
702 template<class Y, class D> void reset( Y * p, D d )
704 this_type( p, d ).swap( *this );
707 template<class Y, class D, class A> void reset( Y * p, D d, A a )
709 this_type( p, d, a ).swap( *this );
712 template<class Y> void reset( shared_ptr<Y> const & r, element_type * p ) BOOST_SP_NOEXCEPT
714 this_type( r, p ).swap( *this );
717 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
719 template<class Y> void reset( shared_ptr<Y> && r, element_type * p ) BOOST_SP_NOEXCEPT
721 this_type( static_cast< shared_ptr<Y> && >( r ), p ).swap( *this );
726 typename boost::detail::sp_dereference< T >::type operator* () const BOOST_SP_NOEXCEPT_WITH_ASSERT
728 BOOST_ASSERT( px != 0 );
732 typename boost::detail::sp_member_access< T >::type operator-> () const BOOST_SP_NOEXCEPT_WITH_ASSERT
734 BOOST_ASSERT( px != 0 );
738 typename boost::detail::sp_array_access< T >::type operator[] ( std::ptrdiff_t i ) const BOOST_SP_NOEXCEPT_WITH_ASSERT
740 BOOST_ASSERT( px != 0 );
741 BOOST_ASSERT( i >= 0 && ( i < boost::detail::sp_extent< T >::value || boost::detail::sp_extent< T >::value == 0 ) );
743 return static_cast< typename boost::detail::sp_array_access< T >::type >( px[ i ] );
746 element_type * get() const BOOST_SP_NOEXCEPT
751 // implicit conversion to "bool"
752 #include <boost/smart_ptr/detail/operator_bool.hpp>
754 bool unique() const BOOST_SP_NOEXCEPT
759 long use_count() const BOOST_SP_NOEXCEPT
761 return pn.use_count();
764 void swap( shared_ptr & other ) BOOST_SP_NOEXCEPT
766 std::swap(px, other.px);
770 template<class Y> bool owner_before( shared_ptr<Y> const & rhs ) const BOOST_SP_NOEXCEPT
775 template<class Y> bool owner_before( weak_ptr<Y> const & rhs ) const BOOST_SP_NOEXCEPT
780 void * _internal_get_deleter( boost::detail::sp_typeinfo_ const & ti ) const BOOST_SP_NOEXCEPT
782 return pn.get_deleter( ti );
785 void * _internal_get_local_deleter( boost::detail::sp_typeinfo_ const & ti ) const BOOST_SP_NOEXCEPT
787 return pn.get_local_deleter( ti );
790 void * _internal_get_untyped_deleter() const BOOST_SP_NOEXCEPT
792 return pn.get_untyped_deleter();
795 bool _internal_equiv( shared_ptr const & r ) const BOOST_SP_NOEXCEPT
797 return px == r.px && pn == r.pn;
800 boost::detail::shared_count _internal_count() const BOOST_SP_NOEXCEPT
805 // Tasteless as this may seem, making all members public allows member templates
806 // to work in the absence of member template friends. (Matthew Langston)
808 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
812 template<class Y> friend class shared_ptr;
813 template<class Y> friend class weak_ptr;
818 element_type * px; // contained pointer
819 boost::detail::shared_count pn; // reference counter
823 template<class T, class U> inline bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b) BOOST_SP_NOEXCEPT
825 return a.get() == b.get();
828 template<class T, class U> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<U> const & b) BOOST_SP_NOEXCEPT
830 return a.get() != b.get();
833 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
835 // Resolve the ambiguity between our op!= and the one in rel_ops
837 template<class T> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<T> const & b) BOOST_SP_NOEXCEPT
839 return a.get() != b.get();
844 #if !defined( BOOST_NO_CXX11_NULLPTR )
846 template<class T> inline bool operator==( shared_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT
851 template<class T> inline bool operator==( boost::detail::sp_nullptr_t, shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
856 template<class T> inline bool operator!=( shared_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT
861 template<class T> inline bool operator!=( boost::detail::sp_nullptr_t, shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
868 template<class T, class U> inline bool operator<(shared_ptr<T> const & a, shared_ptr<U> const & b) BOOST_SP_NOEXCEPT
870 return a.owner_before( b );
873 template<class T> inline void swap(shared_ptr<T> & a, shared_ptr<T> & b) BOOST_SP_NOEXCEPT
878 template<class T, class U> shared_ptr<T> static_pointer_cast( shared_ptr<U> const & r ) BOOST_SP_NOEXCEPT
880 (void) static_cast< T* >( static_cast< U* >( 0 ) );
882 typedef typename shared_ptr<T>::element_type E;
884 E * p = static_cast< E* >( r.get() );
885 return shared_ptr<T>( r, p );
888 template<class T, class U> shared_ptr<T> const_pointer_cast( shared_ptr<U> const & r ) BOOST_SP_NOEXCEPT
890 (void) const_cast< T* >( static_cast< U* >( 0 ) );
892 typedef typename shared_ptr<T>::element_type E;
894 E * p = const_cast< E* >( r.get() );
895 return shared_ptr<T>( r, p );
898 template<class T, class U> shared_ptr<T> dynamic_pointer_cast( shared_ptr<U> const & r ) BOOST_SP_NOEXCEPT
900 (void) dynamic_cast< T* >( static_cast< U* >( 0 ) );
902 typedef typename shared_ptr<T>::element_type E;
904 E * p = dynamic_cast< E* >( r.get() );
905 return p? shared_ptr<T>( r, p ): shared_ptr<T>();
908 template<class T, class U> shared_ptr<T> reinterpret_pointer_cast( shared_ptr<U> const & r ) BOOST_SP_NOEXCEPT
910 (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
912 typedef typename shared_ptr<T>::element_type E;
914 E * p = reinterpret_cast< E* >( r.get() );
915 return shared_ptr<T>( r, p );
918 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
920 template<class T, class U> shared_ptr<T> static_pointer_cast( shared_ptr<U> && r ) BOOST_SP_NOEXCEPT
922 (void) static_cast< T* >( static_cast< U* >( 0 ) );
924 typedef typename shared_ptr<T>::element_type E;
926 E * p = static_cast< E* >( r.get() );
927 return shared_ptr<T>( std::move(r), p );
930 template<class T, class U> shared_ptr<T> const_pointer_cast( shared_ptr<U> && r ) BOOST_SP_NOEXCEPT
932 (void) const_cast< T* >( static_cast< U* >( 0 ) );
934 typedef typename shared_ptr<T>::element_type E;
936 E * p = const_cast< E* >( r.get() );
937 return shared_ptr<T>( std::move(r), p );
940 template<class T, class U> shared_ptr<T> dynamic_pointer_cast( shared_ptr<U> && r ) BOOST_SP_NOEXCEPT
942 (void) dynamic_cast< T* >( static_cast< U* >( 0 ) );
944 typedef typename shared_ptr<T>::element_type E;
946 E * p = dynamic_cast< E* >( r.get() );
947 return p? shared_ptr<T>( std::move(r), p ): shared_ptr<T>();
950 template<class T, class U> shared_ptr<T> reinterpret_pointer_cast( shared_ptr<U> && r ) BOOST_SP_NOEXCEPT
952 (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
954 typedef typename shared_ptr<T>::element_type E;
956 E * p = reinterpret_cast< E* >( r.get() );
957 return shared_ptr<T>( std::move(r), p );
960 #endif // !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
962 // get_pointer() enables boost::mem_fn to recognize shared_ptr
964 template<class T> inline typename shared_ptr<T>::element_type * get_pointer(shared_ptr<T> const & p) BOOST_SP_NOEXCEPT
971 #if !defined(BOOST_NO_IOSTREAM)
973 #if defined(BOOST_NO_TEMPLATED_IOSTREAMS) || ( defined(__GNUC__) && (__GNUC__ < 3) )
975 template<class Y> std::ostream & operator<< (std::ostream & os, shared_ptr<Y> const & p)
983 // in STLport's no-iostreams mode no iostream symbols can be used
984 #ifndef _STLP_NO_IOSTREAMS
986 # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300 && __SGI_STL_PORT)
987 // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
988 using std::basic_ostream;
989 template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, shared_ptr<Y> const & p)
991 template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, shared_ptr<Y> const & p)
998 #endif // _STLP_NO_IOSTREAMS
1000 #endif // __GNUC__ < 3
1002 #endif // !defined(BOOST_NO_IOSTREAM)
1009 template<class D, class T> D * basic_get_deleter( shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
1011 return static_cast<D *>( p._internal_get_deleter(BOOST_SP_TYPEID_(D)) );
1014 template<class D, class T> D * basic_get_local_deleter( D *, shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT;
1015 template<class D, class T> D const * basic_get_local_deleter( D const *, shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT;
1017 class esft2_deleter_wrapper
1021 shared_ptr<void const volatile> deleter_;
1025 esft2_deleter_wrapper() BOOST_SP_NOEXCEPT
1029 template< class T > void set_deleter( shared_ptr<T> const & deleter ) BOOST_SP_NOEXCEPT
1034 template<typename D> D* get_deleter() const BOOST_SP_NOEXCEPT
1036 return boost::detail::basic_get_deleter<D>( deleter_ );
1039 template< class T> void operator()( T* ) BOOST_SP_NOEXCEPT_WITH_ASSERT
1041 BOOST_ASSERT( deleter_.use_count() <= 1 );
1046 } // namespace detail
1048 template<class D, class T> D * get_deleter( shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
1050 D * d = boost::detail::basic_get_deleter<D>( p );
1054 d = boost::detail::basic_get_local_deleter( d, p );
1059 boost::detail::esft2_deleter_wrapper *del_wrapper = boost::detail::basic_get_deleter<boost::detail::esft2_deleter_wrapper>(p);
1060 // The following get_deleter method call is fully qualified because
1061 // older versions of gcc (2.95, 3.2.3) fail to compile it when written del_wrapper->get_deleter<D>()
1062 if(del_wrapper) d = del_wrapper->::boost::detail::esft2_deleter_wrapper::get_deleter<D>();
1070 #if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
1072 template<class T> inline bool atomic_is_lock_free( shared_ptr<T> const * /*p*/ ) BOOST_SP_NOEXCEPT
1077 template<class T> shared_ptr<T> atomic_load( shared_ptr<T> const * p ) BOOST_SP_NOEXCEPT
1079 boost::detail::spinlock_pool<2>::scoped_lock lock( p );
1083 template<class T, class M> inline shared_ptr<T> atomic_load_explicit( shared_ptr<T> const * p, /*memory_order mo*/ M ) BOOST_SP_NOEXCEPT
1085 return atomic_load( p );
1088 template<class T> void atomic_store( shared_ptr<T> * p, shared_ptr<T> r ) BOOST_SP_NOEXCEPT
1090 boost::detail::spinlock_pool<2>::scoped_lock lock( p );
1094 template<class T, class M> inline void atomic_store_explicit( shared_ptr<T> * p, shared_ptr<T> r, /*memory_order mo*/ M ) BOOST_SP_NOEXCEPT
1096 atomic_store( p, r ); // std::move( r )
1099 template<class T> shared_ptr<T> atomic_exchange( shared_ptr<T> * p, shared_ptr<T> r ) BOOST_SP_NOEXCEPT
1101 boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( p );
1107 return r; // return std::move( r )
1110 template<class T, class M> shared_ptr<T> inline atomic_exchange_explicit( shared_ptr<T> * p, shared_ptr<T> r, /*memory_order mo*/ M ) BOOST_SP_NOEXCEPT
1112 return atomic_exchange( p, r ); // std::move( r )
1115 template<class T> bool atomic_compare_exchange( shared_ptr<T> * p, shared_ptr<T> * v, shared_ptr<T> w ) BOOST_SP_NOEXCEPT
1117 boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( p );
1121 if( p->_internal_equiv( *v ) )
1131 shared_ptr<T> tmp( *p );
1140 template<class T, class M> inline bool atomic_compare_exchange_explicit( shared_ptr<T> * p, shared_ptr<T> * v, shared_ptr<T> w, /*memory_order success*/ M, /*memory_order failure*/ M ) BOOST_SP_NOEXCEPT
1142 return atomic_compare_exchange( p, v, w ); // std::move( w )
1145 #endif // !defined(BOOST_SP_NO_ATOMIC_ACCESS)
1149 template< class T > struct hash;
1151 template< class T > std::size_t hash_value( boost::shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
1153 return boost::hash< typename boost::shared_ptr<T>::element_type* >()( p.get() );
1156 } // namespace boost
1158 #include <boost/smart_ptr/detail/local_sp_deleter.hpp>
1166 template<class D, class T> D * basic_get_local_deleter( D *, shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
1168 return static_cast<D *>( p._internal_get_local_deleter( BOOST_SP_TYPEID_(local_sp_deleter<D>) ) );
1171 template<class D, class T> D const * basic_get_local_deleter( D const *, shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
1173 return static_cast<D *>( p._internal_get_local_deleter( BOOST_SP_TYPEID_(local_sp_deleter<D>) ) );
1176 } // namespace detail
1178 } // namespace boost
1180 #if defined( BOOST_SP_DISABLE_DEPRECATED )
1181 #pragma GCC diagnostic pop
1184 #endif // #ifndef BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED