1 #ifndef BOOST_DETAIL_SHARED_COUNT_HPP_INCLUDED
2 #define BOOST_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/detail/bad_weak_ptr.hpp>
29 #include <boost/detail/sp_counted_base.hpp>
30 #include <boost/detail/sp_counted_impl.hpp>
31 // In order to avoid circular dependencies with Boost.TR1
32 // we make sure that our include of <memory> doesn't try to
33 // pull in the TR1 headers: that's why we use this header
34 // rather than including <memory> directly:
35 #include <boost/config/no_tr1/memory.hpp> // std::auto_ptr
36 #include <functional> // std::less
37 #include <new> // std::bad_alloc
45 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
47 int const shared_count_id = 0x2C35F101;
48 int const weak_count_id = 0x298C38A4;
52 struct sp_nothrow_tag {};
60 sp_counted_base * pi_;
62 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
66 friend class weak_count;
70 shared_count(): pi_(0) // nothrow
71 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
72 , id_(shared_count_id)
77 template<class Y> explicit shared_count( Y * p ): pi_( 0 )
78 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
79 , id_(shared_count_id)
82 #ifndef BOOST_NO_EXCEPTIONS
86 pi_ = new sp_counted_impl_p<Y>( p );
90 boost::checked_delete( p );
96 pi_ = new sp_counted_impl_p<Y>( p );
100 boost::checked_delete( p );
101 boost::throw_exception( std::bad_alloc() );
107 #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, <= 1200 )
108 template<class Y, class D> shared_count( Y * p, D d ): pi_(0)
110 template<class P, class D> shared_count( P p, D d ): pi_(0)
112 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
113 , id_(shared_count_id)
116 #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, <= 1200 )
119 #ifndef BOOST_NO_EXCEPTIONS
123 pi_ = new sp_counted_impl_pd<P, D>(p, d);
133 pi_ = new sp_counted_impl_pd<P, D>(p, d);
138 boost::throw_exception(std::bad_alloc());
144 template<class P, class D, class A> shared_count( P p, D d, A a ): pi_( 0 )
145 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
146 , id_(shared_count_id)
149 typedef sp_counted_impl_pda<P, D, A> impl_type;
150 typedef typename A::template rebind< impl_type >::other A2;
154 #ifndef BOOST_NO_EXCEPTIONS
158 pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );
159 new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
167 a2.deallocate( static_cast< impl_type* >( pi_ ), 1 );
175 pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );
179 new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
184 boost::throw_exception( std::bad_alloc() );
190 #ifndef BOOST_NO_AUTO_PTR
192 // auto_ptr<Y> is special cased to provide the strong guarantee
195 explicit shared_count( std::auto_ptr<Y> & r ): pi_( new sp_counted_impl_p<Y>( r.get() ) )
196 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
197 , id_(shared_count_id)
200 #ifdef BOOST_NO_EXCEPTIONS
204 boost::throw_exception(std::bad_alloc());
214 ~shared_count() // nothrow
216 if( pi_ != 0 ) pi_->release();
217 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
222 shared_count(shared_count const & r): pi_(r.pi_) // nothrow
223 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
224 , id_(shared_count_id)
227 if( pi_ != 0 ) pi_->add_ref_copy();
230 explicit shared_count(weak_count const & r); // throws bad_weak_ptr when r.use_count() == 0
231 shared_count( weak_count const & r, sp_nothrow_tag ); // constructs an empty *this when r.use_count() == 0
233 shared_count & operator= (shared_count const & r) // nothrow
235 sp_counted_base * tmp = r.pi_;
239 if( tmp != 0 ) tmp->add_ref_copy();
240 if( pi_ != 0 ) pi_->release();
247 void swap(shared_count & r) // nothrow
249 sp_counted_base * tmp = r.pi_;
254 long use_count() const // nothrow
256 return pi_ != 0? pi_->use_count(): 0;
259 bool unique() const // nothrow
261 return use_count() == 1;
264 bool empty() const // nothrow
269 friend inline bool operator==(shared_count const & a, shared_count const & b)
271 return a.pi_ == b.pi_;
274 friend inline bool operator<(shared_count const & a, shared_count const & b)
276 return std::less<sp_counted_base *>()( a.pi_, b.pi_ );
279 void * get_deleter( sp_typeinfo const & ti ) const
281 return pi_? pi_->get_deleter( ti ): 0;
290 sp_counted_base * pi_;
292 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
296 friend class shared_count;
300 weak_count(): pi_(0) // nothrow
301 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
307 weak_count(shared_count const & r): pi_(r.pi_) // nothrow
308 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
309 , id_(shared_count_id)
312 if(pi_ != 0) pi_->weak_add_ref();
315 weak_count(weak_count const & r): pi_(r.pi_) // nothrow
316 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
317 , id_(shared_count_id)
320 if(pi_ != 0) pi_->weak_add_ref();
323 ~weak_count() // nothrow
325 if(pi_ != 0) pi_->weak_release();
326 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
331 weak_count & operator= (shared_count const & r) // nothrow
333 sp_counted_base * tmp = r.pi_;
337 if(tmp != 0) tmp->weak_add_ref();
338 if(pi_ != 0) pi_->weak_release();
345 weak_count & operator= (weak_count const & r) // nothrow
347 sp_counted_base * tmp = r.pi_;
351 if(tmp != 0) tmp->weak_add_ref();
352 if(pi_ != 0) pi_->weak_release();
359 void swap(weak_count & r) // nothrow
361 sp_counted_base * tmp = r.pi_;
366 long use_count() const // nothrow
368 return pi_ != 0? pi_->use_count(): 0;
371 friend inline bool operator==(weak_count const & a, weak_count const & b)
373 return a.pi_ == b.pi_;
376 friend inline bool operator<(weak_count const & a, weak_count const & b)
378 return std::less<sp_counted_base *>()(a.pi_, b.pi_);
382 inline shared_count::shared_count( weak_count const & r ): pi_( r.pi_ )
383 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
384 , id_(shared_count_id)
387 if( pi_ == 0 || !pi_->add_ref_lock() )
389 boost::throw_exception( boost::bad_weak_ptr() );
393 inline shared_count::shared_count( weak_count const & r, sp_nothrow_tag ): pi_( r.pi_ )
394 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
395 , id_(shared_count_id)
398 if( pi_ != 0 && !pi_->add_ref_lock() )
404 } // namespace detail
409 # pragma warn .8027 // Functions containing try are not expanded inline
412 #endif // #ifndef BOOST_DETAIL_SHARED_COUNT_HPP_INCLUDED