weak_ptr.hpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // This file is the adaptation for Interprocess of boost/weak_ptr.hpp
  4. //
  5. // (C) Copyright Peter Dimov 2001, 2002, 2003
  6. // (C) Copyright Ion Gaztanaga 2006-2012.
  7. // Distributed under the Boost Software License, Version 1.0.
  8. // (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // See http://www.boost.org/libs/interprocess for documentation.
  12. //
  13. //////////////////////////////////////////////////////////////////////////////
  14. #ifndef BOOST_INTERPROCESS_WEAK_PTR_HPP_INCLUDED
  15. #define BOOST_INTERPROCESS_WEAK_PTR_HPP_INCLUDED
  16. #include <boost/interprocess/detail/config_begin.hpp>
  17. #include <boost/interprocess/detail/workaround.hpp>
  18. #include <boost/interprocess/smart_ptr/shared_ptr.hpp>
  19. #include <boost/detail/no_exceptions_support.hpp>
  20. #include <boost/interprocess/allocators/allocator.hpp>
  21. #include <boost/interprocess/smart_ptr/deleter.hpp>
  22. #include <boost/intrusive/pointer_traits.hpp>
  23. //!\file
  24. //!Describes the smart pointer weak_ptr.
  25. namespace boost{
  26. namespace interprocess{
  27. //!The weak_ptr class template stores a "weak reference" to an object
  28. //!that's already managed by a shared_ptr. To access the object, a weak_ptr
  29. //!can be converted to a shared_ptr using the shared_ptr constructor or the
  30. //!member function lock. When the last shared_ptr to the object goes away
  31. //!and the object is deleted, the attempt to obtain a shared_ptr from the
  32. //!weak_ptr instances that refer to the deleted object will fail: the constructor
  33. //!will throw an exception of type bad_weak_ptr, and weak_ptr::lock will
  34. //!return an empty shared_ptr.
  35. //!
  36. //!Every weak_ptr meets the CopyConstructible and Assignable requirements
  37. //!of the C++ Standard Library, and so can be used in standard library containers.
  38. //!Comparison operators are supplied so that weak_ptr works with the standard
  39. //!library's associative containers.
  40. //!
  41. //!weak_ptr operations never throw exceptions.
  42. //!
  43. //!The class template is parameterized on T, the type of the object pointed to.
  44. template<class T, class A, class D>
  45. class weak_ptr
  46. {
  47. /// @cond
  48. private:
  49. // Borland 5.5.1 specific workarounds
  50. typedef weak_ptr<T, A, D> this_type;
  51. typedef typename boost::intrusive::
  52. pointer_traits<typename A::pointer>::template
  53. rebind_pointer<T>::type pointer;
  54. typedef typename ipcdetail::add_reference
  55. <T>::type reference;
  56. typedef typename ipcdetail::add_reference
  57. <T>::type const_reference;
  58. /// @endcond
  59. public:
  60. typedef T element_type;
  61. typedef T value_type;
  62. //!Effects: Constructs an empty weak_ptr.
  63. //!Postconditions: use_count() == 0.
  64. weak_ptr()
  65. : m_pn() // never throws
  66. {}
  67. // generated copy constructor, assignment, destructor are fine
  68. //
  69. // The "obvious" converting constructor implementation:
  70. //
  71. // template<class Y>
  72. // weak_ptr(weak_ptr<Y> const & r): m_px(r.m_px), m_pn(r.m_pn) // never throws
  73. // {
  74. // }
  75. //
  76. // has a serious problem.
  77. //
  78. // r.m_px may already have been invalidated. The m_px(r.m_px)
  79. // conversion may require access to *r.m_px (virtual inheritance).
  80. //
  81. // It is not possible to avoid spurious access violations since
  82. // in multithreaded programs r.m_px may be invalidated at any point.
  83. //!Effects: If r is empty, constructs an empty weak_ptr; otherwise,
  84. //!constructs a weak_ptr that shares ownership with r as if by storing a
  85. //!copy of the pointer stored in r.
  86. //!
  87. //!Postconditions: use_count() == r.use_count().
  88. //!
  89. //!Throws: nothing.
  90. template<class Y>
  91. weak_ptr(weak_ptr<Y, A, D> const & r)
  92. : m_pn(r.m_pn) // never throws
  93. {
  94. //Construct a temporary shared_ptr so that nobody
  95. //can destroy the value while constructing this
  96. const shared_ptr<T, A, D> &ref = r.lock();
  97. m_pn.set_pointer(ref.get());
  98. }
  99. //!Effects: If r is empty, constructs an empty weak_ptr; otherwise,
  100. //!constructs a weak_ptr that shares ownership with r as if by storing a
  101. //!copy of the pointer stored in r.
  102. //!
  103. //!Postconditions: use_count() == r.use_count().
  104. //!
  105. //!Throws: nothing.
  106. template<class Y>
  107. weak_ptr(shared_ptr<Y, A, D> const & r)
  108. : m_pn(r.m_pn) // never throws
  109. {}
  110. //!Effects: Equivalent to weak_ptr(r).swap(*this).
  111. //!
  112. //!Throws: nothing.
  113. //!
  114. //!Notes: The implementation is free to meet the effects (and the
  115. //!implied guarantees) via different means, without creating a temporary.
  116. template<class Y>
  117. weak_ptr & operator=(weak_ptr<Y, A, D> const & r) // never throws
  118. {
  119. //Construct a temporary shared_ptr so that nobody
  120. //can destroy the value while constructing this
  121. const shared_ptr<T, A, D> &ref = r.lock();
  122. m_pn = r.m_pn;
  123. m_pn.set_pointer(ref.get());
  124. return *this;
  125. }
  126. //!Effects: Equivalent to weak_ptr(r).swap(*this).
  127. //!
  128. //!Throws: nothing.
  129. //!
  130. //!Notes: The implementation is free to meet the effects (and the
  131. //!implied guarantees) via different means, without creating a temporary.
  132. template<class Y>
  133. weak_ptr & operator=(shared_ptr<Y, A, D> const & r) // never throws
  134. { m_pn = r.m_pn; return *this; }
  135. //!Returns: expired()? shared_ptr<T>(): shared_ptr<T>(*this).
  136. //!
  137. //!Throws: nothing.
  138. shared_ptr<T, A, D> lock() const // never throws
  139. {
  140. // optimization: avoid throw overhead
  141. if(expired()){
  142. return shared_ptr<element_type, A, D>();
  143. }
  144. BOOST_TRY{
  145. return shared_ptr<element_type, A, D>(*this);
  146. }
  147. BOOST_CATCH(bad_weak_ptr const &){
  148. // Q: how can we get here?
  149. // A: another thread may have invalidated r after the use_count test above.
  150. return shared_ptr<element_type, A, D>();
  151. }
  152. BOOST_CATCH_END
  153. }
  154. //!Returns: 0 if *this is empty; otherwise, the number of shared_ptr objects
  155. //!that share ownership with *this.
  156. //!
  157. //!Throws: nothing.
  158. //!
  159. //!Notes: use_count() is not necessarily efficient. Use only for debugging and
  160. //!testing purposes, not for production code.
  161. long use_count() const // never throws
  162. { return m_pn.use_count(); }
  163. //!Returns: Returns: use_count() == 0.
  164. //!
  165. //!Throws: nothing.
  166. //!
  167. //!Notes: expired() may be faster than use_count().
  168. bool expired() const // never throws
  169. { return m_pn.use_count() == 0; }
  170. //!Effects: Equivalent to:
  171. //!weak_ptr().swap(*this).
  172. void reset() // never throws in 1.30+
  173. { this_type().swap(*this); }
  174. //!Effects: Exchanges the contents of the two
  175. //!smart pointers.
  176. //!
  177. //!Throws: nothing.
  178. void swap(this_type & other) // never throws
  179. { ipcdetail::do_swap(m_pn, other.m_pn); }
  180. /// @cond
  181. template<class T2, class A2, class D2>
  182. bool _internal_less(weak_ptr<T2, A2, D2> const & rhs) const
  183. { return m_pn < rhs.m_pn; }
  184. template<class Y>
  185. void _internal_assign(const ipcdetail::shared_count<Y, A, D> & pn2)
  186. {
  187. m_pn = pn2;
  188. }
  189. private:
  190. template<class T2, class A2, class D2> friend class shared_ptr;
  191. template<class T2, class A2, class D2> friend class weak_ptr;
  192. ipcdetail::weak_count<T, A, D> m_pn; // reference counter
  193. /// @endcond
  194. }; // weak_ptr
  195. template<class T, class A, class D, class U, class A2, class D2> inline
  196. bool operator<(weak_ptr<T, A, D> const & a, weak_ptr<U, A2, D2> const & b)
  197. { return a._internal_less(b); }
  198. template<class T, class A, class D> inline
  199. void swap(weak_ptr<T, A, D> & a, weak_ptr<T, A, D> & b)
  200. { a.swap(b); }
  201. //!Returns the type of a weak pointer
  202. //!of type T with the allocator boost::interprocess::allocator allocator
  203. //!and boost::interprocess::deleter deleter
  204. //!that can be constructed in the given managed segment type.
  205. template<class T, class ManagedMemory>
  206. struct managed_weak_ptr
  207. {
  208. typedef weak_ptr
  209. < T
  210. , typename ManagedMemory::template allocator<void>::type
  211. , typename ManagedMemory::template deleter<T>::type
  212. > type;
  213. };
  214. //!Returns an instance of a weak pointer constructed
  215. //!with the default allocator and deleter from a pointer
  216. //!of type T that has been allocated in the passed managed segment
  217. template<class T, class ManagedMemory>
  218. inline typename managed_weak_ptr<T, ManagedMemory>::type
  219. make_managed_weak_ptr(T *constructed_object, ManagedMemory &managed_memory)
  220. {
  221. return typename managed_weak_ptr<T, ManagedMemory>::type
  222. ( constructed_object
  223. , managed_memory.template get_allocator<void>()
  224. , managed_memory.template get_deleter<T>()
  225. );
  226. }
  227. } // namespace interprocess
  228. } // namespace boost
  229. #include <boost/interprocess/detail/config_end.hpp>
  230. #endif // #ifndef BOOST_INTERPROCESS_WEAK_PTR_HPP_INCLUDED