First public contribution.
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
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 template<class P, class D, class A> shared_count( P p, D d, A a ): pi_( 0 )
134 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
135 , id_(shared_count_id)
138 typedef sp_counted_impl_pda<P, D, A> impl_type;
139 typedef typename A::template rebind< impl_type >::other A2;
143 #ifndef BOOST_NO_EXCEPTIONS
147 pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );
148 new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
156 a2.deallocate( static_cast< impl_type* >( pi_ ), 1 );
164 pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );
168 new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
173 boost::throw_exception( std::bad_alloc() );
179 #ifndef BOOST_NO_AUTO_PTR
181 // auto_ptr<Y> is special cased to provide the strong guarantee
184 explicit shared_count( std::auto_ptr<Y> & r ): pi_( new sp_counted_impl_p<Y>( r.get() ) )
185 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
186 , id_(shared_count_id)
189 #ifdef BOOST_NO_EXCEPTIONS
193 boost::throw_exception(std::bad_alloc());
203 ~shared_count() // nothrow
205 if( pi_ != 0 ) pi_->release();
206 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
211 shared_count(shared_count const & r): pi_(r.pi_) // nothrow
212 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
213 , id_(shared_count_id)
216 if( pi_ != 0 ) pi_->add_ref_copy();
219 explicit shared_count(weak_count const & r); // throws bad_weak_ptr when r.use_count() == 0
221 shared_count & operator= (shared_count const & r) // nothrow
223 sp_counted_base * tmp = r.pi_;
227 if( tmp != 0 ) tmp->add_ref_copy();
228 if( pi_ != 0 ) pi_->release();
235 void swap(shared_count & r) // nothrow
237 sp_counted_base * tmp = r.pi_;
242 long use_count() const // nothrow
244 return pi_ != 0? pi_->use_count(): 0;
247 bool unique() const // nothrow
249 return use_count() == 1;
252 friend inline bool operator==(shared_count const & a, shared_count const & b)
254 return a.pi_ == b.pi_;
257 friend inline bool operator<(shared_count const & a, shared_count const & b)
259 return std::less<sp_counted_base *>()( a.pi_, b.pi_ );
262 void * get_deleter(std::type_info const & ti) const
264 return pi_? pi_->get_deleter( ti ): 0;
273 sp_counted_base * pi_;
275 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
279 friend class shared_count;
283 weak_count(): pi_(0) // nothrow
284 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
290 weak_count(shared_count const & r): pi_(r.pi_) // nothrow
291 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
292 , id_(shared_count_id)
295 if(pi_ != 0) pi_->weak_add_ref();
298 weak_count(weak_count const & r): pi_(r.pi_) // nothrow
299 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
300 , id_(shared_count_id)
303 if(pi_ != 0) pi_->weak_add_ref();
306 ~weak_count() // nothrow
308 if(pi_ != 0) pi_->weak_release();
309 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
314 weak_count & operator= (shared_count const & r) // nothrow
316 sp_counted_base * tmp = r.pi_;
317 if(tmp != 0) tmp->weak_add_ref();
318 if(pi_ != 0) pi_->weak_release();
324 weak_count & operator= (weak_count const & r) // nothrow
326 sp_counted_base * tmp = r.pi_;
327 if(tmp != 0) tmp->weak_add_ref();
328 if(pi_ != 0) pi_->weak_release();
334 void swap(weak_count & r) // nothrow
336 sp_counted_base * tmp = r.pi_;
341 long use_count() const // nothrow
343 return pi_ != 0? pi_->use_count(): 0;
346 friend inline bool operator==(weak_count const & a, weak_count const & b)
348 return a.pi_ == b.pi_;
351 friend inline bool operator<(weak_count const & a, weak_count const & b)
353 return std::less<sp_counted_base *>()(a.pi_, b.pi_);
357 inline shared_count::shared_count( weak_count const & r ): pi_( r.pi_ )
358 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
359 , id_(shared_count_id)
362 if( pi_ == 0 || !pi_->add_ref_lock() )
364 boost::throw_exception( boost::bad_weak_ptr() );
368 } // namespace detail
373 # pragma warn .8027 // Functions containing try are not expanded inline
376 #endif // #ifndef BOOST_DETAIL_SHARED_COUNT_HPP_INCLUDED