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
38 #include <new> // std::bad_alloc
46 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
48 int const shared_count_id = 0x2C35F101;
49 int const weak_count_id = 0x298C38A4;
53 struct sp_nothrow_tag {};
55 template< class D > struct sp_inplace_tag
65 sp_counted_base * pi_;
67 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
71 friend class weak_count;
75 shared_count(): pi_(0) // nothrow
76 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
77 , id_(shared_count_id)
82 template<class Y> explicit shared_count( Y * p ): pi_( 0 )
83 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
84 , id_(shared_count_id)
87 #ifndef BOOST_NO_EXCEPTIONS
91 pi_ = new sp_counted_impl_p<Y>( p );
95 boost::checked_delete( p );
101 pi_ = new sp_counted_impl_p<Y>( p );
105 boost::checked_delete( p );
106 boost::throw_exception( std::bad_alloc() );
112 #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, <= 1200 )
113 template<class Y, class D> shared_count( Y * p, D d ): pi_(0)
115 template<class P, class D> shared_count( P p, D d ): pi_(0)
117 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
118 , id_(shared_count_id)
121 #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, <= 1200 )
124 #ifndef BOOST_NO_EXCEPTIONS
128 pi_ = new sp_counted_impl_pd<P, D>(p, d);
138 pi_ = new sp_counted_impl_pd<P, D>(p, d);
143 boost::throw_exception(std::bad_alloc());
149 #if !defined( BOOST_NO_FUNCTION_TEMPLATE_ORDERING )
151 template< class P, class D > shared_count( P p, sp_inplace_tag<D> ): pi_( 0 )
152 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
153 , id_(shared_count_id)
156 #ifndef BOOST_NO_EXCEPTIONS
160 pi_ = new sp_counted_impl_pd< P, D >( p );
164 D()( p ); // delete p
170 pi_ = new sp_counted_impl_pd< P, D >( p );
174 D()( p ); // delete p
175 boost::throw_exception( std::bad_alloc() );
178 #endif // #ifndef BOOST_NO_EXCEPTIONS
181 #endif // !defined( BOOST_NO_FUNCTION_TEMPLATE_ORDERING )
183 template<class P, class D, class A> shared_count( P p, D d, A a ): pi_( 0 )
184 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
185 , id_(shared_count_id)
188 typedef sp_counted_impl_pda<P, D, A> impl_type;
189 typedef typename A::template rebind< impl_type >::other A2;
193 #ifndef BOOST_NO_EXCEPTIONS
197 pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );
198 new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
206 a2.deallocate( static_cast< impl_type* >( pi_ ), 1 );
214 pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );
218 new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
223 boost::throw_exception( std::bad_alloc() );
229 #if !defined( BOOST_NO_FUNCTION_TEMPLATE_ORDERING )
231 template< class P, class D, class A > shared_count( P p, sp_inplace_tag< D >, A a ): pi_( 0 )
232 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
233 , id_(shared_count_id)
236 typedef sp_counted_impl_pda< P, D, A > impl_type;
237 typedef typename A::template rebind< impl_type >::other A2;
241 #ifndef BOOST_NO_EXCEPTIONS
245 pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );
246 new( static_cast< void* >( pi_ ) ) impl_type( p, a );
254 a2.deallocate( static_cast< impl_type* >( pi_ ), 1 );
262 pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );
266 new( static_cast< void* >( pi_ ) ) impl_type( p, a );
271 boost::throw_exception( std::bad_alloc() );
274 #endif // #ifndef BOOST_NO_EXCEPTIONS
277 #endif // !defined( BOOST_NO_FUNCTION_TEMPLATE_ORDERING )
279 #ifndef BOOST_NO_AUTO_PTR
281 // auto_ptr<Y> is special cased to provide the strong guarantee
284 explicit shared_count( std::auto_ptr<Y> & r ): pi_( new sp_counted_impl_p<Y>( r.get() ) )
285 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
286 , id_(shared_count_id)
289 #ifdef BOOST_NO_EXCEPTIONS
293 boost::throw_exception(std::bad_alloc());
303 ~shared_count() // nothrow
305 if( pi_ != 0 ) pi_->release();
306 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
311 shared_count(shared_count const & r): pi_(r.pi_) // nothrow
312 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
313 , id_(shared_count_id)
316 if( pi_ != 0 ) pi_->add_ref_copy();
319 #if defined( BOOST_HAS_RVALUE_REFS )
321 shared_count(shared_count && r): pi_(r.pi_) // nothrow
322 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
323 , id_(shared_count_id)
331 explicit shared_count(weak_count const & r); // throws bad_weak_ptr when r.use_count() == 0
332 shared_count( weak_count const & r, sp_nothrow_tag ); // constructs an empty *this when r.use_count() == 0
334 shared_count & operator= (shared_count const & r) // nothrow
336 sp_counted_base * tmp = r.pi_;
340 if( tmp != 0 ) tmp->add_ref_copy();
341 if( pi_ != 0 ) pi_->release();
348 void swap(shared_count & r) // nothrow
350 sp_counted_base * tmp = r.pi_;
355 long use_count() const // nothrow
357 return pi_ != 0? pi_->use_count(): 0;
360 bool unique() const // nothrow
362 return use_count() == 1;
365 bool empty() const // nothrow
370 friend inline bool operator==(shared_count const & a, shared_count const & b)
372 return a.pi_ == b.pi_;
375 friend inline bool operator<(shared_count const & a, shared_count const & b)
377 return std::less<sp_counted_base *>()( a.pi_, b.pi_ );
380 void * get_deleter( sp_typeinfo const & ti ) const
382 return pi_? pi_->get_deleter( ti ): 0;
391 sp_counted_base * pi_;
393 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
397 friend class shared_count;
401 weak_count(): pi_(0) // nothrow
402 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
408 weak_count(shared_count const & r): pi_(r.pi_) // nothrow
409 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
413 if(pi_ != 0) pi_->weak_add_ref();
416 weak_count(weak_count const & r): pi_(r.pi_) // nothrow
417 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
421 if(pi_ != 0) pi_->weak_add_ref();
426 #if defined( BOOST_HAS_RVALUE_REFS )
428 weak_count(weak_count && r): pi_(r.pi_) // nothrow
429 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
438 ~weak_count() // nothrow
440 if(pi_ != 0) pi_->weak_release();
441 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
446 weak_count & operator= (shared_count const & r) // nothrow
448 sp_counted_base * tmp = r.pi_;
452 if(tmp != 0) tmp->weak_add_ref();
453 if(pi_ != 0) pi_->weak_release();
460 weak_count & operator= (weak_count const & r) // nothrow
462 sp_counted_base * tmp = r.pi_;
466 if(tmp != 0) tmp->weak_add_ref();
467 if(pi_ != 0) pi_->weak_release();
474 void swap(weak_count & r) // nothrow
476 sp_counted_base * tmp = r.pi_;
481 long use_count() const // nothrow
483 return pi_ != 0? pi_->use_count(): 0;
486 bool empty() const // nothrow
491 friend inline bool operator==(weak_count const & a, weak_count const & b)
493 return a.pi_ == b.pi_;
496 friend inline bool operator<(weak_count const & a, weak_count const & b)
498 return std::less<sp_counted_base *>()(a.pi_, b.pi_);
502 inline shared_count::shared_count( weak_count const & r ): pi_( r.pi_ )
503 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
504 , id_(shared_count_id)
507 if( pi_ == 0 || !pi_->add_ref_lock() )
509 boost::throw_exception( boost::bad_weak_ptr() );
513 inline shared_count::shared_count( weak_count const & r, sp_nothrow_tag ): pi_( r.pi_ )
514 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
515 , id_(shared_count_id)
518 if( pi_ != 0 && !pi_->add_ref_lock() )
524 } // namespace detail
529 # pragma warn .8027 // Functions containing try are not expanded inline
532 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SHARED_COUNT_HPP_INCLUDED