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 <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. // aliasing
  105. template<class Y> weak_ptr(shared_ptr<Y> const & r, element_type * p) BOOST_SP_NOEXCEPT: px( p ), pn( r.pn )
  106. {
  107. }
  108. template<class Y> weak_ptr(weak_ptr<Y> const & r, element_type * p) BOOST_SP_NOEXCEPT: px( p ), pn( r.pn )
  109. {
  110. }
  111. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  112. template<class Y> weak_ptr(weak_ptr<Y> && r, element_type * p) BOOST_SP_NOEXCEPT: px( p ), pn( std::move( r.pn ) )
  113. {
  114. }
  115. #endif
  116. #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1300)
  117. template<class Y>
  118. weak_ptr & operator=( weak_ptr<Y> const & r ) BOOST_SP_NOEXCEPT
  119. {
  120. boost::detail::sp_assert_convertible< Y, T >();
  121. px = r.lock().get();
  122. pn = r.pn;
  123. return *this;
  124. }
  125. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  126. template<class Y>
  127. weak_ptr & operator=( weak_ptr<Y> && r ) BOOST_SP_NOEXCEPT
  128. {
  129. this_type( static_cast< weak_ptr<Y> && >( r ) ).swap( *this );
  130. return *this;
  131. }
  132. #endif
  133. template<class Y>
  134. weak_ptr & operator=( shared_ptr<Y> const & r ) BOOST_SP_NOEXCEPT
  135. {
  136. boost::detail::sp_assert_convertible< Y, T >();
  137. px = r.px;
  138. pn = r.pn;
  139. return *this;
  140. }
  141. #endif
  142. shared_ptr<T> lock() const BOOST_SP_NOEXCEPT
  143. {
  144. return shared_ptr<T>( *this, boost::detail::sp_nothrow_tag() );
  145. }
  146. long use_count() const BOOST_SP_NOEXCEPT
  147. {
  148. return pn.use_count();
  149. }
  150. bool expired() const BOOST_SP_NOEXCEPT
  151. {
  152. return pn.use_count() == 0;
  153. }
  154. bool _empty() const BOOST_SP_NOEXCEPT // extension, not in std::weak_ptr
  155. {
  156. return pn.empty();
  157. }
  158. bool empty() const BOOST_SP_NOEXCEPT // extension, not in std::weak_ptr
  159. {
  160. return pn.empty();
  161. }
  162. void reset() BOOST_SP_NOEXCEPT
  163. {
  164. this_type().swap(*this);
  165. }
  166. void swap(this_type & other) BOOST_SP_NOEXCEPT
  167. {
  168. std::swap(px, other.px);
  169. pn.swap(other.pn);
  170. }
  171. template<class Y> bool owner_before( weak_ptr<Y> const & rhs ) const BOOST_SP_NOEXCEPT
  172. {
  173. return pn < rhs.pn;
  174. }
  175. template<class Y> bool owner_before( shared_ptr<Y> const & rhs ) const BOOST_SP_NOEXCEPT
  176. {
  177. return pn < rhs.pn;
  178. }
  179. // Tasteless as this may seem, making all members public allows member templates
  180. // to work in the absence of member template friends. (Matthew Langston)
  181. #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  182. private:
  183. template<class Y> friend class weak_ptr;
  184. template<class Y> friend class shared_ptr;
  185. #endif
  186. element_type * px; // contained pointer
  187. boost::detail::weak_count pn; // reference counter
  188. }; // weak_ptr
  189. template<class T, class U> inline bool operator<(weak_ptr<T> const & a, weak_ptr<U> const & b) BOOST_SP_NOEXCEPT
  190. {
  191. return a.owner_before( b );
  192. }
  193. template<class T> void swap(weak_ptr<T> & a, weak_ptr<T> & b) BOOST_SP_NOEXCEPT
  194. {
  195. a.swap(b);
  196. }
  197. } // namespace boost
  198. #endif // #ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED