mirror of
				https://github.com/boostorg/smart_ptr.git
				synced 2025-11-03 01:01:54 +01:00 
			
		
		
		
	[SVN r51581]
This commit is contained in:
		@@ -48,5 +48,9 @@ import testing ;
 | 
			
		||||
          [ run ip_convertible_test.cpp ]
 | 
			
		||||
          [ run allocate_shared_test.cpp ]
 | 
			
		||||
          [ run sp_atomic_test.cpp ]
 | 
			
		||||
          [ run esft_void_test.cpp ]
 | 
			
		||||
          [ run esft_second_ptr_test.cpp ]
 | 
			
		||||
          [ run make_shared_esft_test.cpp ]
 | 
			
		||||
          [ run allocate_shared_esft_test.cpp ]
 | 
			
		||||
        ;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										264
									
								
								test/allocate_shared_esft_test.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										264
									
								
								test/allocate_shared_esft_test.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,264 @@
 | 
			
		||||
//  allocate_shared_esft_test.cpp
 | 
			
		||||
//
 | 
			
		||||
//  Copyright 2007-2009 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/detail/lightweight_test.hpp>
 | 
			
		||||
#include <boost/make_shared.hpp>
 | 
			
		||||
#include <boost/shared_ptr.hpp>
 | 
			
		||||
#include <boost/enable_shared_from_this.hpp>
 | 
			
		||||
#include <memory>
 | 
			
		||||
 | 
			
		||||
class X: public boost::enable_shared_from_this<X>
 | 
			
		||||
{
 | 
			
		||||
private:
 | 
			
		||||
 | 
			
		||||
    X( X const & );
 | 
			
		||||
    X & operator=( X const & );
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
 | 
			
		||||
    static int instances;
 | 
			
		||||
 | 
			
		||||
    explicit X( int = 0, int = 0, int = 0, int = 0, int = 0, int = 0, int = 0, int = 0, int = 0 )
 | 
			
		||||
    {
 | 
			
		||||
        ++instances;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ~X()
 | 
			
		||||
    {
 | 
			
		||||
        --instances;
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
int X::instances = 0;
 | 
			
		||||
 | 
			
		||||
int main()
 | 
			
		||||
{
 | 
			
		||||
    BOOST_TEST( X::instances == 0 );
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        boost::shared_ptr< X > px = boost::allocate_shared< X >( std::allocator<void>() );
 | 
			
		||||
        BOOST_TEST( X::instances == 1 );
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            boost::shared_ptr< X > qx = px->shared_from_this();
 | 
			
		||||
 | 
			
		||||
            BOOST_TEST( px == qx );
 | 
			
		||||
            BOOST_TEST( !( px < qx ) && !( qx < px ) );
 | 
			
		||||
 | 
			
		||||
            px.reset();
 | 
			
		||||
            BOOST_TEST( X::instances == 1 );
 | 
			
		||||
        }
 | 
			
		||||
        catch( boost::bad_weak_ptr const& )
 | 
			
		||||
        {
 | 
			
		||||
            BOOST_ERROR( "px->shared_from_this() failed" );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    BOOST_TEST( X::instances == 0 );
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        boost::shared_ptr< X > px = boost::allocate_shared< X >( std::allocator<void>(), 1 );
 | 
			
		||||
        BOOST_TEST( X::instances == 1 );
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            boost::shared_ptr< X > qx = px->shared_from_this();
 | 
			
		||||
 | 
			
		||||
            BOOST_TEST( px == qx );
 | 
			
		||||
            BOOST_TEST( !( px < qx ) && !( qx < px ) );
 | 
			
		||||
 | 
			
		||||
            px.reset();
 | 
			
		||||
            BOOST_TEST( X::instances == 1 );
 | 
			
		||||
        }
 | 
			
		||||
        catch( boost::bad_weak_ptr const& )
 | 
			
		||||
        {
 | 
			
		||||
            BOOST_ERROR( "px->shared_from_this() failed" );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    BOOST_TEST( X::instances == 0 );
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        boost::shared_ptr< X > px = boost::allocate_shared< X >( std::allocator<void>(), 1, 2 );
 | 
			
		||||
        BOOST_TEST( X::instances == 1 );
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            boost::shared_ptr< X > qx = px->shared_from_this();
 | 
			
		||||
 | 
			
		||||
            BOOST_TEST( px == qx );
 | 
			
		||||
            BOOST_TEST( !( px < qx ) && !( qx < px ) );
 | 
			
		||||
 | 
			
		||||
            px.reset();
 | 
			
		||||
            BOOST_TEST( X::instances == 1 );
 | 
			
		||||
        }
 | 
			
		||||
        catch( boost::bad_weak_ptr const& )
 | 
			
		||||
        {
 | 
			
		||||
            BOOST_ERROR( "px->shared_from_this() failed" );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    BOOST_TEST( X::instances == 0 );
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        boost::shared_ptr< X > px = boost::allocate_shared< X >( std::allocator<void>(), 1, 2, 3 );
 | 
			
		||||
        BOOST_TEST( X::instances == 1 );
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            boost::shared_ptr< X > qx = px->shared_from_this();
 | 
			
		||||
 | 
			
		||||
            BOOST_TEST( px == qx );
 | 
			
		||||
            BOOST_TEST( !( px < qx ) && !( qx < px ) );
 | 
			
		||||
 | 
			
		||||
            px.reset();
 | 
			
		||||
            BOOST_TEST( X::instances == 1 );
 | 
			
		||||
        }
 | 
			
		||||
        catch( boost::bad_weak_ptr const& )
 | 
			
		||||
        {
 | 
			
		||||
            BOOST_ERROR( "px->shared_from_this() failed" );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    BOOST_TEST( X::instances == 0 );
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        boost::shared_ptr< X > px = boost::allocate_shared< X >( std::allocator<void>(), 1, 2, 3, 4 );
 | 
			
		||||
        BOOST_TEST( X::instances == 1 );
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            boost::shared_ptr< X > qx = px->shared_from_this();
 | 
			
		||||
 | 
			
		||||
            BOOST_TEST( px == qx );
 | 
			
		||||
            BOOST_TEST( !( px < qx ) && !( qx < px ) );
 | 
			
		||||
 | 
			
		||||
            px.reset();
 | 
			
		||||
            BOOST_TEST( X::instances == 1 );
 | 
			
		||||
        }
 | 
			
		||||
        catch( boost::bad_weak_ptr const& )
 | 
			
		||||
        {
 | 
			
		||||
            BOOST_ERROR( "px->shared_from_this() failed" );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    BOOST_TEST( X::instances == 0 );
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        boost::shared_ptr< X > px = boost::allocate_shared< X >( std::allocator<void>(), 1, 2, 3, 4, 5 );
 | 
			
		||||
        BOOST_TEST( X::instances == 1 );
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            boost::shared_ptr< X > qx = px->shared_from_this();
 | 
			
		||||
 | 
			
		||||
            BOOST_TEST( px == qx );
 | 
			
		||||
            BOOST_TEST( !( px < qx ) && !( qx < px ) );
 | 
			
		||||
 | 
			
		||||
            px.reset();
 | 
			
		||||
            BOOST_TEST( X::instances == 1 );
 | 
			
		||||
        }
 | 
			
		||||
        catch( boost::bad_weak_ptr const& )
 | 
			
		||||
        {
 | 
			
		||||
            BOOST_ERROR( "px->shared_from_this() failed" );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    BOOST_TEST( X::instances == 0 );
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        boost::shared_ptr< X > px = boost::allocate_shared< X >( std::allocator<void>(), 1, 2, 3, 4, 5, 6 );
 | 
			
		||||
        BOOST_TEST( X::instances == 1 );
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            boost::shared_ptr< X > qx = px->shared_from_this();
 | 
			
		||||
 | 
			
		||||
            BOOST_TEST( px == qx );
 | 
			
		||||
            BOOST_TEST( !( px < qx ) && !( qx < px ) );
 | 
			
		||||
 | 
			
		||||
            px.reset();
 | 
			
		||||
            BOOST_TEST( X::instances == 1 );
 | 
			
		||||
        }
 | 
			
		||||
        catch( boost::bad_weak_ptr const& )
 | 
			
		||||
        {
 | 
			
		||||
            BOOST_ERROR( "px->shared_from_this() failed" );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    BOOST_TEST( X::instances == 0 );
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        boost::shared_ptr< X > px = boost::allocate_shared< X >( std::allocator<void>(), 1, 2, 3, 4, 5, 6, 7 );
 | 
			
		||||
        BOOST_TEST( X::instances == 1 );
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            boost::shared_ptr< X > qx = px->shared_from_this();
 | 
			
		||||
 | 
			
		||||
            BOOST_TEST( px == qx );
 | 
			
		||||
            BOOST_TEST( !( px < qx ) && !( qx < px ) );
 | 
			
		||||
 | 
			
		||||
            px.reset();
 | 
			
		||||
            BOOST_TEST( X::instances == 1 );
 | 
			
		||||
        }
 | 
			
		||||
        catch( boost::bad_weak_ptr const& )
 | 
			
		||||
        {
 | 
			
		||||
            BOOST_ERROR( "px->shared_from_this() failed" );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    BOOST_TEST( X::instances == 0 );
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        boost::shared_ptr< X > px = boost::allocate_shared< X >( std::allocator<void>(), 1, 2, 3, 4, 5, 6, 7, 8 );
 | 
			
		||||
        BOOST_TEST( X::instances == 1 );
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            boost::shared_ptr< X > qx = px->shared_from_this();
 | 
			
		||||
 | 
			
		||||
            BOOST_TEST( px == qx );
 | 
			
		||||
            BOOST_TEST( !( px < qx ) && !( qx < px ) );
 | 
			
		||||
 | 
			
		||||
            px.reset();
 | 
			
		||||
            BOOST_TEST( X::instances == 1 );
 | 
			
		||||
        }
 | 
			
		||||
        catch( boost::bad_weak_ptr const& )
 | 
			
		||||
        {
 | 
			
		||||
            BOOST_ERROR( "px->shared_from_this() failed" );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    BOOST_TEST( X::instances == 0 );
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        boost::shared_ptr< X > px = boost::allocate_shared< X >( std::allocator<void>(), 1, 2, 3, 4, 5, 6, 7, 8, 9 );
 | 
			
		||||
        BOOST_TEST( X::instances == 1 );
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            boost::shared_ptr< X > qx = px->shared_from_this();
 | 
			
		||||
 | 
			
		||||
            BOOST_TEST( px == qx );
 | 
			
		||||
            BOOST_TEST( !( px < qx ) && !( qx < px ) );
 | 
			
		||||
 | 
			
		||||
            px.reset();
 | 
			
		||||
            BOOST_TEST( X::instances == 1 );
 | 
			
		||||
        }
 | 
			
		||||
        catch( boost::bad_weak_ptr const& )
 | 
			
		||||
        {
 | 
			
		||||
            BOOST_ERROR( "px->shared_from_this() failed" );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    BOOST_TEST( X::instances == 0 );
 | 
			
		||||
 | 
			
		||||
    return boost::report_errors();
 | 
			
		||||
}
 | 
			
		||||
@@ -10,6 +10,7 @@
 | 
			
		||||
#include <boost/make_shared.hpp>
 | 
			
		||||
#include <boost/shared_ptr.hpp>
 | 
			
		||||
#include <boost/weak_ptr.hpp>
 | 
			
		||||
#include <cstddef>
 | 
			
		||||
 | 
			
		||||
class X
 | 
			
		||||
{
 | 
			
		||||
@@ -18,6 +19,14 @@ private:
 | 
			
		||||
    X( X const & );
 | 
			
		||||
    X & operator=( X const & );
 | 
			
		||||
 | 
			
		||||
    void * operator new( std::size_t );
 | 
			
		||||
 | 
			
		||||
    void operator delete( void * p )
 | 
			
		||||
    {
 | 
			
		||||
        // lack of this definition causes link errors on MSVC
 | 
			
		||||
        ::operator delete( p );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
 | 
			
		||||
    static int instances;
 | 
			
		||||
 
 | 
			
		||||
@@ -1,169 +0,0 @@
 | 
			
		||||
//
 | 
			
		||||
//  esft_constructor_test.cpp
 | 
			
		||||
//
 | 
			
		||||
//  A test for the new enable_shared_from_this support for calling
 | 
			
		||||
//  shared_from_this from constructors (that is, prior to the
 | 
			
		||||
//  object's ownership being passed to an external shared_ptr).
 | 
			
		||||
//
 | 
			
		||||
//  Copyright (c) 2008 Frank Mori Hess
 | 
			
		||||
//  Copyright (c) 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)
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#include <boost/enable_shared_from_this.hpp>
 | 
			
		||||
#include <boost/shared_ptr.hpp>
 | 
			
		||||
#include <boost/weak_ptr.hpp>
 | 
			
		||||
#include <boost/detail/lightweight_test.hpp>
 | 
			
		||||
#include <memory>
 | 
			
		||||
 | 
			
		||||
class X: public boost::enable_shared_from_this< X >
 | 
			
		||||
{
 | 
			
		||||
private:
 | 
			
		||||
 | 
			
		||||
    int destroyed_;
 | 
			
		||||
    int deleted_;
 | 
			
		||||
    int expected_;
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
 | 
			
		||||
    X( X const& );
 | 
			
		||||
    X& operator=( X const& );
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
 | 
			
		||||
    static int instances;
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
 | 
			
		||||
    explicit X( int expected, boost::shared_ptr<X> *early_px = 0 ): destroyed_( 0 ), deleted_( 0 ), expected_( expected )
 | 
			
		||||
    {
 | 
			
		||||
        ++instances;
 | 
			
		||||
        if( early_px ) *early_px = shared_from_this();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ~X()
 | 
			
		||||
    {
 | 
			
		||||
        BOOST_TEST( deleted_ == expected_ );
 | 
			
		||||
        BOOST_TEST( destroyed_ == 0 );
 | 
			
		||||
        ++destroyed_;
 | 
			
		||||
        --instances;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    typedef void (*deleter_type)( X* );
 | 
			
		||||
 | 
			
		||||
    static void deleter( X * px )
 | 
			
		||||
    {
 | 
			
		||||
        ++px->deleted_;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    static void deleter2( X * px )
 | 
			
		||||
    {
 | 
			
		||||
        ++px->deleted_;
 | 
			
		||||
        delete px;
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
int X::instances = 0;
 | 
			
		||||
 | 
			
		||||
template<typename T, typename U>
 | 
			
		||||
bool are_shared_owners(const boost::shared_ptr<T> &a, const boost::shared_ptr<U> &b)
 | 
			
		||||
{
 | 
			
		||||
    return !(a < b) && !(b < a);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct Y: public boost::enable_shared_from_this<Y>
 | 
			
		||||
{};
 | 
			
		||||
 | 
			
		||||
int main()
 | 
			
		||||
{
 | 
			
		||||
    BOOST_TEST( X::instances == 0 );
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        boost::shared_ptr<X> early_px;
 | 
			
		||||
        X* x = new X( 1, &early_px );
 | 
			
		||||
        BOOST_TEST( early_px.use_count() > 0 );
 | 
			
		||||
        BOOST_TEST( boost::get_deleter<X::deleter_type>(early_px) == 0 );
 | 
			
		||||
        boost::shared_ptr<X> px( x, &X::deleter2 );
 | 
			
		||||
        BOOST_TEST( early_px.use_count() == 2 && px.use_count() == 2 );
 | 
			
		||||
        BOOST_TEST(are_shared_owners(early_px, px));
 | 
			
		||||
        px.reset();
 | 
			
		||||
        BOOST_TEST( early_px.use_count() == 1 );
 | 
			
		||||
        BOOST_TEST( X::instances == 1 );
 | 
			
		||||
        X::deleter_type *pd = boost::get_deleter<X::deleter_type>(early_px);
 | 
			
		||||
        BOOST_TEST(pd && *pd == &X::deleter2 );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    BOOST_TEST( X::instances == 0 );
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        boost::shared_ptr<X> early_px;
 | 
			
		||||
        X* x = new X( 1, &early_px );
 | 
			
		||||
        boost::weak_ptr<X> early_weak_px = early_px;
 | 
			
		||||
        early_px.reset();
 | 
			
		||||
        BOOST_TEST( !early_weak_px.expired() );
 | 
			
		||||
        boost::shared_ptr<X> px( x, &X::deleter2 );
 | 
			
		||||
        BOOST_TEST( px.use_count() == 1 );
 | 
			
		||||
        BOOST_TEST( X::instances == 1 );
 | 
			
		||||
        BOOST_TEST(are_shared_owners(early_weak_px.lock(), px));
 | 
			
		||||
        px.reset();
 | 
			
		||||
        BOOST_TEST( early_weak_px.expired() );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    BOOST_TEST( X::instances == 0 );
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        boost::shared_ptr<X> early_px;
 | 
			
		||||
        X x( 1, &early_px );
 | 
			
		||||
        BOOST_TEST( early_px.use_count() > 0 );
 | 
			
		||||
        boost::shared_ptr<X> px( &x, &X::deleter );
 | 
			
		||||
        BOOST_TEST( early_px.use_count() == 2 && px.use_count() == 2 );
 | 
			
		||||
        early_px.reset();
 | 
			
		||||
        BOOST_TEST( px.use_count() == 1 );
 | 
			
		||||
        BOOST_TEST( X::instances == 1 );
 | 
			
		||||
        px.reset();
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            x.shared_from_this();
 | 
			
		||||
            BOOST_ERROR("x did not throw bad_weak_ptr");
 | 
			
		||||
        }
 | 
			
		||||
        catch( const boost::bad_weak_ptr &err)
 | 
			
		||||
        {}
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    BOOST_TEST( X::instances == 0 );
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        boost::weak_ptr<X> early_weak_px;
 | 
			
		||||
        {
 | 
			
		||||
            boost::shared_ptr<X> early_px;
 | 
			
		||||
            X x( 0, &early_px );
 | 
			
		||||
            early_weak_px = early_px;
 | 
			
		||||
            early_px.reset();
 | 
			
		||||
            BOOST_TEST( !early_weak_px.expired() );
 | 
			
		||||
            BOOST_TEST( X::instances == 1 );
 | 
			
		||||
        }
 | 
			
		||||
        BOOST_TEST( early_weak_px.expired() );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    BOOST_TEST( X::instances == 0 );
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        boost::shared_ptr<Y> px(new Y());
 | 
			
		||||
        Y y(*px);
 | 
			
		||||
        px.reset();
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            y.shared_from_this();
 | 
			
		||||
        }
 | 
			
		||||
        catch( const boost::bad_weak_ptr &err)
 | 
			
		||||
        {
 | 
			
		||||
            BOOST_ERROR("y threw bad_weak_ptr");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    return boost::report_errors();
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										51
									
								
								test/esft_second_ptr_test.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								test/esft_second_ptr_test.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,51 @@
 | 
			
		||||
//
 | 
			
		||||
//  esft_second_ptr_test.cpp
 | 
			
		||||
//
 | 
			
		||||
//  This test has been extracted from a real
 | 
			
		||||
//  scenario that occurs in Boost.Python
 | 
			
		||||
//
 | 
			
		||||
//  Copyright 2009 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/enable_shared_from_this.hpp>
 | 
			
		||||
#include <boost/shared_ptr.hpp>
 | 
			
		||||
#include <boost/detail/lightweight_test.hpp>
 | 
			
		||||
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
class X: public boost::enable_shared_from_this<X>
 | 
			
		||||
{
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
void null_deleter( void const* )
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main()
 | 
			
		||||
{
 | 
			
		||||
    boost::shared_ptr<X> px( new X );
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        boost::shared_ptr<X> px2( px.get(), null_deleter );
 | 
			
		||||
        BOOST_TEST( px == px2 );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    try
 | 
			
		||||
    {
 | 
			
		||||
        boost::shared_ptr< X > qx = px->shared_from_this();
 | 
			
		||||
 | 
			
		||||
        BOOST_TEST( px == qx );
 | 
			
		||||
        BOOST_TEST( !( px < qx ) && !( qx < px ) );
 | 
			
		||||
    }
 | 
			
		||||
    catch( boost::bad_weak_ptr const& )
 | 
			
		||||
    {
 | 
			
		||||
        BOOST_ERROR( "px->shared_from_this() failed" );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return boost::report_errors();
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										41
									
								
								test/esft_void_test.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								test/esft_void_test.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,41 @@
 | 
			
		||||
//
 | 
			
		||||
//  esft_void_test.cpp
 | 
			
		||||
//
 | 
			
		||||
//  Copyright 2009 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/enable_shared_from_this.hpp>
 | 
			
		||||
#include <boost/shared_ptr.hpp>
 | 
			
		||||
#include <boost/detail/lightweight_test.hpp>
 | 
			
		||||
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
class X: public boost::enable_shared_from_this<X>
 | 
			
		||||
{
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
int main()
 | 
			
		||||
{
 | 
			
		||||
    boost::shared_ptr< void const volatile > pv( new X );
 | 
			
		||||
    boost::shared_ptr< void > pv2 = boost::const_pointer_cast< void >( pv );
 | 
			
		||||
    boost::shared_ptr< X > px = boost::static_pointer_cast< X >( pv2 );
 | 
			
		||||
 | 
			
		||||
    try
 | 
			
		||||
    {
 | 
			
		||||
        boost::shared_ptr< X > qx = px->shared_from_this();
 | 
			
		||||
 | 
			
		||||
        BOOST_TEST( px == qx );
 | 
			
		||||
        BOOST_TEST( !( px < qx ) && !( qx < px ) );
 | 
			
		||||
    }
 | 
			
		||||
    catch( boost::bad_weak_ptr const& )
 | 
			
		||||
    {
 | 
			
		||||
        BOOST_ERROR( "px->shared_from_this() failed" );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return boost::report_errors();
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										263
									
								
								test/make_shared_esft_test.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										263
									
								
								test/make_shared_esft_test.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,263 @@
 | 
			
		||||
//  make_shared_esft_test.cpp
 | 
			
		||||
//
 | 
			
		||||
//  Copyright 2007-2009 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/detail/lightweight_test.hpp>
 | 
			
		||||
#include <boost/make_shared.hpp>
 | 
			
		||||
#include <boost/shared_ptr.hpp>
 | 
			
		||||
#include <boost/enable_shared_from_this.hpp>
 | 
			
		||||
 | 
			
		||||
class X: public boost::enable_shared_from_this<X>
 | 
			
		||||
{
 | 
			
		||||
private:
 | 
			
		||||
 | 
			
		||||
    X( X const & );
 | 
			
		||||
    X & operator=( X const & );
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
 | 
			
		||||
    static int instances;
 | 
			
		||||
 | 
			
		||||
    explicit X( int = 0, int = 0, int = 0, int = 0, int = 0, int = 0, int = 0, int = 0, int = 0 )
 | 
			
		||||
    {
 | 
			
		||||
        ++instances;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ~X()
 | 
			
		||||
    {
 | 
			
		||||
        --instances;
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
int X::instances = 0;
 | 
			
		||||
 | 
			
		||||
int main()
 | 
			
		||||
{
 | 
			
		||||
    BOOST_TEST( X::instances == 0 );
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        boost::shared_ptr< X > px = boost::make_shared< X >();
 | 
			
		||||
        BOOST_TEST( X::instances == 1 );
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            boost::shared_ptr< X > qx = px->shared_from_this();
 | 
			
		||||
 | 
			
		||||
            BOOST_TEST( px == qx );
 | 
			
		||||
            BOOST_TEST( !( px < qx ) && !( qx < px ) );
 | 
			
		||||
 | 
			
		||||
            px.reset();
 | 
			
		||||
            BOOST_TEST( X::instances == 1 );
 | 
			
		||||
        }
 | 
			
		||||
        catch( boost::bad_weak_ptr const& )
 | 
			
		||||
        {
 | 
			
		||||
            BOOST_ERROR( "px->shared_from_this() failed" );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    BOOST_TEST( X::instances == 0 );
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        boost::shared_ptr< X > px = boost::make_shared< X >( 1 );
 | 
			
		||||
        BOOST_TEST( X::instances == 1 );
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            boost::shared_ptr< X > qx = px->shared_from_this();
 | 
			
		||||
 | 
			
		||||
            BOOST_TEST( px == qx );
 | 
			
		||||
            BOOST_TEST( !( px < qx ) && !( qx < px ) );
 | 
			
		||||
 | 
			
		||||
            px.reset();
 | 
			
		||||
            BOOST_TEST( X::instances == 1 );
 | 
			
		||||
        }
 | 
			
		||||
        catch( boost::bad_weak_ptr const& )
 | 
			
		||||
        {
 | 
			
		||||
            BOOST_ERROR( "px->shared_from_this() failed" );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    BOOST_TEST( X::instances == 0 );
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        boost::shared_ptr< X > px = boost::make_shared< X >( 1, 2 );
 | 
			
		||||
        BOOST_TEST( X::instances == 1 );
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            boost::shared_ptr< X > qx = px->shared_from_this();
 | 
			
		||||
 | 
			
		||||
            BOOST_TEST( px == qx );
 | 
			
		||||
            BOOST_TEST( !( px < qx ) && !( qx < px ) );
 | 
			
		||||
 | 
			
		||||
            px.reset();
 | 
			
		||||
            BOOST_TEST( X::instances == 1 );
 | 
			
		||||
        }
 | 
			
		||||
        catch( boost::bad_weak_ptr const& )
 | 
			
		||||
        {
 | 
			
		||||
            BOOST_ERROR( "px->shared_from_this() failed" );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    BOOST_TEST( X::instances == 0 );
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        boost::shared_ptr< X > px = boost::make_shared< X >( 1, 2, 3 );
 | 
			
		||||
        BOOST_TEST( X::instances == 1 );
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            boost::shared_ptr< X > qx = px->shared_from_this();
 | 
			
		||||
 | 
			
		||||
            BOOST_TEST( px == qx );
 | 
			
		||||
            BOOST_TEST( !( px < qx ) && !( qx < px ) );
 | 
			
		||||
 | 
			
		||||
            px.reset();
 | 
			
		||||
            BOOST_TEST( X::instances == 1 );
 | 
			
		||||
        }
 | 
			
		||||
        catch( boost::bad_weak_ptr const& )
 | 
			
		||||
        {
 | 
			
		||||
            BOOST_ERROR( "px->shared_from_this() failed" );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    BOOST_TEST( X::instances == 0 );
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        boost::shared_ptr< X > px = boost::make_shared< X >( 1, 2, 3, 4 );
 | 
			
		||||
        BOOST_TEST( X::instances == 1 );
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            boost::shared_ptr< X > qx = px->shared_from_this();
 | 
			
		||||
 | 
			
		||||
            BOOST_TEST( px == qx );
 | 
			
		||||
            BOOST_TEST( !( px < qx ) && !( qx < px ) );
 | 
			
		||||
 | 
			
		||||
            px.reset();
 | 
			
		||||
            BOOST_TEST( X::instances == 1 );
 | 
			
		||||
        }
 | 
			
		||||
        catch( boost::bad_weak_ptr const& )
 | 
			
		||||
        {
 | 
			
		||||
            BOOST_ERROR( "px->shared_from_this() failed" );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    BOOST_TEST( X::instances == 0 );
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        boost::shared_ptr< X > px = boost::make_shared< X >( 1, 2, 3, 4, 5 );
 | 
			
		||||
        BOOST_TEST( X::instances == 1 );
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            boost::shared_ptr< X > qx = px->shared_from_this();
 | 
			
		||||
 | 
			
		||||
            BOOST_TEST( px == qx );
 | 
			
		||||
            BOOST_TEST( !( px < qx ) && !( qx < px ) );
 | 
			
		||||
 | 
			
		||||
            px.reset();
 | 
			
		||||
            BOOST_TEST( X::instances == 1 );
 | 
			
		||||
        }
 | 
			
		||||
        catch( boost::bad_weak_ptr const& )
 | 
			
		||||
        {
 | 
			
		||||
            BOOST_ERROR( "px->shared_from_this() failed" );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    BOOST_TEST( X::instances == 0 );
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        boost::shared_ptr< X > px = boost::make_shared< X >( 1, 2, 3, 4, 5, 6 );
 | 
			
		||||
        BOOST_TEST( X::instances == 1 );
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            boost::shared_ptr< X > qx = px->shared_from_this();
 | 
			
		||||
 | 
			
		||||
            BOOST_TEST( px == qx );
 | 
			
		||||
            BOOST_TEST( !( px < qx ) && !( qx < px ) );
 | 
			
		||||
 | 
			
		||||
            px.reset();
 | 
			
		||||
            BOOST_TEST( X::instances == 1 );
 | 
			
		||||
        }
 | 
			
		||||
        catch( boost::bad_weak_ptr const& )
 | 
			
		||||
        {
 | 
			
		||||
            BOOST_ERROR( "px->shared_from_this() failed" );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    BOOST_TEST( X::instances == 0 );
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        boost::shared_ptr< X > px = boost::make_shared< X >( 1, 2, 3, 4, 5, 6, 7 );
 | 
			
		||||
        BOOST_TEST( X::instances == 1 );
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            boost::shared_ptr< X > qx = px->shared_from_this();
 | 
			
		||||
 | 
			
		||||
            BOOST_TEST( px == qx );
 | 
			
		||||
            BOOST_TEST( !( px < qx ) && !( qx < px ) );
 | 
			
		||||
 | 
			
		||||
            px.reset();
 | 
			
		||||
            BOOST_TEST( X::instances == 1 );
 | 
			
		||||
        }
 | 
			
		||||
        catch( boost::bad_weak_ptr const& )
 | 
			
		||||
        {
 | 
			
		||||
            BOOST_ERROR( "px->shared_from_this() failed" );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    BOOST_TEST( X::instances == 0 );
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        boost::shared_ptr< X > px = boost::make_shared< X >( 1, 2, 3, 4, 5, 6, 7, 8 );
 | 
			
		||||
        BOOST_TEST( X::instances == 1 );
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            boost::shared_ptr< X > qx = px->shared_from_this();
 | 
			
		||||
 | 
			
		||||
            BOOST_TEST( px == qx );
 | 
			
		||||
            BOOST_TEST( !( px < qx ) && !( qx < px ) );
 | 
			
		||||
 | 
			
		||||
            px.reset();
 | 
			
		||||
            BOOST_TEST( X::instances == 1 );
 | 
			
		||||
        }
 | 
			
		||||
        catch( boost::bad_weak_ptr const& )
 | 
			
		||||
        {
 | 
			
		||||
            BOOST_ERROR( "px->shared_from_this() failed" );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    BOOST_TEST( X::instances == 0 );
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        boost::shared_ptr< X > px = boost::make_shared< X >( 1, 2, 3, 4, 5, 6, 7, 8, 9 );
 | 
			
		||||
        BOOST_TEST( X::instances == 1 );
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            boost::shared_ptr< X > qx = px->shared_from_this();
 | 
			
		||||
 | 
			
		||||
            BOOST_TEST( px == qx );
 | 
			
		||||
            BOOST_TEST( !( px < qx ) && !( qx < px ) );
 | 
			
		||||
 | 
			
		||||
            px.reset();
 | 
			
		||||
            BOOST_TEST( X::instances == 1 );
 | 
			
		||||
        }
 | 
			
		||||
        catch( boost::bad_weak_ptr const& )
 | 
			
		||||
        {
 | 
			
		||||
            BOOST_ERROR( "px->shared_from_this() failed" );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    BOOST_TEST( X::instances == 0 );
 | 
			
		||||
 | 
			
		||||
    return boost::report_errors();
 | 
			
		||||
}
 | 
			
		||||
@@ -10,6 +10,7 @@
 | 
			
		||||
#include <boost/make_shared.hpp>
 | 
			
		||||
#include <boost/shared_ptr.hpp>
 | 
			
		||||
#include <boost/weak_ptr.hpp>
 | 
			
		||||
#include <cstddef>
 | 
			
		||||
 | 
			
		||||
class X
 | 
			
		||||
{
 | 
			
		||||
@@ -18,6 +19,14 @@ private:
 | 
			
		||||
    X( X const & );
 | 
			
		||||
    X & operator=( X const & );
 | 
			
		||||
 | 
			
		||||
    void * operator new( std::size_t );
 | 
			
		||||
 | 
			
		||||
    void operator delete( void * p )
 | 
			
		||||
    {
 | 
			
		||||
        // lack of this definition causes link errors on MSVC
 | 
			
		||||
        ::operator delete( p );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
 | 
			
		||||
    static int instances;
 | 
			
		||||
 
 | 
			
		||||
@@ -55,16 +55,23 @@ void test()
 | 
			
		||||
    BOOST_TEST(py.get() != 0);
 | 
			
		||||
    BOOST_TEST(py.use_count() == 1);
 | 
			
		||||
 | 
			
		||||
    boost::shared_ptr<X> px = py->getX();
 | 
			
		||||
    BOOST_TEST(px.get() != 0);
 | 
			
		||||
    BOOST_TEST(py.use_count() == 2);
 | 
			
		||||
    try
 | 
			
		||||
    {
 | 
			
		||||
        boost::shared_ptr<X> px = py->getX();
 | 
			
		||||
        BOOST_TEST(px.get() != 0);
 | 
			
		||||
        BOOST_TEST(py.use_count() == 2);
 | 
			
		||||
 | 
			
		||||
    px->f();
 | 
			
		||||
        px->f();
 | 
			
		||||
 | 
			
		||||
    boost::shared_ptr<Y> py2 = boost::dynamic_pointer_cast<Y>(px);
 | 
			
		||||
    BOOST_TEST(py.get() == py2.get());
 | 
			
		||||
    BOOST_TEST(!(py < py2 || py2 < py));
 | 
			
		||||
    BOOST_TEST(py.use_count() == 3);
 | 
			
		||||
        boost::shared_ptr<Y> py2 = boost::dynamic_pointer_cast<Y>(px);
 | 
			
		||||
        BOOST_TEST(py.get() == py2.get());
 | 
			
		||||
        BOOST_TEST(!(py < py2 || py2 < py));
 | 
			
		||||
        BOOST_TEST(py.use_count() == 3);
 | 
			
		||||
    }
 | 
			
		||||
    catch( boost::bad_weak_ptr const& )
 | 
			
		||||
    {
 | 
			
		||||
        BOOST_ERROR( "py->getX() failed" );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void test2();
 | 
			
		||||
@@ -124,19 +131,25 @@ void test3()
 | 
			
		||||
{
 | 
			
		||||
    boost::shared_ptr<V> p(new V);
 | 
			
		||||
 | 
			
		||||
    boost::shared_ptr<V> q = p->shared_from_this();
 | 
			
		||||
    BOOST_TEST(p == q);
 | 
			
		||||
    BOOST_TEST(!(p < q) && !(q < p));
 | 
			
		||||
    try
 | 
			
		||||
    {
 | 
			
		||||
        boost::shared_ptr<V> q = p->shared_from_this();
 | 
			
		||||
        BOOST_TEST(p == q);
 | 
			
		||||
        BOOST_TEST(!(p < q) && !(q < p));
 | 
			
		||||
    }
 | 
			
		||||
    catch( boost::bad_weak_ptr const & )
 | 
			
		||||
    {
 | 
			
		||||
        BOOST_ERROR( "p->shared_from_this() failed" );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    V v2(*p);
 | 
			
		||||
 | 
			
		||||
    try
 | 
			
		||||
    {
 | 
			
		||||
        boost::shared_ptr<V> r = v2.shared_from_this();
 | 
			
		||||
        BOOST_TEST( p < r || r < p );
 | 
			
		||||
        BOOST_TEST( r.get() == &v2 );
 | 
			
		||||
        BOOST_ERROR("v2.shared_from_this() failed to throw");
 | 
			
		||||
    }
 | 
			
		||||
    catch(boost::bad_weak_ptr const &)
 | 
			
		||||
    catch( boost::bad_weak_ptr const & )
 | 
			
		||||
    {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -147,7 +160,7 @@ void test3()
 | 
			
		||||
        BOOST_TEST(p == r);
 | 
			
		||||
        BOOST_TEST(!(p < r) && !(r < p));
 | 
			
		||||
    }
 | 
			
		||||
    catch(boost::bad_weak_ptr const &)
 | 
			
		||||
    catch( boost::bad_weak_ptr const & )
 | 
			
		||||
    {
 | 
			
		||||
        BOOST_ERROR("p->shared_from_this() threw bad_weak_ptr after *p = V()");
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -8,6 +8,8 @@
 | 
			
		||||
// http://www.boost.org/LICENSE_1_0.txt
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#if defined( BOOST_HAS_RVALUE_REFS )
 | 
			
		||||
 | 
			
		||||
#include <boost/shared_ptr.hpp>
 | 
			
		||||
#include <boost/detail/lightweight_test.hpp>
 | 
			
		||||
 | 
			
		||||
@@ -93,3 +95,12 @@ int main()
 | 
			
		||||
 | 
			
		||||
    return boost::report_errors();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#else // !defined( BOOST_HAS_RVALUE_REFS )
 | 
			
		||||
 | 
			
		||||
int main()
 | 
			
		||||
{
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 
 | 
			
		||||
@@ -1,146 +0,0 @@
 | 
			
		||||
//
 | 
			
		||||
//  sp_accept_owner_test.cpp
 | 
			
		||||
//
 | 
			
		||||
//  Copyright (c) 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)
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#include <boost/shared_ptr.hpp>
 | 
			
		||||
#include <boost/detail/lightweight_test.hpp>
 | 
			
		||||
#include <iostream>
 | 
			
		||||
 | 
			
		||||
namespace N
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
struct D;
 | 
			
		||||
 | 
			
		||||
struct X
 | 
			
		||||
{
 | 
			
		||||
    X * px_;
 | 
			
		||||
 | 
			
		||||
    D * pd_;
 | 
			
		||||
    void * pv_;
 | 
			
		||||
 | 
			
		||||
    X(): px_( 0 ), pd_( 0 ), pv_( 0 )
 | 
			
		||||
    {
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct D
 | 
			
		||||
{
 | 
			
		||||
    template<class T> void operator()( T * p ) const { delete p; }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
} // namespace N
 | 
			
		||||
 | 
			
		||||
#if !defined( BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP )
 | 
			
		||||
 | 
			
		||||
namespace N
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
#else
 | 
			
		||||
 | 
			
		||||
namespace boost
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
template<class Y> inline void sp_accept_owner( boost::shared_ptr<Y> * ps, ::N::X * px )
 | 
			
		||||
{
 | 
			
		||||
    std::cout << "sp_accept_owner( " << ps << ", " << px << " )\n";
 | 
			
		||||
 | 
			
		||||
    BOOST_TEST( ps->get() == px );
 | 
			
		||||
 | 
			
		||||
    if( px != 0 )
 | 
			
		||||
    {
 | 
			
		||||
        px->px_ = px;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
template<class Y> inline void sp_accept_owner( boost::shared_ptr<Y> * ps, ::N::X * px, ::N::D * pd )
 | 
			
		||||
{
 | 
			
		||||
    std::cout << "sp_accept_owner( " << ps << ", " << px << ", (D*)" << pd << " )\n";
 | 
			
		||||
 | 
			
		||||
    BOOST_TEST( ps->get() == px );
 | 
			
		||||
 | 
			
		||||
    if( px != 0 )
 | 
			
		||||
    {
 | 
			
		||||
        px->px_ = px;
 | 
			
		||||
        px->pd_ = pd;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
template<class Y> inline void sp_accept_owner( boost::shared_ptr<Y> * ps, ::N::X * px, void * pv )
 | 
			
		||||
{
 | 
			
		||||
    std::cout << "sp_accept_owner( " << ps << ", " << px << ", (void*)" << pv << " )\n";
 | 
			
		||||
 | 
			
		||||
    BOOST_TEST( ps->get() == px );
 | 
			
		||||
 | 
			
		||||
    if( px != 0 )
 | 
			
		||||
    {
 | 
			
		||||
        px->px_ = px;
 | 
			
		||||
        px->pv_ = pv;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
} // namespace N or boost
 | 
			
		||||
 | 
			
		||||
struct D2
 | 
			
		||||
{
 | 
			
		||||
    template<class T> void operator()( T * p ) const { delete p; }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
template<class T> void test( T* = 0 )
 | 
			
		||||
{
 | 
			
		||||
    {
 | 
			
		||||
        boost::shared_ptr<T> p( static_cast< T* >( 0 ) );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        T * p = new T;
 | 
			
		||||
        boost::shared_ptr<T> q( p );
 | 
			
		||||
 | 
			
		||||
        BOOST_TEST( q->px_ == p );
 | 
			
		||||
        BOOST_TEST( q->pd_ == 0 );
 | 
			
		||||
        BOOST_TEST( q->pv_ == 0 );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        T * p = new T;
 | 
			
		||||
        boost::shared_ptr<T> q( p, N::D() );
 | 
			
		||||
 | 
			
		||||
        BOOST_TEST( q->px_ == p );
 | 
			
		||||
        BOOST_TEST( q->pd_ != 0 );
 | 
			
		||||
        BOOST_TEST( q->pv_ == 0 );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
        T * p = new T;
 | 
			
		||||
        boost::shared_ptr<T> q( p, D2() );
 | 
			
		||||
 | 
			
		||||
        BOOST_TEST( q->px_ == p );
 | 
			
		||||
        BOOST_TEST( q->pd_ == 0 );
 | 
			
		||||
        BOOST_TEST( q->pv_ != 0 );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
namespace N2
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
struct Y: public virtual N::X
 | 
			
		||||
{
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
} // namespace N2
 | 
			
		||||
 | 
			
		||||
int main()
 | 
			
		||||
{
 | 
			
		||||
    test<N::X>();
 | 
			
		||||
    test<N2::Y>();
 | 
			
		||||
 | 
			
		||||
    return boost::report_errors();
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user