shared_count.hpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. #ifdef __BORLANDC__
  18. # pragma warn -8027 // Functions containing try are not expanded inline
  19. #endif
  20. #include <boost/config.hpp>
  21. #include <boost/checked_delete.hpp>
  22. #include <boost/throw_exception.hpp>
  23. #include <boost/smart_ptr/bad_weak_ptr.hpp>
  24. #include <boost/smart_ptr/detail/sp_counted_base.hpp>
  25. #include <boost/smart_ptr/detail/sp_counted_impl.hpp>
  26. #include <boost/detail/workaround.hpp>
  27. // In order to avoid circular dependencies with Boost.TR1
  28. // we make sure that our include of <memory> doesn't try to
  29. // pull in the TR1 headers: that's why we use this header
  30. // rather than including <memory> directly:
  31. #include <boost/config/no_tr1/memory.hpp> // std::auto_ptr
  32. #include <functional> // std::less
  33. #include <new> // std::bad_alloc
  34. namespace boost
  35. {
  36. namespace detail
  37. {
  38. #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
  39. int const shared_count_id = 0x2C35F101;
  40. int const weak_count_id = 0x298C38A4;
  41. #endif
  42. struct sp_nothrow_tag {};
  43. class weak_count;
  44. class shared_count
  45. {
  46. private:
  47. sp_counted_base * pi_;
  48. #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
  49. int id_;
  50. #endif
  51. friend class weak_count;
  52. public:
  53. shared_count(): pi_(0) // nothrow
  54. #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
  55. , id_(shared_count_id)
  56. #endif
  57. {
  58. }
  59. template<class Y> explicit shared_count( Y * p ): pi_( 0 )
  60. #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
  61. , id_(shared_count_id)
  62. #endif
  63. {
  64. #ifndef BOOST_NO_EXCEPTIONS
  65. try
  66. {
  67. pi_ = new sp_counted_impl_p<Y>( p );
  68. }
  69. catch(...)
  70. {
  71. boost::checked_delete( p );
  72. throw;
  73. }
  74. #else
  75. pi_ = new sp_counted_impl_p<Y>( p );
  76. if( pi_ == 0 )
  77. {
  78. boost::checked_delete( p );
  79. boost::throw_exception( std::bad_alloc() );
  80. }
  81. #endif
  82. }
  83. #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, <= 1200 )
  84. template<class Y, class D> shared_count( Y * p, D d ): pi_(0)
  85. #else
  86. template<class P, class D> shared_count( P p, D d ): pi_(0)
  87. #endif
  88. #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
  89. , id_(shared_count_id)
  90. #endif
  91. {
  92. #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, <= 1200 )
  93. typedef Y* P;
  94. #endif
  95. #ifndef BOOST_NO_EXCEPTIONS
  96. try
  97. {
  98. pi_ = new sp_counted_impl_pd<P, D>(p, d);
  99. }
  100. catch(...)
  101. {
  102. d(p); // delete p
  103. throw;
  104. }
  105. #else
  106. pi_ = new sp_counted_impl_pd<P, D>(p, d);
  107. if(pi_ == 0)
  108. {
  109. d(p); // delete p
  110. boost::throw_exception(std::bad_alloc());
  111. }
  112. #endif
  113. }
  114. template<class P, class D, class A> shared_count( P p, D d, A a ): pi_( 0 )
  115. #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
  116. , id_(shared_count_id)
  117. #endif
  118. {
  119. typedef sp_counted_impl_pda<P, D, A> impl_type;
  120. typedef typename A::template rebind< impl_type >::other A2;
  121. A2 a2( a );
  122. #ifndef BOOST_NO_EXCEPTIONS
  123. try
  124. {
  125. pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );
  126. new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
  127. }
  128. catch(...)
  129. {
  130. d( p );
  131. if( pi_ != 0 )
  132. {
  133. a2.deallocate( static_cast< impl_type* >( pi_ ), 1 );
  134. }
  135. throw;
  136. }
  137. #else
  138. pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );
  139. if( pi_ != 0 )
  140. {
  141. new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
  142. }
  143. else
  144. {
  145. d( p );
  146. boost::throw_exception( std::bad_alloc() );
  147. }
  148. #endif
  149. }
  150. #ifndef BOOST_NO_AUTO_PTR
  151. // auto_ptr<Y> is special cased to provide the strong guarantee
  152. template<class Y>
  153. explicit shared_count( std::auto_ptr<Y> & r ): pi_( new sp_counted_impl_p<Y>( r.get() ) )
  154. #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
  155. , id_(shared_count_id)
  156. #endif
  157. {
  158. #ifdef BOOST_NO_EXCEPTIONS
  159. if( pi_ == 0 )
  160. {
  161. boost::throw_exception(std::bad_alloc());
  162. }
  163. #endif
  164. r.release();
  165. }
  166. #endif
  167. ~shared_count() // nothrow
  168. {
  169. if( pi_ != 0 ) pi_->release();
  170. #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
  171. id_ = 0;
  172. #endif
  173. }
  174. shared_count(shared_count const & r): pi_(r.pi_) // nothrow
  175. #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
  176. , id_(shared_count_id)
  177. #endif
  178. {
  179. if( pi_ != 0 ) pi_->add_ref_copy();
  180. }
  181. #if defined( BOOST_HAS_RVALUE_REFS )
  182. shared_count(shared_count && r): pi_(r.pi_) // nothrow
  183. #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
  184. , id_(shared_count_id)
  185. #endif
  186. {
  187. r.pi_ = 0;
  188. }
  189. #endif
  190. explicit shared_count(weak_count const & r); // throws bad_weak_ptr when r.use_count() == 0
  191. shared_count( weak_count const & r, sp_nothrow_tag ); // constructs an empty *this when r.use_count() == 0
  192. shared_count & operator= (shared_count const & r) // nothrow
  193. {
  194. sp_counted_base * tmp = r.pi_;
  195. if( tmp != pi_ )
  196. {
  197. if( tmp != 0 ) tmp->add_ref_copy();
  198. if( pi_ != 0 ) pi_->release();
  199. pi_ = tmp;
  200. }
  201. return *this;
  202. }
  203. void swap(shared_count & r) // nothrow
  204. {
  205. sp_counted_base * tmp = r.pi_;
  206. r.pi_ = pi_;
  207. pi_ = tmp;
  208. }
  209. long use_count() const // nothrow
  210. {
  211. return pi_ != 0? pi_->use_count(): 0;
  212. }
  213. bool unique() const // nothrow
  214. {
  215. return use_count() == 1;
  216. }
  217. bool empty() const // nothrow
  218. {
  219. return pi_ == 0;
  220. }
  221. friend inline bool operator==(shared_count const & a, shared_count const & b)
  222. {
  223. return a.pi_ == b.pi_;
  224. }
  225. friend inline bool operator<(shared_count const & a, shared_count const & b)
  226. {
  227. return std::less<sp_counted_base *>()( a.pi_, b.pi_ );
  228. }
  229. void * get_deleter( sp_typeinfo const & ti ) const
  230. {
  231. return pi_? pi_->get_deleter( ti ): 0;
  232. }
  233. };
  234. class weak_count
  235. {
  236. private:
  237. sp_counted_base * pi_;
  238. #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
  239. int id_;
  240. #endif
  241. friend class shared_count;
  242. public:
  243. weak_count(): pi_(0) // nothrow
  244. #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
  245. , id_(weak_count_id)
  246. #endif
  247. {
  248. }
  249. weak_count(shared_count const & r): pi_(r.pi_) // nothrow
  250. #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
  251. , id_(weak_count_id)
  252. #endif
  253. {
  254. if(pi_ != 0) pi_->weak_add_ref();
  255. }
  256. weak_count(weak_count const & r): pi_(r.pi_) // nothrow
  257. #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
  258. , id_(weak_count_id)
  259. #endif
  260. {
  261. if(pi_ != 0) pi_->weak_add_ref();
  262. }
  263. // Move support
  264. #if defined( BOOST_HAS_RVALUE_REFS )
  265. weak_count(weak_count && r): pi_(r.pi_) // nothrow
  266. #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
  267. , id_(weak_count_id)
  268. #endif
  269. {
  270. r.pi_ = 0;
  271. }
  272. #endif
  273. ~weak_count() // nothrow
  274. {
  275. if(pi_ != 0) pi_->weak_release();
  276. #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
  277. id_ = 0;
  278. #endif
  279. }
  280. weak_count & operator= (shared_count const & r) // nothrow
  281. {
  282. sp_counted_base * tmp = r.pi_;
  283. if( tmp != pi_ )
  284. {
  285. if(tmp != 0) tmp->weak_add_ref();
  286. if(pi_ != 0) pi_->weak_release();
  287. pi_ = tmp;
  288. }
  289. return *this;
  290. }
  291. weak_count & operator= (weak_count const & r) // nothrow
  292. {
  293. sp_counted_base * tmp = r.pi_;
  294. if( tmp != pi_ )
  295. {
  296. if(tmp != 0) tmp->weak_add_ref();
  297. if(pi_ != 0) pi_->weak_release();
  298. pi_ = tmp;
  299. }
  300. return *this;
  301. }
  302. void swap(weak_count & r) // nothrow
  303. {
  304. sp_counted_base * tmp = r.pi_;
  305. r.pi_ = pi_;
  306. pi_ = tmp;
  307. }
  308. long use_count() const // nothrow
  309. {
  310. return pi_ != 0? pi_->use_count(): 0;
  311. }
  312. bool empty() const // nothrow
  313. {
  314. return pi_ == 0;
  315. }
  316. friend inline bool operator==(weak_count const & a, weak_count const & b)
  317. {
  318. return a.pi_ == b.pi_;
  319. }
  320. friend inline bool operator<(weak_count const & a, weak_count const & b)
  321. {
  322. return std::less<sp_counted_base *>()(a.pi_, b.pi_);
  323. }
  324. };
  325. inline shared_count::shared_count( weak_count const & r ): pi_( r.pi_ )
  326. #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
  327. , id_(shared_count_id)
  328. #endif
  329. {
  330. if( pi_ == 0 || !pi_->add_ref_lock() )
  331. {
  332. boost::throw_exception( boost::bad_weak_ptr() );
  333. }
  334. }
  335. inline shared_count::shared_count( weak_count const & r, sp_nothrow_tag ): pi_( r.pi_ )
  336. #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
  337. , id_(shared_count_id)
  338. #endif
  339. {
  340. if( pi_ != 0 && !pi_->add_ref_lock() )
  341. {
  342. pi_ = 0;
  343. }
  344. }
  345. } // namespace detail
  346. } // namespace boost
  347. #ifdef __BORLANDC__
  348. # pragma warn .8027 // Functions containing try are not expanded inline
  349. #endif
  350. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SHARED_COUNT_HPP_INCLUDED