shared_count.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. #ifndef BOOST_SMART_PTR_DETAIL_SHARED_COUNT_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_SHARED_COUNT_HPP_INCLUDED
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. //
  8. // detail/shared_count.hpp
  9. //
  10. // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
  11. // Copyright 2004-2005 Peter Dimov
  12. //
  13. // Distributed under the Boost Software License, Version 1.0. (See
  14. // accompanying file LICENSE_1_0.txt or copy at
  15. // http://www.boost.org/LICENSE_1_0.txt)
  16. //
  17. #include <boost/smart_ptr/bad_weak_ptr.hpp>
  18. #include <boost/smart_ptr/detail/sp_counted_base.hpp>
  19. #include <boost/smart_ptr/detail/sp_counted_impl.hpp>
  20. #include <boost/smart_ptr/detail/sp_disable_deprecated.hpp>
  21. #include <boost/smart_ptr/detail/deprecated_macros.hpp>
  22. #include <boost/core/checked_delete.hpp>
  23. #include <boost/throw_exception.hpp>
  24. #include <boost/core/addressof.hpp>
  25. #include <boost/config.hpp>
  26. #include <boost/config/workaround.hpp>
  27. #include <cstdint>
  28. #include <memory> // std::auto_ptr
  29. #include <functional> // std::less
  30. #include <cstddef> // std::size_t
  31. #ifdef BOOST_NO_EXCEPTIONS
  32. # include <new> // std::bad_alloc
  33. #endif
  34. #if defined( BOOST_SP_DISABLE_DEPRECATED )
  35. #pragma GCC diagnostic push
  36. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  37. #endif
  38. namespace boost
  39. {
  40. namespace movelib
  41. {
  42. template< class T, class D > class unique_ptr;
  43. } // namespace movelib
  44. namespace detail
  45. {
  46. struct sp_nothrow_tag {};
  47. template< class D > struct sp_inplace_tag
  48. {
  49. };
  50. template< class T > class sp_reference_wrapper
  51. {
  52. public:
  53. explicit sp_reference_wrapper( T & t): t_( boost::addressof( t ) )
  54. {
  55. }
  56. template< class Y > void operator()( Y * p ) const
  57. {
  58. (*t_)( p );
  59. }
  60. private:
  61. T * t_;
  62. };
  63. template< class D > struct sp_convert_reference
  64. {
  65. typedef D type;
  66. };
  67. template< class D > struct sp_convert_reference< D& >
  68. {
  69. typedef sp_reference_wrapper< D > type;
  70. };
  71. template<class T> std::size_t sp_hash_pointer( T* p ) noexcept
  72. {
  73. std::uintptr_t v = reinterpret_cast<std::uintptr_t>( p );
  74. // match boost::hash<T*>
  75. return static_cast<std::size_t>( v + ( v >> 3 ) );
  76. }
  77. class weak_count;
  78. class shared_count
  79. {
  80. private:
  81. sp_counted_base * pi_;
  82. friend class weak_count;
  83. public:
  84. constexpr shared_count() noexcept: pi_(0)
  85. {
  86. }
  87. constexpr explicit shared_count( sp_counted_base * pi ) noexcept: pi_( pi )
  88. {
  89. }
  90. template<class Y> explicit shared_count( Y * p ): pi_( 0 )
  91. {
  92. #ifndef BOOST_NO_EXCEPTIONS
  93. try
  94. {
  95. pi_ = new sp_counted_impl_p<Y>( p );
  96. }
  97. catch(...)
  98. {
  99. boost::checked_delete( p );
  100. throw;
  101. }
  102. #else
  103. pi_ = new sp_counted_impl_p<Y>( p );
  104. if( pi_ == 0 )
  105. {
  106. boost::checked_delete( p );
  107. boost::throw_exception( std::bad_alloc() );
  108. }
  109. #endif
  110. }
  111. template<class P, class D> shared_count( P p, D d ): pi_(0)
  112. {
  113. #ifndef BOOST_NO_EXCEPTIONS
  114. try
  115. {
  116. pi_ = new sp_counted_impl_pd<P, D>(p, d);
  117. }
  118. catch(...)
  119. {
  120. d(p); // delete p
  121. throw;
  122. }
  123. #else
  124. pi_ = new sp_counted_impl_pd<P, D>(p, d);
  125. if(pi_ == 0)
  126. {
  127. d(p); // delete p
  128. boost::throw_exception(std::bad_alloc());
  129. }
  130. #endif
  131. }
  132. template< class P, class D > shared_count( P p, sp_inplace_tag<D> ): pi_( 0 )
  133. {
  134. #ifndef BOOST_NO_EXCEPTIONS
  135. try
  136. {
  137. pi_ = new sp_counted_impl_pd< P, D >( p );
  138. }
  139. catch( ... )
  140. {
  141. D::operator_fn( p ); // delete p
  142. throw;
  143. }
  144. #else
  145. pi_ = new sp_counted_impl_pd< P, D >( p );
  146. if( pi_ == 0 )
  147. {
  148. D::operator_fn( p ); // delete p
  149. boost::throw_exception( std::bad_alloc() );
  150. }
  151. #endif // #ifndef BOOST_NO_EXCEPTIONS
  152. }
  153. template<class P, class D, class A> shared_count( P p, D d, A a ): pi_( 0 )
  154. {
  155. typedef sp_counted_impl_pda<P, D, A> impl_type;
  156. typedef typename std::allocator_traits<A>::template rebind_alloc< impl_type > A2;
  157. A2 a2( a );
  158. #ifndef BOOST_NO_EXCEPTIONS
  159. try
  160. {
  161. pi_ = a2.allocate( 1 );
  162. ::new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
  163. }
  164. catch(...)
  165. {
  166. d( p );
  167. if( pi_ != 0 )
  168. {
  169. a2.deallocate( static_cast< impl_type* >( pi_ ), 1 );
  170. }
  171. throw;
  172. }
  173. #else
  174. pi_ = a2.allocate( 1 );
  175. if( pi_ != 0 )
  176. {
  177. ::new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
  178. }
  179. else
  180. {
  181. d( p );
  182. boost::throw_exception( std::bad_alloc() );
  183. }
  184. #endif
  185. }
  186. template< class P, class D, class A > shared_count( P p, sp_inplace_tag< D >, A a ): pi_( 0 )
  187. {
  188. typedef sp_counted_impl_pda< P, D, A > impl_type;
  189. typedef typename std::allocator_traits<A>::template rebind_alloc< impl_type > A2;
  190. A2 a2( a );
  191. #ifndef BOOST_NO_EXCEPTIONS
  192. try
  193. {
  194. pi_ = a2.allocate( 1 );
  195. ::new( static_cast< void* >( pi_ ) ) impl_type( p, a );
  196. }
  197. catch(...)
  198. {
  199. D::operator_fn( p );
  200. if( pi_ != 0 )
  201. {
  202. a2.deallocate( static_cast< impl_type* >( pi_ ), 1 );
  203. }
  204. throw;
  205. }
  206. #else
  207. pi_ = a2.allocate( 1 );
  208. if( pi_ != 0 )
  209. {
  210. ::new( static_cast< void* >( pi_ ) ) impl_type( p, a );
  211. }
  212. else
  213. {
  214. D::operator_fn( p );
  215. boost::throw_exception( std::bad_alloc() );
  216. }
  217. #endif // #ifndef BOOST_NO_EXCEPTIONS
  218. }
  219. #ifndef BOOST_NO_AUTO_PTR
  220. // auto_ptr<Y> is special cased to provide the strong guarantee
  221. template<class Y>
  222. explicit shared_count( std::auto_ptr<Y> & r ): pi_( new sp_counted_impl_p<Y>( r.get() ) )
  223. {
  224. #ifdef BOOST_NO_EXCEPTIONS
  225. if( pi_ == 0 )
  226. {
  227. boost::throw_exception(std::bad_alloc());
  228. }
  229. #endif
  230. r.release();
  231. }
  232. #endif
  233. template<class Y, class D>
  234. explicit shared_count( std::unique_ptr<Y, D> & r ): pi_( 0 )
  235. {
  236. typedef typename sp_convert_reference<D>::type D2;
  237. D2 d2( static_cast<D&&>( r.get_deleter() ) );
  238. pi_ = new sp_counted_impl_pd< typename std::unique_ptr<Y, D>::pointer, D2 >( r.get(), d2 );
  239. #ifdef BOOST_NO_EXCEPTIONS
  240. if( pi_ == 0 )
  241. {
  242. boost::throw_exception( std::bad_alloc() );
  243. }
  244. #endif
  245. r.release();
  246. }
  247. template<class Y, class D>
  248. explicit shared_count( boost::movelib::unique_ptr<Y, D> & r ): pi_( 0 )
  249. {
  250. typedef typename sp_convert_reference<D>::type D2;
  251. D2 d2( r.get_deleter() );
  252. pi_ = new sp_counted_impl_pd< typename boost::movelib::unique_ptr<Y, D>::pointer, D2 >( r.get(), d2 );
  253. #ifdef BOOST_NO_EXCEPTIONS
  254. if( pi_ == 0 )
  255. {
  256. boost::throw_exception( std::bad_alloc() );
  257. }
  258. #endif
  259. r.release();
  260. }
  261. ~shared_count() /*noexcept*/
  262. {
  263. if( pi_ != 0 ) pi_->release();
  264. }
  265. shared_count(shared_count const & r) noexcept: pi_(r.pi_)
  266. {
  267. if( pi_ != 0 ) pi_->add_ref_copy();
  268. }
  269. shared_count(shared_count && r) noexcept: pi_(r.pi_)
  270. {
  271. r.pi_ = 0;
  272. }
  273. explicit shared_count(weak_count const & r); // throws bad_weak_ptr when r.use_count() == 0
  274. shared_count( weak_count const & r, sp_nothrow_tag ) noexcept; // constructs an empty *this when r.use_count() == 0
  275. shared_count & operator= (shared_count const & r) noexcept
  276. {
  277. sp_counted_base * tmp = r.pi_;
  278. if( tmp != pi_ )
  279. {
  280. if( tmp != 0 ) tmp->add_ref_copy();
  281. if( pi_ != 0 ) pi_->release();
  282. pi_ = tmp;
  283. }
  284. return *this;
  285. }
  286. void swap(shared_count & r) noexcept
  287. {
  288. sp_counted_base * tmp = r.pi_;
  289. r.pi_ = pi_;
  290. pi_ = tmp;
  291. }
  292. long use_count() const noexcept
  293. {
  294. return pi_ != 0? pi_->use_count(): 0;
  295. }
  296. bool unique() const noexcept
  297. {
  298. return use_count() == 1;
  299. }
  300. bool empty() const noexcept
  301. {
  302. return pi_ == 0;
  303. }
  304. bool operator==( shared_count const & r ) const noexcept
  305. {
  306. return pi_ == r.pi_;
  307. }
  308. bool operator==( weak_count const & r ) const noexcept;
  309. bool operator<( shared_count const & r ) const noexcept
  310. {
  311. return std::less<sp_counted_base *>()( pi_, r.pi_ );
  312. }
  313. bool operator<( weak_count const & r ) const noexcept;
  314. void * get_deleter( sp_typeinfo_ const & ti ) const noexcept
  315. {
  316. return pi_? pi_->get_deleter( ti ): 0;
  317. }
  318. void * get_local_deleter( sp_typeinfo_ const & ti ) const noexcept
  319. {
  320. return pi_? pi_->get_local_deleter( ti ): 0;
  321. }
  322. void * get_untyped_deleter() const noexcept
  323. {
  324. return pi_? pi_->get_untyped_deleter(): 0;
  325. }
  326. std::size_t hash_value() const noexcept
  327. {
  328. return sp_hash_pointer( pi_ );
  329. }
  330. };
  331. class weak_count
  332. {
  333. private:
  334. sp_counted_base * pi_;
  335. friend class shared_count;
  336. public:
  337. constexpr weak_count() noexcept: pi_(0)
  338. {
  339. }
  340. weak_count(shared_count const & r) noexcept: pi_(r.pi_)
  341. {
  342. if(pi_ != 0) pi_->weak_add_ref();
  343. }
  344. weak_count(weak_count const & r) noexcept: pi_(r.pi_)
  345. {
  346. if(pi_ != 0) pi_->weak_add_ref();
  347. }
  348. // Move support
  349. weak_count(weak_count && r) noexcept: pi_(r.pi_)
  350. {
  351. r.pi_ = 0;
  352. }
  353. ~weak_count() /*noexcept*/
  354. {
  355. if(pi_ != 0) pi_->weak_release();
  356. }
  357. weak_count & operator= (shared_count const & r) noexcept
  358. {
  359. sp_counted_base * tmp = r.pi_;
  360. if( tmp != pi_ )
  361. {
  362. if(tmp != 0) tmp->weak_add_ref();
  363. if(pi_ != 0) pi_->weak_release();
  364. pi_ = tmp;
  365. }
  366. return *this;
  367. }
  368. weak_count & operator= (weak_count const & r) noexcept
  369. {
  370. sp_counted_base * tmp = r.pi_;
  371. if( tmp != pi_ )
  372. {
  373. if(tmp != 0) tmp->weak_add_ref();
  374. if(pi_ != 0) pi_->weak_release();
  375. pi_ = tmp;
  376. }
  377. return *this;
  378. }
  379. void swap(weak_count & r) noexcept
  380. {
  381. sp_counted_base * tmp = r.pi_;
  382. r.pi_ = pi_;
  383. pi_ = tmp;
  384. }
  385. long use_count() const noexcept
  386. {
  387. return pi_ != 0? pi_->use_count(): 0;
  388. }
  389. bool empty() const noexcept
  390. {
  391. return pi_ == 0;
  392. }
  393. bool operator==( weak_count const & r ) const noexcept
  394. {
  395. return pi_ == r.pi_;
  396. }
  397. bool operator==( shared_count const & r ) const noexcept
  398. {
  399. return pi_ == r.pi_;
  400. }
  401. bool operator<( weak_count const & r ) const noexcept
  402. {
  403. return std::less<sp_counted_base *>()( pi_, r.pi_ );
  404. }
  405. bool operator<( shared_count const & r ) const noexcept
  406. {
  407. return std::less<sp_counted_base *>()( pi_, r.pi_ );
  408. }
  409. std::size_t hash_value() const noexcept
  410. {
  411. return sp_hash_pointer( pi_ );
  412. }
  413. };
  414. inline shared_count::shared_count( weak_count const & r ): pi_( r.pi_ )
  415. {
  416. if( pi_ == 0 || !pi_->add_ref_lock() )
  417. {
  418. boost::throw_exception( boost::bad_weak_ptr() );
  419. }
  420. }
  421. inline shared_count::shared_count( weak_count const & r, sp_nothrow_tag ) noexcept: pi_( r.pi_ )
  422. {
  423. if( pi_ != 0 && !pi_->add_ref_lock() )
  424. {
  425. pi_ = 0;
  426. }
  427. }
  428. inline bool shared_count::operator==( weak_count const & r ) const noexcept
  429. {
  430. return pi_ == r.pi_;
  431. }
  432. inline bool shared_count::operator<( weak_count const & r ) const noexcept
  433. {
  434. return std::less<sp_counted_base *>()( pi_, r.pi_ );
  435. }
  436. } // namespace detail
  437. } // namespace boost
  438. #if defined( BOOST_SP_DISABLE_DEPRECATED )
  439. #pragma GCC diagnostic pop
  440. #endif
  441. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SHARED_COUNT_HPP_INCLUDED