weak_ptr.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 <memory> // boost.TR1 include order fix
  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. 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. BOOST_CONSTEXPR weak_ptr() BOOST_SP_NOEXCEPT : px(0), pn()
  28. {
  29. }
  30. // generated copy constructor, assignment, destructor are fine...
  31. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  32. // ... except in C++0x, move disables the implicit copy
  33. weak_ptr( weak_ptr const & r ) BOOST_SP_NOEXCEPT : px( r.px ), pn( r.pn )
  34. {
  35. }
  36. weak_ptr & operator=( weak_ptr const & r ) BOOST_SP_NOEXCEPT
  37. {
  38. px = r.px;
  39. pn = r.pn;
  40. return *this;
  41. }
  42. #endif
  43. //
  44. // The "obvious" converting constructor implementation:
  45. //
  46. // template<class Y>
  47. // weak_ptr(weak_ptr<Y> const & r): px(r.px), pn(r.pn)
  48. // {
  49. // }
  50. //
  51. // has a serious problem.
  52. //
  53. // r.px may already have been invalidated. The px(r.px)
  54. // conversion may require access to *r.px (virtual inheritance).
  55. //
  56. // It is not possible to avoid spurious access violations since
  57. // in multithreaded programs r.px may be invalidated at any point.
  58. //
  59. template<class Y>
  60. #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
  61. weak_ptr( weak_ptr<Y> const & r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
  62. #else
  63. weak_ptr( weak_ptr<Y> const & r )
  64. #endif
  65. BOOST_SP_NOEXCEPT : px(r.lock().get()), pn(r.pn)
  66. {
  67. boost::detail::sp_assert_convertible< Y, T >();
  68. }
  69. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  70. template<class Y>
  71. #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
  72. weak_ptr( weak_ptr<Y> && r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
  73. #else
  74. weak_ptr( weak_ptr<Y> && r )
  75. #endif
  76. BOOST_SP_NOEXCEPT : px( r.lock().get() ), pn( static_cast< boost::detail::weak_count && >( r.pn ) )
  77. {
  78. boost::detail::sp_assert_convertible< Y, T >();
  79. r.px = 0;
  80. }
  81. // for better efficiency in the T == Y case
  82. weak_ptr( weak_ptr && r )
  83. BOOST_SP_NOEXCEPT : px( r.px ), pn( static_cast< boost::detail::weak_count && >( r.pn ) )
  84. {
  85. r.px = 0;
  86. }
  87. // for better efficiency in the T == Y case
  88. weak_ptr & operator=( weak_ptr && r ) BOOST_SP_NOEXCEPT
  89. {
  90. this_type( static_cast< weak_ptr && >( r ) ).swap( *this );
  91. return *this;
  92. }
  93. #endif
  94. template<class Y>
  95. #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
  96. weak_ptr( shared_ptr<Y> const & r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
  97. #else
  98. weak_ptr( shared_ptr<Y> const & r )
  99. #endif
  100. BOOST_SP_NOEXCEPT : px( r.px ), pn( r.pn )
  101. {
  102. boost::detail::sp_assert_convertible< Y, T >();
  103. }
  104. #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1300)
  105. template<class Y>
  106. weak_ptr & operator=( weak_ptr<Y> const & r ) BOOST_SP_NOEXCEPT
  107. {
  108. boost::detail::sp_assert_convertible< Y, T >();
  109. px = r.lock().get();
  110. pn = r.pn;
  111. return *this;
  112. }
  113. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  114. template<class Y>
  115. weak_ptr & operator=( weak_ptr<Y> && r ) BOOST_SP_NOEXCEPT
  116. {
  117. this_type( static_cast< weak_ptr<Y> && >( r ) ).swap( *this );
  118. return *this;
  119. }
  120. #endif
  121. template<class Y>
  122. weak_ptr & operator=( shared_ptr<Y> const & r ) BOOST_SP_NOEXCEPT
  123. {
  124. boost::detail::sp_assert_convertible< Y, T >();
  125. px = r.px;
  126. pn = r.pn;
  127. return *this;
  128. }
  129. #endif
  130. shared_ptr<T> lock() const BOOST_SP_NOEXCEPT
  131. {
  132. return shared_ptr<T>( *this, boost::detail::sp_nothrow_tag() );
  133. }
  134. long use_count() const BOOST_SP_NOEXCEPT
  135. {
  136. return pn.use_count();
  137. }
  138. bool expired() const BOOST_SP_NOEXCEPT
  139. {
  140. return pn.use_count() == 0;
  141. }
  142. bool _empty() const BOOST_SP_NOEXCEPT // extension, not in std::weak_ptr
  143. {
  144. return pn.empty();
  145. }
  146. void reset() BOOST_SP_NOEXCEPT
  147. {
  148. this_type().swap(*this);
  149. }
  150. void swap(this_type & other) BOOST_SP_NOEXCEPT
  151. {
  152. std::swap(px, other.px);
  153. pn.swap(other.pn);
  154. }
  155. template<typename Y>
  156. void _internal_aliasing_assign(weak_ptr<Y> const & r, element_type * px2) BOOST_SP_NOEXCEPT
  157. {
  158. px = px2;
  159. pn = r.pn;
  160. }
  161. template<class Y> bool owner_before( weak_ptr<Y> const & rhs ) const BOOST_SP_NOEXCEPT
  162. {
  163. return pn < rhs.pn;
  164. }
  165. template<class Y> bool owner_before( shared_ptr<Y> const & rhs ) const BOOST_SP_NOEXCEPT
  166. {
  167. return pn < rhs.pn;
  168. }
  169. // Tasteless as this may seem, making all members public allows member templates
  170. // to work in the absence of member template friends. (Matthew Langston)
  171. #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  172. private:
  173. template<class Y> friend class weak_ptr;
  174. template<class Y> friend class shared_ptr;
  175. #endif
  176. element_type * px; // contained pointer
  177. boost::detail::weak_count pn; // reference counter
  178. }; // weak_ptr
  179. template<class T, class U> inline bool operator<(weak_ptr<T> const & a, weak_ptr<U> const & b) BOOST_SP_NOEXCEPT
  180. {
  181. return a.owner_before( b );
  182. }
  183. template<class T> void swap(weak_ptr<T> & a, weak_ptr<T> & b) BOOST_SP_NOEXCEPT
  184. {
  185. a.swap(b);
  186. }
  187. } // namespace boost
  188. #endif // #ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED