weak_ptr.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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/requires_cxx11.hpp>
  15. #include <boost/smart_ptr/detail/shared_count.hpp>
  16. #include <boost/smart_ptr/shared_ptr.hpp>
  17. #include <boost/smart_ptr/detail/sp_noexcept.hpp>
  18. #include <memory>
  19. #include <cstddef>
  20. namespace boost
  21. {
  22. template<class T> class weak_ptr
  23. {
  24. private:
  25. // Borland 5.5.1 specific workarounds
  26. typedef weak_ptr<T> this_type;
  27. public:
  28. typedef typename boost::detail::sp_element< T >::type element_type;
  29. BOOST_CONSTEXPR weak_ptr() BOOST_SP_NOEXCEPT : px(0), pn()
  30. {
  31. }
  32. // generated copy constructor, assignment, destructor are fine...
  33. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  34. // ... except in C++0x, move disables the implicit copy
  35. weak_ptr( weak_ptr const & r ) BOOST_SP_NOEXCEPT : px( r.px ), pn( r.pn )
  36. {
  37. }
  38. weak_ptr & operator=( weak_ptr const & r ) BOOST_SP_NOEXCEPT
  39. {
  40. px = r.px;
  41. pn = r.pn;
  42. return *this;
  43. }
  44. #endif
  45. //
  46. // The "obvious" converting constructor implementation:
  47. //
  48. // template<class Y>
  49. // weak_ptr(weak_ptr<Y> const & r): px(r.px), pn(r.pn)
  50. // {
  51. // }
  52. //
  53. // has a serious problem.
  54. //
  55. // r.px may already have been invalidated. The px(r.px)
  56. // conversion may require access to *r.px (virtual inheritance).
  57. //
  58. // It is not possible to avoid spurious access violations since
  59. // in multithreaded programs r.px may be invalidated at any point.
  60. //
  61. template<class Y>
  62. #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
  63. weak_ptr( weak_ptr<Y> const & r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
  64. #else
  65. weak_ptr( weak_ptr<Y> const & r )
  66. #endif
  67. BOOST_SP_NOEXCEPT : px(r.lock().get()), pn(r.pn)
  68. {
  69. boost::detail::sp_assert_convertible< Y, T >();
  70. }
  71. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  72. template<class Y>
  73. #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
  74. weak_ptr( weak_ptr<Y> && r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
  75. #else
  76. weak_ptr( weak_ptr<Y> && r )
  77. #endif
  78. BOOST_SP_NOEXCEPT : px( r.lock().get() ), pn( static_cast< boost::detail::weak_count && >( r.pn ) )
  79. {
  80. boost::detail::sp_assert_convertible< Y, T >();
  81. r.px = 0;
  82. }
  83. // for better efficiency in the T == Y case
  84. weak_ptr( weak_ptr && r )
  85. BOOST_SP_NOEXCEPT : px( r.px ), pn( static_cast< boost::detail::weak_count && >( r.pn ) )
  86. {
  87. r.px = 0;
  88. }
  89. // for better efficiency in the T == Y case
  90. weak_ptr & operator=( weak_ptr && r ) BOOST_SP_NOEXCEPT
  91. {
  92. this_type( static_cast< weak_ptr && >( r ) ).swap( *this );
  93. return *this;
  94. }
  95. #endif
  96. template<class Y>
  97. #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
  98. weak_ptr( shared_ptr<Y> const & r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
  99. #else
  100. weak_ptr( shared_ptr<Y> const & r )
  101. #endif
  102. BOOST_SP_NOEXCEPT : px( r.px ), pn( r.pn )
  103. {
  104. boost::detail::sp_assert_convertible< Y, T >();
  105. }
  106. // aliasing
  107. template<class Y> weak_ptr(shared_ptr<Y> const & r, element_type * p) BOOST_SP_NOEXCEPT: px( p ), pn( r.pn )
  108. {
  109. }
  110. template<class Y> weak_ptr(weak_ptr<Y> const & r, element_type * p) BOOST_SP_NOEXCEPT: px( p ), pn( r.pn )
  111. {
  112. }
  113. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  114. template<class Y> weak_ptr(weak_ptr<Y> && r, element_type * p) BOOST_SP_NOEXCEPT: px( p ), pn( std::move( r.pn ) )
  115. {
  116. }
  117. #endif
  118. #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1300)
  119. template<class Y>
  120. weak_ptr & operator=( weak_ptr<Y> const & r ) BOOST_SP_NOEXCEPT
  121. {
  122. boost::detail::sp_assert_convertible< Y, T >();
  123. px = r.lock().get();
  124. pn = r.pn;
  125. return *this;
  126. }
  127. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  128. template<class Y>
  129. weak_ptr & operator=( weak_ptr<Y> && r ) BOOST_SP_NOEXCEPT
  130. {
  131. this_type( static_cast< weak_ptr<Y> && >( r ) ).swap( *this );
  132. return *this;
  133. }
  134. #endif
  135. template<class Y>
  136. weak_ptr & operator=( shared_ptr<Y> const & r ) BOOST_SP_NOEXCEPT
  137. {
  138. boost::detail::sp_assert_convertible< Y, T >();
  139. px = r.px;
  140. pn = r.pn;
  141. return *this;
  142. }
  143. #endif
  144. shared_ptr<T> lock() const BOOST_SP_NOEXCEPT
  145. {
  146. return shared_ptr<T>( *this, boost::detail::sp_nothrow_tag() );
  147. }
  148. long use_count() const BOOST_SP_NOEXCEPT
  149. {
  150. return pn.use_count();
  151. }
  152. bool expired() const BOOST_SP_NOEXCEPT
  153. {
  154. return pn.use_count() == 0;
  155. }
  156. bool _empty() const BOOST_SP_NOEXCEPT // extension, not in std::weak_ptr
  157. {
  158. return pn.empty();
  159. }
  160. bool empty() const BOOST_SP_NOEXCEPT // extension, not in std::weak_ptr
  161. {
  162. return pn.empty();
  163. }
  164. void reset() BOOST_SP_NOEXCEPT
  165. {
  166. this_type().swap(*this);
  167. }
  168. void swap(this_type & other) BOOST_SP_NOEXCEPT
  169. {
  170. std::swap(px, other.px);
  171. pn.swap(other.pn);
  172. }
  173. template<class Y> bool owner_before( weak_ptr<Y> const & rhs ) const BOOST_SP_NOEXCEPT
  174. {
  175. return pn < rhs.pn;
  176. }
  177. template<class Y> bool owner_before( shared_ptr<Y> const & rhs ) const BOOST_SP_NOEXCEPT
  178. {
  179. return pn < rhs.pn;
  180. }
  181. template<class Y> bool owner_equals( weak_ptr<Y> const & rhs ) const BOOST_SP_NOEXCEPT
  182. {
  183. return pn == rhs.pn;
  184. }
  185. template<class Y> bool owner_equals( shared_ptr<Y> const & rhs ) const BOOST_SP_NOEXCEPT
  186. {
  187. return pn == rhs.pn;
  188. }
  189. std::size_t owner_hash_value() const BOOST_SP_NOEXCEPT
  190. {
  191. return pn.hash_value();
  192. }
  193. // Tasteless as this may seem, making all members public allows member templates
  194. // to work in the absence of member template friends. (Matthew Langston)
  195. #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  196. private:
  197. template<class Y> friend class weak_ptr;
  198. template<class Y> friend class shared_ptr;
  199. #endif
  200. element_type * px; // contained pointer
  201. boost::detail::weak_count pn; // reference counter
  202. }; // weak_ptr
  203. template<class T, class U> inline bool operator<(weak_ptr<T> const & a, weak_ptr<U> const & b) BOOST_SP_NOEXCEPT
  204. {
  205. return a.owner_before( b );
  206. }
  207. template<class T> void swap(weak_ptr<T> & a, weak_ptr<T> & b) BOOST_SP_NOEXCEPT
  208. {
  209. a.swap(b);
  210. }
  211. #if defined(__cpp_deduction_guides)
  212. template<class T> weak_ptr( shared_ptr<T> ) -> weak_ptr<T>;
  213. #endif
  214. // hash_value
  215. template< class T > std::size_t hash_value( boost::weak_ptr<T> const & p ) BOOST_SP_NOEXCEPT
  216. {
  217. return p.owner_hash_value();
  218. }
  219. } // namespace boost
  220. // std::hash, std::equal_to
  221. namespace std
  222. {
  223. #if !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL)
  224. template<class T> struct hash< ::boost::weak_ptr<T> >
  225. {
  226. std::size_t operator()( ::boost::weak_ptr<T> const & p ) const BOOST_SP_NOEXCEPT
  227. {
  228. return p.owner_hash_value();
  229. }
  230. };
  231. #endif // #if !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL)
  232. template<class T> struct equal_to< ::boost::weak_ptr<T> >
  233. {
  234. bool operator()( ::boost::weak_ptr<T> const & a, ::boost::weak_ptr<T> const & b ) const BOOST_SP_NOEXCEPT
  235. {
  236. return a.owner_equals( b );
  237. }
  238. };
  239. } // namespace std
  240. #endif // #ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED