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 {};
61 sp_counted_base * pi_;
63 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
67 friend class weak_count;
71 shared_count(): pi_(0) // nothrow
72 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
73 , id_(shared_count_id)
78 template<class Y> explicit shared_count( Y * p ): pi_( 0 )
79 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
80 , id_(shared_count_id)
83 #ifndef BOOST_NO_EXCEPTIONS
87 pi_ = new sp_counted_impl_p<Y>( p );
91 boost::checked_delete( p );
97 pi_ = new sp_counted_impl_p<Y>( p );
101 boost::checked_delete( p );
102 boost::throw_exception( std::bad_alloc() );
108 #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, <= 1200 )
109 template<class Y, class D> shared_count( Y * p, D d ): pi_(0)
111 template<class P, class D> shared_count( P p, D d ): pi_(0)
113 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
114 , id_(shared_count_id)
117 #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, <= 1200 )
120 #ifndef BOOST_NO_EXCEPTIONS
124 pi_ = new sp_counted_impl_pd<P, D>(p, d);
134 pi_ = new sp_counted_impl_pd<P, D>(p, d);
139 boost::throw_exception(std::bad_alloc());
145 template<class P, class D, class A> shared_count( P p, D d, A a ): pi_( 0 )
146 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
147 , id_(shared_count_id)
150 typedef sp_counted_impl_pda<P, D, A> impl_type;
151 typedef typename A::template rebind< impl_type >::other A2;
155 #ifndef BOOST_NO_EXCEPTIONS
159 pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );
160 new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
168 a2.deallocate( static_cast< impl_type* >( pi_ ), 1 );
176 pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );
180 new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
185 boost::throw_exception( std::bad_alloc() );
191 #ifndef BOOST_NO_AUTO_PTR
193 // auto_ptr<Y> is special cased to provide the strong guarantee
196 explicit shared_count( std::auto_ptr<Y> & r ): pi_( new sp_counted_impl_p<Y>( r.get() ) )
197 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
198 , id_(shared_count_id)
201 #ifdef BOOST_NO_EXCEPTIONS
205 boost::throw_exception(std::bad_alloc());
215 ~shared_count() // nothrow
217 if( pi_ != 0 ) pi_->release();
218 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
223 shared_count(shared_count const & r): pi_(r.pi_) // nothrow
224 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
225 , id_(shared_count_id)
228 if( pi_ != 0 ) pi_->add_ref_copy();
231 #if defined( BOOST_HAS_RVALUE_REFS )
233 shared_count(shared_count && r): pi_(r.pi_) // nothrow
234 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
235 , id_(shared_count_id)
243 explicit shared_count(weak_count const & r); // throws bad_weak_ptr when r.use_count() == 0
244 shared_count( weak_count const & r, sp_nothrow_tag ); // constructs an empty *this when r.use_count() == 0
246 shared_count & operator= (shared_count const & r) // nothrow
248 sp_counted_base * tmp = r.pi_;
252 if( tmp != 0 ) tmp->add_ref_copy();
253 if( pi_ != 0 ) pi_->release();
260 void swap(shared_count & r) // nothrow
262 sp_counted_base * tmp = r.pi_;
267 long use_count() const // nothrow
269 return pi_ != 0? pi_->use_count(): 0;
272 bool unique() const // nothrow
274 return use_count() == 1;
277 bool empty() const // nothrow
282 friend inline bool operator==(shared_count const & a, shared_count const & b)
284 return a.pi_ == b.pi_;
287 friend inline bool operator<(shared_count const & a, shared_count const & b)
289 return std::less<sp_counted_base *>()( a.pi_, b.pi_ );
292 void * get_deleter( sp_typeinfo const & ti ) const
294 return pi_? pi_->get_deleter( ti ): 0;
303 sp_counted_base * pi_;
305 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
309 friend class shared_count;
313 weak_count(): pi_(0) // nothrow
314 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
320 weak_count(shared_count const & r): pi_(r.pi_) // nothrow
321 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
325 if(pi_ != 0) pi_->weak_add_ref();
328 weak_count(weak_count const & r): pi_(r.pi_) // nothrow
329 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
333 if(pi_ != 0) pi_->weak_add_ref();
338 #if defined( BOOST_HAS_RVALUE_REFS )
340 weak_count(weak_count && r): pi_(r.pi_) // nothrow
341 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
350 ~weak_count() // nothrow
352 if(pi_ != 0) pi_->weak_release();
353 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
358 weak_count & operator= (shared_count const & r) // nothrow
360 sp_counted_base * tmp = r.pi_;
364 if(tmp != 0) tmp->weak_add_ref();
365 if(pi_ != 0) pi_->weak_release();
372 weak_count & operator= (weak_count const & r) // nothrow
374 sp_counted_base * tmp = r.pi_;
378 if(tmp != 0) tmp->weak_add_ref();
379 if(pi_ != 0) pi_->weak_release();
386 void swap(weak_count & r) // nothrow
388 sp_counted_base * tmp = r.pi_;
393 long use_count() const // nothrow
395 return pi_ != 0? pi_->use_count(): 0;
398 bool empty() const // nothrow
403 friend inline bool operator==(weak_count const & a, weak_count const & b)
405 return a.pi_ == b.pi_;
408 friend inline bool operator<(weak_count const & a, weak_count const & b)
410 return std::less<sp_counted_base *>()(a.pi_, b.pi_);
414 inline shared_count::shared_count( weak_count const & r ): pi_( r.pi_ )
415 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
416 , id_(shared_count_id)
419 if( pi_ == 0 || !pi_->add_ref_lock() )
421 boost::throw_exception( boost::bad_weak_ptr() );
425 inline shared_count::shared_count( weak_count const & r, sp_nothrow_tag ): pi_( r.pi_ )
426 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
427 , id_(shared_count_id)
430 if( pi_ != 0 && !pi_->add_ref_lock() )
436 } // namespace detail
441 # pragma warn .8027 // Functions containing try are not expanded inline
444 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SHARED_COUNT_HPP_INCLUDED