1
#ifndef BOOST_DETAIL_SHARED_COUNT_HPP_INCLUDED
2
#define BOOST_DETAIL_SHARED_COUNT_HPP_INCLUDED
4
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
9
// detail/shared_count.hpp
11
// Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
13
// Permission to copy, use, modify, sell and distribute this software
14
// is granted provided this copyright notice appears in all copies.
15
// This software is provided "as is" without express or implied
16
// warranty, and with no claim as to its suitability for any purpose.
19
#include <boost/config.hpp>
21
#if defined(BOOST_SP_USE_STD_ALLOCATOR) && defined(BOOST_SP_USE_QUICK_ALLOCATOR)
22
# error BOOST_SP_USE_STD_ALLOCATOR and BOOST_SP_USE_QUICK_ALLOCATOR are incompatible.
25
#include <boost/checked_delete.hpp>
26
#include <boost/throw_exception.hpp>
27
#include <boost/detail/lightweight_mutex.hpp>
29
#if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
30
#include <boost/detail/quick_allocator.hpp>
33
#include <memory> // std::auto_ptr, std::allocator
34
#include <functional> // std::less
35
#include <exception> // std::exception
36
#include <new> // std::bad_alloc
37
#include <typeinfo> // std::type_info in get_deleter
38
#include <cstddef> // std::size_t
41
# pragma warn -8026 // Functions with excep. spec. are not expanded inline
42
# pragma warn -8027 // Functions containing try are not expanded inline
50
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
52
void sp_scalar_constructor_hook(void * px, std::size_t size, void * pn);
53
void sp_array_constructor_hook(void * px);
54
void sp_scalar_destructor_hook(void * px, std::size_t size, void * pn);
55
void sp_array_destructor_hook(void * px);
60
// The standard library that comes with Borland C++ 5.5.1
61
// defines std::exception and its members as having C calling
62
// convention (-pc). When the definition of bad_weak_ptr
63
// is compiled with -ps, the compiler issues an error.
64
// Hence, the temporary #pragma option -pc below. The version
65
// check is deliberately conservative.
67
#if defined(__BORLANDC__) && __BORLANDC__ == 0x551
68
# pragma option push -pc
71
class bad_weak_ptr: public std::exception
75
virtual char const * what() const throw()
77
return "boost::bad_weak_ptr";
81
#if defined(__BORLANDC__) && __BORLANDC__ == 0x551
92
typedef detail::lightweight_mutex mutex_type;
96
sp_counted_base(): use_count_(1), weak_count_(1)
100
virtual ~sp_counted_base() // nothrow
104
// dispose() is called when use_count_ drops to zero, to release
105
// the resources managed by *this.
107
virtual void dispose() = 0; // nothrow
109
// destruct() is called when weak_count_ drops to zero.
111
virtual void destruct() // nothrow
116
virtual void * get_deleter(std::type_info const & ti) = 0;
120
#if defined(BOOST_HAS_THREADS)
121
mutex_type::scoped_lock lock(mtx_);
123
if(use_count_ == 0 && weak_count_ != 0) boost::throw_exception(boost::bad_weak_ptr());
128
void release() // nothrow
131
#if defined(BOOST_HAS_THREADS)
132
mutex_type::scoped_lock lock(mtx_);
134
long new_use_count = --use_count_;
136
if(new_use_count != 0)
147
void weak_add_ref() // nothrow
149
#if defined(BOOST_HAS_THREADS)
150
mutex_type::scoped_lock lock(mtx_);
155
void weak_release() // nothrow
160
#if defined(BOOST_HAS_THREADS)
161
mutex_type::scoped_lock lock(mtx_);
163
new_weak_count = --weak_count_;
166
if(new_weak_count == 0)
172
long use_count() const // nothrow
174
#if defined(BOOST_HAS_THREADS)
175
mutex_type::scoped_lock lock(mtx_);
182
sp_counted_base(sp_counted_base const &);
183
sp_counted_base & operator= (sp_counted_base const &);
185
// inv: use_count_ <= weak_count_
190
#if defined(BOOST_HAS_THREADS)
191
mutable mutex_type mtx_;
195
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
197
template<class T> void cbi_call_constructor_hook(sp_counted_base * pn, T * px, checked_deleter<T> const &, int)
199
boost::sp_scalar_constructor_hook(px, sizeof(T), pn);
202
template<class T> void cbi_call_constructor_hook(sp_counted_base *, T * px, checked_array_deleter<T> const &, int)
204
boost::sp_array_constructor_hook(px);
207
template<class P, class D> void cbi_call_constructor_hook(sp_counted_base *, P const &, D const &, long)
211
template<class T> void cbi_call_destructor_hook(sp_counted_base * pn, T * px, checked_deleter<T> const &, int)
213
boost::sp_scalar_destructor_hook(px, sizeof(T), pn);
216
template<class T> void cbi_call_destructor_hook(sp_counted_base *, T * px, checked_array_deleter<T> const &, int)
218
boost::sp_array_destructor_hook(px);
221
template<class P, class D> void cbi_call_destructor_hook(sp_counted_base *, P const &, D const &, long)
228
// Borland's Codeguard trips up over the -Vx- option here:
231
# pragma option push -Vx-
234
template<class P, class D> class sp_counted_base_impl: public sp_counted_base
238
P ptr; // copy constructor must not throw
239
D del; // copy constructor must not throw
241
sp_counted_base_impl(sp_counted_base_impl const &);
242
sp_counted_base_impl & operator= (sp_counted_base_impl const &);
244
typedef sp_counted_base_impl<P, D> this_type;
248
// pre: initial_use_count <= initial_weak_count, d(p) must not throw
250
sp_counted_base_impl(P p, D d): ptr(p), del(d)
252
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
253
detail::cbi_call_constructor_hook(this, p, d, 0);
257
virtual void dispose() // nothrow
259
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
260
detail::cbi_call_destructor_hook(this, ptr, del, 0);
265
virtual void * get_deleter(std::type_info const & ti)
267
return ti == typeid(D)? &del: 0;
270
#if defined(BOOST_SP_USE_STD_ALLOCATOR)
272
void * operator new(std::size_t)
274
return std::allocator<this_type>().allocate(1, static_cast<this_type *>(0));
277
void operator delete(void * p)
279
std::allocator<this_type>().deallocate(static_cast<this_type *>(p), 1);
284
#if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
286
void * operator new(std::size_t)
288
return quick_allocator<this_type>::alloc();
291
void operator delete(void * p)
293
quick_allocator<this_type>::dealloc(p);
299
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
301
int const shared_count_id = 0x2C35F101;
302
int const weak_count_id = 0x298C38A4;
312
sp_counted_base * pi_;
314
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
318
friend class weak_count;
322
shared_count(): pi_(0) // nothrow
323
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
324
, id_(shared_count_id)
329
template<class P, class D> shared_count(P p, D d): pi_(0)
330
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
331
, id_(shared_count_id)
334
#ifndef BOOST_NO_EXCEPTIONS
338
pi_ = new sp_counted_base_impl<P, D>(p, d);
348
pi_ = new sp_counted_base_impl<P, D>(p, d);
353
boost::throw_exception(std::bad_alloc());
359
#ifndef BOOST_NO_AUTO_PTR
361
// auto_ptr<Y> is special cased to provide the strong guarantee
364
explicit shared_count(std::auto_ptr<Y> & r): pi_(new sp_counted_base_impl< Y *, checked_deleter<Y> >(r.get(), checked_deleter<Y>()))
365
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
366
, id_(shared_count_id)
374
~shared_count() // nothrow
376
if(pi_ != 0) pi_->release();
377
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
382
shared_count(shared_count const & r): pi_(r.pi_) // nothrow
383
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
384
, id_(shared_count_id)
387
if(pi_ != 0) pi_->add_ref();
390
explicit shared_count(weak_count const & r); // throws bad_weak_ptr when r.use_count() == 0
392
shared_count & operator= (shared_count const & r) // nothrow
394
sp_counted_base * tmp = r.pi_;
395
if(tmp != 0) tmp->add_ref();
396
if(pi_ != 0) pi_->release();
402
void swap(shared_count & r) // nothrow
404
sp_counted_base * tmp = r.pi_;
409
long use_count() const // nothrow
411
return pi_ != 0? pi_->use_count(): 0;
414
bool unique() const // nothrow
416
return use_count() == 1;
419
friend inline bool operator==(shared_count const & a, shared_count const & b)
421
return a.pi_ == b.pi_;
424
friend inline bool operator<(shared_count const & a, shared_count const & b)
426
return std::less<sp_counted_base *>()(a.pi_, b.pi_);
429
void * get_deleter(std::type_info const & ti) const
431
return pi_? pi_->get_deleter(ti): 0;
444
sp_counted_base * pi_;
446
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
450
friend class shared_count;
454
weak_count(): pi_(0) // nothrow
455
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
461
weak_count(shared_count const & r): pi_(r.pi_) // nothrow
462
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
463
, id_(shared_count_id)
466
if(pi_ != 0) pi_->weak_add_ref();
469
weak_count(weak_count const & r): pi_(r.pi_) // nothrow
470
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
471
, id_(shared_count_id)
474
if(pi_ != 0) pi_->weak_add_ref();
477
~weak_count() // nothrow
479
if(pi_ != 0) pi_->weak_release();
480
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
485
weak_count & operator= (shared_count const & r) // nothrow
487
sp_counted_base * tmp = r.pi_;
488
if(tmp != 0) tmp->weak_add_ref();
489
if(pi_ != 0) pi_->weak_release();
495
weak_count & operator= (weak_count const & r) // nothrow
497
sp_counted_base * tmp = r.pi_;
498
if(tmp != 0) tmp->weak_add_ref();
499
if(pi_ != 0) pi_->weak_release();
505
void swap(weak_count & r) // nothrow
507
sp_counted_base * tmp = r.pi_;
512
long use_count() const // nothrow
514
return pi_ != 0? pi_->use_count(): 0;
517
friend inline bool operator==(weak_count const & a, weak_count const & b)
519
return a.pi_ == b.pi_;
522
friend inline bool operator<(weak_count const & a, weak_count const & b)
524
return std::less<sp_counted_base *>()(a.pi_, b.pi_);
528
inline shared_count::shared_count(weak_count const & r): pi_(r.pi_)
529
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
530
, id_(shared_count_id)
539
boost::throw_exception(boost::bad_weak_ptr());
543
} // namespace detail
548
# pragma warn .8027 // Functions containing try are not expanded inline
549
# pragma warn .8026 // Functions with excep. spec. are not expanded inline
552
#endif // #ifndef BOOST_DETAIL_SHARED_COUNT_HPP_INCLUDED