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/detail/workaround.hpp>
32 // In order to avoid circular dependencies with Boost.TR1
33 // we make sure that our include of <memory> doesn't try to
34 // pull in the TR1 headers: that's why we use this header
35 // rather than including <memory> directly:
36 #include <boost/config/no_tr1/memory.hpp> // std::auto_ptr
37 #include <functional> // std::less
39 #ifdef BOOST_NO_EXCEPTIONS
40 # include <new> // std::bad_alloc
43 #if !defined( BOOST_NO_CXX11_SMART_PTR )
44 # include <boost/utility/addressof.hpp>
53 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
55 int const shared_count_id = 0x2C35F101;
56 int const weak_count_id = 0x298C38A4;
60 struct sp_nothrow_tag {};
62 template< class D > struct sp_inplace_tag
66 #if !defined( BOOST_NO_CXX11_SMART_PTR )
68 template< class T > class sp_reference_wrapper
72 explicit sp_reference_wrapper( T & t): t_( boost::addressof( t ) )
76 template< class Y > void operator()( Y * p ) const
86 template< class D > struct sp_convert_reference
91 template< class D > struct sp_convert_reference< D& >
93 typedef sp_reference_wrapper< D > type;
104 sp_counted_base * pi_;
106 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
110 friend class weak_count;
114 shared_count(): pi_(0) // nothrow
115 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
116 , id_(shared_count_id)
121 template<class Y> explicit shared_count( Y * p ): pi_( 0 )
122 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
123 , id_(shared_count_id)
126 #ifndef BOOST_NO_EXCEPTIONS
130 pi_ = new sp_counted_impl_p<Y>( p );
134 boost::checked_delete( p );
140 pi_ = new sp_counted_impl_p<Y>( p );
144 boost::checked_delete( p );
145 boost::throw_exception( std::bad_alloc() );
151 #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, <= 1200 )
152 template<class Y, class D> shared_count( Y * p, D d ): pi_(0)
154 template<class P, class D> shared_count( P p, D d ): pi_(0)
156 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
157 , id_(shared_count_id)
160 #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, <= 1200 )
163 #ifndef BOOST_NO_EXCEPTIONS
167 pi_ = new sp_counted_impl_pd<P, D>(p, d);
177 pi_ = new sp_counted_impl_pd<P, D>(p, d);
182 boost::throw_exception(std::bad_alloc());
188 #if !defined( BOOST_NO_FUNCTION_TEMPLATE_ORDERING )
190 template< class P, class D > shared_count( P p, sp_inplace_tag<D> ): pi_( 0 )
191 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
192 , id_(shared_count_id)
195 #ifndef BOOST_NO_EXCEPTIONS
199 pi_ = new sp_counted_impl_pd< P, D >( p );
203 D::operator_fn( p ); // delete p
209 pi_ = new sp_counted_impl_pd< P, D >( p );
213 D::operator_fn( p ); // delete p
214 boost::throw_exception( std::bad_alloc() );
217 #endif // #ifndef BOOST_NO_EXCEPTIONS
220 #endif // !defined( BOOST_NO_FUNCTION_TEMPLATE_ORDERING )
222 template<class P, class D, class A> shared_count( P p, D d, A a ): pi_( 0 )
223 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
224 , id_(shared_count_id)
227 typedef sp_counted_impl_pda<P, D, A> impl_type;
228 typedef typename A::template rebind< impl_type >::other A2;
232 #ifndef BOOST_NO_EXCEPTIONS
236 pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );
237 new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
245 a2.deallocate( static_cast< impl_type* >( pi_ ), 1 );
253 pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );
257 new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
262 boost::throw_exception( std::bad_alloc() );
268 #if !defined( BOOST_NO_FUNCTION_TEMPLATE_ORDERING )
270 template< class P, class D, class A > shared_count( P p, sp_inplace_tag< D >, A a ): pi_( 0 )
271 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
272 , id_(shared_count_id)
275 typedef sp_counted_impl_pda< P, D, A > impl_type;
276 typedef typename A::template rebind< impl_type >::other A2;
280 #ifndef BOOST_NO_EXCEPTIONS
284 pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );
285 new( static_cast< void* >( pi_ ) ) impl_type( p, a );
293 a2.deallocate( static_cast< impl_type* >( pi_ ), 1 );
301 pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );
305 new( static_cast< void* >( pi_ ) ) impl_type( p, a );
310 boost::throw_exception( std::bad_alloc() );
313 #endif // #ifndef BOOST_NO_EXCEPTIONS
316 #endif // !defined( BOOST_NO_FUNCTION_TEMPLATE_ORDERING )
318 #ifndef BOOST_NO_AUTO_PTR
320 // auto_ptr<Y> is special cased to provide the strong guarantee
323 explicit shared_count( std::auto_ptr<Y> & r ): pi_( new sp_counted_impl_p<Y>( r.get() ) )
324 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
325 , id_(shared_count_id)
328 #ifdef BOOST_NO_EXCEPTIONS
332 boost::throw_exception(std::bad_alloc());
342 #if !defined( BOOST_NO_CXX11_SMART_PTR )
344 template<class Y, class D>
345 explicit shared_count( std::unique_ptr<Y, D> & r ): pi_( 0 )
346 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
347 , id_(shared_count_id)
350 typedef typename sp_convert_reference<D>::type D2;
352 D2 d2( r.get_deleter() );
353 pi_ = new sp_counted_impl_pd< typename std::unique_ptr<Y, D>::pointer, D2 >( r.get(), d2 );
355 #ifdef BOOST_NO_EXCEPTIONS
359 boost::throw_exception( std::bad_alloc() );
369 ~shared_count() // nothrow
371 if( pi_ != 0 ) pi_->release();
372 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
377 shared_count(shared_count const & r): pi_(r.pi_) // nothrow
378 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
379 , id_(shared_count_id)
382 if( pi_ != 0 ) pi_->add_ref_copy();
385 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
387 shared_count(shared_count && r): pi_(r.pi_) // nothrow
388 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
389 , id_(shared_count_id)
397 explicit shared_count(weak_count const & r); // throws bad_weak_ptr when r.use_count() == 0
398 shared_count( weak_count const & r, sp_nothrow_tag ); // constructs an empty *this when r.use_count() == 0
400 shared_count & operator= (shared_count const & r) // nothrow
402 sp_counted_base * tmp = r.pi_;
406 if( tmp != 0 ) tmp->add_ref_copy();
407 if( pi_ != 0 ) pi_->release();
414 void swap(shared_count & r) // nothrow
416 sp_counted_base * tmp = r.pi_;
421 long use_count() const // nothrow
423 return pi_ != 0? pi_->use_count(): 0;
426 bool unique() const // nothrow
428 return use_count() == 1;
431 bool empty() const // nothrow
436 friend inline bool operator==(shared_count const & a, shared_count const & b)
438 return a.pi_ == b.pi_;
441 friend inline bool operator<(shared_count const & a, shared_count const & b)
443 return std::less<sp_counted_base *>()( a.pi_, b.pi_ );
446 void * get_deleter( sp_typeinfo const & ti ) const
448 return pi_? pi_->get_deleter( ti ): 0;
451 void * get_untyped_deleter() const
453 return pi_? pi_->get_untyped_deleter(): 0;
462 sp_counted_base * pi_;
464 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
468 friend class shared_count;
472 weak_count(): pi_(0) // nothrow
473 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
479 weak_count(shared_count const & r): pi_(r.pi_) // nothrow
480 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
484 if(pi_ != 0) pi_->weak_add_ref();
487 weak_count(weak_count const & r): pi_(r.pi_) // nothrow
488 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
492 if(pi_ != 0) pi_->weak_add_ref();
497 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
499 weak_count(weak_count && r): pi_(r.pi_) // nothrow
500 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
509 ~weak_count() // nothrow
511 if(pi_ != 0) pi_->weak_release();
512 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
517 weak_count & operator= (shared_count const & r) // nothrow
519 sp_counted_base * tmp = r.pi_;
523 if(tmp != 0) tmp->weak_add_ref();
524 if(pi_ != 0) pi_->weak_release();
531 weak_count & operator= (weak_count const & r) // nothrow
533 sp_counted_base * tmp = r.pi_;
537 if(tmp != 0) tmp->weak_add_ref();
538 if(pi_ != 0) pi_->weak_release();
545 void swap(weak_count & r) // nothrow
547 sp_counted_base * tmp = r.pi_;
552 long use_count() const // nothrow
554 return pi_ != 0? pi_->use_count(): 0;
557 bool empty() const // nothrow
562 friend inline bool operator==(weak_count const & a, weak_count const & b)
564 return a.pi_ == b.pi_;
567 friend inline bool operator<(weak_count const & a, weak_count const & b)
569 return std::less<sp_counted_base *>()(a.pi_, b.pi_);
573 inline shared_count::shared_count( weak_count const & r ): pi_( r.pi_ )
574 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
575 , id_(shared_count_id)
578 if( pi_ == 0 || !pi_->add_ref_lock() )
580 boost::throw_exception( boost::bad_weak_ptr() );
584 inline shared_count::shared_count( weak_count const & r, sp_nothrow_tag ): pi_( r.pi_ )
585 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
586 , id_(shared_count_id)
589 if( pi_ != 0 && !pi_->add_ref_lock() )
595 } // namespace detail
600 # pragma warn .8027 // Functions containing try are not expanded inline
603 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SHARED_COUNT_HPP_INCLUDED