intrusive_ptr.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. #ifndef BOOST_SMART_PTR_INTRUSIVE_PTR_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_INTRUSIVE_PTR_HPP_INCLUDED
  3. //
  4. // intrusive_ptr.hpp
  5. //
  6. // Copyright (c) 2001, 2002 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/intrusive_ptr.html for documentation.
  13. //
  14. #include <boost/config.hpp>
  15. #include <boost/assert.hpp>
  16. #include <boost/detail/workaround.hpp>
  17. #include <boost/smart_ptr/detail/sp_convertible.hpp>
  18. #include <boost/smart_ptr/detail/sp_nullptr_t.hpp>
  19. #include <boost/config/no_tr1/functional.hpp> // for std::less
  20. #if !defined(BOOST_NO_IOSTREAM)
  21. #if !defined(BOOST_NO_IOSFWD)
  22. #include <iosfwd> // for std::basic_ostream
  23. #else
  24. #include <ostream>
  25. #endif
  26. #endif
  27. namespace boost
  28. {
  29. //
  30. // intrusive_ptr
  31. //
  32. // A smart pointer that uses intrusive reference counting.
  33. //
  34. // Relies on unqualified calls to
  35. //
  36. // void intrusive_ptr_add_ref(T * p);
  37. // void intrusive_ptr_release(T * p);
  38. //
  39. // (p != 0)
  40. //
  41. // The object is responsible for destroying itself.
  42. //
  43. template<class T> class intrusive_ptr
  44. {
  45. private:
  46. typedef intrusive_ptr this_type;
  47. public:
  48. typedef T element_type;
  49. intrusive_ptr() BOOST_NOEXCEPT : px( 0 )
  50. {
  51. }
  52. intrusive_ptr( T * p, bool add_ref = true ): px( p )
  53. {
  54. if( px != 0 && add_ref ) intrusive_ptr_add_ref( px );
  55. }
  56. #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
  57. template<class U>
  58. #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
  59. intrusive_ptr( intrusive_ptr<U> const & rhs, typename boost::detail::sp_enable_if_convertible<U,T>::type = boost::detail::sp_empty() )
  60. #else
  61. intrusive_ptr( intrusive_ptr<U> const & rhs )
  62. #endif
  63. : px( rhs.get() )
  64. {
  65. if( px != 0 ) intrusive_ptr_add_ref( px );
  66. }
  67. #endif
  68. intrusive_ptr(intrusive_ptr const & rhs): px( rhs.px )
  69. {
  70. if( px != 0 ) intrusive_ptr_add_ref( px );
  71. }
  72. ~intrusive_ptr()
  73. {
  74. if( px != 0 ) intrusive_ptr_release( px );
  75. }
  76. #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
  77. template<class U> intrusive_ptr & operator=(intrusive_ptr<U> const & rhs)
  78. {
  79. this_type(rhs).swap(*this);
  80. return *this;
  81. }
  82. #endif
  83. // Move support
  84. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  85. intrusive_ptr(intrusive_ptr && rhs) BOOST_NOEXCEPT : px( rhs.px )
  86. {
  87. rhs.px = 0;
  88. }
  89. intrusive_ptr & operator=(intrusive_ptr && rhs) BOOST_NOEXCEPT
  90. {
  91. this_type( static_cast< intrusive_ptr && >( rhs ) ).swap(*this);
  92. return *this;
  93. }
  94. #endif
  95. intrusive_ptr & operator=(intrusive_ptr const & rhs)
  96. {
  97. this_type(rhs).swap(*this);
  98. return *this;
  99. }
  100. intrusive_ptr & operator=(T * rhs)
  101. {
  102. this_type(rhs).swap(*this);
  103. return *this;
  104. }
  105. void reset() BOOST_NOEXCEPT
  106. {
  107. this_type().swap( *this );
  108. }
  109. void reset( T * rhs )
  110. {
  111. this_type( rhs ).swap( *this );
  112. }
  113. T * get() const BOOST_NOEXCEPT
  114. {
  115. return px;
  116. }
  117. T & operator*() const
  118. {
  119. BOOST_ASSERT( px != 0 );
  120. return *px;
  121. }
  122. T * operator->() const
  123. {
  124. BOOST_ASSERT( px != 0 );
  125. return px;
  126. }
  127. // implicit conversion to "bool"
  128. #include <boost/smart_ptr/detail/operator_bool.hpp>
  129. void swap(intrusive_ptr & rhs) BOOST_NOEXCEPT
  130. {
  131. T * tmp = px;
  132. px = rhs.px;
  133. rhs.px = tmp;
  134. }
  135. private:
  136. T * px;
  137. };
  138. template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
  139. {
  140. return a.get() == b.get();
  141. }
  142. template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
  143. {
  144. return a.get() != b.get();
  145. }
  146. template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, U * b)
  147. {
  148. return a.get() == b;
  149. }
  150. template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, U * b)
  151. {
  152. return a.get() != b;
  153. }
  154. template<class T, class U> inline bool operator==(T * a, intrusive_ptr<U> const & b)
  155. {
  156. return a == b.get();
  157. }
  158. template<class T, class U> inline bool operator!=(T * a, intrusive_ptr<U> const & b)
  159. {
  160. return a != b.get();
  161. }
  162. #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
  163. // Resolve the ambiguity between our op!= and the one in rel_ops
  164. template<class T> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
  165. {
  166. return a.get() != b.get();
  167. }
  168. #endif
  169. #if !defined( BOOST_NO_CXX11_NULLPTR )
  170. template<class T> inline bool operator==( intrusive_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT
  171. {
  172. return p.get() == 0;
  173. }
  174. template<class T> inline bool operator==( boost::detail::sp_nullptr_t, intrusive_ptr<T> const & p ) BOOST_NOEXCEPT
  175. {
  176. return p.get() == 0;
  177. }
  178. template<class T> inline bool operator!=( intrusive_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT
  179. {
  180. return p.get() != 0;
  181. }
  182. template<class T> inline bool operator!=( boost::detail::sp_nullptr_t, intrusive_ptr<T> const & p ) BOOST_NOEXCEPT
  183. {
  184. return p.get() != 0;
  185. }
  186. #endif
  187. template<class T> inline bool operator<(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
  188. {
  189. return std::less<T *>()(a.get(), b.get());
  190. }
  191. template<class T> void swap(intrusive_ptr<T> & lhs, intrusive_ptr<T> & rhs)
  192. {
  193. lhs.swap(rhs);
  194. }
  195. // mem_fn support
  196. template<class T> T * get_pointer(intrusive_ptr<T> const & p)
  197. {
  198. return p.get();
  199. }
  200. template<class T, class U> intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U> const & p)
  201. {
  202. return static_cast<T *>(p.get());
  203. }
  204. template<class T, class U> intrusive_ptr<T> const_pointer_cast(intrusive_ptr<U> const & p)
  205. {
  206. return const_cast<T *>(p.get());
  207. }
  208. template<class T, class U> intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> const & p)
  209. {
  210. return dynamic_cast<T *>(p.get());
  211. }
  212. // operator<<
  213. #if !defined(BOOST_NO_IOSTREAM)
  214. #if defined(BOOST_NO_TEMPLATED_IOSTREAMS) || ( defined(__GNUC__) && (__GNUC__ < 3) )
  215. template<class Y> std::ostream & operator<< (std::ostream & os, intrusive_ptr<Y> const & p)
  216. {
  217. os << p.get();
  218. return os;
  219. }
  220. #else
  221. // in STLport's no-iostreams mode no iostream symbols can be used
  222. #ifndef _STLP_NO_IOSTREAMS
  223. # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300 && __SGI_STL_PORT)
  224. // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
  225. using std::basic_ostream;
  226. template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
  227. # else
  228. template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
  229. # endif
  230. {
  231. os << p.get();
  232. return os;
  233. }
  234. #endif // _STLP_NO_IOSTREAMS
  235. #endif // __GNUC__ < 3
  236. #endif // !defined(BOOST_NO_IOSTREAM)
  237. // hash_value
  238. template< class T > struct hash;
  239. template< class T > std::size_t hash_value( boost::intrusive_ptr<T> const & p )
  240. {
  241. return boost::hash< T* >()( p.get() );
  242. }
  243. } // namespace boost
  244. #endif // #ifndef BOOST_SMART_PTR_INTRUSIVE_PTR_HPP_INCLUDED