Commit 81809516 by Paolo Carlini Committed by Paolo Carlini

boost_shared_ptr.h: Trivial formatting fixes.

2005-11-09  Paolo Carlini  <pcarlini@suse.de>

	* include/tr1/boost_shared_ptr.h: Trivial formatting fixes.

From-SVN: r106689
parent fb3712f6
2005-11-09 Paolo Carlini <pcarlini@suse.de>
* include/tr1/boost_shared_ptr.h: Trivial formatting fixes.
2005-11-08 Benjamin Kosnik <bkoz@redhat.com> 2005-11-08 Benjamin Kosnik <bkoz@redhat.com>
* testsuite/lib/libstdc++.exp (check_v3_target_debug_mode): Use * testsuite/lib/libstdc++.exp (check_v3_target_debug_mode): Use
......
...@@ -64,10 +64,9 @@ class bad_weak_ptr : public std::exception ...@@ -64,10 +64,9 @@ class bad_weak_ptr : public std::exception
{ {
public: public:
virtual char const* what() const throw() virtual char const*
{ what() const throw()
return "tr1::bad_weak_ptr"; { return "tr1::bad_weak_ptr"; }
}
}; };
// Helper for exception objects in <tr1/memory> // Helper for exception objects in <tr1/memory>
...@@ -83,7 +82,7 @@ __throw_bad_weak_ptr() ...@@ -83,7 +82,7 @@ __throw_bad_weak_ptr()
} }
template <typename _Tp> template<typename _Tp>
struct _Sp_deleter struct _Sp_deleter
{ {
typedef void result_type; typedef void result_type;
...@@ -140,22 +139,22 @@ public: ...@@ -140,22 +139,22 @@ public:
{ {
__gnu_cxx::lock lock(_M_mutex); __gnu_cxx::lock lock(_M_mutex);
if (__gnu_cxx::__exchange_and_add(&_M_use_count, 1) == 0) if (__gnu_cxx::__exchange_and_add(&_M_use_count, 1) == 0)
{ {
_M_use_count = 0; _M_use_count = 0;
__throw_bad_weak_ptr(); __throw_bad_weak_ptr();
} }
} }
void void
release() // nothrow release() // nothrow
{ {
if (__gnu_cxx::__exchange_and_add(&_M_use_count, -1) == 1) if (__gnu_cxx::__exchange_and_add(&_M_use_count, -1) == 1)
{ {
dispose(); dispose();
__glibcxx_mutex_lock(_M_mutex); __glibcxx_mutex_lock(_M_mutex);
__glibcxx_mutex_unlock(_M_mutex); __glibcxx_mutex_unlock(_M_mutex);
weak_release(); weak_release();
} }
} }
void void
...@@ -168,11 +167,11 @@ public: ...@@ -168,11 +167,11 @@ public:
weak_release() // nothrow weak_release() // nothrow
{ {
if (__gnu_cxx::__exchange_and_add(&_M_weak_count, -1) == 1) if (__gnu_cxx::__exchange_and_add(&_M_weak_count, -1) == 1)
{ {
__glibcxx_mutex_lock(_M_mutex); __glibcxx_mutex_lock(_M_mutex);
__glibcxx_mutex_unlock(_M_mutex); __glibcxx_mutex_unlock(_M_mutex);
destroy(); destroy();
} }
} }
long long
...@@ -184,45 +183,46 @@ public: ...@@ -184,45 +183,46 @@ public:
private: private:
_Sp_counted_base(_Sp_counted_base const&); _Sp_counted_base(_Sp_counted_base const&);
_Sp_counted_base& operator= (_Sp_counted_base const&); _Sp_counted_base& operator=(_Sp_counted_base const&);
_Atomic_word _M_use_count; // #shared _Atomic_word _M_use_count; // #shared
_Atomic_word _M_weak_count; // #weak + (#shared != 0) _Atomic_word _M_weak_count; // #weak + (#shared != 0)
__gnu_cxx::mutex_type _M_mutex; __gnu_cxx::mutex_type _M_mutex;
}; };
template <typename _Ptr, typename _Deleter> template<typename _Ptr, typename _Deleter>
class _Sp_counted_base_impl : public _Sp_counted_base class _Sp_counted_base_impl
{ : public _Sp_counted_base
public:
/**
* @brief
* @pre d(p) must not throw.
*/
_Sp_counted_base_impl(_Ptr __p, _Deleter __d)
: _M_ptr(__p), _M_del(__d)
{ }
virtual void
dispose() // nothrow
{
_M_del(_M_ptr);
}
virtual void*
get_deleter(const std::type_info& __ti)
{ {
return __ti == typeid(_Deleter) ? &_M_del : 0; public:
}
private:
_Sp_counted_base_impl(const _Sp_counted_base_impl&);
_Sp_counted_base_impl& operator=(const _Sp_counted_base_impl&);
_Ptr _M_ptr; // copy constructor must not throw /**
_Deleter _M_del; // copy constructor must not throw * @brief
}; * @pre d(p) must not throw.
*/
_Sp_counted_base_impl(_Ptr __p, _Deleter __d)
: _M_ptr(__p), _M_del(__d)
{ }
virtual void
dispose() // nothrow
{
_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; // copy constructor must not throw
_Deleter _M_del; // copy constructor must not throw
};
class weak_count; class weak_count;
...@@ -240,28 +240,27 @@ public: ...@@ -240,28 +240,27 @@ public:
: _M_pi(0) // nothrow : _M_pi(0) // nothrow
{ } { }
template <typename _Ptr, typename _Deleter> template<typename _Ptr, typename _Deleter>
shared_count(_Ptr __p, _Deleter __d) shared_count(_Ptr __p, _Deleter __d)
: _M_pi(0) : _M_pi(0)
{ {
try try
{ {
_M_pi = new _Sp_counted_base_impl<_Ptr, _Deleter>(__p, __d); _M_pi = new _Sp_counted_base_impl<_Ptr, _Deleter>(__p, __d);
} }
catch(...) catch(...)
{ {
__d(__p); // delete __p __d(__p); // delete __p
__throw_exception_again; __throw_exception_again;
} }
} }
// auto_ptr<_Tp> is special cased to provide the strong guarantee // auto_ptr<_Tp> is special cased to provide the strong guarantee
template <typename _Tp> template<typename _Tp>
explicit shared_count(std::auto_ptr<_Tp>& __r) explicit shared_count(std::auto_ptr<_Tp>& __r)
: _M_pi(new _Sp_counted_base_impl<_Tp*,_Sp_deleter<_Tp> >( : _M_pi(new _Sp_counted_base_impl<_Tp*,
__r.get(), _Sp_deleter<_Tp>() _Sp_deleter<_Tp> >(__r.get(), _Sp_deleter<_Tp>()))
))
{ __r.release(); } { __r.release(); }
// throws bad_weak_ptr when __r.use_count() == 0 // throws bad_weak_ptr when __r.use_count() == 0
...@@ -286,13 +285,13 @@ public: ...@@ -286,13 +285,13 @@ public:
_Sp_counted_base* __tmp = __r._M_pi; _Sp_counted_base* __tmp = __r._M_pi;
if(__tmp != _M_pi) if(__tmp != _M_pi)
{ {
if(__tmp != 0) if(__tmp != 0)
__tmp->add_ref_copy(); __tmp->add_ref_copy();
if(_M_pi != 0) if(_M_pi != 0)
_M_pi->release(); _M_pi->release();
_M_pi = __tmp; _M_pi = __tmp;
} }
return *this; return *this;
} }
...@@ -411,58 +410,52 @@ shared_count::shared_count(const weak_count& __r) ...@@ -411,58 +410,52 @@ shared_count::shared_count(const weak_count& __r)
: _M_pi(__r._M_pi) : _M_pi(__r._M_pi)
{ {
if (_M_pi != 0) if (_M_pi != 0)
{
_M_pi->add_ref_lock(); _M_pi->add_ref_lock();
}
else else
{
__throw_bad_weak_ptr(); __throw_bad_weak_ptr();
}
} }
// fwd decls // fwd decls
template <typename _Tp> class weak_ptr; template<typename _Tp>
template <typename _Tp> class enable_shared_from_this; class weak_ptr;
template<typename _Tp>
class enable_shared_from_this;
struct __static_cast_tag {}; struct __static_cast_tag {};
struct __const_cast_tag {}; struct __const_cast_tag {};
struct __dynamic_cast_tag {}; struct __dynamic_cast_tag {};
struct __polymorphic_cast_tag {}; struct __polymorphic_cast_tag {};
template<class _Tp> struct shared_ptr_traits template<class _Tp>
{ struct shared_ptr_traits
typedef _Tp & reference; { typedef _Tp& reference; };
};
template<> struct shared_ptr_traits<void> template<>
{ struct shared_ptr_traits<void>
typedef void reference; { typedef void reference; };
};
template<> struct shared_ptr_traits<void const> template<>
{ struct shared_ptr_traits<void const>
typedef void reference; { typedef void reference; };
};
template<> struct shared_ptr_traits<void volatile> template<>
{ struct shared_ptr_traits<void volatile>
typedef void reference; { typedef void reference; };
};
template<> struct shared_ptr_traits<void const volatile> template<>
{ struct shared_ptr_traits<void const volatile>
typedef void reference; { typedef void reference; };
};
// enable_shared_from_this support // enable_shared_from_this support
// friend of enable_shared_from_this // friend of enable_shared_from_this
template <typename _Tp1, typename _Tp2> template<typename _Tp1, typename _Tp2>
void void
__enable_shared_from_this( const shared_count& __pn, __enable_shared_from_this(const shared_count& __pn,
const enable_shared_from_this<_Tp1>* __pe, const enable_shared_from_this<_Tp1>* __pe,
const _Tp2* __px ); const _Tp2* __px );
inline void inline void
__enable_shared_from_this(const shared_count&, ...) __enable_shared_from_this(const shared_count&, ...)
...@@ -476,7 +469,7 @@ __enable_shared_from_this(const shared_count&, ...) ...@@ -476,7 +469,7 @@ __enable_shared_from_this(const shared_count&, ...)
* is destroyed or reset. * is destroyed or reset.
*/ */
template <typename _Tp> template<typename _Tp>
class shared_ptr class shared_ptr
{ {
typedef typename shared_ptr_traits<_Tp>::reference _Reference; typedef typename shared_ptr_traits<_Tp>::reference _Reference;
...@@ -488,15 +481,16 @@ template <typename _Tp> ...@@ -488,15 +481,16 @@ template <typename _Tp>
/** @brief Construct an empty %shared_ptr. /** @brief Construct an empty %shared_ptr.
* @post use_count()==0 && get()==0 * @post use_count()==0 && get()==0
*/ */
shared_ptr() : _M_ptr(0), _M_refcount() // never throws shared_ptr()
: _M_ptr(0), _M_refcount() // never throws
{ } { }
/** @brief Construct a %shared_ptr that owns the pointer @a p. /** @brief Construct a %shared_ptr that owns the pointer @a p.
* @param p A pointer that is convertible to element_type*. * @param p A pointer that is convertible to element_type*.
* @post use_count()==1 && get()==p * @post use_count() == 1 && get() == p
* @throw std::bad_alloc, in which case @c delete @a p is called. * @throw std::bad_alloc, in which case @c delete @a p is called.
*/ */
template <typename _Tp1> template<typename _Tp1>
explicit shared_ptr(_Tp1* __p) explicit shared_ptr(_Tp1* __p)
: _M_ptr(__p), _M_refcount(__p, _Sp_deleter<_Tp1>()) : _M_ptr(__p), _M_refcount(__p, _Sp_deleter<_Tp1>())
{ {
...@@ -515,10 +509,10 @@ template <typename _Tp> ...@@ -515,10 +509,10 @@ template <typename _Tp>
* and the deleter @a d. * and the deleter @a d.
* @param p A pointer. * @param p A pointer.
* @param d A deleter. * @param d A deleter.
* @post use_count()==1 && get()==p * @post use_count() == 1 && get() == p
* @throw std::bad_alloc, in which case @a d(p) is called. * @throw std::bad_alloc, in which case @a d(p) is called.
*/ */
template <typename _Tp1, typename _Deleter> template<typename _Tp1, typename _Deleter>
shared_ptr(_Tp1* __p, _Deleter __d) shared_ptr(_Tp1* __p, _Deleter __d)
: _M_ptr(__p), _M_refcount(__p, __d) : _M_ptr(__p), _M_refcount(__p, __d)
{ {
...@@ -533,10 +527,10 @@ template <typename _Tp> ...@@ -533,10 +527,10 @@ template <typename _Tp>
/** @brief If @a r is empty, constructs an empty %shared_ptr; otherwise /** @brief If @a r is empty, constructs an empty %shared_ptr; otherwise
* construct a %shared_ptr that shares ownership with @a r. * construct a %shared_ptr that shares ownership with @a r.
* @param r A %shared_ptr. * @param r A %shared_ptr.
* @post get()==r.get() && use_count()==r.use_count() * @post get() == r.get() && use_count() == r.use_count()
* @throw std::bad_alloc, in which case * @throw std::bad_alloc, in which case
*/ */
template <typename _Tp1> template<typename _Tp1>
shared_ptr(const shared_ptr<_Tp1>& __r) shared_ptr(const shared_ptr<_Tp1>& __r)
: _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws
{ {
...@@ -546,11 +540,11 @@ template <typename _Tp> ...@@ -546,11 +540,11 @@ template <typename _Tp>
/** @brief Constructs a %shared_ptr that shares ownership with @a r /** @brief Constructs a %shared_ptr that shares ownership with @a r
* and stores a copy of the pointer stored in @a r. * and stores a copy of the pointer stored in @a r.
* @param r A weak_ptr. * @param r A weak_ptr.
* @post use_count()==r.use_count() * @post use_count() == r.use_count()
* @throw bad_weak_ptr when r.expired(), * @throw bad_weak_ptr when r.expired(),
* in which case the constructor has no effect. * in which case the constructor has no effect.
*/ */
template <typename _Tp1> template<typename _Tp1>
explicit shared_ptr(const weak_ptr<_Tp1>& __r) explicit shared_ptr(const weak_ptr<_Tp1>& __r)
: _M_refcount(__r._M_refcount) // may throw : _M_refcount(__r._M_refcount) // may throw
{ {
...@@ -561,9 +555,9 @@ template <typename _Tp> ...@@ -561,9 +555,9 @@ template <typename _Tp>
} }
/** /**
* @post use_count()==1 and r.get()==0 * @post use_count() == 1 and r.get() == 0
*/ */
template <typename _Tp1> template<typename _Tp1>
explicit shared_ptr(std::auto_ptr<_Tp1>& __r) explicit shared_ptr(std::auto_ptr<_Tp1>& __r)
: _M_ptr(__r.get()), _M_refcount() : _M_ptr(__r.get()), _M_refcount()
{ {
...@@ -575,30 +569,28 @@ template <typename _Tp> ...@@ -575,30 +569,28 @@ template <typename _Tp>
__enable_shared_from_this( _M_refcount, __tmp, __tmp ); __enable_shared_from_this( _M_refcount, __tmp, __tmp );
} }
template <typename _Tp1> template<typename _Tp1>
shared_ptr(const shared_ptr<_Tp1>& __r, __static_cast_tag) shared_ptr(const shared_ptr<_Tp1>& __r, __static_cast_tag)
: _M_ptr(static_cast<element_type*>(__r._M_ptr)) : _M_ptr(static_cast<element_type*>(__r._M_ptr)),
, _M_refcount(__r._M_refcount) _M_refcount(__r._M_refcount)
{ } { }
template <typename _Tp1> template<typename _Tp1>
shared_ptr(const shared_ptr<_Tp1>& __r, __const_cast_tag) shared_ptr(const shared_ptr<_Tp1>& __r, __const_cast_tag)
: _M_ptr(const_cast<element_type*>(__r._M_ptr)) : _M_ptr(const_cast<element_type*>(__r._M_ptr)),
, _M_refcount(__r._M_refcount) _M_refcount(__r._M_refcount)
{ } { }
template <typename _Tp1> template<typename _Tp1>
shared_ptr(const shared_ptr<_Tp1>& __r, __dynamic_cast_tag) shared_ptr(const shared_ptr<_Tp1>& __r, __dynamic_cast_tag)
: _M_ptr(dynamic_cast<element_type*>(__r._M_ptr)) : _M_ptr(dynamic_cast<element_type*>(__r._M_ptr)),
, _M_refcount(__r._M_refcount) _M_refcount(__r._M_refcount)
{ {
if (_M_ptr == 0) // need to allocate new counter -- the cast failed if (_M_ptr == 0) // need to allocate new counter -- the cast failed
{
_M_refcount = shared_count(); _M_refcount = shared_count();
}
} }
template <typename _Tp1> template<typename _Tp1>
shared_ptr& shared_ptr&
operator=(const shared_ptr<_Tp1>& __r) // never throws operator=(const shared_ptr<_Tp1>& __r) // never throws
{ {
...@@ -607,7 +599,7 @@ template <typename _Tp> ...@@ -607,7 +599,7 @@ template <typename _Tp>
return *this; return *this;
} }
template <typename _Tp1> template<typename _Tp1>
shared_ptr& shared_ptr&
operator=(std::auto_ptr<_Tp1>& __r) operator=(std::auto_ptr<_Tp1>& __r)
{ {
...@@ -619,15 +611,16 @@ template <typename _Tp> ...@@ -619,15 +611,16 @@ template <typename _Tp>
reset() // never throws reset() // never throws
{ shared_ptr().swap(*this); } { shared_ptr().swap(*this); }
template <typename _Tp1> template<typename _Tp1>
void void
reset(_Tp1* __p) // _Tp1 must be complete reset(_Tp1* __p) // _Tp1 must be complete
{ {
_GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != _M_ptr); // catch self-reset errors _GLIBCXX_DEBUG_ASSERT(__p == 0 || __p != _M_ptr); // catch self-reset
// errors
shared_ptr(__p).swap(*this); shared_ptr(__p).swap(*this);
} }
template <typename _Tp1, typename _Deleter> template<typename _Tp1, typename _Deleter>
void void
reset(_Tp1 * __p, _Deleter __d) reset(_Tp1 * __p, _Deleter __d)
{ shared_ptr(__p, __d).swap(*this); } { shared_ptr(__p, __d).swap(*this); }
...@@ -679,7 +672,7 @@ template <typename _Tp> ...@@ -679,7 +672,7 @@ template <typename _Tp>
{ return _M_refcount.get_deleter(__ti); } { return _M_refcount.get_deleter(__ti); }
private: private:
template <typename _Tp1> template<typename _Tp1>
bool bool
_M_less(const shared_ptr<_Tp1>& __rhs) const _M_less(const shared_ptr<_Tp1>& __rhs) const
{ return _M_refcount < __rhs._M_refcount; } { return _M_refcount < __rhs._M_refcount; }
...@@ -688,17 +681,17 @@ template <typename _Tp> ...@@ -688,17 +681,17 @@ template <typename _Tp>
template <typename _Tp1> friend class weak_ptr; template <typename _Tp1> friend class weak_ptr;
// friends injected into enclosing namespace and found by ADL: // friends injected into enclosing namespace and found by ADL:
template <typename _Tp1> template<typename _Tp1>
friend inline bool friend inline bool
operator==(const shared_ptr& __a, const shared_ptr<_Tp1>& __b) operator==(const shared_ptr& __a, const shared_ptr<_Tp1>& __b)
{ return __a.get() == __b.get(); } { return __a.get() == __b.get(); }
template <typename _Tp1> template<typename _Tp1>
friend inline bool friend inline bool
operator!=(const shared_ptr& __a, const shared_ptr<_Tp1>& __b) operator!=(const shared_ptr& __a, const shared_ptr<_Tp1>& __b)
{ return __a.get() != __b.get(); } { return __a.get() != __b.get(); }
template <typename _Tp1> template<typename _Tp1>
friend inline bool friend inline bool
operator<(const shared_ptr& __a, const shared_ptr<_Tp1>& __b) operator<(const shared_ptr& __a, const shared_ptr<_Tp1>& __b)
{ return __a._M_less(__b); } { return __a._M_less(__b); }
...@@ -714,7 +707,7 @@ template <typename _Tp> ...@@ -714,7 +707,7 @@ template <typename _Tp>
* will eventually result in undefined behaviour, * will eventually result in undefined behaviour,
* attempting to delete the same object twice. * attempting to delete the same object twice.
*/ */
template <typename _Tp, typename _Tp1> template<typename _Tp, typename _Tp1>
shared_ptr<_Tp> shared_ptr<_Tp>
static_pointer_cast(const shared_ptr<_Tp1>& __r) static_pointer_cast(const shared_ptr<_Tp1>& __r)
{ {
...@@ -726,7 +719,7 @@ template <typename _Tp, typename _Tp1> ...@@ -726,7 +719,7 @@ template <typename _Tp, typename _Tp1>
* will eventually result in undefined behaviour, * will eventually result in undefined behaviour,
* attempting to delete the same object twice. * attempting to delete the same object twice.
*/ */
template <typename _Tp, typename _Tp1> template<typename _Tp, typename _Tp1>
shared_ptr<_Tp> shared_ptr<_Tp>
const_pointer_cast(const shared_ptr<_Tp1>& __r) const_pointer_cast(const shared_ptr<_Tp1>& __r)
{ {
...@@ -738,7 +731,7 @@ template <typename _Tp, typename _Tp1> ...@@ -738,7 +731,7 @@ template <typename _Tp, typename _Tp1>
* will eventually result in undefined behaviour, * will eventually result in undefined behaviour,
* attempting to delete the same object twice. * attempting to delete the same object twice.
*/ */
template <typename _Tp, typename _Tp1> template<typename _Tp, typename _Tp1>
shared_ptr<_Tp> shared_ptr<_Tp>
dynamic_pointer_cast(const shared_ptr<_Tp1>& __r) dynamic_pointer_cast(const shared_ptr<_Tp1>& __r)
{ {
...@@ -746,7 +739,7 @@ template <typename _Tp, typename _Tp1> ...@@ -746,7 +739,7 @@ template <typename _Tp, typename _Tp1>
} }
// 2.2.3.7 shared_ptr I/O // 2.2.3.7 shared_ptr I/O
template <typename _Ch, typename _Tr, typename _Tp> template<typename _Ch, typename _Tr, typename _Tp>
std::basic_ostream<_Ch, _Tr>& std::basic_ostream<_Ch, _Tr>&
operator<<(std::basic_ostream<_Ch, _Tr>& __os, const shared_ptr<_Tp>& __p) operator<<(std::basic_ostream<_Ch, _Tr>& __os, const shared_ptr<_Tp>& __p)
{ {
...@@ -755,13 +748,13 @@ template <typename _Ch, typename _Tr, typename _Tp> ...@@ -755,13 +748,13 @@ template <typename _Ch, typename _Tr, typename _Tp>
} }
// 2.2.3.10 shared_ptr get_deleter (experimental) // 2.2.3.10 shared_ptr get_deleter (experimental)
template <typename _Del, typename _Tp> template<typename _Del, typename _Tp>
inline _Del* inline _Del*
get_deleter(const shared_ptr<_Tp>& __p) get_deleter(const shared_ptr<_Tp>& __p)
{ return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del))); } { return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del))); }
template <typename _Tp> template<typename _Tp>
class weak_ptr class weak_ptr
{ {
public: public:
...@@ -772,26 +765,26 @@ template <typename _Tp> ...@@ -772,26 +765,26 @@ template <typename _Tp>
: _M_ptr(0), _M_refcount() // never throws : _M_ptr(0), _M_refcount() // never throws
{ } { }
// generated copy constructor, assignment, destructor are fine // generated copy constructor, assignment, destructor are fine
// //
// The "obvious" converting constructor implementation: // The "obvious" converting constructor implementation:
// //
// template<class Y> // template<class Y>
// weak_ptr(weak_ptr<Y> const & r): _M_ptr(r._M_ptr), _M_refcount(r._M_refcount) // never throws // weak_ptr(weak_ptr<Y> const & r)
// { // : _M_ptr(r._M_ptr), _M_refcount(r._M_refcount) // never throws
// } // { }
// //
// has a serious problem. // has a serious problem.
// //
// r._M_ptr may already have been invalidated. The _M_ptr(r._M_ptr) // r._M_ptr may already have been invalidated. The _M_ptr(r._M_ptr)
// conversion may require access to *r._M_ptr (virtual inheritance). // conversion may require access to *r._M_ptr (virtual inheritance).
// //
// It is not possible to avoid spurious access violations since // It is not possible to avoid spurious access violations since
// in multithreaded programs r._M_ptr may be invalidated at any point. // in multithreaded programs r._M_ptr may be invalidated at any point.
// //
template <typename _Tp1> template<typename _Tp1>
weak_ptr(const weak_ptr<_Tp1>& r) weak_ptr(const weak_ptr<_Tp1>& r)
: _M_refcount(r._M_refcount) // never throws : _M_refcount(r._M_refcount) // never throws
{ {
...@@ -799,14 +792,14 @@ template <typename _Tp> ...@@ -799,14 +792,14 @@ template <typename _Tp>
_M_ptr = r.lock().get(); _M_ptr = r.lock().get();
} }
template <typename _Tp1> template<typename _Tp1>
weak_ptr(const shared_ptr<_Tp1>& r) weak_ptr(const shared_ptr<_Tp1>& r)
: _M_ptr(r._M_ptr), _M_refcount(r._M_refcount) // never throws : _M_ptr(r._M_ptr), _M_refcount(r._M_refcount) // never throws
{ {
__glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
} }
template <typename _Tp1> template<typename _Tp1>
weak_ptr& weak_ptr&
operator=(const weak_ptr<_Tp1>& r) // never throws operator=(const weak_ptr<_Tp1>& r) // never throws
{ {
...@@ -815,7 +808,7 @@ template <typename _Tp> ...@@ -815,7 +808,7 @@ template <typename _Tp>
return *this; return *this;
} }
template <typename _Tp1> template<typename _Tp1>
weak_ptr& weak_ptr&
operator=(const shared_ptr<_Tp1>& r) // never throws operator=(const shared_ptr<_Tp1>& r) // never throws
{ {
...@@ -831,25 +824,25 @@ template <typename _Tp> ...@@ -831,25 +824,25 @@ template <typename _Tp>
// optimization: avoid throw overhead // optimization: avoid throw overhead
if (expired()) if (expired())
{ return shared_ptr<element_type>();
return shared_ptr<element_type>();
}
try try
{ {
return shared_ptr<element_type>(*this); return shared_ptr<element_type>(*this);
} }
catch (const bad_weak_ptr&) catch (const bad_weak_ptr&)
{ {
// Q: how can we get here? // Q: how can we get here?
// A: another thread may have invalidated r after the use_count test above. // A: another thread may have invalidated r after the
return shared_ptr<element_type>(); // use_count test above.
} return shared_ptr<element_type>();
}
#else #else
// optimization: avoid try/catch overhead when single threaded // optimization: avoid try/catch overhead when single threaded
return expired() ? shared_ptr<element_type>() : shared_ptr<element_type>(*this); return expired() ? shared_ptr<element_type>()
: shared_ptr<element_type>(*this);
#endif #endif
} // XXX MT } // XXX MT
...@@ -876,7 +869,7 @@ template <typename _Tp> ...@@ -876,7 +869,7 @@ template <typename _Tp>
private: private:
template <typename _Tp1> template<typename _Tp1>
bool bool
_M_less(const weak_ptr<_Tp1>& __rhs) const _M_less(const weak_ptr<_Tp1>& __rhs) const
{ return _M_refcount < __rhs._M_refcount; } { return _M_refcount < __rhs._M_refcount; }
...@@ -891,13 +884,13 @@ template <typename _Tp> ...@@ -891,13 +884,13 @@ template <typename _Tp>
// friend injected into namespace and found by ADL // friend injected into namespace and found by ADL
template <typename _Tp1> template<typename _Tp1>
friend inline bool friend inline bool
operator<(const weak_ptr& __lhs, const weak_ptr<_Tp1>& __rhs) operator<(const weak_ptr& __lhs, const weak_ptr<_Tp1>& __rhs)
{ return __lhs._M_less(__rhs); } { return __lhs._M_less(__rhs); }
template <typename _Tp1> friend class weak_ptr; template<typename _Tp1> friend class weak_ptr;
template <typename _Tp1> friend class shared_ptr; template<typename _Tp1> friend class shared_ptr;
friend class enable_shared_from_this<_Tp>; friend class enable_shared_from_this<_Tp>;
_Tp* _M_ptr; // contained pointer _Tp* _M_ptr; // contained pointer
...@@ -906,8 +899,7 @@ template <typename _Tp> ...@@ -906,8 +899,7 @@ template <typename _Tp>
}; // weak_ptr }; // weak_ptr
template<typename _Tp>
template <typename _Tp>
class enable_shared_from_this class enable_shared_from_this
{ {
protected: protected:
...@@ -930,26 +922,28 @@ template <typename _Tp> ...@@ -930,26 +922,28 @@ template <typename _Tp>
shared_ptr<_Tp> shared_ptr<_Tp>
shared_from_this() shared_from_this()
{ {
shared_ptr<_Tp> p(this->_M_weak_this); shared_ptr<_Tp> __p(this->_M_weak_this);
return p; return __p;
} }
shared_ptr<const _Tp> shared_ptr<const _Tp>
shared_from_this() const shared_from_this() const
{ {
shared_ptr<const _Tp> p(this->_M_weak_this); shared_ptr<const _Tp> __p(this->_M_weak_this);
return p; return __p;
} }
private: private:
template <typename _Tp1> template<typename _Tp1>
void void
_M_weak_assign(_Tp1* __p, const shared_count& __n) const _M_weak_assign(_Tp1* __p, const shared_count& __n) const
{ _M_weak_this._M_assign(__p, __n); } { _M_weak_this._M_assign(__p, __n); }
template <typename _Tp1> template<typename _Tp1>
friend void friend void
__enable_shared_from_this( const shared_count& __pn, const enable_shared_from_this* __pe, const _Tp1* __px) __enable_shared_from_this(const shared_count& __pn,
const enable_shared_from_this* __pe,
const _Tp1* __px)
{ {
if(__pe != 0) if(__pe != 0)
__pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn); __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
...@@ -964,7 +958,7 @@ template <typename _Tp> ...@@ -964,7 +958,7 @@ template <typename _Tp>
* @brief std::swap() specialisation for shared_ptr. * @brief std::swap() specialisation for shared_ptr.
* @relates shared_ptr. * @relates shared_ptr.
*/ */
template <typename _Tp> template<typename _Tp>
inline void inline void
swap(tr1::shared_ptr<_Tp>& __a, tr1::shared_ptr<_Tp>& __b) swap(tr1::shared_ptr<_Tp>& __a, tr1::shared_ptr<_Tp>& __b)
{ {
...@@ -975,7 +969,7 @@ template <typename _Tp> ...@@ -975,7 +969,7 @@ template <typename _Tp>
* @brief std::swap() specialisation for weak_ptr. * @brief std::swap() specialisation for weak_ptr.
* @relates weak_ptr. * @relates weak_ptr.
*/ */
template <typename _Tp> template<typename _Tp>
void void
swap(tr1::weak_ptr<_Tp>& __a, tr1::weak_ptr<_Tp>& __b) swap(tr1::weak_ptr<_Tp>& __a, tr1::weak_ptr<_Tp>& __b)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment