diff options
Diffstat (limited to 'libstdc++-v3/include/tr1/boost_shared_ptr.h')
-rw-r--r-- | libstdc++-v3/include/tr1/boost_shared_ptr.h | 372 |
1 files changed, 183 insertions, 189 deletions
diff --git a/libstdc++-v3/include/tr1/boost_shared_ptr.h b/libstdc++-v3/include/tr1/boost_shared_ptr.h index 01250aa107c..345e17f8785 100644 --- a/libstdc++-v3/include/tr1/boost_shared_ptr.h +++ b/libstdc++-v3/include/tr1/boost_shared_ptr.h @@ -64,10 +64,9 @@ class bad_weak_ptr : public std::exception { public: - virtual char const* what() const throw() - { - return "tr1::bad_weak_ptr"; - } + virtual char const* + what() const throw() + { return "tr1::bad_weak_ptr"; } }; // Helper for exception objects in <tr1/memory> @@ -83,7 +82,7 @@ __throw_bad_weak_ptr() } -template <typename _Tp> +template<typename _Tp> struct _Sp_deleter { typedef void result_type; @@ -140,22 +139,22 @@ public: { __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(); - } + { + _M_use_count = 0; + __throw_bad_weak_ptr(); + } } void release() // nothrow { if (__gnu_cxx::__exchange_and_add(&_M_use_count, -1) == 1) - { - dispose(); - __glibcxx_mutex_lock(_M_mutex); - __glibcxx_mutex_unlock(_M_mutex); - weak_release(); - } + { + dispose(); + __glibcxx_mutex_lock(_M_mutex); + __glibcxx_mutex_unlock(_M_mutex); + weak_release(); + } } void @@ -168,11 +167,11 @@ public: weak_release() // nothrow { if (__gnu_cxx::__exchange_and_add(&_M_weak_count, -1) == 1) - { - __glibcxx_mutex_lock(_M_mutex); - __glibcxx_mutex_unlock(_M_mutex); - destroy(); - } + { + __glibcxx_mutex_lock(_M_mutex); + __glibcxx_mutex_unlock(_M_mutex); + destroy(); + } } long @@ -184,45 +183,46 @@ public: private: _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_weak_count; // #weak + (#shared != 0) __gnu_cxx::mutex_type _M_mutex; }; -template <typename _Ptr, typename _Deleter> -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) +template<typename _Ptr, typename _Deleter> + class _Sp_counted_base_impl + : public _Sp_counted_base { - 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&); + public: - _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; @@ -240,28 +240,27 @@ public: : _M_pi(0) // nothrow { } - template <typename _Ptr, typename _Deleter> + 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); - } + { + _M_pi = new _Sp_counted_base_impl<_Ptr, _Deleter>(__p, __d); + } catch(...) - { - __d(__p); // delete __p - __throw_exception_again; - } + { + __d(__p); // delete __p + __throw_exception_again; + } } // 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) - : _M_pi(new _Sp_counted_base_impl<_Tp*,_Sp_deleter<_Tp> >( - __r.get(), _Sp_deleter<_Tp>() - )) + : _M_pi(new _Sp_counted_base_impl<_Tp*, + _Sp_deleter<_Tp> >(__r.get(), _Sp_deleter<_Tp>())) { __r.release(); } // throws bad_weak_ptr when __r.use_count() == 0 @@ -286,13 +285,13 @@ public: _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; - } + { + if(__tmp != 0) + __tmp->add_ref_copy(); + if(_M_pi != 0) + _M_pi->release(); + _M_pi = __tmp; + } return *this; } @@ -411,58 +410,52 @@ 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(); - } } // fwd decls -template <typename _Tp> class weak_ptr; -template <typename _Tp> class enable_shared_from_this; +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<class _Tp> + struct shared_ptr_traits + { typedef _Tp& reference; }; -template<> struct shared_ptr_traits<void> -{ - typedef void 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 const> + { typedef void reference; }; -template<> struct shared_ptr_traits<void volatile> -{ - 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<> + struct shared_ptr_traits<void const volatile> + { typedef void reference; }; // enable_shared_from_this support // friend of enable_shared_from_this -template <typename _Tp1, typename _Tp2> +template<typename _Tp1, typename _Tp2> void - __enable_shared_from_this( const shared_count& __pn, - const enable_shared_from_this<_Tp1>* __pe, - const _Tp2* __px ); + __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&, ...) @@ -476,7 +469,7 @@ __enable_shared_from_this(const shared_count&, ...) * is destroyed or reset. */ -template <typename _Tp> +template<typename _Tp> class shared_ptr { typedef typename shared_ptr_traits<_Tp>::reference _Reference; @@ -488,15 +481,16 @@ template <typename _Tp> /** @brief Construct an empty %shared_ptr. * @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. * @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. */ - template <typename _Tp1> + template<typename _Tp1> explicit shared_ptr(_Tp1* __p) : _M_ptr(__p), _M_refcount(__p, _Sp_deleter<_Tp1>()) { @@ -515,10 +509,10 @@ template <typename _Tp> * and the deleter @a d. * @param p A pointer. * @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. */ - template <typename _Tp1, typename _Deleter> + template<typename _Tp1, typename _Deleter> shared_ptr(_Tp1* __p, _Deleter __d) : _M_ptr(__p), _M_refcount(__p, __d) { @@ -533,10 +527,10 @@ template <typename _Tp> /** @brief If @a r is empty, constructs an empty %shared_ptr; otherwise * construct a %shared_ptr that shares ownership with @a r. * @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 */ - template <typename _Tp1> + template<typename _Tp1> shared_ptr(const shared_ptr<_Tp1>& __r) : _M_ptr(__r._M_ptr), _M_refcount(__r._M_refcount) // never throws { @@ -546,11 +540,11 @@ template <typename _Tp> /** @brief Constructs a %shared_ptr that shares ownership with @a r * and stores a copy of the pointer stored in @a r. * @param r A weak_ptr. - * @post use_count()==r.use_count() + * @post use_count() == r.use_count() * @throw bad_weak_ptr when r.expired(), * in which case the constructor has no effect. */ - template <typename _Tp1> + template<typename _Tp1> explicit shared_ptr(const weak_ptr<_Tp1>& __r) : _M_refcount(__r._M_refcount) // may throw { @@ -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) : _M_ptr(__r.get()), _M_refcount() { @@ -575,30 +569,28 @@ template <typename _Tp> __enable_shared_from_this( _M_refcount, __tmp, __tmp ); } - template <typename _Tp1> + 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) + : _M_ptr(static_cast<element_type*>(__r._M_ptr)), + _M_refcount(__r._M_refcount) { } - template <typename _Tp1> + 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) + : _M_ptr(const_cast<element_type*>(__r._M_ptr)), + _M_refcount(__r._M_refcount) { } - template <typename _Tp1> + 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) + : _M_ptr(dynamic_cast<element_type*>(__r._M_ptr)), + _M_refcount(__r._M_refcount) { if (_M_ptr == 0) // need to allocate new counter -- the cast failed - { _M_refcount = shared_count(); - } } - template <typename _Tp1> + template<typename _Tp1> shared_ptr& operator=(const shared_ptr<_Tp1>& __r) // never throws { @@ -607,7 +599,7 @@ template <typename _Tp> return *this; } - template <typename _Tp1> + template<typename _Tp1> shared_ptr& operator=(std::auto_ptr<_Tp1>& __r) { @@ -619,15 +611,16 @@ template <typename _Tp> reset() // never throws { shared_ptr().swap(*this); } - template <typename _Tp1> + template<typename _Tp1> void 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); } - template <typename _Tp1, typename _Deleter> + template<typename _Tp1, typename _Deleter> void reset(_Tp1 * __p, _Deleter __d) { shared_ptr(__p, __d).swap(*this); } @@ -679,7 +672,7 @@ template <typename _Tp> { return _M_refcount.get_deleter(__ti); } private: - template <typename _Tp1> + template<typename _Tp1> bool _M_less(const shared_ptr<_Tp1>& __rhs) const { return _M_refcount < __rhs._M_refcount; } @@ -688,17 +681,17 @@ template <typename _Tp> template <typename _Tp1> friend class weak_ptr; // friends injected into enclosing namespace and found by ADL: - template <typename _Tp1> + template<typename _Tp1> friend inline bool operator==(const shared_ptr& __a, const shared_ptr<_Tp1>& __b) { return __a.get() == __b.get(); } - template <typename _Tp1> + template<typename _Tp1> friend inline bool operator!=(const shared_ptr& __a, const shared_ptr<_Tp1>& __b) { return __a.get() != __b.get(); } - template <typename _Tp1> + template<typename _Tp1> friend inline bool operator<(const shared_ptr& __a, const shared_ptr<_Tp1>& __b) { return __a._M_less(__b); } @@ -714,7 +707,7 @@ template <typename _Tp> * will eventually result in undefined behaviour, * attempting to delete the same object twice. */ -template <typename _Tp, typename _Tp1> +template<typename _Tp, typename _Tp1> shared_ptr<_Tp> static_pointer_cast(const shared_ptr<_Tp1>& __r) { @@ -726,7 +719,7 @@ template <typename _Tp, typename _Tp1> * will eventually result in undefined behaviour, * attempting to delete the same object twice. */ -template <typename _Tp, typename _Tp1> +template<typename _Tp, typename _Tp1> shared_ptr<_Tp> const_pointer_cast(const shared_ptr<_Tp1>& __r) { @@ -738,7 +731,7 @@ template <typename _Tp, typename _Tp1> * will eventually result in undefined behaviour, * attempting to delete the same object twice. */ -template <typename _Tp, typename _Tp1> +template<typename _Tp, typename _Tp1> shared_ptr<_Tp> dynamic_pointer_cast(const shared_ptr<_Tp1>& __r) { @@ -746,7 +739,7 @@ template <typename _Tp, typename _Tp1> } // 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>& operator<<(std::basic_ostream<_Ch, _Tr>& __os, const shared_ptr<_Tp>& __p) { @@ -755,13 +748,13 @@ template <typename _Ch, typename _Tr, typename _Tp> } // 2.2.3.10 shared_ptr get_deleter (experimental) -template <typename _Del, typename _Tp> +template<typename _Del, typename _Tp> inline _Del* get_deleter(const shared_ptr<_Tp>& __p) { return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del))); } -template <typename _Tp> +template<typename _Tp> class weak_ptr { public: @@ -772,26 +765,26 @@ template <typename _Tp> : _M_ptr(0), _M_refcount() // never throws { } - // generated copy constructor, assignment, destructor are fine - - // - // The "obvious" converting constructor implementation: - // - // template<class Y> - // weak_ptr(weak_ptr<Y> const & r): _M_ptr(r._M_ptr), _M_refcount(r._M_refcount) // never throws - // { - // } - // - // has a serious problem. - // - // r._M_ptr may already have been invalidated. The _M_ptr(r._M_ptr) - // conversion may require access to *r._M_ptr (virtual inheritance). - // - // It is not possible to avoid spurious access violations since - // in multithreaded programs r._M_ptr may be invalidated at any point. - // - - template <typename _Tp1> + // generated copy constructor, assignment, destructor are fine + + // + // The "obvious" converting constructor implementation: + // + // template<class Y> + // weak_ptr(weak_ptr<Y> const & r) + // : _M_ptr(r._M_ptr), _M_refcount(r._M_refcount) // never throws + // { } + // + // has a serious problem. + // + // r._M_ptr may already have been invalidated. The _M_ptr(r._M_ptr) + // conversion may require access to *r._M_ptr (virtual inheritance). + // + // It is not possible to avoid spurious access violations since + // in multithreaded programs r._M_ptr may be invalidated at any point. + // + + template<typename _Tp1> weak_ptr(const weak_ptr<_Tp1>& r) : _M_refcount(r._M_refcount) // never throws { @@ -799,14 +792,14 @@ template <typename _Tp> _M_ptr = r.lock().get(); } - template <typename _Tp1> + template<typename _Tp1> weak_ptr(const shared_ptr<_Tp1>& r) : _M_ptr(r._M_ptr), _M_refcount(r._M_refcount) // never throws { __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) } - template <typename _Tp1> + template<typename _Tp1> weak_ptr& operator=(const weak_ptr<_Tp1>& r) // never throws { @@ -815,7 +808,7 @@ template <typename _Tp> return *this; } - template <typename _Tp1> + template<typename _Tp1> weak_ptr& operator=(const shared_ptr<_Tp1>& r) // never throws { @@ -831,25 +824,25 @@ template <typename _Tp> // optimization: avoid throw overhead if (expired()) - { - return shared_ptr<element_type>(); - } - + return shared_ptr<element_type>(); + try - { - return shared_ptr<element_type>(*this); - } + { + return shared_ptr<element_type>(*this); + } catch (const bad_weak_ptr&) - { - // Q: how can we get here? - // A: another thread may have invalidated r after the use_count test above. - return shared_ptr<element_type>(); - } + { + // Q: how can we get here? + // A: another thread may have invalidated r after the + // use_count test above. + return shared_ptr<element_type>(); + } #else // 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 } // XXX MT @@ -876,7 +869,7 @@ template <typename _Tp> private: - template <typename _Tp1> + template<typename _Tp1> bool _M_less(const weak_ptr<_Tp1>& __rhs) const { return _M_refcount < __rhs._M_refcount; } @@ -891,13 +884,13 @@ template <typename _Tp> // friend injected into namespace and found by ADL - template <typename _Tp1> + 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; + template<typename _Tp1> friend class weak_ptr; + template<typename _Tp1> friend class shared_ptr; friend class enable_shared_from_this<_Tp>; _Tp* _M_ptr; // contained pointer @@ -906,8 +899,7 @@ template <typename _Tp> }; // weak_ptr - -template <typename _Tp> +template<typename _Tp> class enable_shared_from_this { protected: @@ -930,26 +922,28 @@ template <typename _Tp> shared_ptr<_Tp> shared_from_this() { - shared_ptr<_Tp> p(this->_M_weak_this); - return p; + 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; + shared_ptr<const _Tp> __p(this->_M_weak_this); + return __p; } private: - template <typename _Tp1> + template<typename _Tp1> void _M_weak_assign(_Tp1* __p, const shared_count& __n) const { _M_weak_this._M_assign(__p, __n); } - template <typename _Tp1> + template<typename _Tp1> 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) __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn); @@ -964,7 +958,7 @@ template <typename _Tp> * @brief std::swap() specialisation for shared_ptr. * @relates shared_ptr. */ -template <typename _Tp> +template<typename _Tp> inline void swap(tr1::shared_ptr<_Tp>& __a, tr1::shared_ptr<_Tp>& __b) { @@ -975,7 +969,7 @@ template <typename _Tp> * @brief std::swap() specialisation for weak_ptr. * @relates weak_ptr. */ -template <typename _Tp> +template<typename _Tp> void swap(tr1::weak_ptr<_Tp>& __a, tr1::weak_ptr<_Tp>& __b) { |