weak_ptr.hpp 8.9 KB

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