intrusive_ptr.hpp 8.9 KB

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