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