1 #ifndef BOOST_SMART_PTR_DETAIL_SHARED_COUNT_HPP_INCLUDED
\r
2 #define BOOST_SMART_PTR_DETAIL_SHARED_COUNT_HPP_INCLUDED
\r
4 // MS compatible compilers support #pragma once
\r
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
\r
11 // detail/shared_count.hpp
\r
13 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
\r
14 // Copyright 2004-2005 Peter Dimov
\r
16 // Distributed under the Boost Software License, Version 1.0. (See
\r
17 // accompanying file LICENSE_1_0.txt or copy at
\r
18 // http://www.boost.org/LICENSE_1_0.txt)
\r
22 # pragma warn -8027 // Functions containing try are not expanded inline
\r
25 #include <boost/config.hpp>
\r
26 #include <boost/checked_delete.hpp>
\r
27 #include <boost/throw_exception.hpp>
\r
28 #include <boost/smart_ptr/bad_weak_ptr.hpp>
\r
29 #include <boost/smart_ptr/detail/sp_counted_base.hpp>
\r
30 #include <boost/smart_ptr/detail/sp_counted_impl.hpp>
\r
31 #include <boost/detail/workaround.hpp>
\r
32 // In order to avoid circular dependencies with Boost.TR1
\r
33 // we make sure that our include of <memory> doesn't try to
\r
34 // pull in the TR1 headers: that's why we use this header
\r
35 // rather than including <memory> directly:
\r
36 #include <boost/config/no_tr1/memory.hpp> // std::auto_ptr
\r
37 #include <functional> // std::less
\r
38 #include <new> // std::bad_alloc
\r
46 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
\r
48 int const shared_count_id = 0x2C35F101;
\r
49 int const weak_count_id = 0x298C38A4;
\r
53 struct sp_nothrow_tag {};
\r
61 sp_counted_base * pi_;
\r
63 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
\r
67 friend class weak_count;
\r
71 shared_count(): pi_(0) // nothrow
\r
72 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
\r
73 , id_(shared_count_id)
\r
78 template<class Y> explicit shared_count( Y * p ): pi_( 0 )
\r
79 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
\r
80 , id_(shared_count_id)
\r
83 #ifndef BOOST_NO_EXCEPTIONS
\r
87 pi_ = new sp_counted_impl_p<Y>( p );
\r
91 boost::checked_delete( p );
\r
97 pi_ = new sp_counted_impl_p<Y>( p );
\r
101 boost::checked_delete( p );
\r
102 boost::throw_exception( std::bad_alloc() );
\r
108 #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, <= 1200 )
\r
109 template<class Y, class D> shared_count( Y * p, D d ): pi_(0)
\r
111 template<class P, class D> shared_count( P p, D d ): pi_(0)
\r
113 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
\r
114 , id_(shared_count_id)
\r
117 #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, <= 1200 )
\r
120 #ifndef BOOST_NO_EXCEPTIONS
\r
124 pi_ = new sp_counted_impl_pd<P, D>(p, d);
\r
134 pi_ = new sp_counted_impl_pd<P, D>(p, d);
\r
139 boost::throw_exception(std::bad_alloc());
\r
145 template<class P, class D, class A> shared_count( P p, D d, A a ): pi_( 0 )
\r
146 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
\r
147 , id_(shared_count_id)
\r
150 typedef sp_counted_impl_pda<P, D, A> impl_type;
\r
151 typedef typename A::template rebind< impl_type >::other A2;
\r
155 #ifndef BOOST_NO_EXCEPTIONS
\r
159 pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );
\r
160 new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
\r
168 a2.deallocate( static_cast< impl_type* >( pi_ ), 1 );
\r
176 pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );
\r
180 new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
\r
185 boost::throw_exception( std::bad_alloc() );
\r
191 #ifndef BOOST_NO_AUTO_PTR
\r
193 // auto_ptr<Y> is special cased to provide the strong guarantee
\r
196 explicit shared_count( std::auto_ptr<Y> & r ): pi_( new sp_counted_impl_p<Y>( r.get() ) )
\r
197 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
\r
198 , id_(shared_count_id)
\r
201 #ifdef BOOST_NO_EXCEPTIONS
\r
205 boost::throw_exception(std::bad_alloc());
\r
215 ~shared_count() // nothrow
\r
217 if( pi_ != 0 ) pi_->release();
\r
218 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
\r
223 shared_count(shared_count const & r): pi_(r.pi_) // nothrow
\r
224 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
\r
225 , id_(shared_count_id)
\r
228 if( pi_ != 0 ) pi_->add_ref_copy();
\r
231 #if defined( BOOST_HAS_RVALUE_REFS )
\r
233 shared_count(shared_count && r): pi_(r.pi_) // nothrow
\r
234 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
\r
235 , id_(shared_count_id)
\r
243 explicit shared_count(weak_count const & r); // throws bad_weak_ptr when r.use_count() == 0
\r
244 shared_count( weak_count const & r, sp_nothrow_tag ); // constructs an empty *this when r.use_count() == 0
\r
246 shared_count & operator= (shared_count const & r) // nothrow
\r
248 sp_counted_base * tmp = r.pi_;
\r
252 if( tmp != 0 ) tmp->add_ref_copy();
\r
253 if( pi_ != 0 ) pi_->release();
\r
260 void swap(shared_count & r) // nothrow
\r
262 sp_counted_base * tmp = r.pi_;
\r
267 long use_count() const // nothrow
\r
269 return pi_ != 0? pi_->use_count(): 0;
\r
272 bool unique() const // nothrow
\r
274 return use_count() == 1;
\r
277 bool empty() const // nothrow
\r
282 friend inline bool operator==(shared_count const & a, shared_count const & b)
\r
284 return a.pi_ == b.pi_;
\r
287 friend inline bool operator<(shared_count const & a, shared_count const & b)
\r
289 return std::less<sp_counted_base *>()( a.pi_, b.pi_ );
\r
292 void * get_deleter( sp_typeinfo const & ti ) const
\r
294 return pi_? pi_->get_deleter( ti ): 0;
\r
303 sp_counted_base * pi_;
\r
305 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
\r
309 friend class shared_count;
\r
313 weak_count(): pi_(0) // nothrow
\r
314 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
\r
315 , id_(weak_count_id)
\r
320 weak_count(shared_count const & r): pi_(r.pi_) // nothrow
\r
321 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
\r
322 , id_(weak_count_id)
\r
325 if(pi_ != 0) pi_->weak_add_ref();
\r
328 weak_count(weak_count const & r): pi_(r.pi_) // nothrow
\r
329 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
\r
330 , id_(weak_count_id)
\r
333 if(pi_ != 0) pi_->weak_add_ref();
\r
338 #if defined( BOOST_HAS_RVALUE_REFS )
\r
340 weak_count(weak_count && r): pi_(r.pi_) // nothrow
\r
341 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
\r
342 , id_(weak_count_id)
\r
350 ~weak_count() // nothrow
\r
352 if(pi_ != 0) pi_->weak_release();
\r
353 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
\r
358 weak_count & operator= (shared_count const & r) // nothrow
\r
360 sp_counted_base * tmp = r.pi_;
\r
364 if(tmp != 0) tmp->weak_add_ref();
\r
365 if(pi_ != 0) pi_->weak_release();
\r
372 weak_count & operator= (weak_count const & r) // nothrow
\r
374 sp_counted_base * tmp = r.pi_;
\r
378 if(tmp != 0) tmp->weak_add_ref();
\r
379 if(pi_ != 0) pi_->weak_release();
\r
386 void swap(weak_count & r) // nothrow
\r
388 sp_counted_base * tmp = r.pi_;
\r
393 long use_count() const // nothrow
\r
395 return pi_ != 0? pi_->use_count(): 0;
\r
398 bool empty() const // nothrow
\r
403 friend inline bool operator==(weak_count const & a, weak_count const & b)
\r
405 return a.pi_ == b.pi_;
\r
408 friend inline bool operator<(weak_count const & a, weak_count const & b)
\r
410 return std::less<sp_counted_base *>()(a.pi_, b.pi_);
\r
414 inline shared_count::shared_count( weak_count const & r ): pi_( r.pi_ )
\r
415 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
\r
416 , id_(shared_count_id)
\r
419 if( pi_ == 0 || !pi_->add_ref_lock() )
\r
421 boost::throw_exception( boost::bad_weak_ptr() );
\r
425 inline shared_count::shared_count( weak_count const & r, sp_nothrow_tag ): pi_( r.pi_ )
\r
426 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
\r
427 , id_(shared_count_id)
\r
430 if( pi_ != 0 && !pi_->add_ref_lock() )
\r
436 } // namespace detail
\r
438 } // namespace boost
\r
440 #ifdef __BORLANDC__
\r
441 # pragma warn .8027 // Functions containing try are not expanded inline
\r
444 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SHARED_COUNT_HPP_INCLUDED
\r