williamr@2: #ifndef BOOST_WEAK_PTR_HPP_INCLUDED williamr@2: #define BOOST_WEAK_PTR_HPP_INCLUDED williamr@2: williamr@2: // williamr@2: // weak_ptr.hpp williamr@2: // williamr@2: // Copyright (c) 2001, 2002, 2003 Peter Dimov williamr@2: // williamr@2: // Distributed under the Boost Software License, Version 1.0. (See williamr@2: // accompanying file LICENSE_1_0.txt or copy at williamr@2: // http://www.boost.org/LICENSE_1_0.txt) williamr@2: // williamr@2: // See http://www.boost.org/libs/smart_ptr/weak_ptr.htm for documentation. williamr@2: // williamr@2: williamr@2: #include // boost.TR1 include order fix williamr@2: #include williamr@2: #include williamr@2: williamr@2: #ifdef BOOST_MSVC // moved here to work around VC++ compiler crash williamr@2: # pragma warning(push) williamr@2: # pragma warning(disable:4284) // odd return type for operator-> williamr@2: #endif williamr@2: williamr@2: namespace boost williamr@2: { williamr@2: williamr@2: template class weak_ptr williamr@2: { williamr@2: private: williamr@2: williamr@2: // Borland 5.5.1 specific workarounds williamr@2: typedef weak_ptr this_type; williamr@2: williamr@2: public: williamr@2: williamr@2: typedef T element_type; williamr@2: williamr@2: weak_ptr(): px(0), pn() // never throws in 1.30+ williamr@2: { williamr@2: } williamr@2: williamr@2: // generated copy constructor, assignment, destructor are fine williamr@2: williamr@2: williamr@2: // williamr@2: // The "obvious" converting constructor implementation: williamr@2: // williamr@2: // template williamr@2: // weak_ptr(weak_ptr const & r): px(r.px), pn(r.pn) // never throws williamr@2: // { williamr@2: // } williamr@2: // williamr@2: // has a serious problem. williamr@2: // williamr@2: // r.px may already have been invalidated. The px(r.px) williamr@2: // conversion may require access to *r.px (virtual inheritance). williamr@2: // williamr@2: // It is not possible to avoid spurious access violations since williamr@2: // in multithreaded programs r.px may be invalidated at any point. williamr@2: // williamr@2: williamr@2: template williamr@2: weak_ptr(weak_ptr const & r): pn(r.pn) // never throws williamr@2: { williamr@2: px = r.lock().get(); williamr@2: } williamr@2: williamr@2: template williamr@2: weak_ptr(shared_ptr const & r): px(r.px), pn(r.pn) // never throws williamr@2: { williamr@2: } williamr@2: williamr@2: #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1300) williamr@2: williamr@2: template williamr@2: weak_ptr & operator=(weak_ptr const & r) // never throws williamr@2: { williamr@2: px = r.lock().get(); williamr@2: pn = r.pn; williamr@2: return *this; williamr@2: } williamr@2: williamr@2: template williamr@2: weak_ptr & operator=(shared_ptr const & r) // never throws williamr@2: { williamr@2: px = r.px; williamr@2: pn = r.pn; williamr@2: return *this; williamr@2: } williamr@2: williamr@2: #endif williamr@2: williamr@2: shared_ptr lock() const // never throws williamr@2: { williamr@2: #if defined(BOOST_HAS_THREADS) williamr@2: williamr@2: // optimization: avoid throw overhead williamr@2: if(expired()) williamr@2: { williamr@2: return shared_ptr(); williamr@2: } williamr@2: williamr@2: try williamr@2: { williamr@2: return shared_ptr(*this); williamr@2: } williamr@2: catch(bad_weak_ptr const &) williamr@2: { williamr@2: // Q: how can we get here? williamr@2: // A: another thread may have invalidated r after the use_count test above. williamr@2: return shared_ptr(); williamr@2: } williamr@2: williamr@2: #else williamr@2: williamr@2: // optimization: avoid try/catch overhead when single threaded williamr@2: return expired()? shared_ptr(): shared_ptr(*this); williamr@2: williamr@2: #endif williamr@2: } williamr@2: williamr@2: long use_count() const // never throws williamr@2: { williamr@2: return pn.use_count(); williamr@2: } williamr@2: williamr@2: bool expired() const // never throws williamr@2: { williamr@2: return pn.use_count() == 0; williamr@2: } williamr@2: williamr@2: void reset() // never throws in 1.30+ williamr@2: { williamr@2: this_type().swap(*this); williamr@2: } williamr@2: williamr@2: void swap(this_type & other) // never throws williamr@2: { williamr@2: std::swap(px, other.px); williamr@2: pn.swap(other.pn); williamr@2: } williamr@2: williamr@2: void _internal_assign(T * px2, boost::detail::shared_count const & pn2) williamr@2: { williamr@2: px = px2; williamr@2: pn = pn2; williamr@2: } williamr@2: williamr@2: template bool _internal_less(weak_ptr const & rhs) const williamr@2: { williamr@2: return pn < rhs.pn; williamr@2: } williamr@2: williamr@2: // Tasteless as this may seem, making all members public allows member templates williamr@2: // to work in the absence of member template friends. (Matthew Langston) williamr@2: williamr@2: #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS williamr@2: williamr@2: private: williamr@2: williamr@2: template friend class weak_ptr; williamr@2: template friend class shared_ptr; williamr@2: williamr@2: #endif williamr@2: williamr@2: T * px; // contained pointer williamr@2: boost::detail::weak_count pn; // reference counter williamr@2: williamr@2: }; // weak_ptr williamr@2: williamr@2: template inline bool operator<(weak_ptr const & a, weak_ptr const & b) williamr@2: { williamr@2: return a._internal_less(b); williamr@2: } williamr@2: williamr@2: template void swap(weak_ptr & a, weak_ptr & b) williamr@2: { williamr@2: a.swap(b); williamr@2: } williamr@2: williamr@2: // deprecated, provided for backward compatibility williamr@2: template shared_ptr make_shared(weak_ptr const & r) williamr@2: { williamr@2: return r.lock(); williamr@2: } williamr@2: williamr@2: } // namespace boost williamr@2: williamr@2: #ifdef BOOST_MSVC williamr@2: # pragma warning(pop) williamr@2: #endif williamr@2: williamr@2: #endif // #ifndef BOOST_WEAK_PTR_HPP_INCLUDED