intrusive_ptr.hpp 9.0 KB

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