forked from boostorg/smart_ptr
Merged in smart_ptr changes from the sandbox/boost0x branch created for
BoostCon '09. This adds move semantics to weak_ptr and intrusive_ptr. [SVN r52937]
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();
|
||||
|
@ -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)
|
||||
|
@ -368,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
|
||||
{
|
||||
|
@ -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 ]
|
||||
|
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