Chris@16: #ifndef BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED Chris@16: #define BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED Chris@16: Chris@16: // Chris@16: // shared_ptr.hpp Chris@16: // Chris@16: // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999. Chris@16: // Copyright (c) 2001-2008 Peter Dimov Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // See http://www.boost.org/libs/smart_ptr/shared_ptr.htm for documentation. Chris@16: // Chris@16: Chris@16: #include // for broken compiler workarounds Chris@16: Chris@16: // In order to avoid circular dependencies with Boost.TR1 Chris@16: // we make sure that our include of doesn't try to Chris@16: // pull in the TR1 headers: that's why we use this header Chris@16: // rather than including directly: Chris@16: #include // std::auto_ptr Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #if !defined(BOOST_SP_NO_ATOMIC_ACCESS) Chris@16: #include Chris@16: #endif Chris@16: Chris@16: #include // for std::swap Chris@16: #include // for std::less Chris@16: #include // for std::bad_cast Chris@16: #include // for std::size_t Chris@16: Chris@16: #if !defined(BOOST_NO_IOSTREAM) Chris@16: #if !defined(BOOST_NO_IOSFWD) Chris@16: #include // for std::basic_ostream Chris@16: #else Chris@16: #include Chris@16: #endif Chris@16: #endif Chris@16: Chris@16: namespace boost Chris@16: { Chris@16: Chris@16: template class shared_ptr; Chris@16: template class weak_ptr; Chris@16: template class enable_shared_from_this; Chris@16: class enable_shared_from_raw; Chris@16: Chris@16: namespace detail Chris@16: { Chris@16: Chris@16: // sp_element, element_type Chris@16: Chris@16: template< class T > struct sp_element Chris@16: { Chris@16: typedef T type; Chris@16: }; Chris@16: Chris@16: #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) Chris@16: Chris@16: template< class T > struct sp_element< T[] > Chris@16: { Chris@16: typedef T type; Chris@16: }; Chris@16: Chris@16: #if !defined( __BORLANDC__ ) || !BOOST_WORKAROUND( __BORLANDC__, < 0x600 ) Chris@16: Chris@16: template< class T, std::size_t N > struct sp_element< T[N] > Chris@16: { Chris@16: typedef T type; Chris@16: }; Chris@16: Chris@16: #endif Chris@16: Chris@16: #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) Chris@16: Chris@16: // sp_dereference, return type of operator* Chris@16: Chris@16: template< class T > struct sp_dereference Chris@16: { Chris@16: typedef T & type; Chris@16: }; Chris@16: Chris@16: template<> struct sp_dereference< void > Chris@16: { Chris@16: typedef void type; Chris@16: }; Chris@16: Chris@16: #if !defined(BOOST_NO_CV_VOID_SPECIALIZATIONS) Chris@16: Chris@16: template<> struct sp_dereference< void const > Chris@16: { Chris@16: typedef void type; Chris@16: }; Chris@16: Chris@16: template<> struct sp_dereference< void volatile > Chris@16: { Chris@16: typedef void type; Chris@16: }; Chris@16: Chris@16: template<> struct sp_dereference< void const volatile > Chris@16: { Chris@16: typedef void type; Chris@16: }; Chris@16: Chris@16: #endif // !defined(BOOST_NO_CV_VOID_SPECIALIZATIONS) Chris@16: Chris@16: #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) Chris@16: Chris@16: template< class T > struct sp_dereference< T[] > Chris@16: { Chris@16: typedef void type; Chris@16: }; Chris@16: Chris@16: #if !defined( __BORLANDC__ ) || !BOOST_WORKAROUND( __BORLANDC__, < 0x600 ) Chris@16: Chris@16: template< class T, std::size_t N > struct sp_dereference< T[N] > Chris@16: { Chris@16: typedef void type; Chris@16: }; Chris@16: Chris@16: #endif Chris@16: Chris@16: #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) Chris@16: Chris@16: // sp_member_access, return type of operator-> Chris@16: Chris@16: template< class T > struct sp_member_access Chris@16: { Chris@16: typedef T * type; Chris@16: }; Chris@16: Chris@16: #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) Chris@16: Chris@16: template< class T > struct sp_member_access< T[] > Chris@16: { Chris@16: typedef void type; Chris@16: }; Chris@16: Chris@16: #if !defined( __BORLANDC__ ) || !BOOST_WORKAROUND( __BORLANDC__, < 0x600 ) Chris@16: Chris@16: template< class T, std::size_t N > struct sp_member_access< T[N] > Chris@16: { Chris@16: typedef void type; Chris@16: }; Chris@16: Chris@16: #endif Chris@16: Chris@16: #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) Chris@16: Chris@16: // sp_array_access, return type of operator[] Chris@16: Chris@16: template< class T > struct sp_array_access Chris@16: { Chris@16: typedef void type; Chris@16: }; Chris@16: Chris@16: #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) Chris@16: Chris@16: template< class T > struct sp_array_access< T[] > Chris@16: { Chris@16: typedef T & type; Chris@16: }; Chris@16: Chris@16: #if !defined( __BORLANDC__ ) || !BOOST_WORKAROUND( __BORLANDC__, < 0x600 ) Chris@16: Chris@16: template< class T, std::size_t N > struct sp_array_access< T[N] > Chris@16: { Chris@16: typedef T & type; Chris@16: }; Chris@16: Chris@16: #endif Chris@16: Chris@16: #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) Chris@16: Chris@16: // sp_extent, for operator[] index check Chris@16: Chris@16: template< class T > struct sp_extent Chris@16: { Chris@16: enum _vt { value = 0 }; Chris@16: }; Chris@16: Chris@16: #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) Chris@16: Chris@16: template< class T, std::size_t N > struct sp_extent< T[N] > Chris@16: { Chris@16: enum _vt { value = N }; Chris@16: }; Chris@16: Chris@16: #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) Chris@16: Chris@16: // enable_shared_from_this support Chris@16: Chris@16: template< class X, class Y, class T > inline void sp_enable_shared_from_this( boost::shared_ptr const * ppx, Y const * py, boost::enable_shared_from_this< T > const * pe ) Chris@16: { Chris@16: if( pe != 0 ) Chris@16: { Chris@16: pe->_internal_accept_owner( ppx, const_cast< Y* >( py ) ); Chris@16: } Chris@16: } Chris@16: Chris@16: template< class X, class Y > inline void sp_enable_shared_from_this( boost::shared_ptr * ppx, Y const * py, boost::enable_shared_from_raw const * pe ); Chris@16: Chris@16: #ifdef _MANAGED Chris@16: Chris@16: // Avoid C4793, ... causes native code generation Chris@16: Chris@16: struct sp_any_pointer Chris@16: { Chris@16: template sp_any_pointer( T* ) {} Chris@16: }; Chris@16: Chris@16: inline void sp_enable_shared_from_this( sp_any_pointer, sp_any_pointer, sp_any_pointer ) Chris@16: { Chris@16: } Chris@16: Chris@16: #else // _MANAGED Chris@16: Chris@16: inline void sp_enable_shared_from_this( ... ) Chris@16: { Chris@16: } Chris@16: Chris@16: #endif // _MANAGED Chris@16: Chris@16: #if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) && !defined( BOOST_NO_AUTO_PTR ) Chris@16: Chris@16: // rvalue auto_ptr support based on a technique by Dave Abrahams Chris@16: Chris@16: template< class T, class R > struct sp_enable_if_auto_ptr Chris@16: { Chris@16: }; Chris@16: Chris@16: template< class T, class R > struct sp_enable_if_auto_ptr< std::auto_ptr< T >, R > Chris@16: { Chris@16: typedef R type; Chris@16: }; Chris@16: Chris@16: #endif Chris@16: Chris@16: // sp_assert_convertible Chris@16: Chris@16: template< class Y, class T > inline void sp_assert_convertible() Chris@16: { Chris@16: #if !defined( BOOST_SP_NO_SP_CONVERTIBLE ) Chris@16: Chris@16: // static_assert( sp_convertible< Y, T >::value ); Chris@16: typedef char tmp[ sp_convertible< Y, T >::value? 1: -1 ]; Chris@16: (void)sizeof( tmp ); Chris@16: Chris@16: #else Chris@16: Chris@16: T* p = static_cast< Y* >( 0 ); Chris@16: (void)p; Chris@16: Chris@16: #endif Chris@16: } Chris@16: Chris@16: // pointer constructor helper Chris@16: Chris@16: template< class T, class Y > inline void sp_pointer_construct( boost::shared_ptr< T > * ppx, Y * p, boost::detail::shared_count & pn ) Chris@16: { Chris@16: boost::detail::shared_count( p ).swap( pn ); Chris@16: boost::detail::sp_enable_shared_from_this( ppx, p, p ); Chris@16: } Chris@16: Chris@16: #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) Chris@16: Chris@16: template< class T, class Y > inline void sp_pointer_construct( boost::shared_ptr< T[] > * /*ppx*/, Y * p, boost::detail::shared_count & pn ) Chris@16: { Chris@16: sp_assert_convertible< Y[], T[] >(); Chris@16: boost::detail::shared_count( p, boost::checked_array_deleter< T >() ).swap( pn ); Chris@16: } Chris@16: Chris@16: 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 ) Chris@16: { Chris@16: sp_assert_convertible< Y[N], T[N] >(); Chris@16: boost::detail::shared_count( p, boost::checked_array_deleter< T >() ).swap( pn ); Chris@16: } Chris@16: Chris@16: #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) Chris@16: Chris@16: // deleter constructor helper Chris@16: Chris@16: template< class T, class Y > inline void sp_deleter_construct( boost::shared_ptr< T > * ppx, Y * p ) Chris@16: { Chris@16: boost::detail::sp_enable_shared_from_this( ppx, p, p ); Chris@16: } Chris@16: Chris@16: #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) Chris@16: Chris@16: template< class T, class Y > inline void sp_deleter_construct( boost::shared_ptr< T[] > * /*ppx*/, Y * /*p*/ ) Chris@16: { Chris@16: sp_assert_convertible< Y[], T[] >(); Chris@16: } Chris@16: Chris@16: template< class T, std::size_t N, class Y > inline void sp_deleter_construct( boost::shared_ptr< T[N] > * /*ppx*/, Y * /*p*/ ) Chris@16: { Chris@16: sp_assert_convertible< Y[N], T[N] >(); Chris@16: } Chris@16: Chris@16: #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) Chris@16: Chris@16: } // namespace detail Chris@16: Chris@16: Chris@16: // Chris@16: // shared_ptr Chris@16: // Chris@16: // An enhanced relative of scoped_ptr with reference counted copy semantics. Chris@16: // The object pointed to is deleted when the last shared_ptr pointing to it Chris@16: // is destroyed or reset. Chris@16: // Chris@16: Chris@16: template class shared_ptr Chris@16: { Chris@16: private: Chris@16: Chris@16: // Borland 5.5.1 specific workaround Chris@16: typedef shared_ptr this_type; Chris@16: Chris@16: public: Chris@16: Chris@16: typedef typename boost::detail::sp_element< T >::type element_type; Chris@16: Chris@16: shared_ptr() BOOST_NOEXCEPT : px( 0 ), pn() // never throws in 1.30+ Chris@16: { Chris@16: } Chris@16: Chris@16: #if !defined( BOOST_NO_CXX11_NULLPTR ) Chris@16: Chris@16: shared_ptr( boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT : px( 0 ), pn() // never throws Chris@16: { Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: template Chris@16: explicit shared_ptr( Y * p ): px( p ), pn() // Y must be complete Chris@16: { Chris@16: boost::detail::sp_pointer_construct( this, p, pn ); Chris@16: } Chris@16: Chris@16: // Chris@16: // Requirements: D's copy constructor must not throw Chris@16: // Chris@16: // shared_ptr will release p by calling d(p) Chris@16: // Chris@16: Chris@16: template shared_ptr( Y * p, D d ): px( p ), pn( p, d ) Chris@16: { Chris@16: boost::detail::sp_deleter_construct( this, p ); Chris@16: } Chris@16: Chris@16: #if !defined( BOOST_NO_CXX11_NULLPTR ) Chris@16: Chris@16: template shared_ptr( boost::detail::sp_nullptr_t p, D d ): px( p ), pn( p, d ) Chris@16: { Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: // As above, but with allocator. A's copy constructor shall not throw. Chris@16: Chris@16: template shared_ptr( Y * p, D d, A a ): px( p ), pn( p, d, a ) Chris@16: { Chris@16: boost::detail::sp_deleter_construct( this, p ); Chris@16: } Chris@16: Chris@16: #if !defined( BOOST_NO_CXX11_NULLPTR ) Chris@16: Chris@16: template shared_ptr( boost::detail::sp_nullptr_t p, D d, A a ): px( p ), pn( p, d, a ) Chris@16: { Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: // generated copy constructor, destructor are fine... Chris@16: Chris@16: #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) Chris@16: Chris@16: // ... except in C++0x, move disables the implicit copy Chris@16: Chris@16: shared_ptr( shared_ptr const & r ) BOOST_NOEXCEPT : px( r.px ), pn( r.pn ) Chris@16: { Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: template Chris@16: explicit shared_ptr( weak_ptr const & r ): pn( r.pn ) // may throw Chris@16: { Chris@16: boost::detail::sp_assert_convertible< Y, T >(); Chris@16: Chris@16: // it is now safe to copy r.px, as pn(r.pn) did not throw Chris@16: px = r.px; Chris@16: } Chris@16: Chris@16: template Chris@16: shared_ptr( weak_ptr const & r, boost::detail::sp_nothrow_tag ) Chris@16: BOOST_NOEXCEPT : px( 0 ), pn( r.pn, boost::detail::sp_nothrow_tag() ) Chris@16: { Chris@16: if( !pn.empty() ) Chris@16: { Chris@16: px = r.px; Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: #if !defined( BOOST_SP_NO_SP_CONVERTIBLE ) Chris@16: Chris@16: shared_ptr( shared_ptr const & r, typename boost::detail::sp_enable_if_convertible::type = boost::detail::sp_empty() ) Chris@16: Chris@16: #else Chris@16: Chris@16: shared_ptr( shared_ptr const & r ) Chris@16: Chris@16: #endif Chris@16: BOOST_NOEXCEPT : px( r.px ), pn( r.pn ) Chris@16: { Chris@16: boost::detail::sp_assert_convertible< Y, T >(); Chris@16: } Chris@16: Chris@16: // aliasing Chris@16: template< class Y > Chris@16: shared_ptr( shared_ptr const & r, element_type * p ) BOOST_NOEXCEPT : px( p ), pn( r.pn ) Chris@16: { Chris@16: } Chris@16: Chris@16: #ifndef BOOST_NO_AUTO_PTR Chris@16: Chris@16: template Chris@16: explicit shared_ptr( std::auto_ptr & r ): px(r.get()), pn() Chris@16: { Chris@16: boost::detail::sp_assert_convertible< Y, T >(); Chris@16: Chris@16: Y * tmp = r.get(); Chris@16: pn = boost::detail::shared_count( r ); Chris@16: Chris@16: boost::detail::sp_deleter_construct( this, tmp ); Chris@16: } Chris@16: Chris@16: #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) Chris@16: Chris@16: template Chris@16: shared_ptr( std::auto_ptr && r ): px(r.get()), pn() Chris@16: { Chris@16: boost::detail::sp_assert_convertible< Y, T >(); Chris@16: Chris@16: Y * tmp = r.get(); Chris@16: pn = boost::detail::shared_count( r ); Chris@16: Chris@16: boost::detail::sp_deleter_construct( this, tmp ); Chris@16: } Chris@16: Chris@16: #elif !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) Chris@16: Chris@16: template Chris@16: explicit shared_ptr( Ap r, typename boost::detail::sp_enable_if_auto_ptr::type = 0 ): px( r.get() ), pn() Chris@16: { Chris@16: typedef typename Ap::element_type Y; Chris@16: Chris@16: boost::detail::sp_assert_convertible< Y, T >(); Chris@16: Chris@16: Y * tmp = r.get(); Chris@16: pn = boost::detail::shared_count( r ); Chris@16: Chris@16: boost::detail::sp_deleter_construct( this, tmp ); Chris@16: } Chris@16: Chris@16: #endif // BOOST_NO_SFINAE, BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION Chris@16: Chris@16: #endif // BOOST_NO_AUTO_PTR Chris@16: Chris@16: #if !defined( BOOST_NO_CXX11_SMART_PTR ) && !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) Chris@16: Chris@16: template< class Y, class D > Chris@16: shared_ptr( std::unique_ptr< Y, D > && r ): px( r.get() ), pn() Chris@16: { Chris@16: boost::detail::sp_assert_convertible< Y, T >(); Chris@16: Chris@16: typename std::unique_ptr< Y, D >::pointer tmp = r.get(); Chris@16: pn = boost::detail::shared_count( r ); Chris@16: Chris@16: boost::detail::sp_deleter_construct( this, tmp ); Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: // assignment Chris@16: Chris@16: shared_ptr & operator=( shared_ptr const & r ) BOOST_NOEXCEPT Chris@16: { Chris@16: this_type(r).swap(*this); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1400) Chris@16: Chris@16: template Chris@16: shared_ptr & operator=(shared_ptr const & r) BOOST_NOEXCEPT Chris@16: { Chris@16: this_type(r).swap(*this); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: #ifndef BOOST_NO_AUTO_PTR Chris@16: Chris@16: template Chris@16: shared_ptr & operator=( std::auto_ptr & r ) Chris@16: { Chris@16: this_type( r ).swap( *this ); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) Chris@16: Chris@16: template Chris@16: shared_ptr & operator=( std::auto_ptr && r ) Chris@16: { Chris@16: this_type( static_cast< std::auto_ptr && >( r ) ).swap( *this ); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: #elif !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) Chris@16: Chris@16: template Chris@16: typename boost::detail::sp_enable_if_auto_ptr< Ap, shared_ptr & >::type operator=( Ap r ) Chris@16: { Chris@16: this_type( r ).swap( *this ); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: #endif // BOOST_NO_SFINAE, BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION Chris@16: Chris@16: #endif // BOOST_NO_AUTO_PTR Chris@16: Chris@16: #if !defined( BOOST_NO_CXX11_SMART_PTR ) && !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) Chris@16: Chris@16: template Chris@16: shared_ptr & operator=( std::unique_ptr && r ) Chris@16: { Chris@16: this_type( static_cast< std::unique_ptr && >( r ) ).swap(*this); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: // Move support Chris@16: Chris@16: #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) Chris@16: Chris@16: shared_ptr( shared_ptr && r ) BOOST_NOEXCEPT : px( r.px ), pn() Chris@16: { Chris@16: pn.swap( r.pn ); Chris@16: r.px = 0; Chris@16: } Chris@16: Chris@16: template Chris@16: #if !defined( BOOST_SP_NO_SP_CONVERTIBLE ) Chris@16: Chris@16: shared_ptr( shared_ptr && r, typename boost::detail::sp_enable_if_convertible::type = boost::detail::sp_empty() ) Chris@16: Chris@16: #else Chris@16: Chris@16: shared_ptr( shared_ptr && r ) Chris@16: Chris@16: #endif Chris@16: BOOST_NOEXCEPT : px( r.px ), pn() Chris@16: { Chris@16: boost::detail::sp_assert_convertible< Y, T >(); Chris@16: Chris@16: pn.swap( r.pn ); Chris@16: r.px = 0; Chris@16: } Chris@16: Chris@16: shared_ptr & operator=( shared_ptr && r ) BOOST_NOEXCEPT Chris@16: { Chris@16: this_type( static_cast< shared_ptr && >( r ) ).swap( *this ); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: template Chris@16: shared_ptr & operator=( shared_ptr && r ) BOOST_NOEXCEPT Chris@16: { Chris@16: this_type( static_cast< shared_ptr && >( r ) ).swap( *this ); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: #if !defined( BOOST_NO_CXX11_NULLPTR ) Chris@16: Chris@16: shared_ptr & operator=( boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT // never throws Chris@16: { Chris@16: this_type().swap(*this); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: void reset() BOOST_NOEXCEPT // never throws in 1.30+ Chris@16: { Chris@16: this_type().swap(*this); Chris@16: } Chris@16: Chris@16: template void reset( Y * p ) // Y must be complete Chris@16: { Chris@16: BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors Chris@16: this_type( p ).swap( *this ); Chris@16: } Chris@16: Chris@16: template void reset( Y * p, D d ) Chris@16: { Chris@16: this_type( p, d ).swap( *this ); Chris@16: } Chris@16: Chris@16: template void reset( Y * p, D d, A a ) Chris@16: { Chris@16: this_type( p, d, a ).swap( *this ); Chris@16: } Chris@16: Chris@16: template void reset( shared_ptr const & r, element_type * p ) Chris@16: { Chris@16: this_type( r, p ).swap( *this ); Chris@16: } Chris@16: Chris@16: // never throws (but has a BOOST_ASSERT in it, so not marked with BOOST_NOEXCEPT) Chris@16: typename boost::detail::sp_dereference< T >::type operator* () const Chris@16: { Chris@16: BOOST_ASSERT( px != 0 ); Chris@16: return *px; Chris@16: } Chris@16: Chris@16: // never throws (but has a BOOST_ASSERT in it, so not marked with BOOST_NOEXCEPT) Chris@16: typename boost::detail::sp_member_access< T >::type operator-> () const Chris@16: { Chris@16: BOOST_ASSERT( px != 0 ); Chris@16: return px; Chris@16: } Chris@16: Chris@16: // never throws (but has a BOOST_ASSERT in it, so not marked with BOOST_NOEXCEPT) Chris@16: typename boost::detail::sp_array_access< T >::type operator[] ( std::ptrdiff_t i ) const Chris@16: { Chris@16: BOOST_ASSERT( px != 0 ); Chris@16: BOOST_ASSERT( i >= 0 && ( i < boost::detail::sp_extent< T >::value || boost::detail::sp_extent< T >::value == 0 ) ); Chris@16: Chris@101: return static_cast< typename boost::detail::sp_array_access< T >::type >( px[ i ] ); Chris@16: } Chris@16: Chris@16: element_type * get() const BOOST_NOEXCEPT Chris@16: { Chris@16: return px; Chris@16: } Chris@16: Chris@16: // implicit conversion to "bool" Chris@16: #include Chris@16: Chris@16: bool unique() const BOOST_NOEXCEPT Chris@16: { Chris@16: return pn.unique(); Chris@16: } Chris@16: Chris@16: long use_count() const BOOST_NOEXCEPT Chris@16: { Chris@16: return pn.use_count(); Chris@16: } Chris@16: Chris@16: void swap( shared_ptr & other ) BOOST_NOEXCEPT Chris@16: { Chris@16: std::swap(px, other.px); Chris@16: pn.swap(other.pn); Chris@16: } Chris@16: Chris@16: template bool owner_before( shared_ptr const & rhs ) const BOOST_NOEXCEPT Chris@16: { Chris@16: return pn < rhs.pn; Chris@16: } Chris@16: Chris@16: template bool owner_before( weak_ptr const & rhs ) const BOOST_NOEXCEPT Chris@16: { Chris@16: return pn < rhs.pn; Chris@16: } Chris@16: Chris@16: void * _internal_get_deleter( boost::detail::sp_typeinfo const & ti ) const BOOST_NOEXCEPT Chris@16: { Chris@16: return pn.get_deleter( ti ); Chris@16: } Chris@16: Chris@16: void * _internal_get_untyped_deleter() const BOOST_NOEXCEPT Chris@16: { Chris@16: return pn.get_untyped_deleter(); Chris@16: } Chris@16: Chris@16: bool _internal_equiv( shared_ptr const & r ) const BOOST_NOEXCEPT Chris@16: { Chris@16: return px == r.px && pn == r.pn; Chris@16: } Chris@16: Chris@16: // Tasteless as this may seem, making all members public allows member templates Chris@16: // to work in the absence of member template friends. (Matthew Langston) Chris@16: Chris@16: #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS Chris@16: Chris@16: private: Chris@16: Chris@16: template friend class shared_ptr; Chris@16: template friend class weak_ptr; Chris@16: Chris@16: Chris@16: #endif Chris@16: Chris@16: element_type * px; // contained pointer Chris@16: boost::detail::shared_count pn; // reference counter Chris@16: Chris@16: }; // shared_ptr Chris@16: Chris@16: template inline bool operator==(shared_ptr const & a, shared_ptr const & b) BOOST_NOEXCEPT Chris@16: { Chris@16: return a.get() == b.get(); Chris@16: } Chris@16: Chris@16: template inline bool operator!=(shared_ptr const & a, shared_ptr const & b) BOOST_NOEXCEPT Chris@16: { Chris@16: return a.get() != b.get(); Chris@16: } Chris@16: Chris@16: #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96 Chris@16: Chris@16: // Resolve the ambiguity between our op!= and the one in rel_ops Chris@16: Chris@16: template inline bool operator!=(shared_ptr const & a, shared_ptr const & b) BOOST_NOEXCEPT Chris@16: { Chris@16: return a.get() != b.get(); Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: #if !defined( BOOST_NO_CXX11_NULLPTR ) Chris@16: Chris@16: template inline bool operator==( shared_ptr const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT Chris@16: { Chris@16: return p.get() == 0; Chris@16: } Chris@16: Chris@16: template inline bool operator==( boost::detail::sp_nullptr_t, shared_ptr const & p ) BOOST_NOEXCEPT Chris@16: { Chris@16: return p.get() == 0; Chris@16: } Chris@16: Chris@16: template inline bool operator!=( shared_ptr const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT Chris@16: { Chris@16: return p.get() != 0; Chris@16: } Chris@16: Chris@16: template inline bool operator!=( boost::detail::sp_nullptr_t, shared_ptr const & p ) BOOST_NOEXCEPT Chris@16: { Chris@16: return p.get() != 0; Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: template inline bool operator<(shared_ptr const & a, shared_ptr const & b) BOOST_NOEXCEPT Chris@16: { Chris@16: return a.owner_before( b ); Chris@16: } Chris@16: Chris@16: template inline void swap(shared_ptr & a, shared_ptr & b) BOOST_NOEXCEPT Chris@16: { Chris@16: a.swap(b); Chris@16: } Chris@16: Chris@16: template shared_ptr static_pointer_cast( shared_ptr const & r ) BOOST_NOEXCEPT Chris@16: { Chris@16: (void) static_cast< T* >( static_cast< U* >( 0 ) ); Chris@16: Chris@16: typedef typename shared_ptr::element_type E; Chris@16: Chris@16: E * p = static_cast< E* >( r.get() ); Chris@16: return shared_ptr( r, p ); Chris@16: } Chris@16: Chris@16: template shared_ptr const_pointer_cast( shared_ptr const & r ) BOOST_NOEXCEPT Chris@16: { Chris@16: (void) const_cast< T* >( static_cast< U* >( 0 ) ); Chris@16: Chris@16: typedef typename shared_ptr::element_type E; Chris@16: Chris@16: E * p = const_cast< E* >( r.get() ); Chris@16: return shared_ptr( r, p ); Chris@16: } Chris@16: Chris@16: template shared_ptr dynamic_pointer_cast( shared_ptr const & r ) BOOST_NOEXCEPT Chris@16: { Chris@16: (void) dynamic_cast< T* >( static_cast< U* >( 0 ) ); Chris@16: Chris@16: typedef typename shared_ptr::element_type E; Chris@16: Chris@16: E * p = dynamic_cast< E* >( r.get() ); Chris@16: return p? shared_ptr( r, p ): shared_ptr(); Chris@16: } Chris@16: Chris@16: template shared_ptr reinterpret_pointer_cast( shared_ptr const & r ) BOOST_NOEXCEPT Chris@16: { Chris@16: (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) ); Chris@16: Chris@16: typedef typename shared_ptr::element_type E; Chris@16: Chris@16: E * p = reinterpret_cast< E* >( r.get() ); Chris@16: return shared_ptr( r, p ); Chris@16: } Chris@16: Chris@16: // get_pointer() enables boost::mem_fn to recognize shared_ptr Chris@16: Chris@16: template inline typename shared_ptr::element_type * get_pointer(shared_ptr const & p) BOOST_NOEXCEPT Chris@16: { Chris@16: return p.get(); Chris@16: } Chris@16: Chris@16: // operator<< Chris@16: Chris@16: #if !defined(BOOST_NO_IOSTREAM) Chris@16: Chris@16: #if defined(BOOST_NO_TEMPLATED_IOSTREAMS) || ( defined(__GNUC__) && (__GNUC__ < 3) ) Chris@16: Chris@16: template std::ostream & operator<< (std::ostream & os, shared_ptr const & p) Chris@16: { Chris@16: os << p.get(); Chris@16: return os; Chris@16: } Chris@16: Chris@16: #else Chris@16: Chris@16: // in STLport's no-iostreams mode no iostream symbols can be used Chris@16: #ifndef _STLP_NO_IOSTREAMS Chris@16: Chris@16: # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300 && __SGI_STL_PORT) Chris@16: // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL Chris@16: using std::basic_ostream; Chris@16: template basic_ostream & operator<< (basic_ostream & os, shared_ptr const & p) Chris@16: # else Chris@16: template std::basic_ostream & operator<< (std::basic_ostream & os, shared_ptr const & p) Chris@16: # endif Chris@16: { Chris@16: os << p.get(); Chris@16: return os; Chris@16: } Chris@16: Chris@16: #endif // _STLP_NO_IOSTREAMS Chris@16: Chris@16: #endif // __GNUC__ < 3 Chris@16: Chris@16: #endif // !defined(BOOST_NO_IOSTREAM) Chris@16: Chris@16: // get_deleter Chris@16: Chris@16: namespace detail Chris@16: { Chris@16: Chris@16: #if ( defined(__GNUC__) && BOOST_WORKAROUND(__GNUC__, < 3) ) || \ Chris@16: ( defined(__EDG_VERSION__) && BOOST_WORKAROUND(__EDG_VERSION__, <= 238) ) || \ Chris@16: ( defined(__HP_aCC) && BOOST_WORKAROUND(__HP_aCC, <= 33500) ) Chris@16: Chris@16: // g++ 2.9x doesn't allow static_cast(void *) Chris@16: // apparently EDG 2.38 and HP aCC A.03.35 also don't accept it Chris@16: Chris@16: template D * basic_get_deleter(shared_ptr const & p) Chris@16: { Chris@16: void const * q = p._internal_get_deleter(BOOST_SP_TYPEID(D)); Chris@16: return const_cast(static_cast(q)); Chris@16: } Chris@16: Chris@16: #else Chris@16: Chris@16: template D * basic_get_deleter( shared_ptr const & p ) BOOST_NOEXCEPT Chris@16: { Chris@16: return static_cast( p._internal_get_deleter(BOOST_SP_TYPEID(D)) ); Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: class esft2_deleter_wrapper Chris@16: { Chris@16: private: Chris@16: Chris@101: shared_ptr deleter_; Chris@16: Chris@16: public: Chris@16: Chris@16: esft2_deleter_wrapper() Chris@16: { Chris@16: } Chris@16: Chris@16: template< class T > void set_deleter( shared_ptr const & deleter ) Chris@16: { Chris@16: deleter_ = deleter; Chris@16: } Chris@16: Chris@16: template D* get_deleter() const BOOST_NOEXCEPT Chris@16: { Chris@16: return boost::detail::basic_get_deleter( deleter_ ); Chris@16: } Chris@16: Chris@16: template< class T> void operator()( T* ) Chris@16: { Chris@16: BOOST_ASSERT( deleter_.use_count() <= 1 ); Chris@16: deleter_.reset(); Chris@16: } Chris@16: }; Chris@16: Chris@16: } // namespace detail Chris@16: Chris@16: template D * get_deleter( shared_ptr const & p ) BOOST_NOEXCEPT Chris@16: { Chris@16: D *del = boost::detail::basic_get_deleter(p); Chris@16: Chris@16: if(del == 0) Chris@16: { Chris@16: boost::detail::esft2_deleter_wrapper *del_wrapper = boost::detail::basic_get_deleter(p); Chris@16: // The following get_deleter method call is fully qualified because Chris@16: // older versions of gcc (2.95, 3.2.3) fail to compile it when written del_wrapper->get_deleter() Chris@16: if(del_wrapper) del = del_wrapper->::boost::detail::esft2_deleter_wrapper::get_deleter(); Chris@16: } Chris@16: Chris@16: return del; Chris@16: } Chris@16: Chris@16: // atomic access Chris@16: Chris@16: #if !defined(BOOST_SP_NO_ATOMIC_ACCESS) Chris@16: Chris@16: template inline bool atomic_is_lock_free( shared_ptr const * /*p*/ ) BOOST_NOEXCEPT Chris@16: { Chris@16: return false; Chris@16: } Chris@16: Chris@16: template shared_ptr atomic_load( shared_ptr const * p ) Chris@16: { Chris@16: boost::detail::spinlock_pool<2>::scoped_lock lock( p ); Chris@16: return *p; Chris@16: } Chris@16: Chris@101: template inline shared_ptr atomic_load_explicit( shared_ptr const * p, /*memory_order mo*/ int ) Chris@16: { Chris@16: return atomic_load( p ); Chris@16: } Chris@16: Chris@16: template void atomic_store( shared_ptr * p, shared_ptr r ) Chris@16: { Chris@16: boost::detail::spinlock_pool<2>::scoped_lock lock( p ); Chris@16: p->swap( r ); Chris@16: } Chris@16: Chris@101: template inline void atomic_store_explicit( shared_ptr * p, shared_ptr r, /*memory_order mo*/ int ) Chris@16: { Chris@16: atomic_store( p, r ); // std::move( r ) Chris@16: } Chris@16: Chris@16: template shared_ptr atomic_exchange( shared_ptr * p, shared_ptr r ) Chris@16: { Chris@16: boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( p ); Chris@16: Chris@16: sp.lock(); Chris@16: p->swap( r ); Chris@16: sp.unlock(); Chris@16: Chris@16: return r; // return std::move( r ) Chris@16: } Chris@16: Chris@101: template shared_ptr atomic_exchange_explicit( shared_ptr * p, shared_ptr r, /*memory_order mo*/ int ) Chris@16: { Chris@16: return atomic_exchange( p, r ); // std::move( r ) Chris@16: } Chris@16: Chris@16: template bool atomic_compare_exchange( shared_ptr * p, shared_ptr * v, shared_ptr w ) Chris@16: { Chris@16: boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( p ); Chris@16: Chris@16: sp.lock(); Chris@16: Chris@16: if( p->_internal_equiv( *v ) ) Chris@16: { Chris@16: p->swap( w ); Chris@16: Chris@16: sp.unlock(); Chris@16: Chris@16: return true; Chris@16: } Chris@16: else Chris@16: { Chris@16: shared_ptr tmp( *p ); Chris@16: Chris@16: sp.unlock(); Chris@16: Chris@16: tmp.swap( *v ); Chris@16: return false; Chris@16: } Chris@16: } Chris@16: Chris@101: template inline bool atomic_compare_exchange_explicit( shared_ptr * p, shared_ptr * v, shared_ptr w, /*memory_order success*/ int, /*memory_order failure*/ int ) Chris@16: { Chris@16: return atomic_compare_exchange( p, v, w ); // std::move( w ) Chris@16: } Chris@16: Chris@16: #endif // !defined(BOOST_SP_NO_ATOMIC_ACCESS) Chris@16: Chris@16: // hash_value Chris@16: Chris@16: template< class T > struct hash; Chris@16: Chris@16: template< class T > std::size_t hash_value( boost::shared_ptr const & p ) BOOST_NOEXCEPT Chris@16: { Chris@16: return boost::hash< T* >()( p.get() ); Chris@16: } Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #endif // #ifndef BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED