weak_ptr.hpp 7.4 KB

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