weak_ptr.hpp 9.0 KB

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