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/detail/workaround.hpp>
33 // In order to avoid circular dependencies with Boost.TR1
34 // we make sure that our include of <memory> doesn't try to
35 // pull in the TR1 headers: that's why we use this header
36 // rather than including <memory> directly:
37 #include <boost/config/no_tr1/memory.hpp> // std::auto_ptr
38 #include <functional> // std::less
40 #ifdef BOOST_NO_EXCEPTIONS
41 # include <new> // std::bad_alloc
44 #include <boost/core/addressof.hpp>
46 #if defined( BOOST_SP_DISABLE_DEPRECATED )
47 #pragma GCC diagnostic push
48 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
57 template< class T, class D > class unique_ptr;
59 } // namespace movelib
64 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
66 int const shared_count_id = 0x2C35F101;
67 int const weak_count_id = 0x298C38A4;
71 struct sp_nothrow_tag {};
73 template< class D > struct sp_inplace_tag
77 template< class T > class sp_reference_wrapper
81 explicit sp_reference_wrapper( T & t): t_( boost::addressof( t ) )
85 template< class Y > void operator()( Y * p ) const
95 template< class D > struct sp_convert_reference
100 template< class D > struct sp_convert_reference< D& >
102 typedef sp_reference_wrapper< D > type;
111 sp_counted_base * pi_;
113 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
117 friend class weak_count;
121 shared_count(): pi_(0) // nothrow
122 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
123 , id_(shared_count_id)
128 template<class Y> explicit shared_count( Y * p ): pi_( 0 )
129 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
130 , id_(shared_count_id)
133 #ifndef BOOST_NO_EXCEPTIONS
137 pi_ = new sp_counted_impl_p<Y>( p );
141 boost::checked_delete( p );
147 pi_ = new sp_counted_impl_p<Y>( p );
151 boost::checked_delete( p );
152 boost::throw_exception( std::bad_alloc() );
158 #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, <= 1200 )
159 template<class Y, class D> shared_count( Y * p, D d ): pi_(0)
161 template<class P, class D> shared_count( P p, D d ): pi_(0)
163 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
164 , id_(shared_count_id)
167 #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, <= 1200 )
170 #ifndef BOOST_NO_EXCEPTIONS
174 pi_ = new sp_counted_impl_pd<P, D>(p, d);
184 pi_ = new sp_counted_impl_pd<P, D>(p, d);
189 boost::throw_exception(std::bad_alloc());
195 #if !defined( BOOST_NO_FUNCTION_TEMPLATE_ORDERING )
197 template< class P, class D > shared_count( P p, sp_inplace_tag<D> ): pi_( 0 )
198 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
199 , id_(shared_count_id)
202 #ifndef BOOST_NO_EXCEPTIONS
206 pi_ = new sp_counted_impl_pd< P, D >( p );
210 D::operator_fn( p ); // delete p
216 pi_ = new sp_counted_impl_pd< P, D >( p );
220 D::operator_fn( p ); // delete p
221 boost::throw_exception( std::bad_alloc() );
224 #endif // #ifndef BOOST_NO_EXCEPTIONS
227 #endif // !defined( BOOST_NO_FUNCTION_TEMPLATE_ORDERING )
229 template<class P, class D, class A> shared_count( P p, D d, A a ): pi_( 0 )
230 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
231 , id_(shared_count_id)
234 typedef sp_counted_impl_pda<P, D, A> impl_type;
236 #if !defined( BOOST_NO_CXX11_ALLOCATOR )
238 typedef typename std::allocator_traits<A>::template rebind_alloc< impl_type > A2;
242 typedef typename A::template rebind< impl_type >::other A2;
248 #ifndef BOOST_NO_EXCEPTIONS
252 #if !defined( BOOST_NO_CXX11_ALLOCATOR )
254 impl_type * pi = std::allocator_traits<A2>::allocate( a2, 1 );
256 std::allocator_traits<A2>::construct( a2, pi, p, d, a );
260 pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );
261 ::new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
271 a2.deallocate( static_cast< impl_type* >( pi_ ), 1 );
279 #if !defined( BOOST_NO_CXX11_ALLOCATOR )
281 impl_type * pi = std::allocator_traits<A2>::allocate( a2, 1 );
286 pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );
292 #if !defined( BOOST_NO_CXX11_ALLOCATOR )
294 std::allocator_traits<A2>::construct( a2, pi, p, d, a );
298 ::new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
305 boost::throw_exception( std::bad_alloc() );
311 #if !defined( BOOST_NO_FUNCTION_TEMPLATE_ORDERING )
313 template< class P, class D, class A > shared_count( P p, sp_inplace_tag< D >, A a ): pi_( 0 )
314 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
315 , id_(shared_count_id)
318 typedef sp_counted_impl_pda< P, D, A > impl_type;
320 #if !defined( BOOST_NO_CXX11_ALLOCATOR )
322 typedef typename std::allocator_traits<A>::template rebind_alloc< impl_type > A2;
326 typedef typename A::template rebind< impl_type >::other A2;
332 #ifndef BOOST_NO_EXCEPTIONS
336 #if !defined( BOOST_NO_CXX11_ALLOCATOR )
338 impl_type * pi = std::allocator_traits<A2>::allocate( a2, 1 );
340 std::allocator_traits<A2>::construct( a2, pi, p, a );
344 pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );
345 ::new( static_cast< void* >( pi_ ) ) impl_type( p, a );
355 a2.deallocate( static_cast< impl_type* >( pi_ ), 1 );
363 #if !defined( BOOST_NO_CXX11_ALLOCATOR )
365 impl_type * pi = std::allocator_traits<A2>::allocate( a2, 1 );
370 pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );
376 #if !defined( BOOST_NO_CXX11_ALLOCATOR )
378 std::allocator_traits<A2>::construct( a2, pi, p, a );
382 ::new( static_cast< void* >( pi_ ) ) impl_type( p, a );
389 boost::throw_exception( std::bad_alloc() );
392 #endif // #ifndef BOOST_NO_EXCEPTIONS
395 #endif // !defined( BOOST_NO_FUNCTION_TEMPLATE_ORDERING )
397 #ifndef BOOST_NO_AUTO_PTR
399 // auto_ptr<Y> is special cased to provide the strong guarantee
402 explicit shared_count( std::auto_ptr<Y> & r ): pi_( new sp_counted_impl_p<Y>( r.get() ) )
403 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
404 , id_(shared_count_id)
407 #ifdef BOOST_NO_EXCEPTIONS
411 boost::throw_exception(std::bad_alloc());
421 #if !defined( BOOST_NO_CXX11_SMART_PTR )
423 template<class Y, class D>
424 explicit shared_count( std::unique_ptr<Y, D> & r ): pi_( 0 )
425 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
426 , id_(shared_count_id)
429 typedef typename sp_convert_reference<D>::type D2;
431 D2 d2( r.get_deleter() );
432 pi_ = new sp_counted_impl_pd< typename std::unique_ptr<Y, D>::pointer, D2 >( r.get(), d2 );
434 #ifdef BOOST_NO_EXCEPTIONS
438 boost::throw_exception( std::bad_alloc() );
448 template<class Y, class D>
449 explicit shared_count( boost::movelib::unique_ptr<Y, D> & r ): pi_( 0 )
450 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
451 , id_(shared_count_id)
454 typedef typename sp_convert_reference<D>::type D2;
456 D2 d2( r.get_deleter() );
457 pi_ = new sp_counted_impl_pd< typename boost::movelib::unique_ptr<Y, D>::pointer, D2 >( r.get(), d2 );
459 #ifdef BOOST_NO_EXCEPTIONS
463 boost::throw_exception( std::bad_alloc() );
471 ~shared_count() // nothrow
473 if( pi_ != 0 ) pi_->release();
474 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
479 shared_count(shared_count const & r): pi_(r.pi_) // nothrow
480 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
481 , id_(shared_count_id)
484 if( pi_ != 0 ) pi_->add_ref_copy();
487 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
489 shared_count(shared_count && r): pi_(r.pi_) // nothrow
490 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
491 , id_(shared_count_id)
499 explicit shared_count(weak_count const & r); // throws bad_weak_ptr when r.use_count() == 0
500 shared_count( weak_count const & r, sp_nothrow_tag ); // constructs an empty *this when r.use_count() == 0
502 shared_count & operator= (shared_count const & r) // nothrow
504 sp_counted_base * tmp = r.pi_;
508 if( tmp != 0 ) tmp->add_ref_copy();
509 if( pi_ != 0 ) pi_->release();
516 void swap(shared_count & r) // nothrow
518 sp_counted_base * tmp = r.pi_;
523 long use_count() const // nothrow
525 return pi_ != 0? pi_->use_count(): 0;
528 bool unique() const // nothrow
530 return use_count() == 1;
533 bool empty() const // nothrow
538 friend inline bool operator==(shared_count const & a, shared_count const & b)
540 return a.pi_ == b.pi_;
543 friend inline bool operator<(shared_count const & a, shared_count const & b)
545 return std::less<sp_counted_base *>()( a.pi_, b.pi_ );
548 void * get_deleter( sp_typeinfo const & ti ) const
550 return pi_? pi_->get_deleter( ti ): 0;
553 void * get_untyped_deleter() const
555 return pi_? pi_->get_untyped_deleter(): 0;
564 sp_counted_base * pi_;
566 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
570 friend class shared_count;
574 weak_count(): pi_(0) // nothrow
575 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
581 weak_count(shared_count const & r): pi_(r.pi_) // nothrow
582 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
586 if(pi_ != 0) pi_->weak_add_ref();
589 weak_count(weak_count const & r): pi_(r.pi_) // nothrow
590 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
594 if(pi_ != 0) pi_->weak_add_ref();
599 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
601 weak_count(weak_count && r): pi_(r.pi_) // nothrow
602 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
611 ~weak_count() // nothrow
613 if(pi_ != 0) pi_->weak_release();
614 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
619 weak_count & operator= (shared_count const & r) // nothrow
621 sp_counted_base * tmp = r.pi_;
625 if(tmp != 0) tmp->weak_add_ref();
626 if(pi_ != 0) pi_->weak_release();
633 weak_count & operator= (weak_count const & r) // nothrow
635 sp_counted_base * tmp = r.pi_;
639 if(tmp != 0) tmp->weak_add_ref();
640 if(pi_ != 0) pi_->weak_release();
647 void swap(weak_count & r) // nothrow
649 sp_counted_base * tmp = r.pi_;
654 long use_count() const // nothrow
656 return pi_ != 0? pi_->use_count(): 0;
659 bool empty() const // nothrow
664 friend inline bool operator==(weak_count const & a, weak_count const & b)
666 return a.pi_ == b.pi_;
669 friend inline bool operator<(weak_count const & a, weak_count const & b)
671 return std::less<sp_counted_base *>()(a.pi_, b.pi_);
675 inline shared_count::shared_count( weak_count const & r ): pi_( r.pi_ )
676 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
677 , id_(shared_count_id)
680 if( pi_ == 0 || !pi_->add_ref_lock() )
682 boost::throw_exception( boost::bad_weak_ptr() );
686 inline shared_count::shared_count( weak_count const & r, sp_nothrow_tag ): pi_( r.pi_ )
687 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
688 , id_(shared_count_id)
691 if( pi_ != 0 && !pi_->add_ref_lock() )
697 } // namespace detail
701 #if defined( BOOST_SP_DISABLE_DEPRECATED )
702 #pragma GCC diagnostic pop
706 # pragma warn .8027 // Functions containing try are not expanded inline
709 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SHARED_COUNT_HPP_INCLUDED