forked from boostorg/smart_ptr
Merge [51909], [51912], [52937], [53672] to release.
[SVN r55479]
This commit is contained in:
@ -333,6 +333,20 @@ public:
|
||||
if(pi_ != 0) pi_->weak_add_ref();
|
||||
}
|
||||
|
||||
// Move support
|
||||
|
||||
#if defined( BOOST_HAS_RVALUE_REFS )
|
||||
|
||||
weak_count(weak_count && r): pi_(r.pi_) // nothrow
|
||||
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
|
||||
, id_(weak_count_id)
|
||||
#endif
|
||||
{
|
||||
r.pi_ = 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
~weak_count() // nothrow
|
||||
{
|
||||
if(pi_ != 0) pi_->weak_release();
|
||||
|
@ -45,7 +45,7 @@ template< class Y, class T > struct sp_convertible
|
||||
static yes f( T* );
|
||||
static no f( ... );
|
||||
|
||||
enum _vt { value = sizeof( f( (Y*)0 ) ) == sizeof(yes) };
|
||||
enum _vt { value = sizeof( f( static_cast<Y*>(0) ) ) == sizeof(yes) };
|
||||
};
|
||||
|
||||
struct sp_empty
|
||||
|
132
include/boost/smart_ptr/enable_shared_from_this2.hpp
Normal file
132
include/boost/smart_ptr/enable_shared_from_this2.hpp
Normal file
@ -0,0 +1,132 @@
|
||||
#ifndef BOOST_ENABLE_SHARED_FROM_THIS2_HPP_INCLUDED
|
||||
#define BOOST_ENABLE_SHARED_FROM_THIS2_HPP_INCLUDED
|
||||
|
||||
//
|
||||
// enable_shared_from_this2.hpp
|
||||
//
|
||||
// Copyright 2002, 2009 Peter Dimov
|
||||
// Copyright 2008 Frank Mori Hess
|
||||
//
|
||||
// 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/config.hpp>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/assert.hpp>
|
||||
#include <boost/detail/workaround.hpp>
|
||||
|
||||
namespace boost
|
||||
{
|
||||
|
||||
namespace detail
|
||||
{
|
||||
|
||||
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< class T> void operator()( T* )
|
||||
{
|
||||
BOOST_ASSERT( deleter_.use_count() <= 1 );
|
||||
deleter_.reset();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template< class T > class enable_shared_from_this2
|
||||
{
|
||||
protected:
|
||||
|
||||
enable_shared_from_this2()
|
||||
{
|
||||
}
|
||||
|
||||
enable_shared_from_this2( enable_shared_from_this2 const & )
|
||||
{
|
||||
}
|
||||
|
||||
enable_shared_from_this2 & operator=( enable_shared_from_this2 const & )
|
||||
{
|
||||
return *this;
|
||||
}
|
||||
|
||||
~enable_shared_from_this2()
|
||||
{
|
||||
BOOST_ASSERT( shared_this_.use_count() <= 1 ); // make sure no dangling shared_ptr objects exist
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
mutable weak_ptr<T> weak_this_;
|
||||
mutable shared_ptr<T> shared_this_;
|
||||
|
||||
public:
|
||||
|
||||
shared_ptr<T> shared_from_this()
|
||||
{
|
||||
init_weak_once();
|
||||
return shared_ptr<T>( weak_this_ );
|
||||
}
|
||||
|
||||
shared_ptr<T const> shared_from_this() const
|
||||
{
|
||||
init_weak_once();
|
||||
return shared_ptr<T>( weak_this_ );
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
void init_weak_once() const
|
||||
{
|
||||
if( weak_this_._empty() )
|
||||
{
|
||||
shared_this_.reset( static_cast< T* >( 0 ), detail::esft2_deleter_wrapper() );
|
||||
weak_this_ = shared_this_;
|
||||
}
|
||||
}
|
||||
|
||||
public: // actually private, but avoids compiler template friendship issues
|
||||
|
||||
// Note: invoked automatically by shared_ptr; do not call
|
||||
template<class X, class Y> void _internal_accept_owner( shared_ptr<X> * ppx, Y * py ) const
|
||||
{
|
||||
BOOST_ASSERT( ppx != 0 );
|
||||
|
||||
if( weak_this_.use_count() == 0 )
|
||||
{
|
||||
weak_this_ = shared_ptr<T>( *ppx, py );
|
||||
}
|
||||
else if( shared_this_.use_count() != 0 )
|
||||
{
|
||||
BOOST_ASSERT( ppx->unique() ); // no weak_ptrs should exist either, but there's no way to check that
|
||||
|
||||
detail::esft2_deleter_wrapper * pd = boost::get_deleter<detail::esft2_deleter_wrapper>( shared_this_ );
|
||||
BOOST_ASSERT( pd != 0 );
|
||||
|
||||
pd->set_deleter( *ppx );
|
||||
|
||||
ppx->reset( shared_this_, ppx->get() );
|
||||
shared_this_.reset();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#endif // #ifndef BOOST_ENABLE_SHARED_FROM_THIS2_HPP_INCLUDED
|
@ -109,6 +109,23 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
// Move support
|
||||
|
||||
#if defined( BOOST_HAS_RVALUE_REFS )
|
||||
|
||||
intrusive_ptr(intrusive_ptr && rhs): px( rhs.px )
|
||||
{
|
||||
rhs.px = 0;
|
||||
}
|
||||
|
||||
intrusive_ptr & operator=(intrusive_ptr && rhs)
|
||||
{
|
||||
this_type(std::move(rhs)).swap(*this);
|
||||
return *this;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
intrusive_ptr & operator=(intrusive_ptr const & rhs)
|
||||
|
@ -61,6 +61,7 @@ namespace boost
|
||||
template<class T> class shared_ptr;
|
||||
template<class T> class weak_ptr;
|
||||
template<class T> class enable_shared_from_this;
|
||||
template<class T> class enable_shared_from_this2;
|
||||
|
||||
namespace detail
|
||||
{
|
||||
@ -109,6 +110,14 @@ template< class X, class Y, class T > inline void sp_enable_shared_from_this( bo
|
||||
}
|
||||
}
|
||||
|
||||
template< class X, class Y, class T > inline void sp_enable_shared_from_this( boost::shared_ptr<X> * ppx, Y const * py, boost::enable_shared_from_this2< T > const * pe )
|
||||
{
|
||||
if( pe != 0 )
|
||||
{
|
||||
pe->_internal_accept_owner( ppx, const_cast< Y* >( py ) );
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _MANAGED
|
||||
|
||||
// Avoid C4793, ... causes native code generation
|
||||
@ -359,14 +368,14 @@ public:
|
||||
|
||||
shared_ptr & operator=( shared_ptr && r ) // never throws
|
||||
{
|
||||
this_type( static_cast< shared_ptr && >( r ) ).swap( *this );
|
||||
this_type( std::move( r ) ).swap( *this );
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class Y>
|
||||
shared_ptr & operator=( shared_ptr<Y> && r ) // never throws
|
||||
{
|
||||
this_type( static_cast< shared_ptr<Y> && >( r ) ).swap( *this );
|
||||
this_type( std::move( r ) ).swap( *this );
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
@ -70,11 +70,43 @@ public:
|
||||
weak_ptr( weak_ptr<Y> const & r )
|
||||
|
||||
#endif
|
||||
: pn(r.pn) // never throws
|
||||
: px(r.lock().get()), pn(r.pn) // never throws
|
||||
{
|
||||
px = r.lock().get();
|
||||
}
|
||||
|
||||
#if defined( BOOST_HAS_RVALUE_REFS )
|
||||
|
||||
template<class Y>
|
||||
#if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
|
||||
|
||||
weak_ptr( weak_ptr<Y> && r, typename detail::sp_enable_if_convertible<Y,T>::type = detail::sp_empty() )
|
||||
|
||||
#else
|
||||
|
||||
weak_ptr( weak_ptr<Y> && r )
|
||||
|
||||
#endif
|
||||
: px(r.lock().get()), pn(std::move(r.pn)) // never throws
|
||||
{
|
||||
r.px = 0;
|
||||
}
|
||||
|
||||
// for better efficiency in the T == Y case
|
||||
weak_ptr( weak_ptr && r ): px( r.px ), pn(std::move(r.pn)) // never throws
|
||||
{
|
||||
r.px = 0;
|
||||
}
|
||||
|
||||
// for better efficiency in the T == Y case
|
||||
weak_ptr & operator=( weak_ptr && r ) // never throws
|
||||
{
|
||||
this_type( std::move( r ) ).swap( *this );
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
template<class Y>
|
||||
#if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
|
||||
|
||||
@ -99,6 +131,17 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
#if defined( BOOST_HAS_RVALUE_REFS )
|
||||
|
||||
template<class Y>
|
||||
weak_ptr & operator=(weak_ptr<Y> && r)
|
||||
{
|
||||
this_type( std::move( r ) ).swap( *this );
|
||||
return *this;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
template<class Y>
|
||||
weak_ptr & operator=(shared_ptr<Y> const & r) // never throws
|
||||
{
|
||||
@ -124,6 +167,11 @@ public:
|
||||
return pn.use_count() == 0;
|
||||
}
|
||||
|
||||
bool _empty() const // extension, not in std::weak_ptr
|
||||
{
|
||||
return pn.empty();
|
||||
}
|
||||
|
||||
void reset() // never throws in 1.30+
|
||||
{
|
||||
this_type().swap(*this);
|
||||
|
@ -16,9 +16,11 @@ import testing ;
|
||||
[ run shared_ptr_basic_test.cpp : : : <toolset>gcc:<cxxflags>-Wno-non-virtual-dtor ]
|
||||
[ run shared_ptr_test.cpp : : : <toolset>gcc:<cxxflags>-Wno-non-virtual-dtor ]
|
||||
[ run weak_ptr_test.cpp ]
|
||||
[ run weak_ptr_move_test.cpp ]
|
||||
[ run shared_from_this_test.cpp : : : <toolset>gcc:<cxxflags>-Wno-non-virtual-dtor ]
|
||||
[ run get_deleter_test.cpp ]
|
||||
[ run intrusive_ptr_test.cpp ]
|
||||
[ run intrusive_ptr_move_test.cpp ]
|
||||
[ run atomic_count_test.cpp ]
|
||||
[ run lw_mutex_test.cpp ]
|
||||
[ compile-fail shared_ptr_assign_fail.cpp ]
|
||||
@ -56,6 +58,7 @@ import testing ;
|
||||
[ run sp_recursive_assign2_test.cpp ]
|
||||
[ run sp_recursive_assign_rv_test.cpp ]
|
||||
[ run sp_recursive_assign2_rv_test.cpp ]
|
||||
[ run esft_constructor_test.cpp ]
|
||||
[ compile-fail auto_ptr_lv_fail.cpp ]
|
||||
[ run atomic_count_test2.cpp ]
|
||||
;
|
||||
|
169
test/esft_constructor_test.cpp
Normal file
169
test/esft_constructor_test.cpp
Normal file
@ -0,0 +1,169 @@
|
||||
//
|
||||
// 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/smart_ptr/enable_shared_from_this2.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_this2< 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_this2<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 & )
|
||||
{}
|
||||
}
|
||||
|
||||
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 & )
|
||||
{
|
||||
BOOST_ERROR("y threw bad_weak_ptr");
|
||||
}
|
||||
}
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
184
test/intrusive_ptr_move_test.cpp
Normal file
184
test/intrusive_ptr_move_test.cpp
Normal file
@ -0,0 +1,184 @@
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#if defined(BOOST_MSVC)
|
||||
|
||||
#pragma warning(disable: 4786) // identifier truncated in debug info
|
||||
#pragma warning(disable: 4710) // function not inlined
|
||||
#pragma warning(disable: 4711) // function selected for automatic inline expansion
|
||||
#pragma warning(disable: 4514) // unreferenced inline removed
|
||||
#pragma warning(disable: 4355) // 'this' : used in base member initializer list
|
||||
#pragma warning(disable: 4511) // copy constructor could not be generated
|
||||
#pragma warning(disable: 4512) // assignment operator could not be generated
|
||||
|
||||
#if (BOOST_MSVC >= 1310)
|
||||
#pragma warning(disable: 4675) // resolved overload found with Koenig lookup
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
//
|
||||
// intrusive_ptr_move_test.cpp
|
||||
//
|
||||
// Copyright (c) 2002-2005 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/intrusive_ptr.hpp>
|
||||
#include <boost/detail/atomic_count.hpp>
|
||||
#include <boost/config.hpp>
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
|
||||
#if defined( BOOST_HAS_RVALUE_REFS )
|
||||
|
||||
namespace N
|
||||
{
|
||||
|
||||
class base
|
||||
{
|
||||
private:
|
||||
|
||||
boost::detail::atomic_count use_count_;
|
||||
|
||||
base(base const &);
|
||||
base & operator=(base const &);
|
||||
|
||||
protected:
|
||||
|
||||
base(): use_count_(0)
|
||||
{
|
||||
++instances;
|
||||
}
|
||||
|
||||
virtual ~base()
|
||||
{
|
||||
--instances;
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
static long instances;
|
||||
|
||||
long use_count() const
|
||||
{
|
||||
return use_count_;
|
||||
}
|
||||
|
||||
#if !defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
|
||||
|
||||
inline friend void intrusive_ptr_add_ref(base * p)
|
||||
{
|
||||
++p->use_count_;
|
||||
}
|
||||
|
||||
inline friend void intrusive_ptr_release(base * p)
|
||||
{
|
||||
if(--p->use_count_ == 0) delete p;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
void add_ref()
|
||||
{
|
||||
++use_count_;
|
||||
}
|
||||
|
||||
void release()
|
||||
{
|
||||
if(--use_count_ == 0) delete this;
|
||||
}
|
||||
|
||||
#endif
|
||||
};
|
||||
|
||||
long base::instances = 0;
|
||||
|
||||
} // namespace N
|
||||
|
||||
#if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
|
||||
|
||||
namespace boost
|
||||
{
|
||||
|
||||
inline void intrusive_ptr_add_ref(N::base * p)
|
||||
{
|
||||
p->add_ref();
|
||||
}
|
||||
|
||||
inline void intrusive_ptr_release(N::base * p)
|
||||
{
|
||||
p->release();
|
||||
}
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#endif
|
||||
|
||||
//
|
||||
|
||||
struct X: public virtual N::base
|
||||
{
|
||||
};
|
||||
|
||||
struct Y: public X
|
||||
{
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
BOOST_TEST( N::base::instances == 0 );
|
||||
|
||||
{
|
||||
boost::intrusive_ptr<X> p( new X );
|
||||
BOOST_TEST( N::base::instances == 1 );
|
||||
|
||||
boost::intrusive_ptr<X> p2( std::move( p ) );
|
||||
BOOST_TEST( N::base::instances == 1 );
|
||||
BOOST_TEST( p.get() == 0 );
|
||||
|
||||
p2.reset();
|
||||
BOOST_TEST( N::base::instances == 0 );
|
||||
}
|
||||
|
||||
{
|
||||
boost::intrusive_ptr<X> p( new X );
|
||||
BOOST_TEST( N::base::instances == 1 );
|
||||
|
||||
boost::intrusive_ptr<X> p2;
|
||||
p2 = std::move( p );
|
||||
BOOST_TEST( N::base::instances == 1 );
|
||||
BOOST_TEST( p.get() == 0 );
|
||||
|
||||
p2.reset();
|
||||
BOOST_TEST( N::base::instances == 0 );
|
||||
}
|
||||
|
||||
{
|
||||
boost::intrusive_ptr<X> p( new X );
|
||||
BOOST_TEST( N::base::instances == 1 );
|
||||
|
||||
boost::intrusive_ptr<X> p2( new X );
|
||||
BOOST_TEST( N::base::instances == 2 );
|
||||
p2 = std::move( p );
|
||||
BOOST_TEST( N::base::instances == 1 );
|
||||
BOOST_TEST( p.get() == 0 );
|
||||
|
||||
p2.reset();
|
||||
BOOST_TEST( N::base::instances == 0 );
|
||||
}
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
||||
#else // !defined( BOOST_HAS_RVALUE_REFS )
|
||||
|
||||
int main()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
@ -8,11 +8,11 @@
|
||||
// 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>
|
||||
|
||||
#if defined( BOOST_HAS_RVALUE_REFS )
|
||||
|
||||
struct X
|
||||
{
|
||||
static long instances;
|
||||
@ -43,11 +43,11 @@ int main()
|
||||
boost::shared_ptr<X> p( new X );
|
||||
BOOST_TEST( X::instances == 1 );
|
||||
|
||||
boost::shared_ptr<X> p2( static_cast< boost::shared_ptr<X> && >( p ) );
|
||||
boost::shared_ptr<X> p2( std::move( p ) );
|
||||
BOOST_TEST( X::instances == 1 );
|
||||
BOOST_TEST( p.get() == 0 );
|
||||
|
||||
boost::shared_ptr<void> p3( static_cast< boost::shared_ptr<X> && >( p2 ) );
|
||||
boost::shared_ptr<void> p3( std::move( p2 ) );
|
||||
BOOST_TEST( X::instances == 1 );
|
||||
BOOST_TEST( p2.get() == 0 );
|
||||
|
||||
@ -60,12 +60,12 @@ int main()
|
||||
BOOST_TEST( X::instances == 1 );
|
||||
|
||||
boost::shared_ptr<X> p2;
|
||||
p2 = static_cast< boost::shared_ptr<X> && >( p );
|
||||
p2 = std::move( p );
|
||||
BOOST_TEST( X::instances == 1 );
|
||||
BOOST_TEST( p.get() == 0 );
|
||||
|
||||
boost::shared_ptr<void> p3;
|
||||
p3 = static_cast< boost::shared_ptr<X> && >( p2 );
|
||||
p3 = std::move( p2 );
|
||||
BOOST_TEST( X::instances == 1 );
|
||||
BOOST_TEST( p2.get() == 0 );
|
||||
|
||||
@ -79,13 +79,13 @@ int main()
|
||||
|
||||
boost::shared_ptr<X> p2( new X );
|
||||
BOOST_TEST( X::instances == 2 );
|
||||
p2 = static_cast< boost::shared_ptr<X> && >( p );
|
||||
p2 = std::move( p );
|
||||
BOOST_TEST( X::instances == 1 );
|
||||
BOOST_TEST( p.get() == 0 );
|
||||
|
||||
boost::shared_ptr<void> p3( new X );
|
||||
BOOST_TEST( X::instances == 2 );
|
||||
p3 = static_cast< boost::shared_ptr<X> && >( p2 );
|
||||
p3 = std::move( p2 );
|
||||
BOOST_TEST( X::instances == 1 );
|
||||
BOOST_TEST( p2.get() == 0 );
|
||||
|
||||
|
121
test/weak_ptr_move_test.cpp
Normal file
121
test/weak_ptr_move_test.cpp
Normal file
@ -0,0 +1,121 @@
|
||||
//
|
||||
// weak_ptr_move_test.cpp
|
||||
//
|
||||
// Copyright (c) 2007 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/weak_ptr.hpp>
|
||||
#include <boost/detail/lightweight_test.hpp>
|
||||
|
||||
#if defined( BOOST_HAS_RVALUE_REFS )
|
||||
|
||||
struct X
|
||||
{
|
||||
static long instances;
|
||||
|
||||
X()
|
||||
{
|
||||
++instances;
|
||||
}
|
||||
|
||||
~X()
|
||||
{
|
||||
--instances;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
X( X const & );
|
||||
X & operator=( X const & );
|
||||
};
|
||||
|
||||
long X::instances = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
BOOST_TEST( X::instances == 0 );
|
||||
|
||||
{
|
||||
boost::shared_ptr<X> p_( new X );
|
||||
boost::weak_ptr<X> p( p_ );
|
||||
BOOST_TEST( X::instances == 1 );
|
||||
BOOST_TEST( p.use_count() == 1 );
|
||||
|
||||
boost::weak_ptr<X> p2( std::move( p ) );
|
||||
BOOST_TEST( X::instances == 1 );
|
||||
BOOST_TEST( p2.use_count() == 1 );
|
||||
BOOST_TEST( p.expired() );
|
||||
|
||||
boost::weak_ptr<void> p3( std::move( p2 ) );
|
||||
BOOST_TEST( X::instances == 1 );
|
||||
BOOST_TEST( p3.use_count() == 1 );
|
||||
BOOST_TEST( p2.expired() );
|
||||
|
||||
p_.reset();
|
||||
BOOST_TEST( X::instances == 0 );
|
||||
BOOST_TEST( p3.expired() );
|
||||
}
|
||||
|
||||
{
|
||||
boost::shared_ptr<X> p_( new X );
|
||||
boost::weak_ptr<X> p( p_ );
|
||||
BOOST_TEST( X::instances == 1 );
|
||||
BOOST_TEST( p.use_count() == 1 );
|
||||
|
||||
boost::weak_ptr<X> p2;
|
||||
p2 = static_cast< boost::weak_ptr<X> && >( p );
|
||||
BOOST_TEST( X::instances == 1 );
|
||||
BOOST_TEST( p2.use_count() == 1 );
|
||||
BOOST_TEST( p.expired() );
|
||||
|
||||
boost::weak_ptr<void> p3;
|
||||
p3 = std::move( p2 );
|
||||
BOOST_TEST( X::instances == 1 );
|
||||
BOOST_TEST( p3.use_count() == 1 );
|
||||
BOOST_TEST( p2.expired() );
|
||||
|
||||
p_.reset();
|
||||
BOOST_TEST( X::instances == 0 );
|
||||
BOOST_TEST( p3.expired() );
|
||||
}
|
||||
|
||||
{
|
||||
boost::shared_ptr<X> p_( new X );
|
||||
boost::weak_ptr<X> p( p_ );
|
||||
BOOST_TEST( X::instances == 1 );
|
||||
BOOST_TEST( p.use_count() == 1 );
|
||||
|
||||
boost::shared_ptr<X> p_2( new X );
|
||||
boost::weak_ptr<X> p2( p_2 );
|
||||
BOOST_TEST( X::instances == 2 );
|
||||
p2 = std::move( p );
|
||||
BOOST_TEST( X::instances == 2 );
|
||||
BOOST_TEST( p2.use_count() == 1 );
|
||||
BOOST_TEST( p.expired() );
|
||||
BOOST_TEST( p2.lock() != p_2 );
|
||||
|
||||
boost::shared_ptr<void> p_3( new X );
|
||||
boost::weak_ptr<void> p3( p_3 );
|
||||
BOOST_TEST( X::instances == 3 );
|
||||
p3 = std::move( p2 );
|
||||
BOOST_TEST( X::instances == 3 );
|
||||
BOOST_TEST( p3.use_count() == 1 );
|
||||
BOOST_TEST( p2.expired() );
|
||||
BOOST_TEST( p3.lock() != p_3 );
|
||||
}
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
||||
#else // !defined( BOOST_HAS_RVALUE_REFS )
|
||||
|
||||
int main()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user