Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@16: // This file is the adaptation for Interprocess of boost/weak_ptr.hpp Chris@16: // Chris@16: // (C) Copyright Peter Dimov 2001, 2002, 2003 Chris@16: // (C) Copyright Ion Gaztanaga 2006-2012. Chris@16: // Distributed under the Boost Software License, Version 1.0. Chris@16: // (See 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/interprocess for documentation. Chris@16: // Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: Chris@16: #ifndef BOOST_INTERPROCESS_WEAK_PTR_HPP_INCLUDED Chris@16: #define BOOST_INTERPROCESS_WEAK_PTR_HPP_INCLUDED Chris@16: Chris@101: #ifndef BOOST_CONFIG_HPP Chris@101: # include Chris@101: #endif Chris@101: # Chris@101: #if defined(BOOST_HAS_PRAGMA_ONCE) Chris@101: # pragma once Chris@101: #endif Chris@101: Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@101: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@16: Chris@16: //!\file Chris@16: //!Describes the smart pointer weak_ptr. Chris@16: Chris@16: namespace boost{ Chris@16: namespace interprocess{ Chris@16: Chris@16: //!The weak_ptr class template stores a "weak reference" to an object Chris@16: //!that's already managed by a shared_ptr. To access the object, a weak_ptr Chris@16: //!can be converted to a shared_ptr using the shared_ptr constructor or the Chris@16: //!member function lock. When the last shared_ptr to the object goes away Chris@16: //!and the object is deleted, the attempt to obtain a shared_ptr from the Chris@16: //!weak_ptr instances that refer to the deleted object will fail: the constructor Chris@16: //!will throw an exception of type bad_weak_ptr, and weak_ptr::lock will Chris@16: //!return an empty shared_ptr. Chris@16: //! Chris@16: //!Every weak_ptr meets the CopyConstructible and Assignable requirements Chris@16: //!of the C++ Standard Library, and so can be used in standard library containers. Chris@16: //!Comparison operators are supplied so that weak_ptr works with the standard Chris@16: //!library's associative containers. Chris@16: //! Chris@16: //!weak_ptr operations never throw exceptions. Chris@16: //! Chris@16: //!The class template is parameterized on T, the type of the object pointed to. Chris@16: template Chris@16: class weak_ptr Chris@16: { Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: private: Chris@16: // Borland 5.5.1 specific workarounds Chris@16: typedef weak_ptr this_type; Chris@16: typedef typename boost::intrusive:: Chris@16: pointer_traits::template Chris@16: rebind_pointer::type pointer; Chris@16: typedef typename ipcdetail::add_reference Chris@16: ::type reference; Chris@16: typedef typename ipcdetail::add_reference Chris@16: ::type const_reference; Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: Chris@16: public: Chris@16: typedef T element_type; Chris@16: typedef T value_type; Chris@16: Chris@16: //!Effects: Constructs an empty weak_ptr. Chris@16: //!Postconditions: use_count() == 0. Chris@16: weak_ptr() Chris@16: : m_pn() // never throws Chris@16: {} Chris@16: // generated copy constructor, assignment, destructor are fine Chris@16: // Chris@16: // The "obvious" converting constructor implementation: Chris@16: // Chris@16: // template Chris@16: // weak_ptr(weak_ptr const & r): m_px(r.m_px), m_pn(r.m_pn) // never throws Chris@16: // { Chris@16: // } Chris@16: // Chris@16: // has a serious problem. Chris@16: // Chris@16: // r.m_px may already have been invalidated. The m_px(r.m_px) Chris@16: // conversion may require access to *r.m_px (virtual inheritance). Chris@16: // Chris@16: // It is not possible to avoid spurious access violations since Chris@16: // in multithreaded programs r.m_px may be invalidated at any point. Chris@16: Chris@16: //!Effects: If r is empty, constructs an empty weak_ptr; otherwise, Chris@16: //!constructs a weak_ptr that shares ownership with r as if by storing a Chris@16: //!copy of the pointer stored in r. Chris@16: //! Chris@16: //!Postconditions: use_count() == r.use_count(). Chris@16: //! Chris@16: //!Throws: nothing. Chris@16: template Chris@16: weak_ptr(weak_ptr const & r) Chris@16: : m_pn(r.m_pn) // never throws Chris@16: { Chris@16: //Construct a temporary shared_ptr so that nobody Chris@16: //can destroy the value while constructing this Chris@16: const shared_ptr &ref = r.lock(); Chris@16: m_pn.set_pointer(ref.get()); Chris@16: } Chris@16: Chris@16: //!Effects: If r is empty, constructs an empty weak_ptr; otherwise, Chris@16: //!constructs a weak_ptr that shares ownership with r as if by storing a Chris@16: //!copy of the pointer stored in r. Chris@16: //! Chris@16: //!Postconditions: use_count() == r.use_count(). Chris@16: //! Chris@16: //!Throws: nothing. Chris@16: template Chris@16: weak_ptr(shared_ptr const & r) Chris@16: : m_pn(r.m_pn) // never throws Chris@16: {} Chris@16: Chris@16: //!Effects: Equivalent to weak_ptr(r).swap(*this). Chris@16: //! Chris@16: //!Throws: nothing. Chris@16: //! Chris@16: //!Notes: The implementation is free to meet the effects (and the Chris@16: //!implied guarantees) via different means, without creating a temporary. Chris@16: template Chris@16: weak_ptr & operator=(weak_ptr const & r) // never throws Chris@16: { Chris@16: //Construct a temporary shared_ptr so that nobody Chris@16: //can destroy the value while constructing this Chris@16: const shared_ptr &ref = r.lock(); Chris@16: m_pn = r.m_pn; Chris@16: m_pn.set_pointer(ref.get()); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: //!Effects: Equivalent to weak_ptr(r).swap(*this). Chris@16: //! Chris@16: //!Throws: nothing. Chris@16: //! Chris@16: //!Notes: The implementation is free to meet the effects (and the Chris@16: //!implied guarantees) via different means, without creating a temporary. Chris@16: template Chris@16: weak_ptr & operator=(shared_ptr const & r) // never throws Chris@16: { m_pn = r.m_pn; return *this; } Chris@16: Chris@16: //!Returns: expired()? shared_ptr(): shared_ptr(*this). Chris@16: //! Chris@16: //!Throws: nothing. Chris@16: shared_ptr lock() const // never throws Chris@16: { Chris@16: // optimization: avoid throw overhead Chris@16: if(expired()){ Chris@16: return shared_ptr(); Chris@16: } Chris@16: BOOST_TRY{ Chris@16: return shared_ptr(*this); Chris@16: } Chris@16: BOOST_CATCH(bad_weak_ptr const &){ Chris@16: // Q: how can we get here? Chris@16: // A: another thread may have invalidated r after the use_count test above. Chris@16: return shared_ptr(); Chris@16: } Chris@16: BOOST_CATCH_END Chris@16: } Chris@16: Chris@16: //!Returns: 0 if *this is empty; otherwise, the number of shared_ptr objects Chris@16: //!that share ownership with *this. Chris@16: //! Chris@16: //!Throws: nothing. Chris@16: //! Chris@16: //!Notes: use_count() is not necessarily efficient. Use only for debugging and Chris@16: //!testing purposes, not for production code. Chris@16: long use_count() const // never throws Chris@16: { return m_pn.use_count(); } Chris@16: Chris@16: //!Returns: Returns: use_count() == 0. Chris@16: //! Chris@16: //!Throws: nothing. Chris@16: //! Chris@16: //!Notes: expired() may be faster than use_count(). Chris@16: bool expired() const // never throws Chris@16: { return m_pn.use_count() == 0; } Chris@16: Chris@16: //!Effects: Equivalent to: Chris@16: //!weak_ptr().swap(*this). Chris@16: void reset() // never throws in 1.30+ Chris@16: { this_type().swap(*this); } Chris@16: Chris@16: //!Effects: Exchanges the contents of the two Chris@16: //!smart pointers. Chris@16: //! Chris@16: //!Throws: nothing. Chris@16: void swap(this_type & other) // never throws Chris@101: { ::boost::adl_move_swap(m_pn, other.m_pn); } Chris@16: Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: template Chris@16: bool _internal_less(weak_ptr const & rhs) const Chris@16: { return m_pn < rhs.m_pn; } Chris@16: Chris@16: template Chris@16: void _internal_assign(const ipcdetail::shared_count & pn2) Chris@16: { Chris@16: Chris@16: m_pn = pn2; Chris@16: } Chris@16: Chris@16: private: Chris@16: Chris@16: template friend class shared_ptr; Chris@16: template friend class weak_ptr; Chris@16: Chris@16: ipcdetail::weak_count m_pn; // reference counter Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: }; // weak_ptr Chris@16: Chris@16: template inline Chris@16: bool operator<(weak_ptr const & a, weak_ptr const & b) Chris@16: { return a._internal_less(b); } Chris@16: Chris@16: template inline Chris@16: void swap(weak_ptr & a, weak_ptr & b) Chris@16: { a.swap(b); } Chris@16: Chris@16: //!Returns the type of a weak pointer Chris@16: //!of type T with the allocator boost::interprocess::allocator allocator Chris@16: //!and boost::interprocess::deleter deleter Chris@16: //!that can be constructed in the given managed segment type. Chris@16: template Chris@16: struct managed_weak_ptr Chris@16: { Chris@16: typedef weak_ptr Chris@16: < T Chris@16: , typename ManagedMemory::template allocator::type Chris@16: , typename ManagedMemory::template deleter::type Chris@16: > type; Chris@16: }; Chris@16: Chris@16: //!Returns an instance of a weak pointer constructed Chris@16: //!with the default allocator and deleter from a pointer Chris@16: //!of type T that has been allocated in the passed managed segment Chris@16: template Chris@16: inline typename managed_weak_ptr::type Chris@16: make_managed_weak_ptr(T *constructed_object, ManagedMemory &managed_memory) Chris@16: { Chris@16: return typename managed_weak_ptr::type Chris@16: ( constructed_object Chris@16: , managed_memory.template get_allocator() Chris@16: , managed_memory.template get_deleter() Chris@16: ); Chris@16: } Chris@16: Chris@16: } // namespace interprocess Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // #ifndef BOOST_INTERPROCESS_WEAK_PTR_HPP_INCLUDED