forked from boostorg/smart_ptr
Move the unique_ptr deleter instead of copying it
This commit is contained in:
@ -387,8 +387,8 @@ public:
|
|||||||
{
|
{
|
||||||
typedef typename sp_convert_reference<D>::type D2;
|
typedef typename sp_convert_reference<D>::type D2;
|
||||||
|
|
||||||
D2 d2( r.get_deleter() );
|
D2 d2( static_cast<D&&>( r.get_deleter() ) );
|
||||||
pi_ = new sp_counted_impl_pd< typename std::unique_ptr<Y, D>::pointer, D2 >( r.get(), d2 );
|
pi_ = new sp_counted_impl_pd< typename std::unique_ptr<Y, D>::pointer, D2 >( r.get(), static_cast< D2&& >( d2 ) );
|
||||||
|
|
||||||
#ifdef BOOST_NO_EXCEPTIONS
|
#ifdef BOOST_NO_EXCEPTIONS
|
||||||
|
|
||||||
|
@ -145,7 +145,7 @@ template<class P, class D> class BOOST_SYMBOL_VISIBLE sp_counted_impl_pd: public
|
|||||||
private:
|
private:
|
||||||
|
|
||||||
P ptr; // copy constructor must not throw
|
P ptr; // copy constructor must not throw
|
||||||
D del; // copy constructor must not throw
|
D del; // copy/move constructor must not throw
|
||||||
|
|
||||||
sp_counted_impl_pd( sp_counted_impl_pd const & );
|
sp_counted_impl_pd( sp_counted_impl_pd const & );
|
||||||
sp_counted_impl_pd & operator= ( sp_counted_impl_pd const & );
|
sp_counted_impl_pd & operator= ( sp_counted_impl_pd const & );
|
||||||
@ -160,6 +160,14 @@ public:
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
|
||||||
|
|
||||||
|
sp_counted_impl_pd( P p, D && d ): ptr( p ), del( static_cast< D&& >( d ) )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
sp_counted_impl_pd( P p ): ptr( p ), del()
|
sp_counted_impl_pd( P p ): ptr( p ), del()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -408,3 +408,5 @@ run wp_unordered_test.cpp ;
|
|||||||
|
|
||||||
run owner_hash_test.cpp ;
|
run owner_hash_test.cpp ;
|
||||||
run sp_unordered_test.cpp ;
|
run sp_unordered_test.cpp ;
|
||||||
|
|
||||||
|
run sp_unique_ptr_test2.cpp ;
|
||||||
|
101
test/sp_unique_ptr_test2.cpp
Normal file
101
test/sp_unique_ptr_test2.cpp
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
// Copyright 2021 Peter Dimov
|
||||||
|
// Distributed under the Boost Software License, Version 1.0.
|
||||||
|
// https://www.boost.org/LICENSE_1_0.txt
|
||||||
|
|
||||||
|
#include <boost/shared_ptr.hpp>
|
||||||
|
#include <boost/core/lightweight_test.hpp>
|
||||||
|
#include <boost/config.hpp>
|
||||||
|
#include <boost/config/pragma_message.hpp>
|
||||||
|
#include <memory>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
#if defined( BOOST_NO_CXX11_SMART_PTR )
|
||||||
|
|
||||||
|
BOOST_PRAGMA_MESSAGE("Skipping test because BOOST_NO_CXX11_SMART_PTR is defined")
|
||||||
|
int main() {}
|
||||||
|
|
||||||
|
#elif defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
|
||||||
|
|
||||||
|
BOOST_PRAGMA_MESSAGE("Skipping test because BOOST_NO_CXX11_RVALUE_REFERENCES is defined")
|
||||||
|
int main() {}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
struct Y
|
||||||
|
{
|
||||||
|
static int instances;
|
||||||
|
|
||||||
|
bool deleted_;
|
||||||
|
|
||||||
|
Y(): deleted_( false )
|
||||||
|
{
|
||||||
|
++instances;
|
||||||
|
}
|
||||||
|
|
||||||
|
~Y()
|
||||||
|
{
|
||||||
|
BOOST_TEST( deleted_ );
|
||||||
|
--instances;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
Y( Y const & );
|
||||||
|
Y & operator=( Y const & );
|
||||||
|
};
|
||||||
|
|
||||||
|
int Y::instances = 0;
|
||||||
|
|
||||||
|
struct YD
|
||||||
|
{
|
||||||
|
bool moved_;
|
||||||
|
|
||||||
|
YD(): moved_( false )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
YD( YD&& r ): moved_( false )
|
||||||
|
{
|
||||||
|
r.moved_ = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator()( Y* p ) const
|
||||||
|
{
|
||||||
|
BOOST_TEST( !moved_ );
|
||||||
|
|
||||||
|
if( p )
|
||||||
|
{
|
||||||
|
p->deleted_ = true;
|
||||||
|
delete p;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
BOOST_ERROR( "YD::operator()(0) called" );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
YD( YD const & );
|
||||||
|
YD & operator=( YD const & );
|
||||||
|
};
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
BOOST_TEST( Y::instances == 0 );
|
||||||
|
|
||||||
|
std::unique_ptr<Y, YD> p( new Y );
|
||||||
|
BOOST_TEST( Y::instances == 1 );
|
||||||
|
|
||||||
|
boost::shared_ptr<Y> p2( std::move( p ) );
|
||||||
|
BOOST_TEST( Y::instances == 1 );
|
||||||
|
BOOST_TEST( p.get() == 0 );
|
||||||
|
BOOST_TEST( p.get_deleter().moved_ );
|
||||||
|
|
||||||
|
p2.reset();
|
||||||
|
BOOST_TEST( Y::instances == 0 );
|
||||||
|
|
||||||
|
return boost::report_errors();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
Reference in New Issue
Block a user