2009-03-01 16:00:42 +00:00
|
|
|
#ifndef BOOST_SMART_PTR_DETAIL_SHARED_COUNT_HPP_INCLUDED
|
|
|
|
#define BOOST_SMART_PTR_DETAIL_SHARED_COUNT_HPP_INCLUDED
|
2002-01-22 13:38:52 +00:00
|
|
|
|
2003-11-28 15:35:21 +00:00
|
|
|
// MS compatible compilers support #pragma once
|
|
|
|
|
2013-12-07 19:22:43 +02:00
|
|
|
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
|
2003-06-12 17:09:24 +00:00
|
|
|
# pragma once
|
2002-01-22 13:38:52 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
//
|
|
|
|
// detail/shared_count.hpp
|
|
|
|
//
|
2003-01-13 18:32:16 +00:00
|
|
|
// Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
|
2005-03-17 22:45:11 +00:00
|
|
|
// Copyright 2004-2005 Peter Dimov
|
2002-01-22 13:38:52 +00:00
|
|
|
//
|
2004-07-26 00:32:12 +00:00
|
|
|
// 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)
|
2002-01-22 13:38:52 +00:00
|
|
|
//
|
|
|
|
|
2009-03-01 16:00:42 +00:00
|
|
|
#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>
|
2015-09-10 23:45:47 +03:00
|
|
|
#include <boost/smart_ptr/detail/sp_disable_deprecated.hpp>
|
2019-01-28 18:46:39 +02:00
|
|
|
#include <boost/smart_ptr/detail/sp_noexcept.hpp>
|
2024-10-01 17:56:33 +03:00
|
|
|
#include <boost/smart_ptr/detail/deprecated_macros.hpp>
|
2022-04-03 16:49:10 +01:00
|
|
|
#include <boost/core/checked_delete.hpp>
|
2020-06-04 20:40:57 +03:00
|
|
|
#include <boost/throw_exception.hpp>
|
|
|
|
#include <boost/core/addressof.hpp>
|
|
|
|
#include <boost/config.hpp>
|
2019-01-28 18:46:39 +02:00
|
|
|
#include <boost/config/workaround.hpp>
|
2020-06-04 20:40:57 +03:00
|
|
|
#include <boost/cstdint.hpp>
|
|
|
|
#include <memory> // std::auto_ptr
|
|
|
|
#include <functional> // std::less
|
|
|
|
#include <cstddef> // std::size_t
|
2012-11-22 17:39:27 +00:00
|
|
|
|
|
|
|
#ifdef BOOST_NO_EXCEPTIONS
|
|
|
|
# include <new> // std::bad_alloc
|
|
|
|
#endif
|
2002-02-09 12:34:05 +00:00
|
|
|
|
2015-09-10 23:45:47 +03:00
|
|
|
#if defined( BOOST_SP_DISABLE_DEPRECATED )
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
|
|
|
#endif
|
|
|
|
|
2002-01-22 13:38:52 +00:00
|
|
|
namespace boost
|
|
|
|
{
|
|
|
|
|
2015-05-04 01:06:42 +03:00
|
|
|
namespace movelib
|
|
|
|
{
|
|
|
|
|
2017-06-18 02:43:20 +03:00
|
|
|
template< class T, class D > class unique_ptr;
|
2015-05-04 01:06:42 +03:00
|
|
|
|
|
|
|
} // namespace movelib
|
|
|
|
|
2002-11-18 14:37:02 +00:00
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
|
2003-02-10 12:54:43 +00:00
|
|
|
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
|
2002-11-19 16:11:21 +00:00
|
|
|
|
2003-01-07 15:34:56 +00:00
|
|
|
int const shared_count_id = 0x2C35F101;
|
|
|
|
int const weak_count_id = 0x298C38A4;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2008-04-12 14:27:22 +00:00
|
|
|
struct sp_nothrow_tag {};
|
|
|
|
|
2011-02-24 21:51:21 +00:00
|
|
|
template< class D > struct sp_inplace_tag
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2012-10-31 22:16:20 +00:00
|
|
|
template< class T > class sp_reference_wrapper
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
explicit sp_reference_wrapper( T & t): t_( boost::addressof( t ) )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template< class Y > void operator()( Y * p ) const
|
|
|
|
{
|
|
|
|
(*t_)( p );
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
T * t_;
|
|
|
|
};
|
|
|
|
|
|
|
|
template< class D > struct sp_convert_reference
|
|
|
|
{
|
|
|
|
typedef D type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template< class D > struct sp_convert_reference< D& >
|
|
|
|
{
|
|
|
|
typedef sp_reference_wrapper< D > type;
|
|
|
|
};
|
|
|
|
|
2020-06-04 20:40:57 +03:00
|
|
|
template<class T> std::size_t sp_hash_pointer( T* p ) BOOST_NOEXCEPT
|
|
|
|
{
|
|
|
|
boost::uintptr_t v = reinterpret_cast<boost::uintptr_t>( p );
|
|
|
|
|
|
|
|
// match boost::hash<T*>
|
|
|
|
return static_cast<std::size_t>( v + ( v >> 3 ) );
|
|
|
|
}
|
|
|
|
|
2002-02-14 23:08:30 +00:00
|
|
|
class weak_count;
|
2002-01-22 13:38:52 +00:00
|
|
|
|
|
|
|
class shared_count
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
2003-01-14 15:13:53 +00:00
|
|
|
sp_counted_base * pi_;
|
2002-01-22 13:38:52 +00:00
|
|
|
|
2003-02-10 12:54:43 +00:00
|
|
|
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
|
2003-01-07 15:34:56 +00:00
|
|
|
int id_;
|
|
|
|
#endif
|
|
|
|
|
2002-01-22 13:38:52 +00:00
|
|
|
friend class weak_count;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2019-01-28 18:46:39 +02:00
|
|
|
BOOST_CONSTEXPR shared_count() BOOST_SP_NOEXCEPT: pi_(0)
|
2003-02-10 12:54:43 +00:00
|
|
|
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
|
2003-01-07 15:34:56 +00:00
|
|
|
, id_(shared_count_id)
|
|
|
|
#endif
|
2002-02-15 13:31:58 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-28 18:46:39 +02:00
|
|
|
BOOST_CONSTEXPR explicit shared_count( sp_counted_base * pi ) BOOST_SP_NOEXCEPT: pi_( pi )
|
2017-06-22 15:24:49 +03:00
|
|
|
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
|
|
|
|
, id_(shared_count_id)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2005-03-17 23:30:47 +00:00
|
|
|
template<class Y> explicit shared_count( Y * p ): pi_( 0 )
|
|
|
|
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
|
|
|
|
, id_(shared_count_id)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
pi_ = new sp_counted_impl_p<Y>( p );
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
boost::checked_delete( p );
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
pi_ = new sp_counted_impl_p<Y>( p );
|
|
|
|
|
|
|
|
if( pi_ == 0 )
|
|
|
|
{
|
|
|
|
boost::checked_delete( p );
|
|
|
|
boost::throw_exception( std::bad_alloc() );
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-05-01 16:50:39 +00:00
|
|
|
template<class P, class D> shared_count( P p, D d ): pi_(0)
|
2003-02-10 12:54:43 +00:00
|
|
|
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
|
2003-01-07 15:34:56 +00:00
|
|
|
, id_(shared_count_id)
|
|
|
|
#endif
|
2002-01-22 13:38:52 +00:00
|
|
|
{
|
2002-08-14 12:27:22 +00:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
|
2002-01-22 13:38:52 +00:00
|
|
|
try
|
|
|
|
{
|
2005-03-17 23:05:26 +00:00
|
|
|
pi_ = new sp_counted_impl_pd<P, D>(p, d);
|
2002-01-22 13:38:52 +00:00
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
d(p); // delete p
|
|
|
|
throw;
|
|
|
|
}
|
2002-08-14 12:27:22 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2005-03-17 23:05:26 +00:00
|
|
|
pi_ = new sp_counted_impl_pd<P, D>(p, d);
|
2002-08-14 12:27:22 +00:00
|
|
|
|
|
|
|
if(pi_ == 0)
|
|
|
|
{
|
|
|
|
d(p); // delete p
|
|
|
|
boost::throw_exception(std::bad_alloc());
|
|
|
|
}
|
|
|
|
|
2005-11-09 20:05:42 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-02-24 21:51:21 +00:00
|
|
|
template< class P, class D > shared_count( P p, sp_inplace_tag<D> ): pi_( 0 )
|
|
|
|
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
|
|
|
|
, id_(shared_count_id)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
pi_ = new sp_counted_impl_pd< P, D >( p );
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
2013-02-28 08:02:09 +00:00
|
|
|
D::operator_fn( p ); // delete p
|
2011-02-24 21:51:21 +00:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
pi_ = new sp_counted_impl_pd< P, D >( p );
|
|
|
|
|
|
|
|
if( pi_ == 0 )
|
|
|
|
{
|
2013-02-28 08:02:09 +00:00
|
|
|
D::operator_fn( p ); // delete p
|
2011-02-24 21:51:21 +00:00
|
|
|
boost::throw_exception( std::bad_alloc() );
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // #ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
}
|
|
|
|
|
2005-11-09 20:05:42 +00:00
|
|
|
template<class P, class D, class A> shared_count( P p, D d, A a ): pi_( 0 )
|
|
|
|
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
|
|
|
|
, id_(shared_count_id)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
typedef sp_counted_impl_pda<P, D, A> impl_type;
|
2014-02-05 00:17:34 +02:00
|
|
|
|
|
|
|
typedef typename std::allocator_traits<A>::template rebind_alloc< impl_type > A2;
|
|
|
|
|
2005-11-09 20:05:42 +00:00
|
|
|
A2 a2( a );
|
|
|
|
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2017-02-28 07:29:21 -05:00
|
|
|
pi_ = a2.allocate( 1 );
|
2014-02-05 00:17:34 +02:00
|
|
|
::new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
|
2005-11-09 20:05:42 +00:00
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
d( p );
|
|
|
|
|
|
|
|
if( pi_ != 0 )
|
|
|
|
{
|
|
|
|
a2.deallocate( static_cast< impl_type* >( pi_ ), 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2014-02-05 00:17:34 +02:00
|
|
|
#else
|
|
|
|
|
2017-02-28 07:29:21 -05:00
|
|
|
pi_ = a2.allocate( 1 );
|
2014-02-05 00:17:34 +02:00
|
|
|
|
2005-11-09 20:05:42 +00:00
|
|
|
if( pi_ != 0 )
|
|
|
|
{
|
2014-02-05 00:17:34 +02:00
|
|
|
::new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
|
2005-11-09 20:05:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
d( p );
|
|
|
|
boost::throw_exception( std::bad_alloc() );
|
|
|
|
}
|
|
|
|
|
2002-08-14 12:27:22 +00:00
|
|
|
#endif
|
2002-01-22 13:38:52 +00:00
|
|
|
}
|
|
|
|
|
2011-02-24 21:51:21 +00:00
|
|
|
template< class P, class D, class A > shared_count( P p, sp_inplace_tag< D >, A a ): pi_( 0 )
|
|
|
|
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
|
|
|
|
, id_(shared_count_id)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
typedef sp_counted_impl_pda< P, D, A > impl_type;
|
2014-02-05 00:17:34 +02:00
|
|
|
|
|
|
|
typedef typename std::allocator_traits<A>::template rebind_alloc< impl_type > A2;
|
|
|
|
|
2011-02-24 21:51:21 +00:00
|
|
|
A2 a2( a );
|
|
|
|
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2017-02-28 07:29:21 -05:00
|
|
|
pi_ = a2.allocate( 1 );
|
2014-02-05 00:17:34 +02:00
|
|
|
::new( static_cast< void* >( pi_ ) ) impl_type( p, a );
|
2011-02-24 21:51:21 +00:00
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
2013-02-28 08:02:09 +00:00
|
|
|
D::operator_fn( p );
|
2011-02-24 21:51:21 +00:00
|
|
|
|
|
|
|
if( pi_ != 0 )
|
|
|
|
{
|
|
|
|
a2.deallocate( static_cast< impl_type* >( pi_ ), 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2014-02-05 00:17:34 +02:00
|
|
|
#else
|
|
|
|
|
2017-02-28 07:29:21 -05:00
|
|
|
pi_ = a2.allocate( 1 );
|
2014-02-05 00:17:34 +02:00
|
|
|
|
2011-02-24 21:51:21 +00:00
|
|
|
if( pi_ != 0 )
|
|
|
|
{
|
2014-02-05 00:17:34 +02:00
|
|
|
::new( static_cast< void* >( pi_ ) ) impl_type( p, a );
|
2011-02-24 21:51:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-02-28 08:02:09 +00:00
|
|
|
D::operator_fn( p );
|
2011-02-24 21:51:21 +00:00
|
|
|
boost::throw_exception( std::bad_alloc() );
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // #ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
}
|
|
|
|
|
2002-02-01 18:40:35 +00:00
|
|
|
#ifndef BOOST_NO_AUTO_PTR
|
|
|
|
|
|
|
|
// auto_ptr<Y> is special cased to provide the strong guarantee
|
|
|
|
|
2002-10-23 13:55:18 +00:00
|
|
|
template<class Y>
|
2005-03-17 23:30:47 +00:00
|
|
|
explicit shared_count( std::auto_ptr<Y> & r ): pi_( new sp_counted_impl_p<Y>( r.get() ) )
|
2003-02-10 12:54:43 +00:00
|
|
|
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
|
2003-01-07 15:34:56 +00:00
|
|
|
, id_(shared_count_id)
|
|
|
|
#endif
|
2002-02-01 18:40:35 +00:00
|
|
|
{
|
2005-03-17 23:30:47 +00:00
|
|
|
#ifdef BOOST_NO_EXCEPTIONS
|
|
|
|
|
|
|
|
if( pi_ == 0 )
|
|
|
|
{
|
|
|
|
boost::throw_exception(std::bad_alloc());
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2002-02-01 18:40:35 +00:00
|
|
|
r.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2012-10-31 22:16:20 +00:00
|
|
|
template<class Y, class D>
|
|
|
|
explicit shared_count( std::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;
|
|
|
|
|
2021-05-11 01:20:02 +03:00
|
|
|
D2 d2( static_cast<D&&>( r.get_deleter() ) );
|
2021-05-11 01:59:01 +03:00
|
|
|
pi_ = new sp_counted_impl_pd< typename std::unique_ptr<Y, D>::pointer, D2 >( r.get(), d2 );
|
2012-10-31 22:16:20 +00:00
|
|
|
|
|
|
|
#ifdef BOOST_NO_EXCEPTIONS
|
|
|
|
|
|
|
|
if( pi_ == 0 )
|
|
|
|
{
|
|
|
|
boost::throw_exception( std::bad_alloc() );
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
r.release();
|
|
|
|
}
|
|
|
|
|
2015-05-04 01:06:42 +03:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2019-01-28 18:46:39 +02:00
|
|
|
~shared_count() /*BOOST_SP_NOEXCEPT*/
|
2002-01-22 13:38:52 +00:00
|
|
|
{
|
2005-03-17 23:30:47 +00:00
|
|
|
if( pi_ != 0 ) pi_->release();
|
2003-02-10 12:54:43 +00:00
|
|
|
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
|
2003-01-27 14:02:00 +00:00
|
|
|
id_ = 0;
|
|
|
|
#endif
|
2002-01-22 13:38:52 +00:00
|
|
|
}
|
|
|
|
|
2019-01-28 18:46:39 +02:00
|
|
|
shared_count(shared_count const & r) BOOST_SP_NOEXCEPT: pi_(r.pi_)
|
2003-02-10 12:54:43 +00:00
|
|
|
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
|
2003-01-07 15:34:56 +00:00
|
|
|
, id_(shared_count_id)
|
|
|
|
#endif
|
2002-01-22 13:38:52 +00:00
|
|
|
{
|
2005-03-17 23:30:47 +00:00
|
|
|
if( pi_ != 0 ) pi_->add_ref_copy();
|
2002-01-22 13:38:52 +00:00
|
|
|
}
|
|
|
|
|
2019-01-28 18:46:39 +02:00
|
|
|
shared_count(shared_count && r) BOOST_SP_NOEXCEPT: pi_(r.pi_)
|
2008-04-22 19:48:39 +00:00
|
|
|
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
|
|
|
|
, id_(shared_count_id)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
r.pi_ = 0;
|
|
|
|
}
|
|
|
|
|
2002-11-18 14:37:02 +00:00
|
|
|
explicit shared_count(weak_count const & r); // throws bad_weak_ptr when r.use_count() == 0
|
2019-01-28 18:46:39 +02:00
|
|
|
shared_count( weak_count const & r, sp_nothrow_tag ) BOOST_SP_NOEXCEPT; // constructs an empty *this when r.use_count() == 0
|
2002-02-12 16:55:25 +00:00
|
|
|
|
2019-01-28 18:46:39 +02:00
|
|
|
shared_count & operator= (shared_count const & r) BOOST_SP_NOEXCEPT
|
2002-01-22 13:38:52 +00:00
|
|
|
{
|
2003-01-14 15:13:53 +00:00
|
|
|
sp_counted_base * tmp = r.pi_;
|
2004-02-10 23:17:12 +00:00
|
|
|
|
2005-03-17 23:30:47 +00:00
|
|
|
if( tmp != pi_ )
|
2004-02-10 23:17:12 +00:00
|
|
|
{
|
2005-03-17 23:30:47 +00:00
|
|
|
if( tmp != 0 ) tmp->add_ref_copy();
|
|
|
|
if( pi_ != 0 ) pi_->release();
|
2004-02-10 23:17:12 +00:00
|
|
|
pi_ = tmp;
|
|
|
|
}
|
2002-01-22 13:38:52 +00:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-01-28 18:46:39 +02:00
|
|
|
void swap(shared_count & r) BOOST_SP_NOEXCEPT
|
2002-01-22 13:38:52 +00:00
|
|
|
{
|
2003-01-14 15:13:53 +00:00
|
|
|
sp_counted_base * tmp = r.pi_;
|
2002-01-22 13:38:52 +00:00
|
|
|
r.pi_ = pi_;
|
|
|
|
pi_ = tmp;
|
|
|
|
}
|
|
|
|
|
2019-01-28 18:46:39 +02:00
|
|
|
long use_count() const BOOST_SP_NOEXCEPT
|
2002-01-22 13:38:52 +00:00
|
|
|
{
|
2003-01-13 18:32:16 +00:00
|
|
|
return pi_ != 0? pi_->use_count(): 0;
|
2002-01-22 13:38:52 +00:00
|
|
|
}
|
|
|
|
|
2019-01-28 18:46:39 +02:00
|
|
|
bool unique() const BOOST_SP_NOEXCEPT
|
2002-01-22 13:38:52 +00:00
|
|
|
{
|
2002-11-18 14:37:02 +00:00
|
|
|
return use_count() == 1;
|
2002-01-22 13:38:52 +00:00
|
|
|
}
|
2002-02-09 12:34:05 +00:00
|
|
|
|
2019-01-28 18:46:39 +02:00
|
|
|
bool empty() const BOOST_SP_NOEXCEPT
|
2008-04-12 14:27:22 +00:00
|
|
|
{
|
|
|
|
return pi_ == 0;
|
|
|
|
}
|
|
|
|
|
2020-05-31 22:07:36 +03:00
|
|
|
bool operator==( shared_count const & r ) const BOOST_SP_NOEXCEPT
|
2002-02-09 12:34:05 +00:00
|
|
|
{
|
2020-05-31 22:07:36 +03:00
|
|
|
return pi_ == r.pi_;
|
2002-02-09 12:34:05 +00:00
|
|
|
}
|
|
|
|
|
2020-05-31 22:07:36 +03:00
|
|
|
bool operator==( weak_count const & r ) const BOOST_SP_NOEXCEPT;
|
|
|
|
|
2020-05-31 21:12:12 +03:00
|
|
|
bool operator<( shared_count const & r ) const BOOST_SP_NOEXCEPT
|
2002-02-09 12:34:05 +00:00
|
|
|
{
|
2020-05-31 21:12:12 +03:00
|
|
|
return std::less<sp_counted_base *>()( pi_, r.pi_ );
|
2002-02-09 12:34:05 +00:00
|
|
|
}
|
2002-11-21 14:46:45 +00:00
|
|
|
|
2020-05-31 21:12:12 +03:00
|
|
|
bool operator<( weak_count const & r ) const BOOST_SP_NOEXCEPT;
|
|
|
|
|
2019-04-22 05:25:07 +03:00
|
|
|
void * get_deleter( sp_typeinfo_ const & ti ) const BOOST_SP_NOEXCEPT
|
2002-11-21 14:46:45 +00:00
|
|
|
{
|
2005-03-17 23:30:47 +00:00
|
|
|
return pi_? pi_->get_deleter( ti ): 0;
|
2002-11-21 14:46:45 +00:00
|
|
|
}
|
2012-12-11 18:21:29 +00:00
|
|
|
|
2019-04-22 05:25:07 +03:00
|
|
|
void * get_local_deleter( sp_typeinfo_ const & ti ) const BOOST_SP_NOEXCEPT
|
2017-06-20 19:01:16 +03:00
|
|
|
{
|
|
|
|
return pi_? pi_->get_local_deleter( ti ): 0;
|
|
|
|
}
|
|
|
|
|
2019-01-28 18:46:39 +02:00
|
|
|
void * get_untyped_deleter() const BOOST_SP_NOEXCEPT
|
2012-12-11 18:21:29 +00:00
|
|
|
{
|
|
|
|
return pi_? pi_->get_untyped_deleter(): 0;
|
|
|
|
}
|
2020-06-04 20:40:57 +03:00
|
|
|
|
|
|
|
std::size_t hash_value() const BOOST_SP_NOEXCEPT
|
|
|
|
{
|
|
|
|
return sp_hash_pointer( pi_ );
|
|
|
|
}
|
2002-01-22 13:38:52 +00:00
|
|
|
};
|
|
|
|
|
2002-08-20 11:08:11 +00:00
|
|
|
|
2002-01-22 13:38:52 +00:00
|
|
|
class weak_count
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
2003-01-14 15:13:53 +00:00
|
|
|
sp_counted_base * pi_;
|
2002-01-22 13:38:52 +00:00
|
|
|
|
2003-02-10 12:54:43 +00:00
|
|
|
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
|
2003-01-07 15:34:56 +00:00
|
|
|
int id_;
|
|
|
|
#endif
|
|
|
|
|
2002-02-12 16:55:25 +00:00
|
|
|
friend class shared_count;
|
|
|
|
|
2002-01-22 13:38:52 +00:00
|
|
|
public:
|
|
|
|
|
2019-01-28 18:46:39 +02:00
|
|
|
BOOST_CONSTEXPR weak_count() BOOST_SP_NOEXCEPT: pi_(0)
|
2003-02-10 12:54:43 +00:00
|
|
|
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
|
2003-01-07 15:34:56 +00:00
|
|
|
, id_(weak_count_id)
|
|
|
|
#endif
|
2002-01-22 13:38:52 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-28 18:46:39 +02:00
|
|
|
weak_count(shared_count const & r) BOOST_SP_NOEXCEPT: pi_(r.pi_)
|
2003-02-10 12:54:43 +00:00
|
|
|
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
|
2009-03-01 17:10:49 +00:00
|
|
|
, id_(weak_count_id)
|
2003-01-07 15:34:56 +00:00
|
|
|
#endif
|
2002-01-22 13:38:52 +00:00
|
|
|
{
|
2002-11-18 14:37:02 +00:00
|
|
|
if(pi_ != 0) pi_->weak_add_ref();
|
2002-01-22 13:38:52 +00:00
|
|
|
}
|
|
|
|
|
2019-01-28 18:46:39 +02:00
|
|
|
weak_count(weak_count const & r) BOOST_SP_NOEXCEPT: pi_(r.pi_)
|
2003-02-10 12:54:43 +00:00
|
|
|
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
|
2009-03-01 17:10:49 +00:00
|
|
|
, id_(weak_count_id)
|
2003-01-07 15:34:56 +00:00
|
|
|
#endif
|
2002-01-22 13:38:52 +00:00
|
|
|
{
|
2002-11-18 14:37:02 +00:00
|
|
|
if(pi_ != 0) pi_->weak_add_ref();
|
2002-01-22 13:38:52 +00:00
|
|
|
}
|
|
|
|
|
2009-05-12 16:18:15 +00:00
|
|
|
// Move support
|
|
|
|
|
2019-01-28 18:46:39 +02:00
|
|
|
weak_count(weak_count && r) BOOST_SP_NOEXCEPT: pi_(r.pi_)
|
2009-05-12 16:18:15 +00:00
|
|
|
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
|
|
|
|
, id_(weak_count_id)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
r.pi_ = 0;
|
|
|
|
}
|
|
|
|
|
2019-01-28 18:46:39 +02:00
|
|
|
~weak_count() /*BOOST_SP_NOEXCEPT*/
|
2002-01-22 13:38:52 +00:00
|
|
|
{
|
2002-11-18 14:37:02 +00:00
|
|
|
if(pi_ != 0) pi_->weak_release();
|
2003-02-10 12:54:43 +00:00
|
|
|
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
|
2003-01-27 14:02:00 +00:00
|
|
|
id_ = 0;
|
|
|
|
#endif
|
2002-01-22 13:38:52 +00:00
|
|
|
}
|
|
|
|
|
2019-01-28 18:46:39 +02:00
|
|
|
weak_count & operator= (shared_count const & r) BOOST_SP_NOEXCEPT
|
2002-01-22 13:38:52 +00:00
|
|
|
{
|
2003-01-14 15:13:53 +00:00
|
|
|
sp_counted_base * tmp = r.pi_;
|
2008-04-22 06:31:32 +00:00
|
|
|
|
|
|
|
if( tmp != pi_ )
|
|
|
|
{
|
|
|
|
if(tmp != 0) tmp->weak_add_ref();
|
|
|
|
if(pi_ != 0) pi_->weak_release();
|
|
|
|
pi_ = tmp;
|
|
|
|
}
|
2002-01-22 13:38:52 +00:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-01-28 18:46:39 +02:00
|
|
|
weak_count & operator= (weak_count const & r) BOOST_SP_NOEXCEPT
|
2002-01-22 13:38:52 +00:00
|
|
|
{
|
2003-01-14 15:13:53 +00:00
|
|
|
sp_counted_base * tmp = r.pi_;
|
2008-04-22 06:31:32 +00:00
|
|
|
|
|
|
|
if( tmp != pi_ )
|
|
|
|
{
|
|
|
|
if(tmp != 0) tmp->weak_add_ref();
|
|
|
|
if(pi_ != 0) pi_->weak_release();
|
|
|
|
pi_ = tmp;
|
|
|
|
}
|
2002-01-22 13:38:52 +00:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-01-28 18:46:39 +02:00
|
|
|
void swap(weak_count & r) BOOST_SP_NOEXCEPT
|
2002-01-22 13:38:52 +00:00
|
|
|
{
|
2003-01-14 15:13:53 +00:00
|
|
|
sp_counted_base * tmp = r.pi_;
|
2002-01-22 13:38:52 +00:00
|
|
|
r.pi_ = pi_;
|
|
|
|
pi_ = tmp;
|
|
|
|
}
|
|
|
|
|
2019-01-28 18:46:39 +02:00
|
|
|
long use_count() const BOOST_SP_NOEXCEPT
|
2002-01-22 13:38:52 +00:00
|
|
|
{
|
2002-11-21 13:14:04 +00:00
|
|
|
return pi_ != 0? pi_->use_count(): 0;
|
2002-01-22 13:38:52 +00:00
|
|
|
}
|
2002-02-09 12:34:05 +00:00
|
|
|
|
2019-01-28 18:46:39 +02:00
|
|
|
bool empty() const BOOST_SP_NOEXCEPT
|
2008-04-26 19:59:11 +00:00
|
|
|
{
|
|
|
|
return pi_ == 0;
|
|
|
|
}
|
|
|
|
|
2020-05-31 22:07:36 +03:00
|
|
|
bool operator==( weak_count const & r ) const BOOST_SP_NOEXCEPT
|
|
|
|
{
|
|
|
|
return pi_ == r.pi_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==( shared_count const & r ) const BOOST_SP_NOEXCEPT
|
2002-02-09 12:34:05 +00:00
|
|
|
{
|
2020-05-31 22:07:36 +03:00
|
|
|
return pi_ == r.pi_;
|
2002-02-09 12:34:05 +00:00
|
|
|
}
|
|
|
|
|
2020-05-31 21:12:12 +03:00
|
|
|
bool operator<( weak_count const & r ) const BOOST_SP_NOEXCEPT
|
|
|
|
{
|
|
|
|
return std::less<sp_counted_base *>()( pi_, r.pi_ );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<( shared_count const & r ) const BOOST_SP_NOEXCEPT
|
2002-02-09 12:34:05 +00:00
|
|
|
{
|
2020-05-31 21:12:12 +03:00
|
|
|
return std::less<sp_counted_base *>()( pi_, r.pi_ );
|
2002-02-09 12:34:05 +00:00
|
|
|
}
|
2020-06-04 20:40:57 +03:00
|
|
|
|
|
|
|
std::size_t hash_value() const BOOST_SP_NOEXCEPT
|
|
|
|
{
|
|
|
|
return sp_hash_pointer( pi_ );
|
|
|
|
}
|
2002-01-22 13:38:52 +00:00
|
|
|
};
|
|
|
|
|
2005-03-17 23:30:47 +00:00
|
|
|
inline shared_count::shared_count( weak_count const & r ): pi_( r.pi_ )
|
2003-02-10 12:54:43 +00:00
|
|
|
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
|
2003-01-07 15:34:56 +00:00
|
|
|
, id_(shared_count_id)
|
|
|
|
#endif
|
2002-02-12 16:55:25 +00:00
|
|
|
{
|
2005-03-17 22:45:11 +00:00
|
|
|
if( pi_ == 0 || !pi_->add_ref_lock() )
|
2002-11-21 13:14:04 +00:00
|
|
|
{
|
2005-03-17 22:45:11 +00:00
|
|
|
boost::throw_exception( boost::bad_weak_ptr() );
|
2002-11-21 13:14:04 +00:00
|
|
|
}
|
2002-02-12 16:55:25 +00:00
|
|
|
}
|
|
|
|
|
2019-01-28 18:46:39 +02:00
|
|
|
inline shared_count::shared_count( weak_count const & r, sp_nothrow_tag ) BOOST_SP_NOEXCEPT: pi_( r.pi_ )
|
2008-04-12 14:27:22 +00:00
|
|
|
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
|
|
|
|
, id_(shared_count_id)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if( pi_ != 0 && !pi_->add_ref_lock() )
|
|
|
|
{
|
|
|
|
pi_ = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-31 22:07:36 +03:00
|
|
|
inline bool shared_count::operator==( weak_count const & r ) const BOOST_SP_NOEXCEPT
|
|
|
|
{
|
|
|
|
return pi_ == r.pi_;
|
|
|
|
}
|
|
|
|
|
2020-05-31 21:12:12 +03:00
|
|
|
inline bool shared_count::operator<( weak_count const & r ) const BOOST_SP_NOEXCEPT
|
|
|
|
{
|
|
|
|
return std::less<sp_counted_base *>()( pi_, r.pi_ );
|
|
|
|
}
|
|
|
|
|
2002-01-22 13:38:52 +00:00
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
} // namespace boost
|
|
|
|
|
2015-09-10 23:45:47 +03:00
|
|
|
#if defined( BOOST_SP_DISABLE_DEPRECATED )
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
2009-03-01 16:00:42 +00:00
|
|
|
#endif // #ifndef BOOST_SMART_PTR_DETAIL_SHARED_COUNT_HPP_INCLUDED
|