weak_ptr.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED
  3. //
  4. // weak_ptr.hpp
  5. //
  6. // Copyright (c) 2001, 2002, 2003 Peter Dimov
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See
  9. // accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. //
  12. // See http://www.boost.org/libs/smart_ptr/ for documentation.
  13. //
  14. #include <boost/smart_ptr/detail/shared_count.hpp>
  15. #include <boost/smart_ptr/shared_ptr.hpp>
  16. #include <memory>
  17. #include <cstddef>
  18. namespace boost
  19. {
  20. template<class T> class weak_ptr
  21. {
  22. private:
  23. // Borland 5.5.1 specific workarounds
  24. typedef weak_ptr<T> this_type;
  25. public:
  26. typedef typename boost::detail::sp_element< T >::type element_type;
  27. constexpr weak_ptr() noexcept : px(0), pn()
  28. {
  29. }
  30. // generated copy constructor, assignment, destructor are fine...
  31. // ... except in C++0x, move disables the implicit copy
  32. weak_ptr( weak_ptr const & r ) noexcept : px( r.px ), pn( r.pn )
  33. {
  34. }
  35. weak_ptr & operator=( weak_ptr const & r ) noexcept
  36. {
  37. px = r.px;
  38. pn = r.pn;
  39. return *this;
  40. }
  41. //
  42. // The "obvious" converting constructor implementation:
  43. //
  44. // template<class Y>
  45. // weak_ptr(weak_ptr<Y> const & r): px(r.px), pn(r.pn)
  46. // {
  47. // }
  48. //
  49. // has a serious problem.
  50. //
  51. // r.px may already have been invalidated. The px(r.px)
  52. // conversion may require access to *r.px (virtual inheritance).
  53. //
  54. // It is not possible to avoid spurious access violations since
  55. // in multithreaded programs r.px may be invalidated at any point.
  56. //
  57. template<class Y>
  58. weak_ptr( weak_ptr<Y> const & r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
  59. noexcept : px(r.lock().get()), pn(r.pn)
  60. {
  61. boost::detail::sp_assert_convertible< Y, T >();
  62. }
  63. template<class Y>
  64. weak_ptr( weak_ptr<Y> && r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
  65. noexcept : px( r.lock().get() ), pn( static_cast< boost::detail::weak_count && >( r.pn ) )
  66. {
  67. boost::detail::sp_assert_convertible< Y, T >();
  68. r.px = 0;
  69. }
  70. // for better efficiency in the T == Y case
  71. weak_ptr( weak_ptr && r )
  72. noexcept : px( r.px ), pn( static_cast< boost::detail::weak_count && >( r.pn ) )
  73. {
  74. r.px = 0;
  75. }
  76. // for better efficiency in the T == Y case
  77. weak_ptr & operator=( weak_ptr && r ) noexcept
  78. {
  79. this_type( static_cast< weak_ptr && >( r ) ).swap( *this );
  80. return *this;
  81. }
  82. template<class Y>
  83. weak_ptr( shared_ptr<Y> const & r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
  84. noexcept : px( r.px ), pn( r.pn )
  85. {
  86. boost::detail::sp_assert_convertible< Y, T >();
  87. }
  88. // aliasing
  89. template<class Y> weak_ptr(shared_ptr<Y> const & r, element_type * p) noexcept: px( p ), pn( r.pn )
  90. {
  91. }
  92. template<class Y> weak_ptr(weak_ptr<Y> const & r, element_type * p) noexcept: px( p ), pn( r.pn )
  93. {
  94. }
  95. template<class Y> weak_ptr(weak_ptr<Y> && r, element_type * p) noexcept: px( p ), pn( std::move( r.pn ) )
  96. {
  97. }
  98. template<class Y>
  99. weak_ptr & operator=( weak_ptr<Y> const & r ) noexcept
  100. {
  101. boost::detail::sp_assert_convertible< Y, T >();
  102. px = r.lock().get();
  103. pn = r.pn;
  104. return *this;
  105. }
  106. template<class Y>
  107. weak_ptr & operator=( weak_ptr<Y> && r ) noexcept
  108. {
  109. this_type( static_cast< weak_ptr<Y> && >( r ) ).swap( *this );
  110. return *this;
  111. }
  112. template<class Y>
  113. weak_ptr & operator=( shared_ptr<Y> const & r ) noexcept
  114. {
  115. boost::detail::sp_assert_convertible< Y, T >();
  116. px = r.px;
  117. pn = r.pn;
  118. return *this;
  119. }
  120. shared_ptr<T> lock() const noexcept
  121. {
  122. return shared_ptr<T>( *this, boost::detail::sp_nothrow_tag() );
  123. }
  124. long use_count() const noexcept
  125. {
  126. return pn.use_count();
  127. }
  128. bool expired() const noexcept
  129. {
  130. return pn.use_count() == 0;
  131. }
  132. bool _empty() const noexcept // extension, not in std::weak_ptr
  133. {
  134. return pn.empty();
  135. }
  136. bool empty() const noexcept // extension, not in std::weak_ptr
  137. {
  138. return pn.empty();
  139. }
  140. void reset() noexcept
  141. {
  142. this_type().swap(*this);
  143. }
  144. void swap(this_type & other) noexcept
  145. {
  146. std::swap(px, other.px);
  147. pn.swap(other.pn);
  148. }
  149. template<class Y> bool owner_before( weak_ptr<Y> const & rhs ) const noexcept
  150. {
  151. return pn < rhs.pn;
  152. }
  153. template<class Y> bool owner_before( shared_ptr<Y> const & rhs ) const noexcept
  154. {
  155. return pn < rhs.pn;
  156. }
  157. template<class Y> bool owner_equals( weak_ptr<Y> const & rhs ) const noexcept
  158. {
  159. return pn == rhs.pn;
  160. }
  161. template<class Y> bool owner_equals( shared_ptr<Y> const & rhs ) const noexcept
  162. {
  163. return pn == rhs.pn;
  164. }
  165. std::size_t owner_hash_value() const noexcept
  166. {
  167. return pn.hash_value();
  168. }
  169. private:
  170. template<class Y> friend class weak_ptr;
  171. template<class Y> friend class shared_ptr;
  172. element_type * px; // contained pointer
  173. boost::detail::weak_count pn; // reference counter
  174. }; // weak_ptr
  175. template<class T, class U> inline bool operator<(weak_ptr<T> const & a, weak_ptr<U> const & b) noexcept
  176. {
  177. return a.owner_before( b );
  178. }
  179. template<class T> void swap(weak_ptr<T> & a, weak_ptr<T> & b) noexcept
  180. {
  181. a.swap(b);
  182. }
  183. #if defined(__cpp_deduction_guides)
  184. template<class T> weak_ptr( shared_ptr<T> ) -> weak_ptr<T>;
  185. #endif
  186. // hash_value
  187. template< class T > std::size_t hash_value( boost::weak_ptr<T> const & p ) noexcept
  188. {
  189. return p.owner_hash_value();
  190. }
  191. } // namespace boost
  192. // std::hash, std::equal_to
  193. namespace std
  194. {
  195. template<class T> struct hash< ::boost::weak_ptr<T> >
  196. {
  197. std::size_t operator()( ::boost::weak_ptr<T> const & p ) const noexcept
  198. {
  199. return p.owner_hash_value();
  200. }
  201. };
  202. template<class T> struct equal_to< ::boost::weak_ptr<T> >
  203. {
  204. bool operator()( ::boost::weak_ptr<T> const & a, ::boost::weak_ptr<T> const & b ) const noexcept
  205. {
  206. return a.owner_equals( b );
  207. }
  208. };
  209. } // namespace std
  210. #endif // #ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED