Add support for shared_ptr<X[N>.

[SVN r81253]
This commit is contained in:
Peter Dimov
2012-11-08 18:07:49 +00:00
parent c57245d710
commit aa7562c3e5
6 changed files with 341 additions and 22 deletions
+1
View File
@@ -73,6 +73,7 @@ import testing ;
[ run sp_array_test.cpp ]
[ compile sp_array_cv_test.cpp ]
[ run sp_convertible_test.cpp ]
[ run sp_array_n_test.cpp ]
[ compile-fail array_fail_spa_sp_c.cpp ]
[ compile-fail array_fail_sp_spa_c.cpp ]
+36 -18
View File
@@ -14,29 +14,47 @@ struct X
{
};
class B
{
};
class D: public B
{
};
#define TEST_CONV( T, U ) \
{ \
boost::shared_ptr< T > p1; \
boost::shared_ptr< U > p2( p1 ); \
p2 = p1; \
boost::shared_ptr< U > p3 = boost::shared_ptr< T >(); \
p3 = boost::shared_ptr< T >(); \
}
#define TEST_CV_TRUE( T, U ) \
TEST_CONV( T, U ) \
TEST_CONV( T, const U ) \
TEST_CONV( T, volatile U ) \
TEST_CONV( T, const volatile U ) \
TEST_CONV( const T, const U ) \
TEST_CONV( const T, const volatile U ) \
TEST_CONV( volatile T, volatile U ) \
TEST_CONV( volatile T, const volatile U ) \
TEST_CONV( const volatile T, const volatile U )
int main()
{
boost::shared_ptr< X[] > px;
TEST_CV_TRUE( X, X )
TEST_CV_TRUE( X, void )
TEST_CV_TRUE( D, B )
boost::shared_ptr< X const[] > pcx( px );
boost::shared_ptr< X volatile[] > pvx( px );
TEST_CV_TRUE( X[], X[] )
TEST_CV_TRUE( X[3], X[3] )
boost::shared_ptr< X const volatile[] > pcvx( px );
boost::shared_ptr< X const volatile[] > pcvx2( pcx );
boost::shared_ptr< X const volatile[] > pcvx3( pvx );
TEST_CV_TRUE( X[3], X[] )
boost::shared_ptr< void > pv( px );
boost::shared_ptr< void const > pcv( px );
boost::shared_ptr< void const > pcv2( pcx );
boost::shared_ptr< void volatile > pvv( px );
boost::shared_ptr< void volatile > pvv2( pvx );
boost::shared_ptr< void const volatile > pcvv( px );
boost::shared_ptr< void const volatile > pcvv2( pcx );
boost::shared_ptr< void const volatile > pcvv3( pvx );
boost::shared_ptr< void const volatile > pcvv4( pcvx );
TEST_CV_TRUE( X[], void )
TEST_CV_TRUE( X[3], void )
return 0;
}
+248
View File
@@ -0,0 +1,248 @@
//
// sp_array_n_test.cpp
//
// Copyright (c) 2012 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
//
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <memory>
#include <utility>
class X: public boost::enable_shared_from_this< X >
{
public:
static int allocations;
static int instances;
X()
{
++instances;
}
~X()
{
--instances;
}
void* operator new[]( std::size_t n )
{
++allocations;
return ::operator new[]( n );
}
void operator delete[]( void* p )
{
--allocations;
::operator delete[]( p );
}
private:
X( X const& );
X& operator=( X const& );
};
int X::allocations = 0;
int X::instances = 0;
template< class T> class array_deleter
{
public:
static int calls;
void operator()( T * p ) const
{
++calls;
delete[] p;
}
private:
template< class Y > void operator()( Y * p ) const;
};
template< class T > int array_deleter< T >::calls = 0;
int main()
{
BOOST_TEST( X::allocations == 0 );
BOOST_TEST( X::instances == 0 );
{
boost::shared_ptr<X[3]> px;
BOOST_TEST( !px );
BOOST_TEST( X::allocations == 0 );
BOOST_TEST( X::instances == 0 );
boost::shared_ptr<X[3]> px2( new X[ 3 ] );
BOOST_TEST( px2 );
try
{
px2[0].shared_from_this();
BOOST_ERROR( "px2[0].shared_from_this() failed to throw" );
}
catch( boost::bad_weak_ptr const& )
{
}
catch( ... )
{
BOOST_ERROR( "px2[0].shared_from_this() threw something else than bad_weak_ptr" );
}
BOOST_TEST( X::allocations == 1 );
BOOST_TEST( X::instances == 3 );
{
X & rx = px2[ 0 ];
BOOST_TEST( &rx == px2.get() );
}
boost::shared_ptr<X const[3]> px3( px2 );
BOOST_TEST( px3 == px2 );
BOOST_TEST( !( px2 < px3 ) && !( px3 < px2 ) );
{
X const & rx = px3[ 1 ];
BOOST_TEST( &rx == px3.get() + 1 );
}
px3.reset();
px3 = px2;
BOOST_TEST( px3 == px2 );
BOOST_TEST( !( px2 < px3 ) && !( px3 < px2 ) );
boost::shared_ptr<X volatile[3]> px4( px2 );
BOOST_TEST( px4 == px2 );
BOOST_TEST( !( px2 < px4 ) && !( px4 < px2 ) );
{
X volatile & rx = px4[ 2 ];
BOOST_TEST( &rx == px4.get() + 2 );
}
px4.reset();
px4 = px2;
BOOST_TEST( px4 == px2 );
BOOST_TEST( !( px2 < px4 ) && !( px4 < px2 ) );
boost::shared_ptr<void> px5( px2 );
BOOST_TEST( px5 == px2 );
BOOST_TEST( !( px2 < px5 ) && !( px5 < px2 ) );
px5.reset();
px5 = px2;
BOOST_TEST( px5 == px2 );
BOOST_TEST( !( px2 < px5 ) && !( px5 < px2 ) );
boost::weak_ptr<X[3]> wp( px );
BOOST_TEST( wp.lock() == px );
boost::weak_ptr<X[3]> wp2( px2 );
BOOST_TEST( wp2.lock() == px2 );
wp2.reset();
wp2 = px2;
BOOST_TEST( wp2.lock() == px2 );
boost::weak_ptr<X const[3]> wp3( px2 );
BOOST_TEST( wp3.lock() == px2 );
wp3.reset();
wp3 = px2;
BOOST_TEST( wp3.lock() == px2 );
boost::weak_ptr<X volatile[3]> wp4( px2 );
BOOST_TEST( wp4.lock() == px2 );
wp4.reset();
wp4 = px2;
BOOST_TEST( wp4.lock() == px2 );
boost::weak_ptr<void> wp5( px2 );
BOOST_TEST( wp5.lock() == px2 );
wp5.reset();
wp5 = px2;
BOOST_TEST( wp5.lock() == px2 );
px2.reset();
BOOST_TEST( X::allocations == 1 );
BOOST_TEST( X::instances == 3 );
px3.reset();
px4.reset();
px5.reset();
BOOST_TEST( X::allocations == 0 );
BOOST_TEST( X::instances == 0 );
BOOST_TEST( wp2.lock() == 0 );
BOOST_TEST( wp3.lock() == 0 );
BOOST_TEST( wp4.lock() == 0 );
BOOST_TEST( wp5.lock() == 0 );
}
{
boost::shared_ptr<X[5]> px( new X[ 5 ], array_deleter< X >() );
BOOST_TEST( X::allocations == 1 );
BOOST_TEST( X::instances == 5 );
try
{
px[0].shared_from_this();
BOOST_ERROR( "px[0].shared_from_this() failed to throw" );
}
catch( boost::bad_weak_ptr const& )
{
}
catch( ... )
{
BOOST_ERROR( "px[0].shared_from_this() threw something else than bad_weak_ptr" );
}
px.reset();
BOOST_TEST( X::allocations == 0 );
BOOST_TEST( X::instances == 0 );
BOOST_TEST( array_deleter< X >::calls == 1 );
}
{
boost::shared_ptr<X[6]> px( new X[ 6 ], array_deleter< X >(), std::allocator< X >() );
BOOST_TEST( X::allocations == 1 );
BOOST_TEST( X::instances == 6 );
try
{
px[0].shared_from_this();
BOOST_ERROR( "px[0].shared_from_this() failed to throw" );
}
catch( boost::bad_weak_ptr const& )
{
}
catch( ... )
{
BOOST_ERROR( "px[0].shared_from_this() threw something else than bad_weak_ptr" );
}
px.reset();
BOOST_TEST( X::allocations == 0 );
BOOST_TEST( X::instances == 0 );
BOOST_TEST( array_deleter< X >::calls == 2 );
}
return boost::report_errors();
}
+1 -1
View File
@@ -78,7 +78,7 @@ int main()
TEST_CV_FALSE( X[3], X[4] )
TEST_CV_FALSE( D[3], B[3] )
//TEST_CV_TRUE( X[3], X[] )
TEST_CV_TRUE( X[3], X[] )
TEST_CV_FALSE( X[], X[3] )
TEST_CV_TRUE( X[], void )