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