Chris@16: #ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED Chris@16: #define BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED Chris@16: Chris@16: // Chris@16: // weak_ptr.hpp Chris@16: // Chris@16: // Copyright (c) 2001, 2002, 2003 Peter Dimov Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // See http://www.boost.org/libs/smart_ptr/weak_ptr.htm for documentation. Chris@16: // Chris@16: Chris@16: #include // boost.TR1 include order fix Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost Chris@16: { Chris@16: Chris@16: template class weak_ptr Chris@16: { Chris@16: private: Chris@16: Chris@16: // Borland 5.5.1 specific workarounds Chris@16: typedef weak_ptr this_type; Chris@16: Chris@16: public: Chris@16: Chris@16: typedef typename boost::detail::sp_element< T >::type element_type; Chris@16: Chris@16: weak_ptr() BOOST_NOEXCEPT : px(0), pn() // never throws in 1.30+ Chris@16: { Chris@16: } Chris@16: Chris@16: // generated copy constructor, assignment, destructor are fine... Chris@16: Chris@16: #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) Chris@16: Chris@16: // ... except in C++0x, move disables the implicit copy Chris@16: Chris@16: weak_ptr( weak_ptr const & r ) BOOST_NOEXCEPT : px( r.px ), pn( r.pn ) Chris@16: { Chris@16: } Chris@16: Chris@16: weak_ptr & operator=( weak_ptr const & r ) BOOST_NOEXCEPT Chris@16: { Chris@16: px = r.px; Chris@16: pn = r.pn; Chris@16: return *this; Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: // Chris@16: // The "obvious" converting constructor implementation: Chris@16: // Chris@16: // template Chris@16: // weak_ptr(weak_ptr const & r): px(r.px), pn(r.pn) // never throws Chris@16: // { Chris@16: // } Chris@16: // Chris@16: // has a serious problem. Chris@16: // Chris@16: // r.px may already have been invalidated. The px(r.px) Chris@16: // conversion may require access to *r.px (virtual inheritance). Chris@16: // Chris@16: // It is not possible to avoid spurious access violations since Chris@16: // in multithreaded programs r.px may be invalidated at any point. Chris@16: // Chris@16: Chris@16: template Chris@16: #if !defined( BOOST_SP_NO_SP_CONVERTIBLE ) Chris@16: Chris@16: weak_ptr( weak_ptr const & r, typename boost::detail::sp_enable_if_convertible::type = boost::detail::sp_empty() ) Chris@16: Chris@16: #else Chris@16: Chris@16: weak_ptr( weak_ptr const & r ) Chris@16: Chris@16: #endif Chris@16: BOOST_NOEXCEPT : px(r.lock().get()), pn(r.pn) Chris@16: { Chris@16: boost::detail::sp_assert_convertible< Y, T >(); Chris@16: } Chris@16: Chris@16: #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) Chris@16: Chris@16: template Chris@16: #if !defined( BOOST_SP_NO_SP_CONVERTIBLE ) Chris@16: Chris@16: weak_ptr( weak_ptr && r, typename boost::detail::sp_enable_if_convertible::type = boost::detail::sp_empty() ) Chris@16: Chris@16: #else Chris@16: Chris@16: weak_ptr( weak_ptr && r ) Chris@16: Chris@16: #endif Chris@16: BOOST_NOEXCEPT : px( r.lock().get() ), pn( static_cast< boost::detail::weak_count && >( r.pn ) ) Chris@16: { Chris@16: boost::detail::sp_assert_convertible< Y, T >(); Chris@16: r.px = 0; Chris@16: } Chris@16: Chris@16: // for better efficiency in the T == Y case Chris@16: weak_ptr( weak_ptr && r ) Chris@16: BOOST_NOEXCEPT : px( r.px ), pn( static_cast< boost::detail::weak_count && >( r.pn ) ) Chris@16: { Chris@16: r.px = 0; Chris@16: } Chris@16: Chris@16: // for better efficiency in the T == Y case Chris@16: weak_ptr & operator=( weak_ptr && r ) BOOST_NOEXCEPT Chris@16: { Chris@16: this_type( static_cast< weak_ptr && >( r ) ).swap( *this ); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: Chris@16: #endif Chris@16: Chris@16: template Chris@16: #if !defined( BOOST_SP_NO_SP_CONVERTIBLE ) Chris@16: Chris@16: weak_ptr( shared_ptr const & r, typename boost::detail::sp_enable_if_convertible::type = boost::detail::sp_empty() ) Chris@16: Chris@16: #else Chris@16: Chris@16: weak_ptr( shared_ptr const & r ) Chris@16: Chris@16: #endif Chris@16: BOOST_NOEXCEPT : px( r.px ), pn( r.pn ) Chris@16: { Chris@16: boost::detail::sp_assert_convertible< Y, T >(); Chris@16: } Chris@16: Chris@16: #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1300) Chris@16: Chris@16: template Chris@16: weak_ptr & operator=( weak_ptr const & r ) BOOST_NOEXCEPT Chris@16: { Chris@16: boost::detail::sp_assert_convertible< Y, T >(); Chris@16: Chris@16: px = r.lock().get(); Chris@16: pn = r.pn; Chris@16: Chris@16: return *this; Chris@16: } Chris@16: Chris@16: #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) Chris@16: Chris@16: template Chris@16: weak_ptr & operator=( weak_ptr && r ) BOOST_NOEXCEPT Chris@16: { Chris@16: this_type( static_cast< weak_ptr && >( r ) ).swap( *this ); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: template Chris@16: weak_ptr & operator=( shared_ptr const & r ) BOOST_NOEXCEPT Chris@16: { Chris@16: boost::detail::sp_assert_convertible< Y, T >(); Chris@16: Chris@16: px = r.px; Chris@16: pn = r.pn; Chris@16: Chris@16: return *this; Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: shared_ptr lock() const BOOST_NOEXCEPT Chris@16: { Chris@16: return shared_ptr( *this, boost::detail::sp_nothrow_tag() ); Chris@16: } Chris@16: Chris@16: long use_count() const BOOST_NOEXCEPT Chris@16: { Chris@16: return pn.use_count(); Chris@16: } Chris@16: Chris@16: bool expired() const BOOST_NOEXCEPT Chris@16: { Chris@16: return pn.use_count() == 0; Chris@16: } Chris@16: Chris@16: bool _empty() const // extension, not in std::weak_ptr Chris@16: { Chris@16: return pn.empty(); Chris@16: } Chris@16: Chris@16: void reset() BOOST_NOEXCEPT // never throws in 1.30+ Chris@16: { Chris@16: this_type().swap(*this); Chris@16: } Chris@16: Chris@16: void swap(this_type & other) BOOST_NOEXCEPT Chris@16: { Chris@16: std::swap(px, other.px); Chris@16: pn.swap(other.pn); Chris@16: } Chris@16: Chris@16: template Chris@16: void _internal_aliasing_assign(weak_ptr const & r, element_type * px2) Chris@16: { Chris@16: px = px2; Chris@16: pn = r.pn; Chris@16: } Chris@16: Chris@16: template bool owner_before( weak_ptr const & rhs ) const BOOST_NOEXCEPT Chris@16: { Chris@16: return pn < rhs.pn; Chris@16: } Chris@16: Chris@16: template bool owner_before( shared_ptr const & rhs ) const BOOST_NOEXCEPT Chris@16: { Chris@16: return pn < rhs.pn; Chris@16: } Chris@16: Chris@16: // Tasteless as this may seem, making all members public allows member templates Chris@16: // to work in the absence of member template friends. (Matthew Langston) Chris@16: Chris@16: #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS Chris@16: Chris@16: private: Chris@16: Chris@16: template friend class weak_ptr; Chris@16: template friend class shared_ptr; Chris@16: Chris@16: #endif Chris@16: Chris@16: element_type * px; // contained pointer Chris@16: boost::detail::weak_count pn; // reference counter Chris@16: Chris@16: }; // weak_ptr Chris@16: Chris@16: template inline bool operator<(weak_ptr const & a, weak_ptr const & b) BOOST_NOEXCEPT Chris@16: { Chris@16: return a.owner_before( b ); Chris@16: } Chris@16: Chris@16: template void swap(weak_ptr & a, weak_ptr & b) BOOST_NOEXCEPT Chris@16: { Chris@16: a.swap(b); Chris@16: } Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #endif // #ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED