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>
32 #include <memory> // std::auto_ptr, std::allocator
33 #include <functional> // std::less
34 #include <new> // std::bad_alloc
35 #include <typeinfo> // std::type_info in get_deleter
43 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
45 int const shared_count_id = 0x2C35F101;
46 int const weak_count_id = 0x298C38A4;
56 sp_counted_base * pi_;
58 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
62 friend class weak_count;
66 shared_count(): pi_(0) // nothrow
67 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
68 , id_(shared_count_id)
73 template<class Y> explicit shared_count( Y * p ): pi_( 0 )
74 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
75 , id_(shared_count_id)
78 #ifndef BOOST_NO_EXCEPTIONS
82 pi_ = new sp_counted_impl_p<Y>( p );
86 boost::checked_delete( p );
92 pi_ = new sp_counted_impl_p<Y>( p );
96 boost::checked_delete( p );
97 boost::throw_exception( std::bad_alloc() );
103 template<class P, class D> shared_count(P p, D d): pi_(0)
104 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
105 , id_(shared_count_id)
108 #ifndef BOOST_NO_EXCEPTIONS
112 pi_ = new sp_counted_impl_pd<P, D>(p, d);
122 pi_ = new sp_counted_impl_pd<P, D>(p, d);
127 boost::throw_exception(std::bad_alloc());
133 #ifndef BOOST_NO_AUTO_PTR
135 // auto_ptr<Y> is special cased to provide the strong guarantee
138 explicit shared_count( std::auto_ptr<Y> & r ): pi_( new sp_counted_impl_p<Y>( r.get() ) )
139 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
140 , id_(shared_count_id)
143 #ifdef BOOST_NO_EXCEPTIONS
147 boost::throw_exception(std::bad_alloc());
157 ~shared_count() // nothrow
159 if( pi_ != 0 ) pi_->release();
160 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
165 shared_count(shared_count const & r): pi_(r.pi_) // nothrow
166 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
167 , id_(shared_count_id)
170 if( pi_ != 0 ) pi_->add_ref_copy();
173 explicit shared_count(weak_count const & r); // throws bad_weak_ptr when r.use_count() == 0
175 shared_count & operator= (shared_count const & r) // nothrow
177 sp_counted_base * tmp = r.pi_;
181 if( tmp != 0 ) tmp->add_ref_copy();
182 if( pi_ != 0 ) pi_->release();
189 void swap(shared_count & r) // nothrow
191 sp_counted_base * tmp = r.pi_;
196 long use_count() const // nothrow
198 return pi_ != 0? pi_->use_count(): 0;
201 bool unique() const // nothrow
203 return use_count() == 1;
206 friend inline bool operator==(shared_count const & a, shared_count const & b)
208 return a.pi_ == b.pi_;
211 friend inline bool operator<(shared_count const & a, shared_count const & b)
213 return std::less<sp_counted_base *>()( a.pi_, b.pi_ );
216 void * get_deleter(std::type_info const & ti) const
218 return pi_? pi_->get_deleter( ti ): 0;
227 sp_counted_base * pi_;
229 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
233 friend class shared_count;
237 weak_count(): pi_(0) // nothrow
238 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
244 weak_count(shared_count const & r): pi_(r.pi_) // nothrow
245 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
246 , id_(shared_count_id)
249 if(pi_ != 0) pi_->weak_add_ref();
252 weak_count(weak_count const & r): pi_(r.pi_) // nothrow
253 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
254 , id_(shared_count_id)
257 if(pi_ != 0) pi_->weak_add_ref();
260 ~weak_count() // nothrow
262 if(pi_ != 0) pi_->weak_release();
263 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
268 weak_count & operator= (shared_count const & r) // nothrow
270 sp_counted_base * tmp = r.pi_;
271 if(tmp != 0) tmp->weak_add_ref();
272 if(pi_ != 0) pi_->weak_release();
278 weak_count & operator= (weak_count const & r) // nothrow
280 sp_counted_base * tmp = r.pi_;
281 if(tmp != 0) tmp->weak_add_ref();
282 if(pi_ != 0) pi_->weak_release();
288 void swap(weak_count & r) // nothrow
290 sp_counted_base * tmp = r.pi_;
295 long use_count() const // nothrow
297 return pi_ != 0? pi_->use_count(): 0;
300 friend inline bool operator==(weak_count const & a, weak_count const & b)
302 return a.pi_ == b.pi_;
305 friend inline bool operator<(weak_count const & a, weak_count const & b)
307 return std::less<sp_counted_base *>()(a.pi_, b.pi_);
311 inline shared_count::shared_count( weak_count const & r ): pi_( r.pi_ )
312 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
313 , id_(shared_count_id)
316 if( pi_ == 0 || !pi_->add_ref_lock() )
318 boost::throw_exception( boost::bad_weak_ptr() );
322 } // namespace detail
327 # pragma warn .8027 // Functions containing try are not expanded inline
330 #endif // #ifndef BOOST_DETAIL_SHARED_COUNT_HPP_INCLUDED