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.
15 // Permission to copy, use, modify, sell and distribute this software
16 // is granted provided this copyright notice appears in all copies.
17 // This software is provided "as is" without express or implied
18 // warranty, and with no claim as to its suitability for any purpose.
21 #include <boost/config.hpp>
23 #if defined(BOOST_SP_USE_STD_ALLOCATOR) && defined(BOOST_SP_USE_QUICK_ALLOCATOR)
24 # error BOOST_SP_USE_STD_ALLOCATOR and BOOST_SP_USE_QUICK_ALLOCATOR are incompatible.
27 #include <boost/checked_delete.hpp>
28 #include <boost/throw_exception.hpp>
29 #include <boost/detail/lightweight_mutex.hpp>
31 #if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
32 #include <boost/detail/quick_allocator.hpp>
35 #include <memory> // std::auto_ptr, std::allocator
36 #include <functional> // std::less
37 #include <exception> // std::exception
38 #include <new> // std::bad_alloc
39 #include <typeinfo> // std::type_info in get_deleter
40 #include <cstddef> // std::size_t
43 # pragma warn -8026 // Functions with excep. spec. are not expanded inline
44 # pragma warn -8027 // Functions containing try are not expanded inline
52 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
54 void sp_scalar_constructor_hook(void * px, std::size_t size, void * pn);
55 void sp_array_constructor_hook(void * px);
56 void sp_scalar_destructor_hook(void * px, std::size_t size, void * pn);
57 void sp_array_destructor_hook(void * px);
62 // The standard library that comes with Borland C++ 5.5.1
63 // defines std::exception and its members as having C calling
64 // convention (-pc). When the definition of bad_weak_ptr
65 // is compiled with -ps, the compiler issues an error.
66 // Hence, the temporary #pragma option -pc below. The version
67 // check is deliberately conservative.
69 #if defined(__BORLANDC__) && __BORLANDC__ == 0x551
70 # pragma option push -pc
73 class bad_weak_ptr: public std::exception
77 virtual char const * what() const throw()
79 return "boost::bad_weak_ptr";
83 #if defined(__BORLANDC__) && __BORLANDC__ == 0x551
94 typedef detail::lightweight_mutex mutex_type;
98 sp_counted_base(): use_count_(1), weak_count_(1)
102 virtual ~sp_counted_base() // nothrow
106 // dispose() is called when use_count_ drops to zero, to release
107 // the resources managed by *this.
109 virtual void dispose() = 0; // nothrow
111 // destruct() is called when weak_count_ drops to zero.
113 virtual void destruct() // nothrow
118 virtual void * get_deleter(std::type_info const & ti) = 0;
122 #if defined(BOOST_HAS_THREADS)
123 mutex_type::scoped_lock lock(mtx_);
130 #if defined(BOOST_HAS_THREADS)
131 mutex_type::scoped_lock lock(mtx_);
133 if(use_count_ == 0) boost::throw_exception(boost::bad_weak_ptr());
137 void release() // nothrow
140 #if defined(BOOST_HAS_THREADS)
141 mutex_type::scoped_lock lock(mtx_);
143 long new_use_count = --use_count_;
145 if(new_use_count != 0) return;
152 void weak_add_ref() // nothrow
154 #if defined(BOOST_HAS_THREADS)
155 mutex_type::scoped_lock lock(mtx_);
160 void weak_release() // nothrow
165 #if defined(BOOST_HAS_THREADS)
166 mutex_type::scoped_lock lock(mtx_);
168 new_weak_count = --weak_count_;
171 if(new_weak_count == 0)
177 long use_count() const // nothrow
179 #if defined(BOOST_HAS_THREADS)
180 mutex_type::scoped_lock lock(mtx_);
187 sp_counted_base(sp_counted_base const &);
188 sp_counted_base & operator= (sp_counted_base const &);
190 long use_count_; // #shared
191 long weak_count_; // #weak + (#shared != 0)
193 #if defined(BOOST_HAS_THREADS) || defined(BOOST_LWM_WIN32)
194 mutable mutex_type mtx_;
198 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
200 template<class T> void cbi_call_constructor_hook(sp_counted_base * pn, T * px, checked_deleter<T> const &, int)
202 boost::sp_scalar_constructor_hook(px, sizeof(T), pn);
205 template<class T> void cbi_call_constructor_hook(sp_counted_base *, T * px, checked_array_deleter<T> const &, int)
207 boost::sp_array_constructor_hook(px);
210 template<class P, class D> void cbi_call_constructor_hook(sp_counted_base *, P const &, D const &, long)
214 template<class T> void cbi_call_destructor_hook(sp_counted_base * pn, T * px, checked_deleter<T> const &, int)
216 boost::sp_scalar_destructor_hook(px, sizeof(T), pn);
219 template<class T> void cbi_call_destructor_hook(sp_counted_base *, T * px, checked_array_deleter<T> const &, int)
221 boost::sp_array_destructor_hook(px);
224 template<class P, class D> void cbi_call_destructor_hook(sp_counted_base *, P const &, D const &, long)
231 // Borland's Codeguard trips up over the -Vx- option here:
234 # pragma option push -Vx-
237 template<class P, class D> class sp_counted_base_impl: public sp_counted_base
241 P ptr; // copy constructor must not throw
242 D del; // copy constructor must not throw
244 sp_counted_base_impl(sp_counted_base_impl const &);
245 sp_counted_base_impl & operator= (sp_counted_base_impl const &);
247 typedef sp_counted_base_impl<P, D> this_type;
251 // pre: initial_use_count <= initial_weak_count, d(p) must not throw
253 sp_counted_base_impl(P p, D d): ptr(p), del(d)
255 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
256 detail::cbi_call_constructor_hook(this, p, d, 0);
260 virtual void dispose() // nothrow
262 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
263 detail::cbi_call_destructor_hook(this, ptr, del, 0);
268 virtual void * get_deleter(std::type_info const & ti)
270 return ti == typeid(D)? &del: 0;
273 #if defined(BOOST_SP_USE_STD_ALLOCATOR)
275 void * operator new(std::size_t)
277 return std::allocator<this_type>().allocate(1, static_cast<this_type *>(0));
280 void operator delete(void * p)
282 std::allocator<this_type>().deallocate(static_cast<this_type *>(p), 1);
287 #if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
289 void * operator new(std::size_t)
291 return quick_allocator<this_type>::alloc();
294 void operator delete(void * p)
296 quick_allocator<this_type>::dealloc(p);
302 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
304 int const shared_count_id = 0x2C35F101;
305 int const weak_count_id = 0x298C38A4;
315 sp_counted_base * pi_;
317 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
321 friend class weak_count;
325 shared_count(): pi_(0) // nothrow
326 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
327 , id_(shared_count_id)
332 template<class P, class D> shared_count(P p, D d): pi_(0)
333 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
334 , id_(shared_count_id)
337 #ifndef BOOST_NO_EXCEPTIONS
341 pi_ = new sp_counted_base_impl<P, D>(p, d);
351 pi_ = new sp_counted_base_impl<P, D>(p, d);
356 boost::throw_exception(std::bad_alloc());
362 #ifndef BOOST_NO_AUTO_PTR
364 // auto_ptr<Y> is special cased to provide the strong guarantee
367 explicit shared_count(std::auto_ptr<Y> & r): pi_(new sp_counted_base_impl< Y *, checked_deleter<Y> >(r.get(), checked_deleter<Y>()))
368 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
369 , id_(shared_count_id)
377 ~shared_count() // nothrow
379 if(pi_ != 0) pi_->release();
380 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
385 shared_count(shared_count const & r): pi_(r.pi_) // nothrow
386 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
387 , id_(shared_count_id)
390 if(pi_ != 0) pi_->add_ref_copy();
393 explicit shared_count(weak_count const & r); // throws bad_weak_ptr when r.use_count() == 0
395 shared_count & operator= (shared_count const & r) // nothrow
397 sp_counted_base * tmp = r.pi_;
398 if(tmp != 0) tmp->add_ref_copy();
399 if(pi_ != 0) pi_->release();
405 void swap(shared_count & r) // nothrow
407 sp_counted_base * tmp = r.pi_;
412 long use_count() const // nothrow
414 return pi_ != 0? pi_->use_count(): 0;
417 bool unique() const // nothrow
419 return use_count() == 1;
422 friend inline bool operator==(shared_count const & a, shared_count const & b)
424 return a.pi_ == b.pi_;
427 friend inline bool operator<(shared_count const & a, shared_count const & b)
429 return std::less<sp_counted_base *>()(a.pi_, b.pi_);
432 void * get_deleter(std::type_info const & ti) const
434 return pi_? pi_->get_deleter(ti): 0;
447 sp_counted_base * pi_;
449 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
453 friend class shared_count;
457 weak_count(): pi_(0) // nothrow
458 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
464 weak_count(shared_count const & r): pi_(r.pi_) // nothrow
465 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
466 , id_(shared_count_id)
469 if(pi_ != 0) pi_->weak_add_ref();
472 weak_count(weak_count const & r): pi_(r.pi_) // nothrow
473 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
474 , id_(shared_count_id)
477 if(pi_ != 0) pi_->weak_add_ref();
480 ~weak_count() // nothrow
482 if(pi_ != 0) pi_->weak_release();
483 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
488 weak_count & operator= (shared_count const & r) // nothrow
490 sp_counted_base * tmp = r.pi_;
491 if(tmp != 0) tmp->weak_add_ref();
492 if(pi_ != 0) pi_->weak_release();
498 weak_count & operator= (weak_count const & r) // nothrow
500 sp_counted_base * tmp = r.pi_;
501 if(tmp != 0) tmp->weak_add_ref();
502 if(pi_ != 0) pi_->weak_release();
508 void swap(weak_count & r) // nothrow
510 sp_counted_base * tmp = r.pi_;
515 long use_count() const // nothrow
517 return pi_ != 0? pi_->use_count(): 0;
520 friend inline bool operator==(weak_count const & a, weak_count const & b)
522 return a.pi_ == b.pi_;
525 friend inline bool operator<(weak_count const & a, weak_count const & b)
527 return std::less<sp_counted_base *>()(a.pi_, b.pi_);
531 inline shared_count::shared_count(weak_count const & r): pi_(r.pi_)
532 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
533 , id_(shared_count_id)
542 boost::throw_exception(boost::bad_weak_ptr());
546 } // namespace detail
551 # pragma warn .8027 // Functions containing try are not expanded inline
552 # pragma warn .8026 // Functions with excep. spec. are not expanded inline
555 #endif // #ifndef BOOST_DETAIL_SHARED_COUNT_HPP_INCLUDED