Merged revision(s) 81730-81731, 81776 from trunk:

Fix get_pointer for the array case, add operator= for unique_ptr, update auto_ptr signatures to use rvalue reference when available.
........
Update shared_ptr.htm.
........
Add more unique_ptr tests.
........


[SVN r81781]
This commit is contained in:
Peter Dimov
2012-12-08 00:57:04 +00:00
parent 08e5894510
commit c03bfd0b4d
3 changed files with 644 additions and 456 deletions

View File

@ -418,17 +418,30 @@ public:
#ifndef BOOST_NO_AUTO_PTR
template<class Y>
explicit shared_ptr(std::auto_ptr<Y> & r): px(r.get()), pn()
explicit shared_ptr( std::auto_ptr<Y> & r ): px(r.get()), pn()
{
boost::detail::sp_assert_convertible< Y, T >();
Y * tmp = r.get();
pn = boost::detail::shared_count(r);
pn = boost::detail::shared_count( r );
boost::detail::sp_deleter_construct( this, tmp );
}
#if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
#if defined( BOOST_HAS_RVALUE_REFS )
template<class Y>
shared_ptr( std::auto_ptr<Y> && r ): px(r.get()), pn()
{
boost::detail::sp_assert_convertible< Y, T >();
Y * tmp = r.get();
pn = boost::detail::shared_count( r );
boost::detail::sp_deleter_construct( this, tmp );
}
#elif !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
template<class Ap>
explicit shared_ptr( Ap r, typename boost::detail::sp_enable_if_auto_ptr<Ap, int>::type = 0 ): px( r.get() ), pn()
@ -486,11 +499,20 @@ public:
template<class Y>
shared_ptr & operator=( std::auto_ptr<Y> & r )
{
this_type(r).swap(*this);
this_type( r ).swap( *this );
return *this;
}
#if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
#if defined( BOOST_HAS_RVALUE_REFS )
template<class Y>
shared_ptr & operator=( std::auto_ptr<Y> && r )
{
this_type( static_cast< std::auto_ptr<Y> && >( r ) ).swap( *this );
return *this;
}
#elif !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
template<class Ap>
typename boost::detail::sp_enable_if_auto_ptr< Ap, shared_ptr & >::type operator=( Ap r )
@ -503,6 +525,17 @@ public:
#endif // BOOST_NO_AUTO_PTR
#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
// Move support
#if defined( BOOST_HAS_RVALUE_REFS )
@ -730,7 +763,7 @@ template<class T, class U> shared_ptr<T> reinterpret_pointer_cast( shared_ptr<U>
// get_pointer() enables boost::mem_fn to recognize shared_ptr
template<class T> inline T * get_pointer(shared_ptr<T> const & p) BOOST_NOEXCEPT
template<class T> inline typename shared_ptr<T>::element_type * get_pointer(shared_ptr<T> const & p) BOOST_NOEXCEPT
{
return p.get();
}

File diff suppressed because it is too large Load Diff

View File

@ -91,6 +91,66 @@ int main()
p2.reset();
p3.reset();
BOOST_TEST( X::instances == 0 );
p2 = std::unique_ptr<X>( new X );
BOOST_TEST( X::instances == 1 );
p2 = std::unique_ptr<X>( new X );
BOOST_TEST( X::instances == 1 );
p2.reset();
BOOST_TEST( X::instances == 0 );
}
{
BOOST_TEST( X::instances == 0 );
std::unique_ptr<X> p( new X );
BOOST_TEST( X::instances == 1 );
boost::shared_ptr<X const> p2( std::move( p ) );
BOOST_TEST( X::instances == 1 );
BOOST_TEST( p.get() == 0 );
boost::shared_ptr<X const> p3 = p2->shared_from_this();
BOOST_TEST( p2 == p3 );
BOOST_TEST( !(p2 < p3) && !(p3 < p2) );
p2.reset();
p3.reset();
BOOST_TEST( X::instances == 0 );
p2 = std::unique_ptr<X>( new X );
BOOST_TEST( X::instances == 1 );
p2 = std::unique_ptr<X>( new X );
BOOST_TEST( X::instances == 1 );
p2.reset();
BOOST_TEST( X::instances == 0 );
}
{
BOOST_TEST( X::instances == 0 );
std::unique_ptr<X> p( new X );
BOOST_TEST( X::instances == 1 );
boost::shared_ptr<void> p2( std::move( p ) );
BOOST_TEST( X::instances == 1 );
BOOST_TEST( p.get() == 0 );
p2.reset();
BOOST_TEST( X::instances == 0 );
p2 = std::unique_ptr<X>( new X );
BOOST_TEST( X::instances == 1 );
p2 = std::unique_ptr<X>( new X );
BOOST_TEST( X::instances == 1 );
p2.reset();
BOOST_TEST( X::instances == 0 );
}
{
@ -105,6 +165,15 @@ int main()
p2.reset();
BOOST_TEST( Y::instances == 0 );
p2 = std::unique_ptr<Y, YD>( new Y, YD() );
BOOST_TEST( Y::instances == 1 );
p2 = std::unique_ptr<Y, YD>( new Y, YD() );
BOOST_TEST( Y::instances == 1 );
p2.reset();
BOOST_TEST( Y::instances == 0 );
}
{
@ -121,6 +190,15 @@ int main()
p2.reset();
BOOST_TEST( Y::instances == 0 );
p2 = std::unique_ptr<Y, YD&>( new Y, yd );
BOOST_TEST( Y::instances == 1 );
p2 = std::unique_ptr<Y, YD&>( new Y, yd );
BOOST_TEST( Y::instances == 1 );
p2.reset();
BOOST_TEST( Y::instances == 0 );
}
{
@ -137,6 +215,15 @@ int main()
p2.reset();
BOOST_TEST( Y::instances == 0 );
p2 = std::unique_ptr<Y, YD const&>( new Y, yd );
BOOST_TEST( Y::instances == 1 );
p2 = std::unique_ptr<Y, YD const&>( new Y, yd );
BOOST_TEST( Y::instances == 1 );
p2.reset();
BOOST_TEST( Y::instances == 0 );
}
return boost::report_errors();