mirror of
https://github.com/boostorg/smart_ptr.git
synced 2025-11-02 16:51:59 +01:00
Compare commits
16 Commits
boost-1.58
...
boost-1.60
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4db7219c32 | ||
|
|
3f17244225 | ||
|
|
ca93749614 | ||
|
|
a06123eb87 | ||
|
|
df90496583 | ||
|
|
20ead68473 | ||
|
|
79cde147c9 | ||
|
|
abbe975e8f | ||
|
|
8ba0730686 | ||
|
|
686efe100b | ||
|
|
acb880d8c2 | ||
|
|
1712b87cb6 | ||
|
|
f8943703f8 | ||
|
|
a42dda0af4 | ||
|
|
9b9b6d3ca6 | ||
|
|
d875a68ceb |
@@ -6,7 +6,8 @@
|
||||
// See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt
|
||||
|
||||
#if !defined( BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS ) && !defined( BOOST_NO_CXX11_NULLPTR )
|
||||
#if !defined( BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS ) && !defined( BOOST_NO_CXX11_NULLPTR )\
|
||||
&& !(defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x5130))
|
||||
|
||||
explicit operator bool () const BOOST_NOEXCEPT
|
||||
{
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
#include <boost/smart_ptr/bad_weak_ptr.hpp>
|
||||
#include <boost/smart_ptr/detail/sp_counted_base.hpp>
|
||||
#include <boost/smart_ptr/detail/sp_counted_impl.hpp>
|
||||
#include <boost/smart_ptr/detail/sp_disable_deprecated.hpp>
|
||||
#include <boost/detail/workaround.hpp>
|
||||
// In order to avoid circular dependencies with Boost.TR1
|
||||
// we make sure that our include of <memory> doesn't try to
|
||||
@@ -40,13 +41,23 @@
|
||||
# include <new> // std::bad_alloc
|
||||
#endif
|
||||
|
||||
#if !defined( BOOST_NO_CXX11_SMART_PTR )
|
||||
# include <boost/utility/addressof.hpp>
|
||||
#include <boost/core/addressof.hpp>
|
||||
|
||||
#if defined( BOOST_SP_DISABLE_DEPRECATED )
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
||||
#endif
|
||||
|
||||
namespace boost
|
||||
{
|
||||
|
||||
namespace movelib
|
||||
{
|
||||
|
||||
template< class T, class D > class unique_ptr;
|
||||
|
||||
} // namespace movelib
|
||||
|
||||
namespace detail
|
||||
{
|
||||
|
||||
@@ -63,8 +74,6 @@ template< class D > struct sp_inplace_tag
|
||||
{
|
||||
};
|
||||
|
||||
#if !defined( BOOST_NO_CXX11_SMART_PTR )
|
||||
|
||||
template< class T > class sp_reference_wrapper
|
||||
{
|
||||
public:
|
||||
@@ -93,8 +102,6 @@ template< class D > struct sp_convert_reference< D& >
|
||||
typedef sp_reference_wrapper< D > type;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
class weak_count;
|
||||
|
||||
class shared_count
|
||||
@@ -438,6 +445,29 @@ public:
|
||||
|
||||
#endif
|
||||
|
||||
template<class Y, class D>
|
||||
explicit shared_count( boost::movelib::unique_ptr<Y, D> & r ): pi_( 0 )
|
||||
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
|
||||
, id_(shared_count_id)
|
||||
#endif
|
||||
{
|
||||
typedef typename sp_convert_reference<D>::type D2;
|
||||
|
||||
D2 d2( r.get_deleter() );
|
||||
pi_ = new sp_counted_impl_pd< typename boost::movelib::unique_ptr<Y, D>::pointer, D2 >( r.get(), d2 );
|
||||
|
||||
#ifdef BOOST_NO_EXCEPTIONS
|
||||
|
||||
if( pi_ == 0 )
|
||||
{
|
||||
boost::throw_exception( std::bad_alloc() );
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
r.release();
|
||||
}
|
||||
|
||||
~shared_count() // nothrow
|
||||
{
|
||||
if( pi_ != 0 ) pi_->release();
|
||||
@@ -668,6 +698,10 @@ inline shared_count::shared_count( weak_count const & r, sp_nothrow_tag ): pi_(
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#if defined( BOOST_SP_DISABLE_DEPRECATED )
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
#ifdef __BORLANDC__
|
||||
# pragma warn .8027 // Functions containing try are not expanded inline
|
||||
#endif
|
||||
|
||||
40
include/boost/smart_ptr/detail/sp_disable_deprecated.hpp
Normal file
40
include/boost/smart_ptr/detail/sp_disable_deprecated.hpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#ifndef BOOST_SMART_PTR_DETAIL_SP_DISABLE_DEPRECATED_HPP_INCLUDED
|
||||
#define BOOST_SMART_PTR_DETAIL_SP_DISABLE_DEPRECATED_HPP_INCLUDED
|
||||
|
||||
// MS compatible compilers support #pragma once
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
|
||||
# pragma once
|
||||
#endif
|
||||
|
||||
//
|
||||
// boost/smart_ptr/detail/sp_disable_deprecated.hpp
|
||||
//
|
||||
// Copyright 2015 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/config.hpp>
|
||||
|
||||
#if defined( __GNUC__ ) && ( defined( __GXX_EXPERIMENTAL_CXX0X__ ) || ( __cplusplus >= 201103L ) )
|
||||
|
||||
# if defined( BOOST_GCC )
|
||||
|
||||
# if BOOST_GCC >= 40600
|
||||
# define BOOST_SP_DISABLE_DEPRECATED
|
||||
# endif
|
||||
|
||||
# elif defined( __clang__ ) && defined( __has_warning )
|
||||
|
||||
# if __has_warning( "-Wdeprecated-declarations" )
|
||||
# define BOOST_SP_DISABLE_DEPRECATED
|
||||
# endif
|
||||
|
||||
# endif
|
||||
|
||||
#endif
|
||||
|
||||
#endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_DISABLE_DEPRECATED_HPP_INCLUDED
|
||||
@@ -111,6 +111,17 @@ extern "C" long __cdecl _InterlockedCompareExchange( long volatile *, long, long
|
||||
extern "C" long __cdecl _InterlockedExchange( long volatile *, long );
|
||||
extern "C" long __cdecl _InterlockedExchangeAdd( long volatile *, long );
|
||||
|
||||
# if defined( BOOST_MSVC ) && BOOST_MSVC == 1310
|
||||
//From MSDN, Visual Studio .NET 2003 spedific: To declare one of the interlocked functions
|
||||
//for use as an intrinsic, the function must be declared with the leading underscore and
|
||||
//the new function must appear in a #pragma intrinsic statement.
|
||||
# pragma intrinsic( _InterlockedIncrement )
|
||||
# pragma intrinsic( _InterlockedDecrement )
|
||||
# pragma intrinsic( _InterlockedCompareExchange )
|
||||
# pragma intrinsic( _InterlockedExchange )
|
||||
# pragma intrinsic( _InterlockedExchangeAdd )
|
||||
# endif
|
||||
|
||||
#endif
|
||||
|
||||
# define BOOST_SP_INTERLOCKED_INCREMENT _InterlockedIncrement
|
||||
|
||||
@@ -82,6 +82,7 @@ public:
|
||||
{
|
||||
__asm__ __volatile__( BOOST_SP_ARM_BARRIER ::: "memory" );
|
||||
*const_cast< int volatile* >( &v_ ) = 0;
|
||||
__asm__ __volatile__( BOOST_SP_ARM_BARRIER ::: "memory" );
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
@@ -60,7 +60,16 @@ namespace detail
|
||||
{
|
||||
|
||||
#if !defined( BOOST_USE_WINDOWS_H ) && !BOOST_PLAT_WINDOWS_RUNTIME
|
||||
#if !BOOST_COMP_CLANG || !defined __MINGW32__
|
||||
extern "C" void __stdcall Sleep( unsigned long ms );
|
||||
#else
|
||||
#include <_mingw.h>
|
||||
#if !defined __MINGW64_VERSION_MAJOR
|
||||
extern "C" void __stdcall Sleep( unsigned long ms );
|
||||
#else
|
||||
extern "C" __declspec(dllimport) void __stdcall Sleep( unsigned long ms );
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
inline void yield( unsigned k )
|
||||
|
||||
@@ -15,12 +15,18 @@
|
||||
#include <boost/assert.hpp>
|
||||
#include <boost/checked_delete.hpp>
|
||||
#include <boost/smart_ptr/detail/sp_nullptr_t.hpp>
|
||||
#include <boost/smart_ptr/detail/sp_disable_deprecated.hpp>
|
||||
#include <boost/detail/workaround.hpp>
|
||||
|
||||
#ifndef BOOST_NO_AUTO_PTR
|
||||
# include <memory> // for std::auto_ptr
|
||||
#endif
|
||||
|
||||
#if defined( BOOST_SP_DISABLE_DEPRECATED )
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
||||
#endif
|
||||
|
||||
namespace boost
|
||||
{
|
||||
|
||||
@@ -154,4 +160,8 @@ template<class T> inline T * get_pointer(scoped_ptr<T> const & p) BOOST_NOEXCEPT
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#if defined( BOOST_SP_DISABLE_DEPRECATED )
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
#endif // #ifndef BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
#include <boost/detail/workaround.hpp>
|
||||
#include <boost/smart_ptr/detail/sp_convertible.hpp>
|
||||
#include <boost/smart_ptr/detail/sp_nullptr_t.hpp>
|
||||
#include <boost/smart_ptr/detail/sp_disable_deprecated.hpp>
|
||||
|
||||
#if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
|
||||
#include <boost/smart_ptr/detail/spinlock_pool.hpp>
|
||||
@@ -47,6 +48,11 @@
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined( BOOST_SP_DISABLE_DEPRECATED )
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
||||
#endif
|
||||
|
||||
namespace boost
|
||||
{
|
||||
|
||||
@@ -55,6 +61,13 @@ template<class T> class weak_ptr;
|
||||
template<class T> class enable_shared_from_this;
|
||||
class enable_shared_from_raw;
|
||||
|
||||
namespace movelib
|
||||
{
|
||||
|
||||
template< class T, class D > class unique_ptr;
|
||||
|
||||
} // namespace movelib
|
||||
|
||||
namespace detail
|
||||
{
|
||||
|
||||
@@ -495,6 +508,17 @@ public:
|
||||
|
||||
#endif
|
||||
|
||||
template< class Y, class D >
|
||||
shared_ptr( boost::movelib::unique_ptr< Y, D > r ): px( r.get() ), pn()
|
||||
{
|
||||
boost::detail::sp_assert_convertible< Y, T >();
|
||||
|
||||
typename boost::movelib::unique_ptr< Y, D >::pointer tmp = r.get();
|
||||
pn = boost::detail::shared_count( r );
|
||||
|
||||
boost::detail::sp_deleter_construct( this, tmp );
|
||||
}
|
||||
|
||||
// assignment
|
||||
|
||||
shared_ptr & operator=( shared_ptr const & r ) BOOST_NOEXCEPT
|
||||
@@ -556,6 +580,27 @@ public:
|
||||
|
||||
#endif
|
||||
|
||||
template<class Y, class D>
|
||||
shared_ptr & operator=( boost::movelib::unique_ptr<Y, D> r )
|
||||
{
|
||||
// this_type( static_cast< unique_ptr<Y, D> && >( r ) ).swap( *this );
|
||||
|
||||
boost::detail::sp_assert_convertible< Y, T >();
|
||||
|
||||
typename boost::movelib::unique_ptr< Y, D >::pointer p = r.get();
|
||||
|
||||
shared_ptr tmp;
|
||||
|
||||
tmp.px = p;
|
||||
tmp.pn = boost::detail::shared_count( r );
|
||||
|
||||
boost::detail::sp_deleter_construct( &tmp, p );
|
||||
|
||||
tmp.swap( *this );
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Move support
|
||||
|
||||
#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
|
||||
@@ -1025,4 +1070,8 @@ template< class T > std::size_t hash_value( boost::shared_ptr<T> const & p ) BOO
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#if defined( BOOST_SP_DISABLE_DEPRECATED )
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
#endif // #ifndef BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED
|
||||
|
||||
@@ -179,5 +179,7 @@ import testing ;
|
||||
|
||||
[ run weak_from_this_test.cpp ]
|
||||
[ run weak_from_this_test2.cpp ]
|
||||
|
||||
[ run sp_bml_unique_ptr_test.cpp ]
|
||||
;
|
||||
}
|
||||
|
||||
239
test/sp_bml_unique_ptr_test.cpp
Normal file
239
test/sp_bml_unique_ptr_test.cpp
Normal file
@@ -0,0 +1,239 @@
|
||||
//
|
||||
// sp_bml_unique_ptr_test.cpp
|
||||
//
|
||||
// Copyright (c) 2012, 2015 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/enable_shared_from_this.hpp>
|
||||
#include <boost/detail/lightweight_test.hpp>
|
||||
#include <boost/move/unique_ptr.hpp>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
struct X: public boost::enable_shared_from_this< X >
|
||||
{
|
||||
static int instances;
|
||||
|
||||
X()
|
||||
{
|
||||
++instances;
|
||||
}
|
||||
|
||||
~X()
|
||||
{
|
||||
--instances;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
X( X const & );
|
||||
X & operator=( X const & );
|
||||
};
|
||||
|
||||
int X::instances = 0;
|
||||
|
||||
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
|
||||
{
|
||||
void operator()( Y* p ) const
|
||||
{
|
||||
p->deleted_ = true;
|
||||
delete p;
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
BOOST_TEST( X::instances == 0 );
|
||||
|
||||
boost::movelib::unique_ptr<X> p( new X );
|
||||
BOOST_TEST( X::instances == 1 );
|
||||
|
||||
boost::shared_ptr<X> p2( boost::move( p ) );
|
||||
BOOST_TEST( X::instances == 1 );
|
||||
BOOST_TEST( p.get() == 0 );
|
||||
|
||||
boost::shared_ptr<X> p3 = p2->shared_from_this();
|
||||
BOOST_TEST( p2 == p3 );
|
||||
BOOST_TEST( !(p2 < p3) && !(p3 < p2) );
|
||||
|
||||
p2.reset();
|
||||
p3.reset();
|
||||
BOOST_TEST( X::instances == 0 );
|
||||
|
||||
p2 = boost::movelib::unique_ptr<X>( new X );
|
||||
BOOST_TEST( X::instances == 1 );
|
||||
|
||||
p2 = boost::movelib::unique_ptr<X>( new X );
|
||||
BOOST_TEST( X::instances == 1 );
|
||||
|
||||
p3 = p2->shared_from_this();
|
||||
BOOST_TEST( p2 == p3 );
|
||||
BOOST_TEST( !(p2 < p3) && !(p3 < p2) );
|
||||
|
||||
p2.reset();
|
||||
p3.reset();
|
||||
BOOST_TEST( X::instances == 0 );
|
||||
}
|
||||
|
||||
{
|
||||
BOOST_TEST( X::instances == 0 );
|
||||
|
||||
boost::movelib::unique_ptr<X> p( new X );
|
||||
BOOST_TEST( X::instances == 1 );
|
||||
|
||||
boost::shared_ptr<X const> p2( boost::move( p ) );
|
||||
BOOST_TEST( X::instances == 1 );
|
||||
BOOST_TEST( p.get() == 0 );
|
||||
|
||||
boost::shared_ptr<X const> p3 = p2->shared_from_this();
|
||||
BOOST_TEST( p2 == p3 );
|
||||
BOOST_TEST( !(p2 < p3) && !(p3 < p2) );
|
||||
|
||||
p2.reset();
|
||||
p3.reset();
|
||||
BOOST_TEST( X::instances == 0 );
|
||||
|
||||
p2 = boost::movelib::unique_ptr<X>( new X );
|
||||
BOOST_TEST( X::instances == 1 );
|
||||
|
||||
p2 = boost::movelib::unique_ptr<X>( new X );
|
||||
BOOST_TEST( X::instances == 1 );
|
||||
|
||||
p3 = p2->shared_from_this();
|
||||
BOOST_TEST( p2 == p3 );
|
||||
BOOST_TEST( !(p2 < p3) && !(p3 < p2) );
|
||||
|
||||
p2.reset();
|
||||
p3.reset();
|
||||
BOOST_TEST( X::instances == 0 );
|
||||
}
|
||||
|
||||
{
|
||||
BOOST_TEST( X::instances == 0 );
|
||||
|
||||
boost::movelib::unique_ptr<X> p( new X );
|
||||
BOOST_TEST( X::instances == 1 );
|
||||
|
||||
boost::shared_ptr<void> p2( boost::move( p ) );
|
||||
BOOST_TEST( X::instances == 1 );
|
||||
BOOST_TEST( p.get() == 0 );
|
||||
|
||||
p2.reset();
|
||||
BOOST_TEST( X::instances == 0 );
|
||||
|
||||
p2 = boost::movelib::unique_ptr<X>( new X );
|
||||
BOOST_TEST( X::instances == 1 );
|
||||
|
||||
p2 = boost::movelib::unique_ptr<X>( new X );
|
||||
BOOST_TEST( X::instances == 1 );
|
||||
|
||||
p2.reset();
|
||||
BOOST_TEST( X::instances == 0 );
|
||||
}
|
||||
|
||||
{
|
||||
BOOST_TEST( Y::instances == 0 );
|
||||
|
||||
boost::movelib::unique_ptr<Y, YD> p( new Y, YD() );
|
||||
BOOST_TEST( Y::instances == 1 );
|
||||
|
||||
boost::shared_ptr<Y> p2( boost::move( p ) );
|
||||
BOOST_TEST( Y::instances == 1 );
|
||||
BOOST_TEST( p.get() == 0 );
|
||||
|
||||
p2.reset();
|
||||
BOOST_TEST( Y::instances == 0 );
|
||||
|
||||
p2 = boost::movelib::unique_ptr<Y, YD>( new Y, YD() );
|
||||
BOOST_TEST( Y::instances == 1 );
|
||||
|
||||
p2 = boost::movelib::unique_ptr<Y, YD>( new Y, YD() );
|
||||
BOOST_TEST( Y::instances == 1 );
|
||||
|
||||
p2.reset();
|
||||
BOOST_TEST( Y::instances == 0 );
|
||||
}
|
||||
|
||||
{
|
||||
BOOST_TEST( Y::instances == 0 );
|
||||
|
||||
YD yd;
|
||||
|
||||
boost::movelib::unique_ptr<Y, YD&> p( new Y, yd );
|
||||
BOOST_TEST( Y::instances == 1 );
|
||||
|
||||
boost::shared_ptr<Y> p2( boost::move( p ) );
|
||||
BOOST_TEST( Y::instances == 1 );
|
||||
BOOST_TEST( p.get() == 0 );
|
||||
|
||||
p2.reset();
|
||||
BOOST_TEST( Y::instances == 0 );
|
||||
|
||||
p2 = boost::movelib::unique_ptr<Y, YD&>( new Y, yd );
|
||||
BOOST_TEST( Y::instances == 1 );
|
||||
|
||||
p2 = boost::movelib::unique_ptr<Y, YD&>( new Y, yd );
|
||||
BOOST_TEST( Y::instances == 1 );
|
||||
|
||||
p2.reset();
|
||||
BOOST_TEST( Y::instances == 0 );
|
||||
}
|
||||
|
||||
{
|
||||
BOOST_TEST( Y::instances == 0 );
|
||||
|
||||
YD yd;
|
||||
|
||||
boost::movelib::unique_ptr<Y, YD const&> p( new Y, yd );
|
||||
BOOST_TEST( Y::instances == 1 );
|
||||
|
||||
boost::shared_ptr<Y> p2( boost::move( p ) );
|
||||
BOOST_TEST( Y::instances == 1 );
|
||||
BOOST_TEST( p.get() == 0 );
|
||||
|
||||
p2.reset();
|
||||
BOOST_TEST( Y::instances == 0 );
|
||||
|
||||
p2 = boost::movelib::unique_ptr<Y, YD const&>( new Y, yd );
|
||||
BOOST_TEST( Y::instances == 1 );
|
||||
|
||||
p2 = boost::movelib::unique_ptr<Y, YD const&>( new Y, yd );
|
||||
BOOST_TEST( Y::instances == 1 );
|
||||
|
||||
p2.reset();
|
||||
BOOST_TEST( Y::instances == 0 );
|
||||
}
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
Reference in New Issue
Block a user