mirror of
https://github.com/boostorg/smart_ptr.git
synced 2025-11-03 09:11:55 +01:00
Merge [51909], [51912], [52937], [53672] to release.
[SVN r55479]
This commit is contained in:
@@ -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