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/smart_ptr/detail/shared_count.hpp>
18 #include <boost/smart_ptr/detail/sp_convertible.hpp>
19 #include <boost/smart_ptr/detail/sp_nullptr_t.hpp>
20 #include <boost/smart_ptr/detail/sp_disable_deprecated.hpp>
21 #include <boost/smart_ptr/detail/sp_noexcept.hpp>
22 #include <boost/checked_delete.hpp>
23 #include <boost/throw_exception.hpp>
24 #include <boost/assert.hpp>
25 #include <boost/config.hpp>
26 #include <boost/config/workaround.hpp>
28 #if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
29 #include <boost/smart_ptr/detail/spinlock_pool.hpp>
32 #include <algorithm> // for std::swap
33 #include <functional> // for std::less
34 #include <typeinfo> // for std::bad_cast
35 #include <cstddef> // for std::size_t
36 #include <memory> // for std::auto_ptr
38 #if !defined(BOOST_NO_IOSTREAM)
39 #if !defined(BOOST_NO_IOSFWD)
40 #include <iosfwd> // for std::basic_ostream
46 #if defined( BOOST_SP_DISABLE_DEPRECATED )
47 #pragma GCC diagnostic push
48 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
54 template<class T> class shared_ptr;
55 template<class T> class weak_ptr;
56 template<class T> class enable_shared_from_this;
57 class enable_shared_from_raw;
62 template< class T, class D > class unique_ptr;
64 } // namespace movelib
69 // sp_element, element_type
71 template< class T > struct sp_element
76 #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
78 template< class T > struct sp_element< T[] >
83 #if !defined( BOOST_BORLANDC ) || !BOOST_WORKAROUND( BOOST_BORLANDC, < 0x600 )
85 template< class T, std::size_t N > struct sp_element< T[N] >
92 #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
94 // sp_dereference, return type of operator*
96 template< class T > struct sp_dereference
101 template<> struct sp_dereference< void >
106 #if !defined(BOOST_NO_CV_VOID_SPECIALIZATIONS)
108 template<> struct sp_dereference< void const >
113 template<> struct sp_dereference< void volatile >
118 template<> struct sp_dereference< void const volatile >
123 #endif // !defined(BOOST_NO_CV_VOID_SPECIALIZATIONS)
125 #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
127 template< class T > struct sp_dereference< T[] >
132 #if !defined( BOOST_BORLANDC ) || !BOOST_WORKAROUND( BOOST_BORLANDC, < 0x600 )
134 template< class T, std::size_t N > struct sp_dereference< T[N] >
141 #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
143 // sp_member_access, return type of operator->
145 template< class T > struct sp_member_access
150 #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
152 template< class T > struct sp_member_access< T[] >
157 #if !defined( BOOST_BORLANDC ) || !BOOST_WORKAROUND( BOOST_BORLANDC, < 0x600 )
159 template< class T, std::size_t N > struct sp_member_access< T[N] >
166 #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
168 // sp_array_access, return type of operator[]
170 template< class T > struct sp_array_access
175 #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
177 template< class T > struct sp_array_access< T[] >
182 #if !defined( BOOST_BORLANDC ) || !BOOST_WORKAROUND( BOOST_BORLANDC, < 0x600 )
184 template< class T, std::size_t N > struct sp_array_access< T[N] >
191 #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
193 // sp_extent, for operator[] index check
195 template< class T > struct sp_extent
197 enum _vt { value = 0 };
200 #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
202 template< class T, std::size_t N > struct sp_extent< T[N] >
204 enum _vt { value = N };
207 #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
209 // enable_shared_from_this support
211 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 )
215 pe->_internal_accept_owner( ppx, const_cast< Y* >( py ) );
219 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 );
223 // Avoid C4793, ... causes native code generation
225 struct sp_any_pointer
227 template<class T> sp_any_pointer( T* ) {}
230 inline void sp_enable_shared_from_this( sp_any_pointer, sp_any_pointer, sp_any_pointer )
236 inline void sp_enable_shared_from_this( ... )
242 #if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) && !defined( BOOST_NO_AUTO_PTR )
244 // rvalue auto_ptr support based on a technique by Dave Abrahams
246 template< class T, class R > struct sp_enable_if_auto_ptr
250 template< class T, class R > struct sp_enable_if_auto_ptr< std::auto_ptr< T >, R >
257 // sp_assert_convertible
259 template< class Y, class T > inline void sp_assert_convertible() BOOST_SP_NOEXCEPT
261 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
263 // static_assert( sp_convertible< Y, T >::value );
264 typedef char tmp[ sp_convertible< Y, T >::value? 1: -1 ];
269 T* p = static_cast< Y* >( 0 );
275 // pointer constructor helper
277 template< class T, class Y > inline void sp_pointer_construct( boost::shared_ptr< T > * ppx, Y * p, boost::detail::shared_count & pn )
279 boost::detail::shared_count( p ).swap( pn );
280 boost::detail::sp_enable_shared_from_this( ppx, p, p );
283 #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
285 template< class T, class Y > inline void sp_pointer_construct( boost::shared_ptr< T[] > * /*ppx*/, Y * p, boost::detail::shared_count & pn )
287 sp_assert_convertible< Y[], T[] >();
288 boost::detail::shared_count( p, boost::checked_array_deleter< T >() ).swap( pn );
291 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 )
293 sp_assert_convertible< Y[N], T[N] >();
294 boost::detail::shared_count( p, boost::checked_array_deleter< T >() ).swap( pn );
297 #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
299 // deleter constructor helper
301 template< class T, class Y > inline void sp_deleter_construct( boost::shared_ptr< T > * ppx, Y * p )
303 boost::detail::sp_enable_shared_from_this( ppx, p, p );
306 #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
308 template< class T, class Y > inline void sp_deleter_construct( boost::shared_ptr< T[] > * /*ppx*/, Y * /*p*/ )
310 sp_assert_convertible< Y[], T[] >();
313 template< class T, std::size_t N, class Y > inline void sp_deleter_construct( boost::shared_ptr< T[N] > * /*ppx*/, Y * /*p*/ )
315 sp_assert_convertible< Y[N], T[N] >();
318 #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
320 struct sp_internal_constructor_tag
324 } // namespace detail
330 // An enhanced relative of scoped_ptr with reference counted copy semantics.
331 // The object pointed to is deleted when the last shared_ptr pointing to it
332 // is destroyed or reset.
335 template<class T> class shared_ptr
339 // Borland 5.5.1 specific workaround
340 typedef shared_ptr<T> this_type;
344 typedef typename boost::detail::sp_element< T >::type element_type;
346 BOOST_CONSTEXPR shared_ptr() BOOST_SP_NOEXCEPT : px( 0 ), pn()
350 #if !defined( BOOST_NO_CXX11_NULLPTR )
352 BOOST_CONSTEXPR shared_ptr( boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT : px( 0 ), pn()
358 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_ )
362 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
364 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_ ) )
371 explicit shared_ptr( Y * p ): px( p ), pn() // Y must be complete
373 boost::detail::sp_pointer_construct( this, p, pn );
377 // Requirements: D's copy constructor must not throw
379 // shared_ptr will release p by calling d(p)
382 template<class Y, class D> shared_ptr( Y * p, D d ): px( p ), pn( p, d )
384 boost::detail::sp_deleter_construct( this, p );
387 #if !defined( BOOST_NO_CXX11_NULLPTR )
389 template<class D> shared_ptr( boost::detail::sp_nullptr_t p, D d ): px( p ), pn( p, d )
395 // As above, but with allocator. A's copy constructor shall not throw.
397 template<class Y, class D, class A> shared_ptr( Y * p, D d, A a ): px( p ), pn( p, d, a )
399 boost::detail::sp_deleter_construct( this, p );
402 #if !defined( BOOST_NO_CXX11_NULLPTR )
404 template<class D, class A> shared_ptr( boost::detail::sp_nullptr_t p, D d, A a ): px( p ), pn( p, d, a )
410 // generated copy constructor, destructor are fine...
412 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
414 // ... except in C++0x, move disables the implicit copy
416 shared_ptr( shared_ptr const & r ) BOOST_SP_NOEXCEPT : px( r.px ), pn( r.pn )
423 explicit shared_ptr( weak_ptr<Y> const & r ): pn( r.pn ) // may throw
425 boost::detail::sp_assert_convertible< Y, T >();
427 // it is now safe to copy r.px, as pn(r.pn) did not throw
432 shared_ptr( weak_ptr<Y> const & r, boost::detail::sp_nothrow_tag )
433 BOOST_SP_NOEXCEPT : px( 0 ), pn( r.pn, boost::detail::sp_nothrow_tag() )
442 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
444 shared_ptr( shared_ptr<Y> const & r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
448 shared_ptr( shared_ptr<Y> const & r )
451 BOOST_SP_NOEXCEPT : px( r.px ), pn( r.pn )
453 boost::detail::sp_assert_convertible< Y, T >();
458 shared_ptr( shared_ptr<Y> const & r, element_type * p ) BOOST_SP_NOEXCEPT : px( p ), pn( r.pn )
462 #ifndef BOOST_NO_AUTO_PTR
465 explicit shared_ptr( std::auto_ptr<Y> & r ): px(r.get()), pn()
467 boost::detail::sp_assert_convertible< Y, T >();
470 pn = boost::detail::shared_count( r );
472 boost::detail::sp_deleter_construct( this, tmp );
475 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
478 shared_ptr( std::auto_ptr<Y> && r ): px(r.get()), pn()
480 boost::detail::sp_assert_convertible< Y, T >();
483 pn = boost::detail::shared_count( r );
485 boost::detail::sp_deleter_construct( this, tmp );
488 #elif !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
491 explicit shared_ptr( Ap r, typename boost::detail::sp_enable_if_auto_ptr<Ap, int>::type = 0 ): px( r.get() ), pn()
493 typedef typename Ap::element_type Y;
495 boost::detail::sp_assert_convertible< Y, T >();
498 pn = boost::detail::shared_count( r );
500 boost::detail::sp_deleter_construct( this, tmp );
503 #endif // BOOST_NO_SFINAE, BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
505 #endif // BOOST_NO_AUTO_PTR
507 #if !defined( BOOST_NO_CXX11_SMART_PTR ) && !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
509 template< class Y, class D >
510 shared_ptr( std::unique_ptr< Y, D > && r ): px( r.get() ), pn()
512 boost::detail::sp_assert_convertible< Y, T >();
514 typename std::unique_ptr< Y, D >::pointer tmp = r.get();
518 pn = boost::detail::shared_count( r );
519 boost::detail::sp_deleter_construct( this, tmp );
525 template< class Y, class D >
526 shared_ptr( boost::movelib::unique_ptr< Y, D > r ): px( r.get() ), pn()
528 boost::detail::sp_assert_convertible< Y, T >();
530 typename boost::movelib::unique_ptr< Y, D >::pointer tmp = r.get();
534 pn = boost::detail::shared_count( r );
535 boost::detail::sp_deleter_construct( this, tmp );
541 shared_ptr & operator=( shared_ptr const & r ) BOOST_SP_NOEXCEPT
543 this_type(r).swap(*this);
547 #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1400)
550 shared_ptr & operator=(shared_ptr<Y> const & r) BOOST_SP_NOEXCEPT
552 this_type(r).swap(*this);
558 #ifndef BOOST_NO_AUTO_PTR
561 shared_ptr & operator=( std::auto_ptr<Y> & r )
563 this_type( r ).swap( *this );
567 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
570 shared_ptr & operator=( std::auto_ptr<Y> && r )
572 this_type( static_cast< std::auto_ptr<Y> && >( r ) ).swap( *this );
576 #elif !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
579 typename boost::detail::sp_enable_if_auto_ptr< Ap, shared_ptr & >::type operator=( Ap r )
581 this_type( r ).swap( *this );
585 #endif // BOOST_NO_SFINAE, BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
587 #endif // BOOST_NO_AUTO_PTR
589 #if !defined( BOOST_NO_CXX11_SMART_PTR ) && !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
591 template<class Y, class D>
592 shared_ptr & operator=( std::unique_ptr<Y, D> && r )
594 this_type( static_cast< std::unique_ptr<Y, D> && >( r ) ).swap(*this);
600 template<class Y, class D>
601 shared_ptr & operator=( boost::movelib::unique_ptr<Y, D> r )
603 // this_type( static_cast< unique_ptr<Y, D> && >( r ) ).swap( *this );
605 boost::detail::sp_assert_convertible< Y, T >();
607 typename boost::movelib::unique_ptr< Y, D >::pointer p = r.get();
614 tmp.pn = boost::detail::shared_count( r );
616 boost::detail::sp_deleter_construct( &tmp, p );
626 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
628 shared_ptr( shared_ptr && r ) BOOST_SP_NOEXCEPT : px( r.px ), pn()
635 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
637 shared_ptr( shared_ptr<Y> && r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
641 shared_ptr( shared_ptr<Y> && r )
644 BOOST_SP_NOEXCEPT : px( r.px ), pn()
646 boost::detail::sp_assert_convertible< Y, T >();
652 shared_ptr & operator=( shared_ptr && r ) BOOST_SP_NOEXCEPT
654 this_type( static_cast< shared_ptr && >( r ) ).swap( *this );
659 shared_ptr & operator=( shared_ptr<Y> && r ) BOOST_SP_NOEXCEPT
661 this_type( static_cast< shared_ptr<Y> && >( r ) ).swap( *this );
667 shared_ptr( shared_ptr<Y> && r, element_type * p ) BOOST_SP_NOEXCEPT : px( p ), pn()
675 #if !defined( BOOST_NO_CXX11_NULLPTR )
677 shared_ptr & operator=( boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT
679 this_type().swap(*this);
685 void reset() BOOST_SP_NOEXCEPT
687 this_type().swap(*this);
690 template<class Y> void reset( Y * p ) // Y must be complete
692 BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors
693 this_type( p ).swap( *this );
696 template<class Y, class D> void reset( Y * p, D d )
698 this_type( p, d ).swap( *this );
701 template<class Y, class D, class A> void reset( Y * p, D d, A a )
703 this_type( p, d, a ).swap( *this );
706 template<class Y> void reset( shared_ptr<Y> const & r, element_type * p ) BOOST_SP_NOEXCEPT
708 this_type( r, p ).swap( *this );
711 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
713 template<class Y> void reset( shared_ptr<Y> && r, element_type * p ) BOOST_SP_NOEXCEPT
715 this_type( static_cast< shared_ptr<Y> && >( r ), p ).swap( *this );
720 typename boost::detail::sp_dereference< T >::type operator* () const BOOST_SP_NOEXCEPT_WITH_ASSERT
722 BOOST_ASSERT( px != 0 );
726 typename boost::detail::sp_member_access< T >::type operator-> () const BOOST_SP_NOEXCEPT_WITH_ASSERT
728 BOOST_ASSERT( px != 0 );
732 typename boost::detail::sp_array_access< T >::type operator[] ( std::ptrdiff_t i ) const BOOST_SP_NOEXCEPT_WITH_ASSERT
734 BOOST_ASSERT( px != 0 );
735 BOOST_ASSERT( i >= 0 && ( i < boost::detail::sp_extent< T >::value || boost::detail::sp_extent< T >::value == 0 ) );
737 return static_cast< typename boost::detail::sp_array_access< T >::type >( px[ i ] );
740 element_type * get() const BOOST_SP_NOEXCEPT
745 // implicit conversion to "bool"
746 #include <boost/smart_ptr/detail/operator_bool.hpp>
748 bool unique() const BOOST_SP_NOEXCEPT
753 long use_count() const BOOST_SP_NOEXCEPT
755 return pn.use_count();
758 void swap( shared_ptr & other ) BOOST_SP_NOEXCEPT
760 std::swap(px, other.px);
764 template<class Y> bool owner_before( shared_ptr<Y> const & rhs ) const BOOST_SP_NOEXCEPT
769 template<class Y> bool owner_before( weak_ptr<Y> const & rhs ) const BOOST_SP_NOEXCEPT
774 template<class Y> bool owner_equals( shared_ptr<Y> const & rhs ) const BOOST_SP_NOEXCEPT
779 template<class Y> bool owner_equals( weak_ptr<Y> const & rhs ) const BOOST_SP_NOEXCEPT
784 std::size_t owner_hash_value() const BOOST_SP_NOEXCEPT
786 return pn.hash_value();
789 void * _internal_get_deleter( boost::detail::sp_typeinfo_ const & ti ) const BOOST_SP_NOEXCEPT
791 return pn.get_deleter( ti );
794 void * _internal_get_local_deleter( boost::detail::sp_typeinfo_ const & ti ) const BOOST_SP_NOEXCEPT
796 return pn.get_local_deleter( ti );
799 void * _internal_get_untyped_deleter() const BOOST_SP_NOEXCEPT
801 return pn.get_untyped_deleter();
804 bool _internal_equiv( shared_ptr const & r ) const BOOST_SP_NOEXCEPT
806 return px == r.px && pn == r.pn;
809 boost::detail::shared_count _internal_count() const BOOST_SP_NOEXCEPT
814 // Tasteless as this may seem, making all members public allows member templates
815 // to work in the absence of member template friends. (Matthew Langston)
817 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
821 template<class Y> friend class shared_ptr;
822 template<class Y> friend class weak_ptr;
827 element_type * px; // contained pointer
828 boost::detail::shared_count pn; // reference counter
832 template<class T, class U> inline bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b) BOOST_SP_NOEXCEPT
834 return a.get() == b.get();
837 template<class T, class U> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<U> const & b) BOOST_SP_NOEXCEPT
839 return a.get() != b.get();
842 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
844 // Resolve the ambiguity between our op!= and the one in rel_ops
846 template<class T> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<T> const & b) BOOST_SP_NOEXCEPT
848 return a.get() != b.get();
853 #if !defined( BOOST_NO_CXX11_NULLPTR )
855 template<class T> inline bool operator==( shared_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT
860 template<class T> inline bool operator==( boost::detail::sp_nullptr_t, shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
865 template<class T> inline bool operator!=( shared_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT
870 template<class T> inline bool operator!=( boost::detail::sp_nullptr_t, shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
877 template<class T, class U> inline bool operator<(shared_ptr<T> const & a, shared_ptr<U> const & b) BOOST_SP_NOEXCEPT
879 return a.owner_before( b );
882 template<class T> inline void swap(shared_ptr<T> & a, shared_ptr<T> & b) BOOST_SP_NOEXCEPT
887 template<class T, class U> shared_ptr<T> static_pointer_cast( shared_ptr<U> const & r ) BOOST_SP_NOEXCEPT
889 (void) static_cast< T* >( static_cast< U* >( 0 ) );
891 typedef typename shared_ptr<T>::element_type E;
893 E * p = static_cast< E* >( r.get() );
894 return shared_ptr<T>( r, p );
897 template<class T, class U> shared_ptr<T> const_pointer_cast( shared_ptr<U> const & r ) BOOST_SP_NOEXCEPT
899 (void) const_cast< T* >( static_cast< U* >( 0 ) );
901 typedef typename shared_ptr<T>::element_type E;
903 E * p = const_cast< E* >( r.get() );
904 return shared_ptr<T>( r, p );
907 template<class T, class U> shared_ptr<T> dynamic_pointer_cast( shared_ptr<U> const & r ) BOOST_SP_NOEXCEPT
909 (void) dynamic_cast< T* >( static_cast< U* >( 0 ) );
911 typedef typename shared_ptr<T>::element_type E;
913 E * p = dynamic_cast< E* >( r.get() );
914 return p? shared_ptr<T>( r, p ): shared_ptr<T>();
917 template<class T, class U> shared_ptr<T> reinterpret_pointer_cast( shared_ptr<U> const & r ) BOOST_SP_NOEXCEPT
919 (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
921 typedef typename shared_ptr<T>::element_type E;
923 E * p = reinterpret_cast< E* >( r.get() );
924 return shared_ptr<T>( r, p );
927 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
929 template<class T, class U> shared_ptr<T> static_pointer_cast( shared_ptr<U> && r ) BOOST_SP_NOEXCEPT
931 (void) static_cast< T* >( static_cast< U* >( 0 ) );
933 typedef typename shared_ptr<T>::element_type E;
935 E * p = static_cast< E* >( r.get() );
936 return shared_ptr<T>( std::move(r), p );
939 template<class T, class U> shared_ptr<T> const_pointer_cast( shared_ptr<U> && r ) BOOST_SP_NOEXCEPT
941 (void) const_cast< T* >( static_cast< U* >( 0 ) );
943 typedef typename shared_ptr<T>::element_type E;
945 E * p = const_cast< E* >( r.get() );
946 return shared_ptr<T>( std::move(r), p );
949 template<class T, class U> shared_ptr<T> dynamic_pointer_cast( shared_ptr<U> && r ) BOOST_SP_NOEXCEPT
951 (void) dynamic_cast< T* >( static_cast< U* >( 0 ) );
953 typedef typename shared_ptr<T>::element_type E;
955 E * p = dynamic_cast< E* >( r.get() );
956 return p? shared_ptr<T>( std::move(r), p ): shared_ptr<T>();
959 template<class T, class U> shared_ptr<T> reinterpret_pointer_cast( shared_ptr<U> && r ) BOOST_SP_NOEXCEPT
961 (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
963 typedef typename shared_ptr<T>::element_type E;
965 E * p = reinterpret_cast< E* >( r.get() );
966 return shared_ptr<T>( std::move(r), p );
969 #endif // !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
971 // get_pointer() enables boost::mem_fn to recognize shared_ptr
973 template<class T> inline typename shared_ptr<T>::element_type * get_pointer(shared_ptr<T> const & p) BOOST_SP_NOEXCEPT
980 #if !defined(BOOST_NO_IOSTREAM)
982 #if defined(BOOST_NO_TEMPLATED_IOSTREAMS) || ( defined(__GNUC__) && (__GNUC__ < 3) )
984 template<class Y> std::ostream & operator<< (std::ostream & os, shared_ptr<Y> const & p)
992 // in STLport's no-iostreams mode no iostream symbols can be used
993 #ifndef _STLP_NO_IOSTREAMS
995 # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300 && __SGI_STL_PORT)
996 // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
997 using std::basic_ostream;
998 template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, shared_ptr<Y> const & p)
1000 template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, shared_ptr<Y> const & p)
1007 #endif // _STLP_NO_IOSTREAMS
1009 #endif // __GNUC__ < 3
1011 #endif // !defined(BOOST_NO_IOSTREAM)
1018 template<class D, class T> D * basic_get_deleter( shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
1020 return static_cast<D *>( p._internal_get_deleter(BOOST_SP_TYPEID_(D)) );
1023 template<class D, class T> D * basic_get_local_deleter( D *, shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT;
1024 template<class D, class T> D const * basic_get_local_deleter( D const *, shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT;
1026 class esft2_deleter_wrapper
1030 shared_ptr<void const volatile> deleter_;
1034 esft2_deleter_wrapper() BOOST_SP_NOEXCEPT
1038 template< class T > void set_deleter( shared_ptr<T> const & deleter ) BOOST_SP_NOEXCEPT
1043 template<typename D> D* get_deleter() const BOOST_SP_NOEXCEPT
1045 return boost::detail::basic_get_deleter<D>( deleter_ );
1048 template< class T> void operator()( T* ) BOOST_SP_NOEXCEPT_WITH_ASSERT
1050 BOOST_ASSERT( deleter_.use_count() <= 1 );
1055 } // namespace detail
1057 template<class D, class T> D * get_deleter( shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
1059 D * d = boost::detail::basic_get_deleter<D>( p );
1063 d = boost::detail::basic_get_local_deleter( d, p );
1068 boost::detail::esft2_deleter_wrapper *del_wrapper = boost::detail::basic_get_deleter<boost::detail::esft2_deleter_wrapper>(p);
1069 // The following get_deleter method call is fully qualified because
1070 // older versions of gcc (2.95, 3.2.3) fail to compile it when written del_wrapper->get_deleter<D>()
1071 if(del_wrapper) d = del_wrapper->::boost::detail::esft2_deleter_wrapper::get_deleter<D>();
1079 #if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
1081 template<class T> inline bool atomic_is_lock_free( shared_ptr<T> const * /*p*/ ) BOOST_SP_NOEXCEPT
1086 template<class T> shared_ptr<T> atomic_load( shared_ptr<T> const * p ) BOOST_SP_NOEXCEPT
1088 boost::detail::spinlock_pool<2>::scoped_lock lock( p );
1092 template<class T, class M> inline shared_ptr<T> atomic_load_explicit( shared_ptr<T> const * p, /*memory_order mo*/ M ) BOOST_SP_NOEXCEPT
1094 return atomic_load( p );
1097 template<class T> void atomic_store( shared_ptr<T> * p, shared_ptr<T> r ) BOOST_SP_NOEXCEPT
1099 boost::detail::spinlock_pool<2>::scoped_lock lock( p );
1103 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
1105 atomic_store( p, r ); // std::move( r )
1108 template<class T> shared_ptr<T> atomic_exchange( shared_ptr<T> * p, shared_ptr<T> r ) BOOST_SP_NOEXCEPT
1110 boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( p );
1116 return r; // return std::move( r )
1119 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
1121 return atomic_exchange( p, r ); // std::move( r )
1124 template<class T> bool atomic_compare_exchange( shared_ptr<T> * p, shared_ptr<T> * v, shared_ptr<T> w ) BOOST_SP_NOEXCEPT
1126 boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( p );
1130 if( p->_internal_equiv( *v ) )
1140 shared_ptr<T> tmp( *p );
1149 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
1151 return atomic_compare_exchange( p, v, w ); // std::move( w )
1154 #endif // !defined(BOOST_SP_NO_ATOMIC_ACCESS)
1158 template< class T > struct hash;
1160 template< class T > std::size_t hash_value( boost::shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
1162 return boost::hash< typename boost::shared_ptr<T>::element_type* >()( p.get() );
1165 } // namespace boost
1169 #if !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL)
1174 template<class T> struct hash< ::boost::shared_ptr<T> >
1176 std::size_t operator()( ::boost::shared_ptr<T> const & p ) const BOOST_SP_NOEXCEPT
1178 return std::hash< typename ::boost::shared_ptr<T>::element_type* >()( p.get() );
1184 #endif // #if !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL)
1186 #include <boost/smart_ptr/detail/local_sp_deleter.hpp>
1194 template<class D, class T> D * basic_get_local_deleter( D *, shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
1196 return static_cast<D *>( p._internal_get_local_deleter( BOOST_SP_TYPEID_(local_sp_deleter<D>) ) );
1199 template<class D, class T> D const * basic_get_local_deleter( D const *, shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
1201 return static_cast<D *>( p._internal_get_local_deleter( BOOST_SP_TYPEID_(local_sp_deleter<D>) ) );
1204 } // namespace detail
1206 #if defined(__cpp_deduction_guides)
1208 template<class T> shared_ptr( weak_ptr<T> ) -> shared_ptr<T>;
1209 template<class T, class D> shared_ptr( std::unique_ptr<T, D> ) -> shared_ptr<T>;
1213 } // namespace boost
1215 #if defined( BOOST_SP_DISABLE_DEPRECATED )
1216 #pragma GCC diagnostic pop
1219 #endif // #ifndef BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED