1 #ifndef BOOST_SMART_PTR_DETAIL_SHARED_COUNT_HPP_INCLUDED
2 #define BOOST_SMART_PTR_DETAIL_SHARED_COUNT_HPP_INCLUDED
4 // MS compatible compilers support #pragma once
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
11 // detail/shared_count.hpp
13 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
14 // Copyright 2004-2005 Peter Dimov
16 // Distributed under the Boost Software License, Version 1.0. (See
17 // accompanying file LICENSE_1_0.txt or copy at
18 // http://www.boost.org/LICENSE_1_0.txt)
22 # pragma warn -8027 // Functions containing try are not expanded inline
25 #include <boost/config.hpp>
26 #include <boost/checked_delete.hpp>
27 #include <boost/throw_exception.hpp>
28 #include <boost/smart_ptr/bad_weak_ptr.hpp>
29 #include <boost/smart_ptr/detail/sp_counted_base.hpp>
30 #include <boost/smart_ptr/detail/sp_counted_impl.hpp>
31 #include <boost/smart_ptr/detail/sp_disable_deprecated.hpp>
32 #include <boost/smart_ptr/detail/sp_noexcept.hpp>
33 #include <boost/config/workaround.hpp>
34 // In order to avoid circular dependencies with Boost.TR1
35 // we make sure that our include of <memory> doesn't try to
36 // pull in the TR1 headers: that's why we use this header
37 // rather than including <memory> directly:
38 #include <boost/config/no_tr1/memory.hpp> // std::auto_ptr
39 #include <functional> // std::less
41 #ifdef BOOST_NO_EXCEPTIONS
42 # include <new> // std::bad_alloc
45 #include <boost/core/addressof.hpp>
47 #if defined( BOOST_SP_DISABLE_DEPRECATED )
48 #pragma GCC diagnostic push
49 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
58 template< class T, class D > class unique_ptr;
60 } // namespace movelib
65 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
67 int const shared_count_id = 0x2C35F101;
68 int const weak_count_id = 0x298C38A4;
72 struct sp_nothrow_tag {};
74 template< class D > struct sp_inplace_tag
78 template< class T > class sp_reference_wrapper
82 explicit sp_reference_wrapper( T & t): t_( boost::addressof( t ) )
86 template< class Y > void operator()( Y * p ) const
96 template< class D > struct sp_convert_reference
101 template< class D > struct sp_convert_reference< D& >
103 typedef sp_reference_wrapper< D > type;
112 sp_counted_base * pi_;
114 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
118 friend class weak_count;
122 BOOST_CONSTEXPR shared_count() BOOST_SP_NOEXCEPT: pi_(0)
123 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
124 , id_(shared_count_id)
129 BOOST_CONSTEXPR explicit shared_count( sp_counted_base * pi ) BOOST_SP_NOEXCEPT: pi_( pi )
130 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
131 , id_(shared_count_id)
136 template<class Y> explicit shared_count( Y * p ): pi_( 0 )
137 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
138 , id_(shared_count_id)
141 #ifndef BOOST_NO_EXCEPTIONS
145 pi_ = new sp_counted_impl_p<Y>( p );
149 boost::checked_delete( p );
155 pi_ = new sp_counted_impl_p<Y>( p );
159 boost::checked_delete( p );
160 boost::throw_exception( std::bad_alloc() );
166 #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, <= 1200 )
167 template<class Y, class D> shared_count( Y * p, D d ): pi_(0)
169 template<class P, class D> shared_count( P p, D d ): pi_(0)
171 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
172 , id_(shared_count_id)
175 #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, <= 1200 )
178 #ifndef BOOST_NO_EXCEPTIONS
182 pi_ = new sp_counted_impl_pd<P, D>(p, d);
192 pi_ = new sp_counted_impl_pd<P, D>(p, d);
197 boost::throw_exception(std::bad_alloc());
203 #if !defined( BOOST_NO_FUNCTION_TEMPLATE_ORDERING )
205 template< class P, class D > shared_count( P p, sp_inplace_tag<D> ): pi_( 0 )
206 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
207 , id_(shared_count_id)
210 #ifndef BOOST_NO_EXCEPTIONS
214 pi_ = new sp_counted_impl_pd< P, D >( p );
218 D::operator_fn( p ); // delete p
224 pi_ = new sp_counted_impl_pd< P, D >( p );
228 D::operator_fn( p ); // delete p
229 boost::throw_exception( std::bad_alloc() );
232 #endif // #ifndef BOOST_NO_EXCEPTIONS
235 #endif // !defined( BOOST_NO_FUNCTION_TEMPLATE_ORDERING )
237 template<class P, class D, class A> shared_count( P p, D d, A a ): pi_( 0 )
238 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
239 , id_(shared_count_id)
242 typedef sp_counted_impl_pda<P, D, A> impl_type;
244 #if !defined( BOOST_NO_CXX11_ALLOCATOR )
246 typedef typename std::allocator_traits<A>::template rebind_alloc< impl_type > A2;
250 typedef typename A::template rebind< impl_type >::other A2;
256 #ifndef BOOST_NO_EXCEPTIONS
260 pi_ = a2.allocate( 1 );
261 ::new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
269 a2.deallocate( static_cast< impl_type* >( pi_ ), 1 );
277 pi_ = a2.allocate( 1 );
281 ::new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
286 boost::throw_exception( std::bad_alloc() );
292 #if !defined( BOOST_NO_FUNCTION_TEMPLATE_ORDERING )
294 template< class P, class D, class A > shared_count( P p, sp_inplace_tag< D >, A a ): pi_( 0 )
295 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
296 , id_(shared_count_id)
299 typedef sp_counted_impl_pda< P, D, A > impl_type;
301 #if !defined( BOOST_NO_CXX11_ALLOCATOR )
303 typedef typename std::allocator_traits<A>::template rebind_alloc< impl_type > A2;
307 typedef typename A::template rebind< impl_type >::other A2;
313 #ifndef BOOST_NO_EXCEPTIONS
317 pi_ = a2.allocate( 1 );
318 ::new( static_cast< void* >( pi_ ) ) impl_type( p, a );
326 a2.deallocate( static_cast< impl_type* >( pi_ ), 1 );
334 pi_ = a2.allocate( 1 );
338 ::new( static_cast< void* >( pi_ ) ) impl_type( p, a );
343 boost::throw_exception( std::bad_alloc() );
346 #endif // #ifndef BOOST_NO_EXCEPTIONS
349 #endif // !defined( BOOST_NO_FUNCTION_TEMPLATE_ORDERING )
351 #ifndef BOOST_NO_AUTO_PTR
353 // auto_ptr<Y> is special cased to provide the strong guarantee
356 explicit shared_count( std::auto_ptr<Y> & r ): pi_( new sp_counted_impl_p<Y>( r.get() ) )
357 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
358 , id_(shared_count_id)
361 #ifdef BOOST_NO_EXCEPTIONS
365 boost::throw_exception(std::bad_alloc());
375 #if !defined( BOOST_NO_CXX11_SMART_PTR )
377 template<class Y, class D>
378 explicit shared_count( std::unique_ptr<Y, D> & r ): pi_( 0 )
379 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
380 , id_(shared_count_id)
383 typedef typename sp_convert_reference<D>::type D2;
385 D2 d2( r.get_deleter() );
386 pi_ = new sp_counted_impl_pd< typename std::unique_ptr<Y, D>::pointer, D2 >( r.get(), d2 );
388 #ifdef BOOST_NO_EXCEPTIONS
392 boost::throw_exception( std::bad_alloc() );
402 template<class Y, class D>
403 explicit shared_count( boost::movelib::unique_ptr<Y, D> & r ): pi_( 0 )
404 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
405 , id_(shared_count_id)
408 typedef typename sp_convert_reference<D>::type D2;
410 D2 d2( r.get_deleter() );
411 pi_ = new sp_counted_impl_pd< typename boost::movelib::unique_ptr<Y, D>::pointer, D2 >( r.get(), d2 );
413 #ifdef BOOST_NO_EXCEPTIONS
417 boost::throw_exception( std::bad_alloc() );
425 ~shared_count() /*BOOST_SP_NOEXCEPT*/
427 if( pi_ != 0 ) pi_->release();
428 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
433 shared_count(shared_count const & r) BOOST_SP_NOEXCEPT: pi_(r.pi_)
434 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
435 , id_(shared_count_id)
438 if( pi_ != 0 ) pi_->add_ref_copy();
441 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
443 shared_count(shared_count && r) BOOST_SP_NOEXCEPT: pi_(r.pi_)
444 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
445 , id_(shared_count_id)
453 explicit shared_count(weak_count const & r); // throws bad_weak_ptr when r.use_count() == 0
454 shared_count( weak_count const & r, sp_nothrow_tag ) BOOST_SP_NOEXCEPT; // constructs an empty *this when r.use_count() == 0
456 shared_count & operator= (shared_count const & r) BOOST_SP_NOEXCEPT
458 sp_counted_base * tmp = r.pi_;
462 if( tmp != 0 ) tmp->add_ref_copy();
463 if( pi_ != 0 ) pi_->release();
470 void swap(shared_count & r) BOOST_SP_NOEXCEPT
472 sp_counted_base * tmp = r.pi_;
477 long use_count() const BOOST_SP_NOEXCEPT
479 return pi_ != 0? pi_->use_count(): 0;
482 bool unique() const BOOST_SP_NOEXCEPT
484 return use_count() == 1;
487 bool empty() const BOOST_SP_NOEXCEPT
492 friend inline bool operator==(shared_count const & a, shared_count const & b) BOOST_SP_NOEXCEPT
494 return a.pi_ == b.pi_;
497 friend inline bool operator<(shared_count const & a, shared_count const & b) BOOST_SP_NOEXCEPT
499 return std::less<sp_counted_base *>()( a.pi_, b.pi_ );
502 void * get_deleter( sp_typeinfo_ const & ti ) const BOOST_SP_NOEXCEPT
504 return pi_? pi_->get_deleter( ti ): 0;
507 void * get_local_deleter( sp_typeinfo_ const & ti ) const BOOST_SP_NOEXCEPT
509 return pi_? pi_->get_local_deleter( ti ): 0;
512 void * get_untyped_deleter() const BOOST_SP_NOEXCEPT
514 return pi_? pi_->get_untyped_deleter(): 0;
523 sp_counted_base * pi_;
525 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
529 friend class shared_count;
533 BOOST_CONSTEXPR weak_count() BOOST_SP_NOEXCEPT: pi_(0)
534 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
540 weak_count(shared_count const & r) BOOST_SP_NOEXCEPT: pi_(r.pi_)
541 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
545 if(pi_ != 0) pi_->weak_add_ref();
548 weak_count(weak_count const & r) BOOST_SP_NOEXCEPT: pi_(r.pi_)
549 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
553 if(pi_ != 0) pi_->weak_add_ref();
558 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
560 weak_count(weak_count && r) BOOST_SP_NOEXCEPT: pi_(r.pi_)
561 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
570 ~weak_count() /*BOOST_SP_NOEXCEPT*/
572 if(pi_ != 0) pi_->weak_release();
573 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
578 weak_count & operator= (shared_count const & r) BOOST_SP_NOEXCEPT
580 sp_counted_base * tmp = r.pi_;
584 if(tmp != 0) tmp->weak_add_ref();
585 if(pi_ != 0) pi_->weak_release();
592 weak_count & operator= (weak_count const & r) BOOST_SP_NOEXCEPT
594 sp_counted_base * tmp = r.pi_;
598 if(tmp != 0) tmp->weak_add_ref();
599 if(pi_ != 0) pi_->weak_release();
606 void swap(weak_count & r) BOOST_SP_NOEXCEPT
608 sp_counted_base * tmp = r.pi_;
613 long use_count() const BOOST_SP_NOEXCEPT
615 return pi_ != 0? pi_->use_count(): 0;
618 bool empty() const BOOST_SP_NOEXCEPT
623 friend inline bool operator==(weak_count const & a, weak_count const & b) BOOST_SP_NOEXCEPT
625 return a.pi_ == b.pi_;
628 friend inline bool operator<(weak_count const & a, weak_count const & b) BOOST_SP_NOEXCEPT
630 return std::less<sp_counted_base *>()(a.pi_, b.pi_);
634 inline shared_count::shared_count( weak_count const & r ): pi_( r.pi_ )
635 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
636 , id_(shared_count_id)
639 if( pi_ == 0 || !pi_->add_ref_lock() )
641 boost::throw_exception( boost::bad_weak_ptr() );
645 inline shared_count::shared_count( weak_count const & r, sp_nothrow_tag ) BOOST_SP_NOEXCEPT: pi_( r.pi_ )
646 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
647 , id_(shared_count_id)
650 if( pi_ != 0 && !pi_->add_ref_lock() )
656 } // namespace detail
660 #if defined( BOOST_SP_DISABLE_DEPRECATED )
661 #pragma GCC diagnostic pop
665 # pragma warn .8027 // Functions containing try are not expanded inline
668 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SHARED_COUNT_HPP_INCLUDED