intrusive_ptr.hpp 6.3 KB

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