shared_ptr.hpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. #ifndef BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED
  3. //
  4. // shared_ptr.hpp
  5. //
  6. // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
  7. // Copyright (c) 2001-2008 Peter Dimov
  8. //
  9. // Distributed under the Boost Software License, Version 1.0. (See
  10. // accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. //
  13. // See http://www.boost.org/libs/smart_ptr/ for documentation.
  14. //
  15. #include <boost/smart_ptr/detail/shared_count.hpp>
  16. #include <boost/smart_ptr/detail/sp_convertible.hpp>
  17. #include <boost/smart_ptr/detail/sp_disable_deprecated.hpp>
  18. #include <boost/smart_ptr/detail/sp_noexcept.hpp>
  19. #include <boost/core/checked_delete.hpp>
  20. #include <boost/throw_exception.hpp>
  21. #include <boost/assert.hpp>
  22. #include <boost/config.hpp>
  23. #include <boost/config/workaround.hpp>
  24. #if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
  25. #include <boost/smart_ptr/detail/spinlock_pool.hpp>
  26. #endif
  27. #include <algorithm> // for std::swap
  28. #include <functional> // for std::less
  29. #include <typeinfo> // for std::bad_cast
  30. #include <cstddef> // for std::size_t
  31. #include <memory> // for std::auto_ptr
  32. #include <iosfwd> // for std::basic_ostream
  33. #if defined( BOOST_SP_DISABLE_DEPRECATED )
  34. #pragma GCC diagnostic push
  35. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  36. #endif
  37. namespace boost
  38. {
  39. template<class T> class shared_ptr;
  40. template<class T> class weak_ptr;
  41. template<class T> class enable_shared_from_this;
  42. class enable_shared_from_raw;
  43. namespace movelib
  44. {
  45. template< class T, class D > class unique_ptr;
  46. } // namespace movelib
  47. namespace detail
  48. {
  49. // sp_element, element_type
  50. template< class T > struct sp_element
  51. {
  52. typedef T type;
  53. };
  54. template< class T > struct sp_element< T[] >
  55. {
  56. typedef T type;
  57. };
  58. template< class T, std::size_t N > struct sp_element< T[N] >
  59. {
  60. typedef T type;
  61. };
  62. // sp_dereference, return type of operator*
  63. template< class T > struct sp_dereference
  64. {
  65. typedef T & type;
  66. };
  67. template<> struct sp_dereference< void >
  68. {
  69. typedef void type;
  70. };
  71. template<> struct sp_dereference< void const >
  72. {
  73. typedef void type;
  74. };
  75. template<> struct sp_dereference< void volatile >
  76. {
  77. typedef void type;
  78. };
  79. template<> struct sp_dereference< void const volatile >
  80. {
  81. typedef void type;
  82. };
  83. template< class T > struct sp_dereference< T[] >
  84. {
  85. typedef void type;
  86. };
  87. template< class T, std::size_t N > struct sp_dereference< T[N] >
  88. {
  89. typedef void type;
  90. };
  91. // sp_member_access, return type of operator->
  92. template< class T > struct sp_member_access
  93. {
  94. typedef T * type;
  95. };
  96. template< class T > struct sp_member_access< T[] >
  97. {
  98. typedef void type;
  99. };
  100. template< class T, std::size_t N > struct sp_member_access< T[N] >
  101. {
  102. typedef void type;
  103. };
  104. // sp_array_access, return type of operator[]
  105. template< class T > struct sp_array_access
  106. {
  107. typedef void type;
  108. };
  109. template< class T > struct sp_array_access< T[] >
  110. {
  111. typedef T & type;
  112. };
  113. template< class T, std::size_t N > struct sp_array_access< T[N] >
  114. {
  115. typedef T & type;
  116. };
  117. // sp_extent, for operator[] index check
  118. template< class T > struct sp_extent
  119. {
  120. enum _vt { value = 0 };
  121. };
  122. template< class T, std::size_t N > struct sp_extent< T[N] >
  123. {
  124. enum _vt { value = N };
  125. };
  126. // enable_shared_from_this support
  127. template< class X, class Y, class T > inline void sp_enable_shared_from_this( boost::shared_ptr<X> const * ppx, Y const * py, boost::enable_shared_from_this< T > const * pe )
  128. {
  129. if( pe != 0 )
  130. {
  131. pe->_internal_accept_owner( ppx, const_cast< Y* >( py ) );
  132. }
  133. }
  134. template< class X, class Y > inline void sp_enable_shared_from_this( boost::shared_ptr<X> * ppx, Y const * py, boost::enable_shared_from_raw const * pe );
  135. #ifdef _MANAGED
  136. // Avoid C4793, ... causes native code generation
  137. struct sp_any_pointer
  138. {
  139. template<class T> sp_any_pointer( T* ) {}
  140. };
  141. inline void sp_enable_shared_from_this( sp_any_pointer, sp_any_pointer, sp_any_pointer )
  142. {
  143. }
  144. #else // _MANAGED
  145. inline void sp_enable_shared_from_this( ... )
  146. {
  147. }
  148. #endif // _MANAGED
  149. // sp_assert_convertible
  150. template< class Y, class T > inline void sp_assert_convertible() noexcept
  151. {
  152. static_assert( sp_convertible< Y, T >::value, "incompatible pointer type" );
  153. }
  154. // pointer constructor helper
  155. template< class T, class Y > inline void sp_pointer_construct( boost::shared_ptr< T > * ppx, Y * p, boost::detail::shared_count & pn )
  156. {
  157. boost::detail::shared_count( p ).swap( pn );
  158. boost::detail::sp_enable_shared_from_this( ppx, p, p );
  159. }
  160. template< class T, class Y > inline void sp_pointer_construct( boost::shared_ptr< T[] > * /*ppx*/, Y * p, boost::detail::shared_count & pn )
  161. {
  162. sp_assert_convertible< Y[], T[] >();
  163. boost::detail::shared_count( p, boost::checked_array_deleter< T >() ).swap( pn );
  164. }
  165. template< class T, std::size_t N, class Y > inline void sp_pointer_construct( boost::shared_ptr< T[N] > * /*ppx*/, Y * p, boost::detail::shared_count & pn )
  166. {
  167. sp_assert_convertible< Y[N], T[N] >();
  168. boost::detail::shared_count( p, boost::checked_array_deleter< T >() ).swap( pn );
  169. }
  170. // deleter constructor helper
  171. template< class T, class Y > inline void sp_deleter_construct( boost::shared_ptr< T > * ppx, Y * p )
  172. {
  173. boost::detail::sp_enable_shared_from_this( ppx, p, p );
  174. }
  175. template< class T, class Y > inline void sp_deleter_construct( boost::shared_ptr< T[] > * /*ppx*/, Y * /*p*/ )
  176. {
  177. sp_assert_convertible< Y[], T[] >();
  178. }
  179. template< class T, std::size_t N, class Y > inline void sp_deleter_construct( boost::shared_ptr< T[N] > * /*ppx*/, Y * /*p*/ )
  180. {
  181. sp_assert_convertible< Y[N], T[N] >();
  182. }
  183. struct sp_internal_constructor_tag
  184. {
  185. };
  186. } // namespace detail
  187. //
  188. // shared_ptr
  189. //
  190. // An enhanced relative of scoped_ptr with reference counted copy semantics.
  191. // The object pointed to is deleted when the last shared_ptr pointing to it
  192. // is destroyed or reset.
  193. //
  194. template<class T> class shared_ptr
  195. {
  196. private:
  197. // Borland 5.5.1 specific workaround
  198. typedef shared_ptr<T> this_type;
  199. public:
  200. typedef typename boost::detail::sp_element< T >::type element_type;
  201. constexpr shared_ptr() noexcept : px( 0 ), pn()
  202. {
  203. }
  204. constexpr shared_ptr( std::nullptr_t ) noexcept : px( 0 ), pn()
  205. {
  206. }
  207. constexpr shared_ptr( boost::detail::sp_internal_constructor_tag, element_type * px_, boost::detail::shared_count const & pn_ ) noexcept : px( px_ ), pn( pn_ )
  208. {
  209. }
  210. constexpr shared_ptr( boost::detail::sp_internal_constructor_tag, element_type * px_, boost::detail::shared_count && pn_ ) noexcept : px( px_ ), pn( std::move( pn_ ) )
  211. {
  212. }
  213. template<class Y>
  214. explicit shared_ptr( Y * p ): px( p ), pn() // Y must be complete
  215. {
  216. boost::detail::sp_pointer_construct( this, p, pn );
  217. }
  218. //
  219. // Requirements: D's copy/move constructors must not throw
  220. //
  221. // shared_ptr will release p by calling d(p)
  222. //
  223. template<class Y, class D> shared_ptr( Y * p, D d ): px( p ), pn( p, static_cast< D&& >( d ) )
  224. {
  225. boost::detail::sp_deleter_construct( this, p );
  226. }
  227. template<class D> shared_ptr( std::nullptr_t p, D d ): px( p ), pn( p, static_cast< D&& >( d ) )
  228. {
  229. }
  230. // As above, but with allocator. A's copy constructor shall not throw.
  231. template<class Y, class D, class A> shared_ptr( Y * p, D d, A a ): px( p ), pn( p, static_cast< D&& >( d ), a )
  232. {
  233. boost::detail::sp_deleter_construct( this, p );
  234. }
  235. template<class D, class A> shared_ptr( std::nullptr_t p, D d, A a ): px( p ), pn( p, static_cast< D&& >( d ), a )
  236. {
  237. }
  238. // generated copy constructor, destructor are fine...
  239. // ... except in C++0x, move disables the implicit copy
  240. shared_ptr( shared_ptr const & r ) noexcept : px( r.px ), pn( r.pn )
  241. {
  242. }
  243. template<class Y>
  244. explicit shared_ptr( weak_ptr<Y> const & r ): pn( r.pn ) // may throw
  245. {
  246. boost::detail::sp_assert_convertible< Y, T >();
  247. // it is now safe to copy r.px, as pn(r.pn) did not throw
  248. px = r.px;
  249. }
  250. template<class Y>
  251. shared_ptr( weak_ptr<Y> const & r, boost::detail::sp_nothrow_tag )
  252. noexcept : px( 0 ), pn( r.pn, boost::detail::sp_nothrow_tag() )
  253. {
  254. if( !pn.empty() )
  255. {
  256. px = r.px;
  257. }
  258. }
  259. template<class Y>
  260. shared_ptr( shared_ptr<Y> const & r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
  261. noexcept : px( r.px ), pn( r.pn )
  262. {
  263. boost::detail::sp_assert_convertible< Y, T >();
  264. }
  265. // aliasing
  266. template< class Y >
  267. shared_ptr( shared_ptr<Y> const & r, element_type * p ) noexcept : px( p ), pn( r.pn )
  268. {
  269. }
  270. #ifndef BOOST_NO_AUTO_PTR
  271. template<class Y>
  272. explicit shared_ptr( std::auto_ptr<Y> & r ): px(r.get()), pn()
  273. {
  274. boost::detail::sp_assert_convertible< Y, T >();
  275. Y * tmp = r.get();
  276. pn = boost::detail::shared_count( r );
  277. boost::detail::sp_deleter_construct( this, tmp );
  278. }
  279. template<class Y>
  280. shared_ptr( std::auto_ptr<Y> && r ): px(r.get()), pn()
  281. {
  282. boost::detail::sp_assert_convertible< Y, T >();
  283. Y * tmp = r.get();
  284. pn = boost::detail::shared_count( r );
  285. boost::detail::sp_deleter_construct( this, tmp );
  286. }
  287. #endif // BOOST_NO_AUTO_PTR
  288. template< class Y, class D >
  289. shared_ptr( std::unique_ptr< Y, D > && r ): px( r.get() ), pn()
  290. {
  291. boost::detail::sp_assert_convertible< Y, T >();
  292. typename std::unique_ptr< Y, D >::pointer tmp = r.get();
  293. if( tmp != 0 )
  294. {
  295. pn = boost::detail::shared_count( r );
  296. boost::detail::sp_deleter_construct( this, tmp );
  297. }
  298. }
  299. template< class Y, class D >
  300. shared_ptr( boost::movelib::unique_ptr< Y, D > r ): px( r.get() ), pn()
  301. {
  302. boost::detail::sp_assert_convertible< Y, T >();
  303. typename boost::movelib::unique_ptr< Y, D >::pointer tmp = r.get();
  304. if( tmp != 0 )
  305. {
  306. pn = boost::detail::shared_count( r );
  307. boost::detail::sp_deleter_construct( this, tmp );
  308. }
  309. }
  310. // assignment
  311. shared_ptr & operator=( shared_ptr const & r ) noexcept
  312. {
  313. this_type(r).swap(*this);
  314. return *this;
  315. }
  316. template<class Y>
  317. shared_ptr & operator=(shared_ptr<Y> const & r) noexcept
  318. {
  319. this_type(r).swap(*this);
  320. return *this;
  321. }
  322. #ifndef BOOST_NO_AUTO_PTR
  323. template<class Y>
  324. shared_ptr & operator=( std::auto_ptr<Y> & r )
  325. {
  326. this_type( r ).swap( *this );
  327. return *this;
  328. }
  329. template<class Y>
  330. shared_ptr & operator=( std::auto_ptr<Y> && r )
  331. {
  332. this_type( static_cast< std::auto_ptr<Y> && >( r ) ).swap( *this );
  333. return *this;
  334. }
  335. #endif // BOOST_NO_AUTO_PTR
  336. template<class Y, class D>
  337. shared_ptr & operator=( std::unique_ptr<Y, D> && r )
  338. {
  339. this_type( static_cast< std::unique_ptr<Y, D> && >( r ) ).swap(*this);
  340. return *this;
  341. }
  342. template<class Y, class D>
  343. shared_ptr & operator=( boost::movelib::unique_ptr<Y, D> r )
  344. {
  345. // this_type( static_cast< unique_ptr<Y, D> && >( r ) ).swap( *this );
  346. boost::detail::sp_assert_convertible< Y, T >();
  347. typename boost::movelib::unique_ptr< Y, D >::pointer p = r.get();
  348. shared_ptr tmp;
  349. if( p != 0 )
  350. {
  351. tmp.px = p;
  352. tmp.pn = boost::detail::shared_count( r );
  353. boost::detail::sp_deleter_construct( &tmp, p );
  354. }
  355. tmp.swap( *this );
  356. return *this;
  357. }
  358. // Move support
  359. shared_ptr( shared_ptr && r ) noexcept : px( r.px ), pn( static_cast< boost::detail::shared_count && >( r.pn ) )
  360. {
  361. r.px = 0;
  362. }
  363. template<class Y>
  364. shared_ptr( shared_ptr<Y> && r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
  365. noexcept : px( r.px ), pn( static_cast< boost::detail::shared_count && >( r.pn ) )
  366. {
  367. boost::detail::sp_assert_convertible< Y, T >();
  368. r.px = 0;
  369. }
  370. shared_ptr & operator=( shared_ptr && r ) noexcept
  371. {
  372. this_type( static_cast< shared_ptr && >( r ) ).swap( *this );
  373. return *this;
  374. }
  375. template<class Y>
  376. shared_ptr & operator=( shared_ptr<Y> && r ) noexcept
  377. {
  378. this_type( static_cast< shared_ptr<Y> && >( r ) ).swap( *this );
  379. return *this;
  380. }
  381. // aliasing move
  382. template<class Y>
  383. shared_ptr( shared_ptr<Y> && r, element_type * p ) noexcept : px( p ), pn()
  384. {
  385. pn.swap( r.pn );
  386. r.px = 0;
  387. }
  388. shared_ptr & operator=( std::nullptr_t ) noexcept
  389. {
  390. this_type().swap(*this);
  391. return *this;
  392. }
  393. void reset() noexcept
  394. {
  395. this_type().swap(*this);
  396. }
  397. template<class Y> void reset( Y * p ) // Y must be complete
  398. {
  399. BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors
  400. this_type( p ).swap( *this );
  401. }
  402. template<class Y, class D> void reset( Y * p, D d )
  403. {
  404. this_type( p, static_cast< D&& >( d ) ).swap( *this );
  405. }
  406. template<class Y, class D, class A> void reset( Y * p, D d, A a )
  407. {
  408. this_type( p, static_cast< D&& >( d ), a ).swap( *this );
  409. }
  410. template<class Y> void reset( shared_ptr<Y> const & r, element_type * p ) noexcept
  411. {
  412. this_type( r, p ).swap( *this );
  413. }
  414. template<class Y> void reset( shared_ptr<Y> && r, element_type * p ) noexcept
  415. {
  416. this_type( static_cast< shared_ptr<Y> && >( r ), p ).swap( *this );
  417. }
  418. typename boost::detail::sp_dereference< T >::type operator* () const BOOST_SP_NOEXCEPT_WITH_ASSERT
  419. {
  420. BOOST_ASSERT( px != 0 );
  421. return *px;
  422. }
  423. typename boost::detail::sp_member_access< T >::type operator-> () const BOOST_SP_NOEXCEPT_WITH_ASSERT
  424. {
  425. BOOST_ASSERT( px != 0 );
  426. return px;
  427. }
  428. typename boost::detail::sp_array_access< T >::type operator[] ( std::ptrdiff_t i ) const BOOST_SP_NOEXCEPT_WITH_ASSERT
  429. {
  430. BOOST_ASSERT( px != 0 );
  431. BOOST_ASSERT( i >= 0 && ( i < boost::detail::sp_extent< T >::value || boost::detail::sp_extent< T >::value == 0 ) );
  432. return static_cast< typename boost::detail::sp_array_access< T >::type >( px[ i ] );
  433. }
  434. element_type * get() const noexcept
  435. {
  436. return px;
  437. }
  438. explicit operator bool () const noexcept
  439. {
  440. return px != 0;
  441. }
  442. bool unique() const noexcept
  443. {
  444. return pn.unique();
  445. }
  446. long use_count() const noexcept
  447. {
  448. return pn.use_count();
  449. }
  450. void swap( shared_ptr & other ) noexcept
  451. {
  452. std::swap(px, other.px);
  453. pn.swap(other.pn);
  454. }
  455. template<class Y> bool owner_before( shared_ptr<Y> const & rhs ) const noexcept
  456. {
  457. return pn < rhs.pn;
  458. }
  459. template<class Y> bool owner_before( weak_ptr<Y> const & rhs ) const noexcept
  460. {
  461. return pn < rhs.pn;
  462. }
  463. template<class Y> bool owner_equals( shared_ptr<Y> const & rhs ) const noexcept
  464. {
  465. return pn == rhs.pn;
  466. }
  467. template<class Y> bool owner_equals( weak_ptr<Y> const & rhs ) const noexcept
  468. {
  469. return pn == rhs.pn;
  470. }
  471. std::size_t owner_hash_value() const noexcept
  472. {
  473. return pn.hash_value();
  474. }
  475. void * _internal_get_deleter( boost::detail::sp_typeinfo_ const & ti ) const noexcept
  476. {
  477. return pn.get_deleter( ti );
  478. }
  479. void * _internal_get_local_deleter( boost::detail::sp_typeinfo_ const & ti ) const noexcept
  480. {
  481. return pn.get_local_deleter( ti );
  482. }
  483. void * _internal_get_untyped_deleter() const noexcept
  484. {
  485. return pn.get_untyped_deleter();
  486. }
  487. bool _internal_equiv( shared_ptr const & r ) const noexcept
  488. {
  489. return px == r.px && pn == r.pn;
  490. }
  491. boost::detail::shared_count _internal_count() const noexcept
  492. {
  493. return pn;
  494. }
  495. private:
  496. template<class Y> friend class shared_ptr;
  497. template<class Y> friend class weak_ptr;
  498. element_type * px; // contained pointer
  499. boost::detail::shared_count pn; // reference counter
  500. }; // shared_ptr
  501. template<class T, class U> inline bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b) noexcept
  502. {
  503. return a.get() == b.get();
  504. }
  505. template<class T, class U> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<U> const & b) noexcept
  506. {
  507. return a.get() != b.get();
  508. }
  509. template<class T> inline bool operator==( shared_ptr<T> const & p, std::nullptr_t ) noexcept
  510. {
  511. return p.get() == 0;
  512. }
  513. template<class T> inline bool operator==( std::nullptr_t, shared_ptr<T> const & p ) noexcept
  514. {
  515. return p.get() == 0;
  516. }
  517. template<class T> inline bool operator!=( shared_ptr<T> const & p, std::nullptr_t ) noexcept
  518. {
  519. return p.get() != 0;
  520. }
  521. template<class T> inline bool operator!=( std::nullptr_t, shared_ptr<T> const & p ) noexcept
  522. {
  523. return p.get() != 0;
  524. }
  525. template<class T, class U> inline bool operator<(shared_ptr<T> const & a, shared_ptr<U> const & b) noexcept
  526. {
  527. return a.owner_before( b );
  528. }
  529. template<class T> inline void swap(shared_ptr<T> & a, shared_ptr<T> & b) noexcept
  530. {
  531. a.swap(b);
  532. }
  533. template<class T, class U> shared_ptr<T> static_pointer_cast( shared_ptr<U> const & r ) noexcept
  534. {
  535. (void) static_cast< T* >( static_cast< U* >( 0 ) );
  536. typedef typename shared_ptr<T>::element_type E;
  537. E * p = static_cast< E* >( r.get() );
  538. return shared_ptr<T>( r, p );
  539. }
  540. template<class T, class U> shared_ptr<T> const_pointer_cast( shared_ptr<U> const & r ) noexcept
  541. {
  542. (void) const_cast< T* >( static_cast< U* >( 0 ) );
  543. typedef typename shared_ptr<T>::element_type E;
  544. E * p = const_cast< E* >( r.get() );
  545. return shared_ptr<T>( r, p );
  546. }
  547. template<class T, class U> shared_ptr<T> dynamic_pointer_cast( shared_ptr<U> const & r ) noexcept
  548. {
  549. (void) dynamic_cast< T* >( static_cast< U* >( 0 ) );
  550. typedef typename shared_ptr<T>::element_type E;
  551. E * p = dynamic_cast< E* >( r.get() );
  552. return p? shared_ptr<T>( r, p ): shared_ptr<T>();
  553. }
  554. template<class T, class U> shared_ptr<T> reinterpret_pointer_cast( shared_ptr<U> const & r ) noexcept
  555. {
  556. (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
  557. typedef typename shared_ptr<T>::element_type E;
  558. E * p = reinterpret_cast< E* >( r.get() );
  559. return shared_ptr<T>( r, p );
  560. }
  561. template<class T, class U> shared_ptr<T> static_pointer_cast( shared_ptr<U> && r ) noexcept
  562. {
  563. (void) static_cast< T* >( static_cast< U* >( 0 ) );
  564. typedef typename shared_ptr<T>::element_type E;
  565. E * p = static_cast< E* >( r.get() );
  566. return shared_ptr<T>( std::move(r), p );
  567. }
  568. template<class T, class U> shared_ptr<T> const_pointer_cast( shared_ptr<U> && r ) noexcept
  569. {
  570. (void) const_cast< T* >( static_cast< U* >( 0 ) );
  571. typedef typename shared_ptr<T>::element_type E;
  572. E * p = const_cast< E* >( r.get() );
  573. return shared_ptr<T>( std::move(r), p );
  574. }
  575. template<class T, class U> shared_ptr<T> dynamic_pointer_cast( shared_ptr<U> && r ) noexcept
  576. {
  577. (void) dynamic_cast< T* >( static_cast< U* >( 0 ) );
  578. typedef typename shared_ptr<T>::element_type E;
  579. E * p = dynamic_cast< E* >( r.get() );
  580. return p? shared_ptr<T>( std::move(r), p ): shared_ptr<T>();
  581. }
  582. template<class T, class U> shared_ptr<T> reinterpret_pointer_cast( shared_ptr<U> && r ) noexcept
  583. {
  584. (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
  585. typedef typename shared_ptr<T>::element_type E;
  586. E * p = reinterpret_cast< E* >( r.get() );
  587. return shared_ptr<T>( std::move(r), p );
  588. }
  589. // get_pointer() enables boost::mem_fn to recognize shared_ptr
  590. template<class T> inline typename shared_ptr<T>::element_type * get_pointer(shared_ptr<T> const & p) noexcept
  591. {
  592. return p.get();
  593. }
  594. // operator<<
  595. template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, shared_ptr<Y> const & p)
  596. {
  597. os << p.get();
  598. return os;
  599. }
  600. // get_deleter
  601. namespace detail
  602. {
  603. template<class D, class T> D * basic_get_deleter( shared_ptr<T> const & p ) noexcept
  604. {
  605. return static_cast<D *>( p._internal_get_deleter(BOOST_SP_TYPEID_(D)) );
  606. }
  607. template<class D, class T> D * basic_get_local_deleter( D *, shared_ptr<T> const & p ) noexcept;
  608. template<class D, class T> D const * basic_get_local_deleter( D const *, shared_ptr<T> const & p ) noexcept;
  609. class esft2_deleter_wrapper
  610. {
  611. private:
  612. shared_ptr<void const volatile> deleter_;
  613. public:
  614. esft2_deleter_wrapper() noexcept
  615. {
  616. }
  617. template< class T > void set_deleter( shared_ptr<T> const & deleter ) noexcept
  618. {
  619. deleter_ = deleter;
  620. }
  621. template<typename D> D* get_deleter() const noexcept
  622. {
  623. return boost::detail::basic_get_deleter<D>( deleter_ );
  624. }
  625. template< class T> void operator()( T* ) BOOST_SP_NOEXCEPT_WITH_ASSERT
  626. {
  627. BOOST_ASSERT( deleter_.use_count() <= 1 );
  628. deleter_.reset();
  629. }
  630. };
  631. } // namespace detail
  632. template<class D, class T> D * get_deleter( shared_ptr<T> const & p ) noexcept
  633. {
  634. D * d = boost::detail::basic_get_deleter<D>( p );
  635. if( d == 0 )
  636. {
  637. d = boost::detail::basic_get_local_deleter( d, p );
  638. }
  639. if( d == 0 )
  640. {
  641. boost::detail::esft2_deleter_wrapper *del_wrapper = boost::detail::basic_get_deleter<boost::detail::esft2_deleter_wrapper>(p);
  642. // The following get_deleter method call is fully qualified because
  643. // older versions of gcc (2.95, 3.2.3) fail to compile it when written del_wrapper->get_deleter<D>()
  644. if(del_wrapper) d = del_wrapper->::boost::detail::esft2_deleter_wrapper::get_deleter<D>();
  645. }
  646. return d;
  647. }
  648. // atomic access
  649. #if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
  650. template<class T> inline bool atomic_is_lock_free( shared_ptr<T> const * /*p*/ ) noexcept
  651. {
  652. return false;
  653. }
  654. template<class T> shared_ptr<T> atomic_load( shared_ptr<T> const * p ) noexcept
  655. {
  656. boost::detail::spinlock_pool<2>::scoped_lock lock( p );
  657. return *p;
  658. }
  659. template<class T, class M> inline shared_ptr<T> atomic_load_explicit( shared_ptr<T> const * p, /*memory_order mo*/ M ) noexcept
  660. {
  661. return atomic_load( p );
  662. }
  663. template<class T> void atomic_store( shared_ptr<T> * p, shared_ptr<T> r ) noexcept
  664. {
  665. boost::detail::spinlock_pool<2>::scoped_lock lock( p );
  666. p->swap( r );
  667. }
  668. template<class T, class M> inline void atomic_store_explicit( shared_ptr<T> * p, shared_ptr<T> r, /*memory_order mo*/ M ) noexcept
  669. {
  670. atomic_store( p, r ); // std::move( r )
  671. }
  672. template<class T> shared_ptr<T> atomic_exchange( shared_ptr<T> * p, shared_ptr<T> r ) noexcept
  673. {
  674. boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( p );
  675. sp.lock();
  676. p->swap( r );
  677. sp.unlock();
  678. return r; // return std::move( r )
  679. }
  680. template<class T, class M> shared_ptr<T> inline atomic_exchange_explicit( shared_ptr<T> * p, shared_ptr<T> r, /*memory_order mo*/ M ) noexcept
  681. {
  682. return atomic_exchange( p, r ); // std::move( r )
  683. }
  684. template<class T> bool atomic_compare_exchange( shared_ptr<T> * p, shared_ptr<T> * v, shared_ptr<T> w ) noexcept
  685. {
  686. boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( p );
  687. sp.lock();
  688. if( p->_internal_equiv( *v ) )
  689. {
  690. p->swap( w );
  691. sp.unlock();
  692. return true;
  693. }
  694. else
  695. {
  696. shared_ptr<T> tmp( *p );
  697. sp.unlock();
  698. tmp.swap( *v );
  699. return false;
  700. }
  701. }
  702. template<class T, class M> inline bool atomic_compare_exchange_explicit( shared_ptr<T> * p, shared_ptr<T> * v, shared_ptr<T> w, /*memory_order success*/ M, /*memory_order failure*/ M ) noexcept
  703. {
  704. return atomic_compare_exchange( p, v, w ); // std::move( w )
  705. }
  706. #endif // !defined(BOOST_SP_NO_ATOMIC_ACCESS)
  707. // hash_value
  708. template< class T > struct hash;
  709. template< class T > std::size_t hash_value( boost::shared_ptr<T> const & p ) noexcept
  710. {
  711. return boost::hash< typename boost::shared_ptr<T>::element_type* >()( p.get() );
  712. }
  713. } // namespace boost
  714. // std::hash
  715. namespace std
  716. {
  717. template<class T> struct hash< ::boost::shared_ptr<T> >
  718. {
  719. std::size_t operator()( ::boost::shared_ptr<T> const & p ) const noexcept
  720. {
  721. return std::hash< typename ::boost::shared_ptr<T>::element_type* >()( p.get() );
  722. }
  723. };
  724. } // namespace std
  725. #include <boost/smart_ptr/detail/local_sp_deleter.hpp>
  726. namespace boost
  727. {
  728. namespace detail
  729. {
  730. template<class D, class T> D * basic_get_local_deleter( D *, shared_ptr<T> const & p ) noexcept
  731. {
  732. return static_cast<D *>( p._internal_get_local_deleter( BOOST_SP_TYPEID_(local_sp_deleter<D>) ) );
  733. }
  734. template<class D, class T> D const * basic_get_local_deleter( D const *, shared_ptr<T> const & p ) noexcept
  735. {
  736. return static_cast<D *>( p._internal_get_local_deleter( BOOST_SP_TYPEID_(local_sp_deleter<D>) ) );
  737. }
  738. } // namespace detail
  739. #if defined(__cpp_deduction_guides)
  740. template<class T> shared_ptr( weak_ptr<T> ) -> shared_ptr<T>;
  741. template<class T, class D> shared_ptr( std::unique_ptr<T, D> ) -> shared_ptr<T>;
  742. #endif
  743. } // namespace boost
  744. #if defined( BOOST_SP_DISABLE_DEPRECATED )
  745. #pragma GCC diagnostic pop
  746. #endif
  747. #endif // #ifndef BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED