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>
34 #if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
35 #include <boost/smart_ptr/detail/spinlock_pool.hpp>
38 #include <algorithm> // for std::swap
39 #include <functional> // for std::less
40 #include <typeinfo> // for std::bad_cast
41 #include <cstddef> // for std::size_t
43 #if !defined(BOOST_NO_IOSTREAM)
44 #if !defined(BOOST_NO_IOSFWD)
45 #include <iosfwd> // for std::basic_ostream
51 #if defined( BOOST_SP_DISABLE_DEPRECATED )
52 #pragma GCC diagnostic push
53 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
59 template<class T> class shared_ptr;
60 template<class T> class weak_ptr;
61 template<class T> class enable_shared_from_this;
62 class enable_shared_from_raw;
67 template< class T, class D > class unique_ptr;
69 } // namespace movelib
74 // sp_element, element_type
76 template< class T > struct sp_element
81 #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
83 template< class T > struct sp_element< T[] >
88 #if !defined( __BORLANDC__ ) || !BOOST_WORKAROUND( __BORLANDC__, < 0x600 )
90 template< class T, std::size_t N > struct sp_element< T[N] >
97 #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
99 // sp_dereference, return type of operator*
101 template< class T > struct sp_dereference
106 template<> struct sp_dereference< void >
111 #if !defined(BOOST_NO_CV_VOID_SPECIALIZATIONS)
113 template<> struct sp_dereference< void const >
118 template<> struct sp_dereference< void volatile >
123 template<> struct sp_dereference< void const volatile >
128 #endif // !defined(BOOST_NO_CV_VOID_SPECIALIZATIONS)
130 #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
132 template< class T > struct sp_dereference< T[] >
137 #if !defined( __BORLANDC__ ) || !BOOST_WORKAROUND( __BORLANDC__, < 0x600 )
139 template< class T, std::size_t N > struct sp_dereference< T[N] >
146 #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
148 // sp_member_access, return type of operator->
150 template< class T > struct sp_member_access
155 #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
157 template< class T > struct sp_member_access< T[] >
162 #if !defined( __BORLANDC__ ) || !BOOST_WORKAROUND( __BORLANDC__, < 0x600 )
164 template< class T, std::size_t N > struct sp_member_access< T[N] >
171 #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
173 // sp_array_access, return type of operator[]
175 template< class T > struct sp_array_access
180 #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
182 template< class T > struct sp_array_access< T[] >
187 #if !defined( __BORLANDC__ ) || !BOOST_WORKAROUND( __BORLANDC__, < 0x600 )
189 template< class T, std::size_t N > struct sp_array_access< T[N] >
196 #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
198 // sp_extent, for operator[] index check
200 template< class T > struct sp_extent
202 enum _vt { value = 0 };
205 #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
207 template< class T, std::size_t N > struct sp_extent< T[N] >
209 enum _vt { value = N };
212 #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
214 // enable_shared_from_this support
216 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 )
220 pe->_internal_accept_owner( ppx, const_cast< Y* >( py ) );
224 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 );
228 // Avoid C4793, ... causes native code generation
230 struct sp_any_pointer
232 template<class T> sp_any_pointer( T* ) {}
235 inline void sp_enable_shared_from_this( sp_any_pointer, sp_any_pointer, sp_any_pointer )
241 inline void sp_enable_shared_from_this( ... )
247 #if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) && !defined( BOOST_NO_AUTO_PTR )
249 // rvalue auto_ptr support based on a technique by Dave Abrahams
251 template< class T, class R > struct sp_enable_if_auto_ptr
255 template< class T, class R > struct sp_enable_if_auto_ptr< std::auto_ptr< T >, R >
262 // sp_assert_convertible
264 template< class Y, class T > inline void sp_assert_convertible()
266 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
268 // static_assert( sp_convertible< Y, T >::value );
269 typedef char tmp[ sp_convertible< Y, T >::value? 1: -1 ];
274 T* p = static_cast< Y* >( 0 );
280 // pointer constructor helper
282 template< class T, class Y > inline void sp_pointer_construct( boost::shared_ptr< T > * ppx, Y * p, boost::detail::shared_count & pn )
284 boost::detail::shared_count( p ).swap( pn );
285 boost::detail::sp_enable_shared_from_this( ppx, p, p );
288 #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
290 template< class T, class Y > inline void sp_pointer_construct( boost::shared_ptr< T[] > * /*ppx*/, Y * p, boost::detail::shared_count & pn )
292 sp_assert_convertible< Y[], T[] >();
293 boost::detail::shared_count( p, boost::checked_array_deleter< T >() ).swap( pn );
296 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 )
298 sp_assert_convertible< Y[N], T[N] >();
299 boost::detail::shared_count( p, boost::checked_array_deleter< T >() ).swap( pn );
302 #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
304 // deleter constructor helper
306 template< class T, class Y > inline void sp_deleter_construct( boost::shared_ptr< T > * ppx, Y * p )
308 boost::detail::sp_enable_shared_from_this( ppx, p, p );
311 #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
313 template< class T, class Y > inline void sp_deleter_construct( boost::shared_ptr< T[] > * /*ppx*/, Y * /*p*/ )
315 sp_assert_convertible< Y[], T[] >();
318 template< class T, std::size_t N, class Y > inline void sp_deleter_construct( boost::shared_ptr< T[N] > * /*ppx*/, Y * /*p*/ )
320 sp_assert_convertible< Y[N], T[N] >();
323 #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
325 } // namespace detail
331 // An enhanced relative of scoped_ptr with reference counted copy semantics.
332 // The object pointed to is deleted when the last shared_ptr pointing to it
333 // is destroyed or reset.
336 template<class T> class shared_ptr
340 // Borland 5.5.1 specific workaround
341 typedef shared_ptr<T> this_type;
345 typedef typename boost::detail::sp_element< T >::type element_type;
347 shared_ptr() BOOST_NOEXCEPT : px( 0 ), pn() // never throws in 1.30+
351 #if !defined( BOOST_NO_CXX11_NULLPTR )
353 shared_ptr( boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT : px( 0 ), pn() // never throws
360 explicit shared_ptr( Y * p ): px( p ), pn() // Y must be complete
362 boost::detail::sp_pointer_construct( this, p, pn );
366 // Requirements: D's copy constructor must not throw
368 // shared_ptr will release p by calling d(p)
371 template<class Y, class D> shared_ptr( Y * p, D d ): px( p ), pn( p, d )
373 boost::detail::sp_deleter_construct( this, p );
376 #if !defined( BOOST_NO_CXX11_NULLPTR )
378 template<class D> shared_ptr( boost::detail::sp_nullptr_t p, D d ): px( p ), pn( p, d )
384 // As above, but with allocator. A's copy constructor shall not throw.
386 template<class Y, class D, class A> shared_ptr( Y * p, D d, A a ): px( p ), pn( p, d, a )
388 boost::detail::sp_deleter_construct( this, p );
391 #if !defined( BOOST_NO_CXX11_NULLPTR )
393 template<class D, class A> shared_ptr( boost::detail::sp_nullptr_t p, D d, A a ): px( p ), pn( p, d, a )
399 // generated copy constructor, destructor are fine...
401 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
403 // ... except in C++0x, move disables the implicit copy
405 shared_ptr( shared_ptr const & r ) BOOST_NOEXCEPT : px( r.px ), pn( r.pn )
412 explicit shared_ptr( weak_ptr<Y> const & r ): pn( r.pn ) // may throw
414 boost::detail::sp_assert_convertible< Y, T >();
416 // it is now safe to copy r.px, as pn(r.pn) did not throw
421 shared_ptr( weak_ptr<Y> const & r, boost::detail::sp_nothrow_tag )
422 BOOST_NOEXCEPT : px( 0 ), pn( r.pn, boost::detail::sp_nothrow_tag() )
431 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
433 shared_ptr( shared_ptr<Y> const & r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
437 shared_ptr( shared_ptr<Y> const & r )
440 BOOST_NOEXCEPT : px( r.px ), pn( r.pn )
442 boost::detail::sp_assert_convertible< Y, T >();
447 shared_ptr( shared_ptr<Y> const & r, element_type * p ) BOOST_NOEXCEPT : px( p ), pn( r.pn )
451 #ifndef BOOST_NO_AUTO_PTR
454 explicit shared_ptr( std::auto_ptr<Y> & r ): px(r.get()), pn()
456 boost::detail::sp_assert_convertible< Y, T >();
459 pn = boost::detail::shared_count( r );
461 boost::detail::sp_deleter_construct( this, tmp );
464 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
467 shared_ptr( std::auto_ptr<Y> && r ): px(r.get()), pn()
469 boost::detail::sp_assert_convertible< Y, T >();
472 pn = boost::detail::shared_count( r );
474 boost::detail::sp_deleter_construct( this, tmp );
477 #elif !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
480 explicit shared_ptr( Ap r, typename boost::detail::sp_enable_if_auto_ptr<Ap, int>::type = 0 ): px( r.get() ), pn()
482 typedef typename Ap::element_type Y;
484 boost::detail::sp_assert_convertible< Y, T >();
487 pn = boost::detail::shared_count( r );
489 boost::detail::sp_deleter_construct( this, tmp );
492 #endif // BOOST_NO_SFINAE, BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
494 #endif // BOOST_NO_AUTO_PTR
496 #if !defined( BOOST_NO_CXX11_SMART_PTR ) && !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
498 template< class Y, class D >
499 shared_ptr( std::unique_ptr< Y, D > && r ): px( r.get() ), pn()
501 boost::detail::sp_assert_convertible< Y, T >();
503 typename std::unique_ptr< Y, D >::pointer tmp = r.get();
504 pn = boost::detail::shared_count( r );
506 boost::detail::sp_deleter_construct( this, tmp );
511 template< class Y, class D >
512 shared_ptr( boost::movelib::unique_ptr< Y, D > r ): px( r.get() ), pn()
514 boost::detail::sp_assert_convertible< Y, T >();
516 typename boost::movelib::unique_ptr< Y, D >::pointer tmp = r.get();
517 pn = boost::detail::shared_count( r );
519 boost::detail::sp_deleter_construct( this, tmp );
524 shared_ptr & operator=( shared_ptr const & r ) BOOST_NOEXCEPT
526 this_type(r).swap(*this);
530 #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1400)
533 shared_ptr & operator=(shared_ptr<Y> const & r) BOOST_NOEXCEPT
535 this_type(r).swap(*this);
541 #ifndef BOOST_NO_AUTO_PTR
544 shared_ptr & operator=( std::auto_ptr<Y> & r )
546 this_type( r ).swap( *this );
550 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
553 shared_ptr & operator=( std::auto_ptr<Y> && r )
555 this_type( static_cast< std::auto_ptr<Y> && >( r ) ).swap( *this );
559 #elif !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
562 typename boost::detail::sp_enable_if_auto_ptr< Ap, shared_ptr & >::type operator=( Ap r )
564 this_type( r ).swap( *this );
568 #endif // BOOST_NO_SFINAE, BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
570 #endif // BOOST_NO_AUTO_PTR
572 #if !defined( BOOST_NO_CXX11_SMART_PTR ) && !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
574 template<class Y, class D>
575 shared_ptr & operator=( std::unique_ptr<Y, D> && r )
577 this_type( static_cast< std::unique_ptr<Y, D> && >( r ) ).swap(*this);
583 template<class Y, class D>
584 shared_ptr & operator=( boost::movelib::unique_ptr<Y, D> r )
586 // this_type( static_cast< unique_ptr<Y, D> && >( r ) ).swap( *this );
588 boost::detail::sp_assert_convertible< Y, T >();
590 typename boost::movelib::unique_ptr< Y, D >::pointer p = r.get();
595 tmp.pn = boost::detail::shared_count( r );
597 boost::detail::sp_deleter_construct( &tmp, p );
606 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
608 shared_ptr( shared_ptr && r ) BOOST_NOEXCEPT : px( r.px ), pn()
615 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
617 shared_ptr( shared_ptr<Y> && r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
621 shared_ptr( shared_ptr<Y> && r )
624 BOOST_NOEXCEPT : px( r.px ), pn()
626 boost::detail::sp_assert_convertible< Y, T >();
632 shared_ptr & operator=( shared_ptr && r ) BOOST_NOEXCEPT
634 this_type( static_cast< shared_ptr && >( r ) ).swap( *this );
639 shared_ptr & operator=( shared_ptr<Y> && r ) BOOST_NOEXCEPT
641 this_type( static_cast< shared_ptr<Y> && >( r ) ).swap( *this );
647 shared_ptr( shared_ptr<Y> && r, element_type * p ) BOOST_NOEXCEPT : px( p ), pn()
655 #if !defined( BOOST_NO_CXX11_NULLPTR )
657 shared_ptr & operator=( boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT // never throws
659 this_type().swap(*this);
665 void reset() BOOST_NOEXCEPT // never throws in 1.30+
667 this_type().swap(*this);
670 template<class Y> void reset( Y * p ) // Y must be complete
672 BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors
673 this_type( p ).swap( *this );
676 template<class Y, class D> void reset( Y * p, D d )
678 this_type( p, d ).swap( *this );
681 template<class Y, class D, class A> void reset( Y * p, D d, A a )
683 this_type( p, d, a ).swap( *this );
686 template<class Y> void reset( shared_ptr<Y> const & r, element_type * p )
688 this_type( r, p ).swap( *this );
691 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
693 template<class Y> void reset( shared_ptr<Y> && r, element_type * p )
695 this_type( static_cast< shared_ptr<Y> && >( r ), p ).swap( *this );
700 // never throws (but has a BOOST_ASSERT in it, so not marked with BOOST_NOEXCEPT)
701 typename boost::detail::sp_dereference< T >::type operator* () const
703 BOOST_ASSERT( px != 0 );
707 // never throws (but has a BOOST_ASSERT in it, so not marked with BOOST_NOEXCEPT)
708 typename boost::detail::sp_member_access< T >::type operator-> () const
710 BOOST_ASSERT( px != 0 );
714 // never throws (but has a BOOST_ASSERT in it, so not marked with BOOST_NOEXCEPT)
715 typename boost::detail::sp_array_access< T >::type operator[] ( std::ptrdiff_t i ) const
717 BOOST_ASSERT( px != 0 );
718 BOOST_ASSERT( i >= 0 && ( i < boost::detail::sp_extent< T >::value || boost::detail::sp_extent< T >::value == 0 ) );
720 return static_cast< typename boost::detail::sp_array_access< T >::type >( px[ i ] );
723 element_type * get() const BOOST_NOEXCEPT
728 // implicit conversion to "bool"
729 #include <boost/smart_ptr/detail/operator_bool.hpp>
731 bool unique() const BOOST_NOEXCEPT
736 long use_count() const BOOST_NOEXCEPT
738 return pn.use_count();
741 void swap( shared_ptr & other ) BOOST_NOEXCEPT
743 std::swap(px, other.px);
747 template<class Y> bool owner_before( shared_ptr<Y> const & rhs ) const BOOST_NOEXCEPT
752 template<class Y> bool owner_before( weak_ptr<Y> const & rhs ) const BOOST_NOEXCEPT
757 void * _internal_get_deleter( boost::detail::sp_typeinfo const & ti ) const BOOST_NOEXCEPT
759 return pn.get_deleter( ti );
762 void * _internal_get_untyped_deleter() const BOOST_NOEXCEPT
764 return pn.get_untyped_deleter();
767 bool _internal_equiv( shared_ptr const & r ) const BOOST_NOEXCEPT
769 return px == r.px && pn == r.pn;
772 // Tasteless as this may seem, making all members public allows member templates
773 // to work in the absence of member template friends. (Matthew Langston)
775 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
779 template<class Y> friend class shared_ptr;
780 template<class Y> friend class weak_ptr;
785 element_type * px; // contained pointer
786 boost::detail::shared_count pn; // reference counter
790 template<class T, class U> inline bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b) BOOST_NOEXCEPT
792 return a.get() == b.get();
795 template<class T, class U> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<U> const & b) BOOST_NOEXCEPT
797 return a.get() != b.get();
800 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
802 // Resolve the ambiguity between our op!= and the one in rel_ops
804 template<class T> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<T> const & b) BOOST_NOEXCEPT
806 return a.get() != b.get();
811 #if !defined( BOOST_NO_CXX11_NULLPTR )
813 template<class T> inline bool operator==( shared_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT
818 template<class T> inline bool operator==( boost::detail::sp_nullptr_t, shared_ptr<T> const & p ) BOOST_NOEXCEPT
823 template<class T> inline bool operator!=( shared_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT
828 template<class T> inline bool operator!=( boost::detail::sp_nullptr_t, shared_ptr<T> const & p ) BOOST_NOEXCEPT
835 template<class T, class U> inline bool operator<(shared_ptr<T> const & a, shared_ptr<U> const & b) BOOST_NOEXCEPT
837 return a.owner_before( b );
840 template<class T> inline void swap(shared_ptr<T> & a, shared_ptr<T> & b) BOOST_NOEXCEPT
845 template<class T, class U> shared_ptr<T> static_pointer_cast( shared_ptr<U> const & r ) BOOST_NOEXCEPT
847 (void) static_cast< T* >( static_cast< U* >( 0 ) );
849 typedef typename shared_ptr<T>::element_type E;
851 E * p = static_cast< E* >( r.get() );
852 return shared_ptr<T>( r, p );
855 template<class T, class U> shared_ptr<T> const_pointer_cast( shared_ptr<U> const & r ) BOOST_NOEXCEPT
857 (void) const_cast< T* >( static_cast< U* >( 0 ) );
859 typedef typename shared_ptr<T>::element_type E;
861 E * p = const_cast< E* >( r.get() );
862 return shared_ptr<T>( r, p );
865 template<class T, class U> shared_ptr<T> dynamic_pointer_cast( shared_ptr<U> const & r ) BOOST_NOEXCEPT
867 (void) dynamic_cast< T* >( static_cast< U* >( 0 ) );
869 typedef typename shared_ptr<T>::element_type E;
871 E * p = dynamic_cast< E* >( r.get() );
872 return p? shared_ptr<T>( r, p ): shared_ptr<T>();
875 template<class T, class U> shared_ptr<T> reinterpret_pointer_cast( shared_ptr<U> const & r ) BOOST_NOEXCEPT
877 (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
879 typedef typename shared_ptr<T>::element_type E;
881 E * p = reinterpret_cast< E* >( r.get() );
882 return shared_ptr<T>( r, p );
885 // get_pointer() enables boost::mem_fn to recognize shared_ptr
887 template<class T> inline typename shared_ptr<T>::element_type * get_pointer(shared_ptr<T> const & p) BOOST_NOEXCEPT
894 #if !defined(BOOST_NO_IOSTREAM)
896 #if defined(BOOST_NO_TEMPLATED_IOSTREAMS) || ( defined(__GNUC__) && (__GNUC__ < 3) )
898 template<class Y> std::ostream & operator<< (std::ostream & os, shared_ptr<Y> const & p)
906 // in STLport's no-iostreams mode no iostream symbols can be used
907 #ifndef _STLP_NO_IOSTREAMS
909 # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300 && __SGI_STL_PORT)
910 // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
911 using std::basic_ostream;
912 template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, shared_ptr<Y> const & p)
914 template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, shared_ptr<Y> const & p)
921 #endif // _STLP_NO_IOSTREAMS
923 #endif // __GNUC__ < 3
925 #endif // !defined(BOOST_NO_IOSTREAM)
932 #if ( defined(__GNUC__) && BOOST_WORKAROUND(__GNUC__, < 3) ) || \
933 ( defined(__EDG_VERSION__) && BOOST_WORKAROUND(__EDG_VERSION__, <= 238) ) || \
934 ( defined(__HP_aCC) && BOOST_WORKAROUND(__HP_aCC, <= 33500) )
936 // g++ 2.9x doesn't allow static_cast<X const *>(void *)
937 // apparently EDG 2.38 and HP aCC A.03.35 also don't accept it
939 template<class D, class T> D * basic_get_deleter(shared_ptr<T> const & p)
941 void const * q = p._internal_get_deleter(BOOST_SP_TYPEID(D));
942 return const_cast<D *>(static_cast<D const *>(q));
947 template<class D, class T> D * basic_get_deleter( shared_ptr<T> const & p ) BOOST_NOEXCEPT
949 return static_cast<D *>( p._internal_get_deleter(BOOST_SP_TYPEID(D)) );
954 class esft2_deleter_wrapper
958 shared_ptr<void const volatile> deleter_;
962 esft2_deleter_wrapper()
966 template< class T > void set_deleter( shared_ptr<T> const & deleter )
971 template<typename D> D* get_deleter() const BOOST_NOEXCEPT
973 return boost::detail::basic_get_deleter<D>( deleter_ );
976 template< class T> void operator()( T* )
978 BOOST_ASSERT( deleter_.use_count() <= 1 );
983 } // namespace detail
985 template<class D, class T> D * get_deleter( shared_ptr<T> const & p ) BOOST_NOEXCEPT
987 D *del = boost::detail::basic_get_deleter<D>(p);
991 boost::detail::esft2_deleter_wrapper *del_wrapper = boost::detail::basic_get_deleter<boost::detail::esft2_deleter_wrapper>(p);
992 // The following get_deleter method call is fully qualified because
993 // older versions of gcc (2.95, 3.2.3) fail to compile it when written del_wrapper->get_deleter<D>()
994 if(del_wrapper) del = del_wrapper->::boost::detail::esft2_deleter_wrapper::get_deleter<D>();
1002 #if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
1004 template<class T> inline bool atomic_is_lock_free( shared_ptr<T> const * /*p*/ ) BOOST_NOEXCEPT
1009 template<class T> shared_ptr<T> atomic_load( shared_ptr<T> const * p )
1011 boost::detail::spinlock_pool<2>::scoped_lock lock( p );
1015 template<class T> inline shared_ptr<T> atomic_load_explicit( shared_ptr<T> const * p, /*memory_order mo*/ int )
1017 return atomic_load( p );
1020 template<class T> void atomic_store( shared_ptr<T> * p, shared_ptr<T> r )
1022 boost::detail::spinlock_pool<2>::scoped_lock lock( p );
1026 template<class T> inline void atomic_store_explicit( shared_ptr<T> * p, shared_ptr<T> r, /*memory_order mo*/ int )
1028 atomic_store( p, r ); // std::move( r )
1031 template<class T> shared_ptr<T> atomic_exchange( shared_ptr<T> * p, shared_ptr<T> r )
1033 boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( p );
1039 return r; // return std::move( r )
1042 template<class T> shared_ptr<T> atomic_exchange_explicit( shared_ptr<T> * p, shared_ptr<T> r, /*memory_order mo*/ int )
1044 return atomic_exchange( p, r ); // std::move( r )
1047 template<class T> bool atomic_compare_exchange( shared_ptr<T> * p, shared_ptr<T> * v, shared_ptr<T> w )
1049 boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( p );
1053 if( p->_internal_equiv( *v ) )
1063 shared_ptr<T> tmp( *p );
1072 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 )
1074 return atomic_compare_exchange( p, v, w ); // std::move( w )
1077 #endif // !defined(BOOST_SP_NO_ATOMIC_ACCESS)
1081 template< class T > struct hash;
1083 template< class T > std::size_t hash_value( boost::shared_ptr<T> const & p ) BOOST_NOEXCEPT
1085 return boost::hash< typename boost::shared_ptr<T>::element_type* >()( p.get() );
1088 } // namespace boost
1090 #if defined( BOOST_SP_DISABLE_DEPRECATED )
1091 #pragma GCC diagnostic pop
1094 #endif // #ifndef BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED