boost_shared_ptr.h [plain text]
#ifndef _BOOST_SHARED_PTR_H
#define _BOOST_SHARED_PTR_H 1
namespace std
{
namespace tr1
{
class bad_weak_ptr : public std::exception
{
public:
virtual char const* what() const throw()
{
return "tr1::bad_weak_ptr";
}
};
inline void
__throw_bad_weak_ptr()
{
#if __EXCEPTIONS
throw bad_weak_ptr();
#else
std::abort();
#endif
}
template <typename _Tp>
struct _Sp_deleter
{
typedef void result_type;
typedef _Tp* argument_type;
void
operator()(_Tp* p) const
{ delete p; }
};
class _Sp_counted_base
{
public:
_Sp_counted_base()
: _M_use_count(1), _M_weak_count(1)
{ }
virtual
~_Sp_counted_base() { }
virtual void
dispose() = 0;
virtual void
destroy() {
delete this;
}
virtual void*
get_deleter(const std::type_info&) = 0;
void
add_ref_copy()
{
__gnu_cxx::__atomic_add(&_M_use_count, 1);
}
void
add_ref_lock()
{
__gnu_cxx::lock lock(_M_mutex);
if (__gnu_cxx::__exchange_and_add(&_M_use_count, 1) == 0)
{
_M_use_count = 0;
__throw_bad_weak_ptr();
}
}
void
release() {
if (__gnu_cxx::__exchange_and_add(&_M_use_count, -1) == 1)
{
dispose();
__glibcxx_mutex_lock(_M_mutex);
__glibcxx_mutex_unlock(_M_mutex);
weak_release();
}
}
void
weak_add_ref() {
__gnu_cxx::__atomic_add(&_M_weak_count, 1);
}
void
weak_release() {
if (__gnu_cxx::__exchange_and_add(&_M_weak_count, -1) == 1)
{
__glibcxx_mutex_lock(_M_mutex);
__glibcxx_mutex_unlock(_M_mutex);
destroy();
}
}
long
use_count() const {
return _M_use_count; }
private:
_Sp_counted_base(_Sp_counted_base const&);
_Sp_counted_base& operator= (_Sp_counted_base const&);
_Atomic_word _M_use_count; _Atomic_word _M_weak_count; __gnu_cxx::mutex_type _M_mutex;
};
template <typename _Ptr, typename _Deleter>
class _Sp_counted_base_impl : public _Sp_counted_base
{
public:
_Sp_counted_base_impl(_Ptr __p, _Deleter __d)
: _M_ptr(__p), _M_del(__d)
{ }
virtual void
dispose() {
_M_del(_M_ptr);
}
virtual void*
get_deleter(const std::type_info& __ti)
{
return __ti == typeid(_Deleter) ? &_M_del : 0;
}
private:
_Sp_counted_base_impl(const _Sp_counted_base_impl&);
_Sp_counted_base_impl& operator=(const _Sp_counted_base_impl&);
_Ptr _M_ptr; _Deleter _M_del; };
class weak_count;
class shared_count
{
private:
_Sp_counted_base* _M_pi;
friend class weak_count;
public:
shared_count()
: _M_pi(0) { }
template <typename _Ptr, typename _Deleter>
shared_count(_Ptr __p, _Deleter __d)
: _M_pi(0)
{
try
{
_M_pi = new _Sp_counted_base_impl<_Ptr, _Deleter>(__p, __d);
}
catch(...)
{
__d(__p); __throw_exception_again;
}
}
template <typename _Tp>
explicit shared_count(std::auto_ptr<_Tp>& __r)
: _M_pi(new _Sp_counted_base_impl<_Tp*,_Sp_deleter<_Tp> >(
__r.get(), _Sp_deleter<_Tp>()
))
{ __r.release(); }
explicit shared_count(const weak_count& __r);
~shared_count() {
if (_M_pi != 0)
_M_pi->release();
}
shared_count(const shared_count& __r)
: _M_pi(__r._M_pi) {
if (_M_pi != 0)
_M_pi->add_ref_copy();
}
shared_count&
operator=(const shared_count& __r) {
_Sp_counted_base* __tmp = __r._M_pi;
if(__tmp != _M_pi)
{
if(__tmp != 0)
__tmp->add_ref_copy();
if(_M_pi != 0)
_M_pi->release();
_M_pi = __tmp;
}
return *this;
}
void swap(shared_count& __r) {
_Sp_counted_base* __tmp = __r._M_pi;
__r._M_pi = _M_pi;
_M_pi = __tmp;
}
long
use_count() const { return _M_pi != 0 ? _M_pi->use_count() : 0; }
bool
unique() const { return this->use_count() == 1; }
friend inline bool
operator==(const shared_count& __a, const shared_count& __b)
{ return __a._M_pi == __b._M_pi; }
friend inline bool
operator<(const shared_count& __a, const shared_count& __b)
{ return std::less<_Sp_counted_base*>()(__a._M_pi, __b._M_pi); }
void*
get_deleter(const std::type_info& __ti) const
{ return _M_pi ? _M_pi->get_deleter(__ti) : 0; }
};
class weak_count
{
private:
_Sp_counted_base * _M_pi;
friend class shared_count;
public:
weak_count()
: _M_pi(0) { }
weak_count(const shared_count& __r)
: _M_pi(__r._M_pi) {
if (_M_pi != 0)
_M_pi->weak_add_ref();
}
weak_count(const weak_count& __r)
: _M_pi(__r._M_pi) {
if (_M_pi != 0)
_M_pi->weak_add_ref();
}
~weak_count() {
if (_M_pi != 0)
_M_pi->weak_release();
}
weak_count&
operator=(const shared_count& __r) {
_Sp_counted_base* __tmp = __r._M_pi;
if (__tmp != 0)
__tmp->weak_add_ref();
if (_M_pi != 0)
_M_pi->weak_release();
_M_pi = __tmp;
return *this;
}
weak_count&
operator=(const weak_count& __r) {
_Sp_counted_base * __tmp = __r._M_pi;
if (__tmp != 0)
__tmp->weak_add_ref();
if (_M_pi != 0)
_M_pi->weak_release();
_M_pi = __tmp;
return *this;
}
void
swap(weak_count& __r) {
_Sp_counted_base * __tmp = __r._M_pi;
__r._M_pi = _M_pi;
_M_pi = __tmp;
}
long
use_count() const { return _M_pi != 0 ? _M_pi->use_count() : 0; }
friend inline bool
operator==(const weak_count& __a, const weak_count& __b)
{ return __a._M_pi == __b._M_pi; }
friend inline bool
operator<(const weak_count& __a, const weak_count& __b)
{ return std::less<_Sp_counted_base*>()(__a._M_pi, __b._M_pi); }
};
inline
shared_count::shared_count(const weak_count& __r)
: _M_pi(__r._M_pi)
{
if (_M_pi != 0)
{
_M_pi->add_ref_lock();
}
else
{
__throw_bad_weak_ptr();
}
}
template <typename _Tp> class weak_ptr;
template <typename _Tp> class enable_shared_from_this;
struct __static_cast_tag {};
struct __const_cast_tag {};
struct __dynamic_cast_tag {};
struct __polymorphic_cast_tag {};
template<class _Tp> struct shared_ptr_traits
{
typedef _Tp & reference;
};
template<> struct shared_ptr_traits<void>
{
typedef void reference;
};
template<> struct shared_ptr_traits<void const>
{
typedef void reference;
};
template<> struct shared_ptr_traits<void volatile>
{
typedef void reference;
};
template<> struct shared_ptr_traits<void const volatile>
{
typedef void reference;
};
template <typename _Tp1, typename _Tp2>
void
__enable_shared_from_this( const shared_count& __pn,
const enable_shared_from_this<_Tp1>* __pe,
const _Tp2* __px );
inline void
__enable_shared_from_this(const shared_count&, ...)
{ }
template <typename _Tp>
class shared_ptr
{
typedef typename shared_ptr_traits<_Tp>::reference _Reference;
public:
typedef _Tp element_type;
shared_ptr() : _M_ptr(0), _M_refcount() { }
template <typename _Tp1>
explicit shared_ptr(_Tp1* __p)
: _M_ptr(__p), _M_refcount(__p, _Sp_deleter<_Tp1>())
{
__glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
__enable_shared_from_this( _M_refcount, __p, __p );
}
template <typename _Tp1, typename _Deleter>
shared_ptr(_Tp1* __p, _Deleter __d)
: _M_ptr(__p), _M_refcount(__p, __d)
{
__glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
__enable_shared_from_this( _M_refcount, __p, __p );
}
template <typename _Tp1>
shared_ptr(const shared_ptr<_Tp1>& __r)
: _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) {
__glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
}
template <typename _Tp1>
explicit shared_ptr(const weak_ptr<_Tp1>& __r)
: _M_refcount(__r._M_refcount) {
__glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
_M_ptr = __r._M_ptr;
}
template <typename _Tp1>
explicit shared_ptr(std::auto_ptr<_Tp1>& __r)
: _M_ptr(__r.get()), _M_refcount()
{
_Tp1 * __tmp = __r.get();
_M_refcount = shared_count(__r);
__enable_shared_from_this( _M_refcount, __tmp, __tmp );
}
template <typename _Tp1>
shared_ptr(const shared_ptr<_Tp1>& __r, __static_cast_tag)
: _M_ptr(static_cast<element_type*>(__r._M_ptr))
, _M_refcount(__r._M_refcount)
{ }
template <typename _Tp1>
shared_ptr(const shared_ptr<_Tp1>& __r, __const_cast_tag)
: _M_ptr(const_cast<element_type*>(__r._M_ptr))
, _M_refcount(__r._M_refcount)
{ }
template <typename _Tp1>
shared_ptr(const shared_ptr<_Tp1>& __r, __dynamic_cast_tag)
: _M_ptr(dynamic_cast<element_type*>(__r._M_ptr))
, _M_refcount(__r._M_refcount)
{
if (_M_ptr == 0) {
_M_refcount = shared_count();
}
}
template <typename _Tp1>
shared_ptr&
operator=(const shared_ptr<_Tp1>& __r) {
_M_ptr = __r._M_ptr;
_M_refcount = __r._M_refcount; return *this;
}
template <typename _Tp1>
shared_ptr&
operator=(std::auto_ptr<_Tp1>& __r)
{
shared_ptr(__r).swap(*this);
return *this;
}
void
reset() { shared_ptr().swap(*this); }
template <typename _Tp1>
void
reset(_Tp1* __p) {
_GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != _M_ptr); shared_ptr(__p).swap(*this);
}
template <typename _Tp1, typename _Deleter>
void
reset(_Tp1 * __p, _Deleter __d)
{ shared_ptr(__p, __d).swap(*this); }
_Reference
operator*() const {
_GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
return *_M_ptr;
}
_Tp*
operator->() const {
_GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
return _M_ptr;
}
_Tp*
get() const { return _M_ptr; }
private:
typedef _Tp* shared_ptr::*__unspecified_bool_type;
public:
operator __unspecified_bool_type() const { return _M_ptr == 0 ? 0 : &shared_ptr::_M_ptr; }
bool
unique() const { return _M_refcount.unique(); }
long
use_count() const { return _M_refcount.use_count(); }
void
swap(shared_ptr<_Tp>& __other) {
std::swap(_M_ptr, __other._M_ptr);
_M_refcount.swap(__other._M_refcount);
}
private:
template <typename _Tp1>
bool
_M_less(const shared_ptr<_Tp1>& __rhs) const
{ return _M_refcount < __rhs._M_refcount; }
void*
_M_get_deleter(const std::type_info& __ti) const
{ return _M_refcount.get_deleter(__ti); }
template <typename _Tp1> friend class shared_ptr;
template <typename _Tp1> friend class weak_ptr;
template <typename _Del>
friend inline _Del*
get_deleter(const shared_ptr& __p)
{ return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del))); }
template <typename _Tp1>
friend inline bool
operator==(const shared_ptr& __a, const shared_ptr<_Tp1>& __b)
{ return __a.get() == __b.get(); }
template <typename _Tp1>
friend inline bool
operator!=(const shared_ptr& __a, const shared_ptr<_Tp1>& __b)
{ return __a.get() != __b.get(); }
template <typename _Tp1>
friend inline bool
operator<(const shared_ptr& __a, const shared_ptr<_Tp1>& __b)
{ return __a._M_less(__b); }
_Tp* _M_ptr; shared_count _M_refcount; };
template <typename _Tp, typename _Tp1>
shared_ptr<_Tp>
static_pointer_cast(const shared_ptr<_Tp1>& __r)
{
return shared_ptr<_Tp>(__r, __static_cast_tag());
}
template <typename _Tp, typename _Tp1>
shared_ptr<_Tp>
const_pointer_cast(const shared_ptr<_Tp1>& __r)
{
return shared_ptr<_Tp>(__r, __const_cast_tag());
}
template <typename _Tp, typename _Tp1>
shared_ptr<_Tp>
dynamic_pointer_cast(const shared_ptr<_Tp1>& __r)
{
return shared_ptr<_Tp>(__r, __dynamic_cast_tag());
}
template <typename _Ch, typename _Tr, typename _Tp>
std::basic_ostream<_Ch,_Tr>&
operator<<(std::basic_ostream<_Ch,_Tr>& __os, const shared_ptr<_Tp>& __p)
{
__os << __p.get();
return __os;
}
template <typename _Tp>
class weak_ptr
{
public:
typedef _Tp element_type;
weak_ptr()
: _M_ptr(0), _M_refcount() { }
template <typename _Tp1>
weak_ptr(const weak_ptr<_Tp1>& r)
: _M_refcount(r._M_refcount) {
__glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
_M_ptr = r.lock().get();
}
template <typename _Tp1>
weak_ptr(const shared_ptr<_Tp1>& r)
: _M_ptr(r._M_ptr), _M_refcount(r._M_refcount) {
__glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
}
template <typename _Tp1>
weak_ptr&
operator=(const weak_ptr<_Tp1>& r) {
_M_ptr = r.lock().get();
_M_refcount = r._M_refcount;
return *this;
}
template <typename _Tp1>
weak_ptr&
operator=(const shared_ptr<_Tp1>& r) {
_M_ptr = r._M_ptr;
_M_refcount = r._M_refcount;
return *this;
}
shared_ptr<_Tp>
lock() const {
#ifdef __GTHREADS
if (expired())
{
return shared_ptr<element_type>();
}
try
{
return shared_ptr<element_type>(*this);
}
catch (const bad_weak_ptr&)
{
return shared_ptr<element_type>();
}
#else
return expired() ? shared_ptr<element_type>() : shared_ptr<element_type>(*this);
#endif
}
long
use_count() const { return _M_refcount.use_count(); }
bool
expired() const { return _M_refcount.use_count() == 0; }
void
reset() { weak_ptr().swap(*this); }
void
swap(weak_ptr& __s) {
std::swap(_M_ptr, __s._M_ptr);
_M_refcount.swap(__s._M_refcount);
}
private:
template <typename _Tp1>
bool
_M_less(const weak_ptr<_Tp1>& __rhs) const
{ return _M_refcount < __rhs._M_refcount; }
void
_M_assign(_Tp* __ptr, const shared_count& __refcount)
{
_M_ptr = __ptr;
_M_refcount = __refcount;
}
template <typename _Tp1>
friend inline bool
operator<(const weak_ptr& __lhs, const weak_ptr<_Tp1>& __rhs)
{ return __lhs._M_less(__rhs); }
template <typename _Tp1> friend class weak_ptr;
template <typename _Tp1> friend class shared_ptr;
friend class enable_shared_from_this<_Tp>;
_Tp* _M_ptr; weak_count _M_refcount;
};
template <typename _Tp>
class enable_shared_from_this
{
protected:
enable_shared_from_this()
{ }
enable_shared_from_this(const enable_shared_from_this&)
{ }
enable_shared_from_this&
operator=(const enable_shared_from_this&)
{ return *this; }
~enable_shared_from_this()
{ }
public:
shared_ptr<_Tp>
shared_from_this()
{
shared_ptr<_Tp> p(this->_M_weak_this);
return p;
}
shared_ptr<const _Tp>
shared_from_this() const
{
shared_ptr<const _Tp> p(this->_M_weak_this);
return p;
}
private:
template <typename _Tp1>
void
_M_weak_assign(_Tp1* __p, const shared_count& __n) const
{ _M_weak_this._M_assign(__p, __n); }
template <typename _Tp1>
friend void
__enable_shared_from_this( const shared_count& __pn, const enable_shared_from_this* __pe, const _Tp1* __px)
{
if(__pe != 0)
__pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
}
mutable weak_ptr<_Tp> _M_weak_this;
};
}
template <typename _Tp>
inline void
swap(tr1::shared_ptr<_Tp>& __a, tr1::shared_ptr<_Tp>& __b)
{
__a.swap(__b);
}
template <typename _Tp>
void
swap(tr1::weak_ptr<_Tp>& __a, tr1::weak_ptr<_Tp>& __b)
{
__a.swap(__b);
}
}
#endif