shared_ptr.hpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  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/requires_cxx11.hpp>
  16. #include <boost/smart_ptr/detail/shared_count.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_disable_deprecated.hpp>
  20. #include <boost/smart_ptr/detail/sp_noexcept.hpp>
  21. #include <boost/core/checked_delete.hpp>
  22. #include <boost/throw_exception.hpp>
  23. #include <boost/assert.hpp>
  24. #include <boost/config.hpp>
  25. #include <boost/config/workaround.hpp>
  26. #if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
  27. #include <boost/smart_ptr/detail/spinlock_pool.hpp>
  28. #endif
  29. #include <algorithm> // for std::swap
  30. #include <functional> // for std::less
  31. #include <typeinfo> // for std::bad_cast
  32. #include <cstddef> // for std::size_t
  33. #include <memory> // for std::auto_ptr
  34. #if !defined(BOOST_NO_IOSTREAM)
  35. #if !defined(BOOST_NO_IOSFWD)
  36. #include <iosfwd> // for std::basic_ostream
  37. #else
  38. #include <ostream>
  39. #endif
  40. #endif
  41. #if defined( BOOST_SP_DISABLE_DEPRECATED )
  42. #pragma GCC diagnostic push
  43. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  44. #endif
  45. namespace boost
  46. {
  47. template<class T> class shared_ptr;
  48. template<class T> class weak_ptr;
  49. template<class T> class enable_shared_from_this;
  50. class enable_shared_from_raw;
  51. namespace movelib
  52. {
  53. template< class T, class D > class unique_ptr;
  54. } // namespace movelib
  55. namespace detail
  56. {
  57. // sp_element, element_type
  58. template< class T > struct sp_element
  59. {
  60. typedef T type;
  61. };
  62. #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
  63. template< class T > struct sp_element< T[] >
  64. {
  65. typedef T type;
  66. };
  67. #if !defined( BOOST_BORLANDC ) || !BOOST_WORKAROUND( BOOST_BORLANDC, < 0x600 )
  68. template< class T, std::size_t N > struct sp_element< T[N] >
  69. {
  70. typedef T type;
  71. };
  72. #endif
  73. #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
  74. // sp_dereference, return type of operator*
  75. template< class T > struct sp_dereference
  76. {
  77. typedef T & type;
  78. };
  79. template<> struct sp_dereference< void >
  80. {
  81. typedef void type;
  82. };
  83. #if !defined(BOOST_NO_CV_VOID_SPECIALIZATIONS)
  84. template<> struct sp_dereference< void const >
  85. {
  86. typedef void type;
  87. };
  88. template<> struct sp_dereference< void volatile >
  89. {
  90. typedef void type;
  91. };
  92. template<> struct sp_dereference< void const volatile >
  93. {
  94. typedef void type;
  95. };
  96. #endif // !defined(BOOST_NO_CV_VOID_SPECIALIZATIONS)
  97. #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
  98. template< class T > struct sp_dereference< T[] >
  99. {
  100. typedef void type;
  101. };
  102. #if !defined( BOOST_BORLANDC ) || !BOOST_WORKAROUND( BOOST_BORLANDC, < 0x600 )
  103. template< class T, std::size_t N > struct sp_dereference< T[N] >
  104. {
  105. typedef void type;
  106. };
  107. #endif
  108. #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
  109. // sp_member_access, return type of operator->
  110. template< class T > struct sp_member_access
  111. {
  112. typedef T * type;
  113. };
  114. #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
  115. template< class T > struct sp_member_access< T[] >
  116. {
  117. typedef void type;
  118. };
  119. #if !defined( BOOST_BORLANDC ) || !BOOST_WORKAROUND( BOOST_BORLANDC, < 0x600 )
  120. template< class T, std::size_t N > struct sp_member_access< T[N] >
  121. {
  122. typedef void type;
  123. };
  124. #endif
  125. #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
  126. // sp_array_access, return type of operator[]
  127. template< class T > struct sp_array_access
  128. {
  129. typedef void type;
  130. };
  131. #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
  132. template< class T > struct sp_array_access< T[] >
  133. {
  134. typedef T & type;
  135. };
  136. #if !defined( BOOST_BORLANDC ) || !BOOST_WORKAROUND( BOOST_BORLANDC, < 0x600 )
  137. template< class T, std::size_t N > struct sp_array_access< T[N] >
  138. {
  139. typedef T & type;
  140. };
  141. #endif
  142. #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
  143. // sp_extent, for operator[] index check
  144. template< class T > struct sp_extent
  145. {
  146. enum _vt { value = 0 };
  147. };
  148. #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
  149. template< class T, std::size_t N > struct sp_extent< T[N] >
  150. {
  151. enum _vt { value = N };
  152. };
  153. #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
  154. // enable_shared_from_this support
  155. 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 )
  156. {
  157. if( pe != 0 )
  158. {
  159. pe->_internal_accept_owner( ppx, const_cast< Y* >( py ) );
  160. }
  161. }
  162. 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 );
  163. #ifdef _MANAGED
  164. // Avoid C4793, ... causes native code generation
  165. struct sp_any_pointer
  166. {
  167. template<class T> sp_any_pointer( T* ) {}
  168. };
  169. inline void sp_enable_shared_from_this( sp_any_pointer, sp_any_pointer, sp_any_pointer )
  170. {
  171. }
  172. #else // _MANAGED
  173. inline void sp_enable_shared_from_this( ... )
  174. {
  175. }
  176. #endif // _MANAGED
  177. #if !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION ) && !defined( BOOST_NO_AUTO_PTR )
  178. // rvalue auto_ptr support based on a technique by Dave Abrahams
  179. template< class T, class R > struct sp_enable_if_auto_ptr
  180. {
  181. };
  182. template< class T, class R > struct sp_enable_if_auto_ptr< std::auto_ptr< T >, R >
  183. {
  184. typedef R type;
  185. };
  186. #endif
  187. // sp_assert_convertible
  188. template< class Y, class T > inline void sp_assert_convertible() BOOST_SP_NOEXCEPT
  189. {
  190. #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
  191. // static_assert( sp_convertible< Y, T >::value );
  192. typedef char tmp[ sp_convertible< Y, T >::value? 1: -1 ];
  193. (void)sizeof( tmp );
  194. #else
  195. T* p = static_cast< Y* >( 0 );
  196. (void)p;
  197. #endif
  198. }
  199. // pointer constructor helper
  200. template< class T, class Y > inline void sp_pointer_construct( boost::shared_ptr< T > * ppx, Y * p, boost::detail::shared_count & pn )
  201. {
  202. boost::detail::shared_count( p ).swap( pn );
  203. boost::detail::sp_enable_shared_from_this( ppx, p, p );
  204. }
  205. #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
  206. template< class T, class Y > inline void sp_pointer_construct( boost::shared_ptr< T[] > * /*ppx*/, Y * p, boost::detail::shared_count & pn )
  207. {
  208. sp_assert_convertible< Y[], T[] >();
  209. boost::detail::shared_count( p, boost::checked_array_deleter< T >() ).swap( pn );
  210. }
  211. 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 )
  212. {
  213. sp_assert_convertible< Y[N], T[N] >();
  214. boost::detail::shared_count( p, boost::checked_array_deleter< T >() ).swap( pn );
  215. }
  216. #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
  217. // deleter constructor helper
  218. template< class T, class Y > inline void sp_deleter_construct( boost::shared_ptr< T > * ppx, Y * p )
  219. {
  220. boost::detail::sp_enable_shared_from_this( ppx, p, p );
  221. }
  222. #if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
  223. template< class T, class Y > inline void sp_deleter_construct( boost::shared_ptr< T[] > * /*ppx*/, Y * /*p*/ )
  224. {
  225. sp_assert_convertible< Y[], T[] >();
  226. }
  227. template< class T, std::size_t N, class Y > inline void sp_deleter_construct( boost::shared_ptr< T[N] > * /*ppx*/, Y * /*p*/ )
  228. {
  229. sp_assert_convertible< Y[N], T[N] >();
  230. }
  231. #endif // !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
  232. struct sp_internal_constructor_tag
  233. {
  234. };
  235. } // namespace detail
  236. //
  237. // shared_ptr
  238. //
  239. // An enhanced relative of scoped_ptr with reference counted copy semantics.
  240. // The object pointed to is deleted when the last shared_ptr pointing to it
  241. // is destroyed or reset.
  242. //
  243. template<class T> class shared_ptr
  244. {
  245. private:
  246. // Borland 5.5.1 specific workaround
  247. typedef shared_ptr<T> this_type;
  248. public:
  249. typedef typename boost::detail::sp_element< T >::type element_type;
  250. BOOST_CONSTEXPR shared_ptr() BOOST_SP_NOEXCEPT : px( 0 ), pn()
  251. {
  252. }
  253. #if !defined( BOOST_NO_CXX11_NULLPTR )
  254. BOOST_CONSTEXPR shared_ptr( boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT : px( 0 ), pn()
  255. {
  256. }
  257. #endif
  258. BOOST_CONSTEXPR shared_ptr( boost::detail::sp_internal_constructor_tag, element_type * px_, boost::detail::shared_count const & pn_ ) BOOST_SP_NOEXCEPT : px( px_ ), pn( pn_ )
  259. {
  260. }
  261. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  262. BOOST_CONSTEXPR shared_ptr( boost::detail::sp_internal_constructor_tag, element_type * px_, boost::detail::shared_count && pn_ ) BOOST_SP_NOEXCEPT : px( px_ ), pn( std::move( pn_ ) )
  263. {
  264. }
  265. #endif
  266. template<class Y>
  267. explicit shared_ptr( Y * p ): px( p ), pn() // Y must be complete
  268. {
  269. boost::detail::sp_pointer_construct( this, p, pn );
  270. }
  271. //
  272. // Requirements: D's copy/move constructors must not throw
  273. //
  274. // shared_ptr will release p by calling d(p)
  275. //
  276. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  277. template<class Y, class D> shared_ptr( Y * p, D d ): px( p ), pn( p, static_cast< D&& >( d ) )
  278. {
  279. boost::detail::sp_deleter_construct( this, p );
  280. }
  281. #else
  282. template<class Y, class D> shared_ptr( Y * p, D d ): px( p ), pn( p, d )
  283. {
  284. boost::detail::sp_deleter_construct( this, p );
  285. }
  286. #endif
  287. #if !defined( BOOST_NO_CXX11_NULLPTR )
  288. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  289. template<class D> shared_ptr( boost::detail::sp_nullptr_t p, D d ): px( p ), pn( p, static_cast< D&& >( d ) )
  290. {
  291. }
  292. #else
  293. template<class D> shared_ptr( boost::detail::sp_nullptr_t p, D d ): px( p ), pn( p, d )
  294. {
  295. }
  296. #endif
  297. #endif
  298. // As above, but with allocator. A's copy constructor shall not throw.
  299. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  300. template<class Y, class D, class A> shared_ptr( Y * p, D d, A a ): px( p ), pn( p, static_cast< D&& >( d ), a )
  301. {
  302. boost::detail::sp_deleter_construct( this, p );
  303. }
  304. #else
  305. template<class Y, class D, class A> shared_ptr( Y * p, D d, A a ): px( p ), pn( p, d, a )
  306. {
  307. boost::detail::sp_deleter_construct( this, p );
  308. }
  309. #endif
  310. #if !defined( BOOST_NO_CXX11_NULLPTR )
  311. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  312. template<class D, class A> shared_ptr( boost::detail::sp_nullptr_t p, D d, A a ): px( p ), pn( p, static_cast< D&& >( d ), a )
  313. {
  314. }
  315. #else
  316. template<class D, class A> shared_ptr( boost::detail::sp_nullptr_t p, D d, A a ): px( p ), pn( p, d, a )
  317. {
  318. }
  319. #endif
  320. #endif
  321. // generated copy constructor, destructor are fine...
  322. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  323. // ... except in C++0x, move disables the implicit copy
  324. shared_ptr( shared_ptr const & r ) BOOST_SP_NOEXCEPT : px( r.px ), pn( r.pn )
  325. {
  326. }
  327. #endif
  328. template<class Y>
  329. explicit shared_ptr( weak_ptr<Y> const & r ): pn( r.pn ) // may throw
  330. {
  331. boost::detail::sp_assert_convertible< Y, T >();
  332. // it is now safe to copy r.px, as pn(r.pn) did not throw
  333. px = r.px;
  334. }
  335. template<class Y>
  336. shared_ptr( weak_ptr<Y> const & r, boost::detail::sp_nothrow_tag )
  337. BOOST_SP_NOEXCEPT : px( 0 ), pn( r.pn, boost::detail::sp_nothrow_tag() )
  338. {
  339. if( !pn.empty() )
  340. {
  341. px = r.px;
  342. }
  343. }
  344. template<class Y>
  345. #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
  346. shared_ptr( shared_ptr<Y> const & r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
  347. #else
  348. shared_ptr( shared_ptr<Y> const & r )
  349. #endif
  350. BOOST_SP_NOEXCEPT : px( r.px ), pn( r.pn )
  351. {
  352. boost::detail::sp_assert_convertible< Y, T >();
  353. }
  354. // aliasing
  355. template< class Y >
  356. shared_ptr( shared_ptr<Y> const & r, element_type * p ) BOOST_SP_NOEXCEPT : px( p ), pn( r.pn )
  357. {
  358. }
  359. #ifndef BOOST_NO_AUTO_PTR
  360. template<class Y>
  361. explicit shared_ptr( std::auto_ptr<Y> & r ): px(r.get()), pn()
  362. {
  363. boost::detail::sp_assert_convertible< Y, T >();
  364. Y * tmp = r.get();
  365. pn = boost::detail::shared_count( r );
  366. boost::detail::sp_deleter_construct( this, tmp );
  367. }
  368. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  369. template<class Y>
  370. shared_ptr( std::auto_ptr<Y> && r ): px(r.get()), pn()
  371. {
  372. boost::detail::sp_assert_convertible< Y, T >();
  373. Y * tmp = r.get();
  374. pn = boost::detail::shared_count( r );
  375. boost::detail::sp_deleter_construct( this, tmp );
  376. }
  377. #elif !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
  378. template<class Ap>
  379. explicit shared_ptr( Ap r, typename boost::detail::sp_enable_if_auto_ptr<Ap, int>::type = 0 ): px( r.get() ), pn()
  380. {
  381. typedef typename Ap::element_type Y;
  382. boost::detail::sp_assert_convertible< Y, T >();
  383. Y * tmp = r.get();
  384. pn = boost::detail::shared_count( r );
  385. boost::detail::sp_deleter_construct( this, tmp );
  386. }
  387. #endif // BOOST_NO_SFINAE, BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  388. #endif // BOOST_NO_AUTO_PTR
  389. #if !defined( BOOST_NO_CXX11_SMART_PTR ) && !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  390. template< class Y, class D >
  391. shared_ptr( std::unique_ptr< Y, D > && r ): px( r.get() ), pn()
  392. {
  393. boost::detail::sp_assert_convertible< Y, T >();
  394. typename std::unique_ptr< Y, D >::pointer tmp = r.get();
  395. if( tmp != 0 )
  396. {
  397. pn = boost::detail::shared_count( r );
  398. boost::detail::sp_deleter_construct( this, tmp );
  399. }
  400. }
  401. #endif
  402. template< class Y, class D >
  403. shared_ptr( boost::movelib::unique_ptr< Y, D > r ): px( r.get() ), pn()
  404. {
  405. boost::detail::sp_assert_convertible< Y, T >();
  406. typename boost::movelib::unique_ptr< Y, D >::pointer tmp = r.get();
  407. if( tmp != 0 )
  408. {
  409. pn = boost::detail::shared_count( r );
  410. boost::detail::sp_deleter_construct( this, tmp );
  411. }
  412. }
  413. // assignment
  414. shared_ptr & operator=( shared_ptr const & r ) BOOST_SP_NOEXCEPT
  415. {
  416. this_type(r).swap(*this);
  417. return *this;
  418. }
  419. #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1400)
  420. template<class Y>
  421. shared_ptr & operator=(shared_ptr<Y> const & r) BOOST_SP_NOEXCEPT
  422. {
  423. this_type(r).swap(*this);
  424. return *this;
  425. }
  426. #endif
  427. #ifndef BOOST_NO_AUTO_PTR
  428. template<class Y>
  429. shared_ptr & operator=( std::auto_ptr<Y> & r )
  430. {
  431. this_type( r ).swap( *this );
  432. return *this;
  433. }
  434. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  435. template<class Y>
  436. shared_ptr & operator=( std::auto_ptr<Y> && r )
  437. {
  438. this_type( static_cast< std::auto_ptr<Y> && >( r ) ).swap( *this );
  439. return *this;
  440. }
  441. #elif !defined( BOOST_NO_SFINAE ) && !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )
  442. template<class Ap>
  443. typename boost::detail::sp_enable_if_auto_ptr< Ap, shared_ptr & >::type operator=( Ap r )
  444. {
  445. this_type( r ).swap( *this );
  446. return *this;
  447. }
  448. #endif // BOOST_NO_SFINAE, BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  449. #endif // BOOST_NO_AUTO_PTR
  450. #if !defined( BOOST_NO_CXX11_SMART_PTR ) && !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  451. template<class Y, class D>
  452. shared_ptr & operator=( std::unique_ptr<Y, D> && r )
  453. {
  454. this_type( static_cast< std::unique_ptr<Y, D> && >( r ) ).swap(*this);
  455. return *this;
  456. }
  457. #endif
  458. template<class Y, class D>
  459. shared_ptr & operator=( boost::movelib::unique_ptr<Y, D> r )
  460. {
  461. // this_type( static_cast< unique_ptr<Y, D> && >( r ) ).swap( *this );
  462. boost::detail::sp_assert_convertible< Y, T >();
  463. typename boost::movelib::unique_ptr< Y, D >::pointer p = r.get();
  464. shared_ptr tmp;
  465. if( p != 0 )
  466. {
  467. tmp.px = p;
  468. tmp.pn = boost::detail::shared_count( r );
  469. boost::detail::sp_deleter_construct( &tmp, p );
  470. }
  471. tmp.swap( *this );
  472. return *this;
  473. }
  474. // Move support
  475. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  476. shared_ptr( shared_ptr && r ) BOOST_SP_NOEXCEPT : px( r.px ), pn( static_cast< boost::detail::shared_count && >( r.pn ) )
  477. {
  478. r.px = 0;
  479. }
  480. template<class Y>
  481. #if !defined( BOOST_SP_NO_SP_CONVERTIBLE )
  482. shared_ptr( shared_ptr<Y> && r, typename boost::detail::sp_enable_if_convertible<Y,T>::type = boost::detail::sp_empty() )
  483. #else
  484. shared_ptr( shared_ptr<Y> && r )
  485. #endif
  486. BOOST_SP_NOEXCEPT : px( r.px ), pn( static_cast< boost::detail::shared_count && >( r.pn ) )
  487. {
  488. boost::detail::sp_assert_convertible< Y, T >();
  489. r.px = 0;
  490. }
  491. shared_ptr & operator=( shared_ptr && r ) BOOST_SP_NOEXCEPT
  492. {
  493. this_type( static_cast< shared_ptr && >( r ) ).swap( *this );
  494. return *this;
  495. }
  496. template<class Y>
  497. shared_ptr & operator=( shared_ptr<Y> && r ) BOOST_SP_NOEXCEPT
  498. {
  499. this_type( static_cast< shared_ptr<Y> && >( r ) ).swap( *this );
  500. return *this;
  501. }
  502. // aliasing move
  503. template<class Y>
  504. shared_ptr( shared_ptr<Y> && r, element_type * p ) BOOST_SP_NOEXCEPT : px( p ), pn()
  505. {
  506. pn.swap( r.pn );
  507. r.px = 0;
  508. }
  509. #endif
  510. #if !defined( BOOST_NO_CXX11_NULLPTR )
  511. shared_ptr & operator=( boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT
  512. {
  513. this_type().swap(*this);
  514. return *this;
  515. }
  516. #endif
  517. void reset() BOOST_SP_NOEXCEPT
  518. {
  519. this_type().swap(*this);
  520. }
  521. template<class Y> void reset( Y * p ) // Y must be complete
  522. {
  523. BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors
  524. this_type( p ).swap( *this );
  525. }
  526. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  527. template<class Y, class D> void reset( Y * p, D d )
  528. {
  529. this_type( p, static_cast< D&& >( d ) ).swap( *this );
  530. }
  531. template<class Y, class D, class A> void reset( Y * p, D d, A a )
  532. {
  533. this_type( p, static_cast< D&& >( d ), a ).swap( *this );
  534. }
  535. #else
  536. template<class Y, class D> void reset( Y * p, D d )
  537. {
  538. this_type( p, d ).swap( *this );
  539. }
  540. template<class Y, class D, class A> void reset( Y * p, D d, A a )
  541. {
  542. this_type( p, d, a ).swap( *this );
  543. }
  544. #endif
  545. template<class Y> void reset( shared_ptr<Y> const & r, element_type * p ) BOOST_SP_NOEXCEPT
  546. {
  547. this_type( r, p ).swap( *this );
  548. }
  549. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  550. template<class Y> void reset( shared_ptr<Y> && r, element_type * p ) BOOST_SP_NOEXCEPT
  551. {
  552. this_type( static_cast< shared_ptr<Y> && >( r ), p ).swap( *this );
  553. }
  554. #endif
  555. typename boost::detail::sp_dereference< T >::type operator* () const BOOST_SP_NOEXCEPT_WITH_ASSERT
  556. {
  557. BOOST_ASSERT( px != 0 );
  558. return *px;
  559. }
  560. typename boost::detail::sp_member_access< T >::type operator-> () const BOOST_SP_NOEXCEPT_WITH_ASSERT
  561. {
  562. BOOST_ASSERT( px != 0 );
  563. return px;
  564. }
  565. typename boost::detail::sp_array_access< T >::type operator[] ( std::ptrdiff_t i ) const BOOST_SP_NOEXCEPT_WITH_ASSERT
  566. {
  567. BOOST_ASSERT( px != 0 );
  568. BOOST_ASSERT( i >= 0 && ( i < boost::detail::sp_extent< T >::value || boost::detail::sp_extent< T >::value == 0 ) );
  569. return static_cast< typename boost::detail::sp_array_access< T >::type >( px[ i ] );
  570. }
  571. element_type * get() const BOOST_SP_NOEXCEPT
  572. {
  573. return px;
  574. }
  575. // implicit conversion to "bool"
  576. #include <boost/smart_ptr/detail/operator_bool.hpp>
  577. bool unique() const BOOST_SP_NOEXCEPT
  578. {
  579. return pn.unique();
  580. }
  581. long use_count() const BOOST_SP_NOEXCEPT
  582. {
  583. return pn.use_count();
  584. }
  585. void swap( shared_ptr & other ) BOOST_SP_NOEXCEPT
  586. {
  587. std::swap(px, other.px);
  588. pn.swap(other.pn);
  589. }
  590. template<class Y> bool owner_before( shared_ptr<Y> const & rhs ) const BOOST_SP_NOEXCEPT
  591. {
  592. return pn < rhs.pn;
  593. }
  594. template<class Y> bool owner_before( weak_ptr<Y> const & rhs ) const BOOST_SP_NOEXCEPT
  595. {
  596. return pn < rhs.pn;
  597. }
  598. template<class Y> bool owner_equals( shared_ptr<Y> const & rhs ) const BOOST_SP_NOEXCEPT
  599. {
  600. return pn == rhs.pn;
  601. }
  602. template<class Y> bool owner_equals( weak_ptr<Y> const & rhs ) const BOOST_SP_NOEXCEPT
  603. {
  604. return pn == rhs.pn;
  605. }
  606. std::size_t owner_hash_value() const BOOST_SP_NOEXCEPT
  607. {
  608. return pn.hash_value();
  609. }
  610. void * _internal_get_deleter( boost::detail::sp_typeinfo_ const & ti ) const BOOST_SP_NOEXCEPT
  611. {
  612. return pn.get_deleter( ti );
  613. }
  614. void * _internal_get_local_deleter( boost::detail::sp_typeinfo_ const & ti ) const BOOST_SP_NOEXCEPT
  615. {
  616. return pn.get_local_deleter( ti );
  617. }
  618. void * _internal_get_untyped_deleter() const BOOST_SP_NOEXCEPT
  619. {
  620. return pn.get_untyped_deleter();
  621. }
  622. bool _internal_equiv( shared_ptr const & r ) const BOOST_SP_NOEXCEPT
  623. {
  624. return px == r.px && pn == r.pn;
  625. }
  626. boost::detail::shared_count _internal_count() const BOOST_SP_NOEXCEPT
  627. {
  628. return pn;
  629. }
  630. // Tasteless as this may seem, making all members public allows member templates
  631. // to work in the absence of member template friends. (Matthew Langston)
  632. #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  633. private:
  634. template<class Y> friend class shared_ptr;
  635. template<class Y> friend class weak_ptr;
  636. #endif
  637. element_type * px; // contained pointer
  638. boost::detail::shared_count pn; // reference counter
  639. }; // shared_ptr
  640. template<class T, class U> inline bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b) BOOST_SP_NOEXCEPT
  641. {
  642. return a.get() == b.get();
  643. }
  644. template<class T, class U> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<U> const & b) BOOST_SP_NOEXCEPT
  645. {
  646. return a.get() != b.get();
  647. }
  648. #if !defined( BOOST_NO_CXX11_NULLPTR )
  649. template<class T> inline bool operator==( shared_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT
  650. {
  651. return p.get() == 0;
  652. }
  653. template<class T> inline bool operator==( boost::detail::sp_nullptr_t, shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
  654. {
  655. return p.get() == 0;
  656. }
  657. template<class T> inline bool operator!=( shared_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT
  658. {
  659. return p.get() != 0;
  660. }
  661. template<class T> inline bool operator!=( boost::detail::sp_nullptr_t, shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
  662. {
  663. return p.get() != 0;
  664. }
  665. #endif
  666. template<class T, class U> inline bool operator<(shared_ptr<T> const & a, shared_ptr<U> const & b) BOOST_SP_NOEXCEPT
  667. {
  668. return a.owner_before( b );
  669. }
  670. template<class T> inline void swap(shared_ptr<T> & a, shared_ptr<T> & b) BOOST_SP_NOEXCEPT
  671. {
  672. a.swap(b);
  673. }
  674. template<class T, class U> shared_ptr<T> static_pointer_cast( shared_ptr<U> const & r ) BOOST_SP_NOEXCEPT
  675. {
  676. (void) static_cast< T* >( static_cast< U* >( 0 ) );
  677. typedef typename shared_ptr<T>::element_type E;
  678. E * p = static_cast< E* >( r.get() );
  679. return shared_ptr<T>( r, p );
  680. }
  681. template<class T, class U> shared_ptr<T> const_pointer_cast( shared_ptr<U> const & r ) BOOST_SP_NOEXCEPT
  682. {
  683. (void) const_cast< T* >( static_cast< U* >( 0 ) );
  684. typedef typename shared_ptr<T>::element_type E;
  685. E * p = const_cast< E* >( r.get() );
  686. return shared_ptr<T>( r, p );
  687. }
  688. template<class T, class U> shared_ptr<T> dynamic_pointer_cast( shared_ptr<U> const & r ) BOOST_SP_NOEXCEPT
  689. {
  690. (void) dynamic_cast< T* >( static_cast< U* >( 0 ) );
  691. typedef typename shared_ptr<T>::element_type E;
  692. E * p = dynamic_cast< E* >( r.get() );
  693. return p? shared_ptr<T>( r, p ): shared_ptr<T>();
  694. }
  695. template<class T, class U> shared_ptr<T> reinterpret_pointer_cast( shared_ptr<U> const & r ) BOOST_SP_NOEXCEPT
  696. {
  697. (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
  698. typedef typename shared_ptr<T>::element_type E;
  699. E * p = reinterpret_cast< E* >( r.get() );
  700. return shared_ptr<T>( r, p );
  701. }
  702. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  703. template<class T, class U> shared_ptr<T> static_pointer_cast( shared_ptr<U> && r ) BOOST_SP_NOEXCEPT
  704. {
  705. (void) static_cast< T* >( static_cast< U* >( 0 ) );
  706. typedef typename shared_ptr<T>::element_type E;
  707. E * p = static_cast< E* >( r.get() );
  708. return shared_ptr<T>( std::move(r), p );
  709. }
  710. template<class T, class U> shared_ptr<T> const_pointer_cast( shared_ptr<U> && r ) BOOST_SP_NOEXCEPT
  711. {
  712. (void) const_cast< T* >( static_cast< U* >( 0 ) );
  713. typedef typename shared_ptr<T>::element_type E;
  714. E * p = const_cast< E* >( r.get() );
  715. return shared_ptr<T>( std::move(r), p );
  716. }
  717. template<class T, class U> shared_ptr<T> dynamic_pointer_cast( shared_ptr<U> && r ) BOOST_SP_NOEXCEPT
  718. {
  719. (void) dynamic_cast< T* >( static_cast< U* >( 0 ) );
  720. typedef typename shared_ptr<T>::element_type E;
  721. E * p = dynamic_cast< E* >( r.get() );
  722. return p? shared_ptr<T>( std::move(r), p ): shared_ptr<T>();
  723. }
  724. template<class T, class U> shared_ptr<T> reinterpret_pointer_cast( shared_ptr<U> && r ) BOOST_SP_NOEXCEPT
  725. {
  726. (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
  727. typedef typename shared_ptr<T>::element_type E;
  728. E * p = reinterpret_cast< E* >( r.get() );
  729. return shared_ptr<T>( std::move(r), p );
  730. }
  731. #endif // !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  732. // get_pointer() enables boost::mem_fn to recognize shared_ptr
  733. template<class T> inline typename shared_ptr<T>::element_type * get_pointer(shared_ptr<T> const & p) BOOST_SP_NOEXCEPT
  734. {
  735. return p.get();
  736. }
  737. // operator<<
  738. #if !defined(BOOST_NO_IOSTREAM)
  739. #if defined(BOOST_NO_TEMPLATED_IOSTREAMS) || ( defined(__GNUC__) && (__GNUC__ < 3) )
  740. template<class Y> std::ostream & operator<< (std::ostream & os, shared_ptr<Y> const & p)
  741. {
  742. os << p.get();
  743. return os;
  744. }
  745. #else
  746. // in STLport's no-iostreams mode no iostream symbols can be used
  747. #ifndef _STLP_NO_IOSTREAMS
  748. # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300 && __SGI_STL_PORT)
  749. // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
  750. using std::basic_ostream;
  751. template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, shared_ptr<Y> const & p)
  752. # else
  753. template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, shared_ptr<Y> const & p)
  754. # endif
  755. {
  756. os << p.get();
  757. return os;
  758. }
  759. #endif // _STLP_NO_IOSTREAMS
  760. #endif // __GNUC__ < 3
  761. #endif // !defined(BOOST_NO_IOSTREAM)
  762. // get_deleter
  763. namespace detail
  764. {
  765. template<class D, class T> D * basic_get_deleter( shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
  766. {
  767. return static_cast<D *>( p._internal_get_deleter(BOOST_SP_TYPEID_(D)) );
  768. }
  769. template<class D, class T> D * basic_get_local_deleter( D *, shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT;
  770. template<class D, class T> D const * basic_get_local_deleter( D const *, shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT;
  771. class esft2_deleter_wrapper
  772. {
  773. private:
  774. shared_ptr<void const volatile> deleter_;
  775. public:
  776. esft2_deleter_wrapper() BOOST_SP_NOEXCEPT
  777. {
  778. }
  779. template< class T > void set_deleter( shared_ptr<T> const & deleter ) BOOST_SP_NOEXCEPT
  780. {
  781. deleter_ = deleter;
  782. }
  783. template<typename D> D* get_deleter() const BOOST_SP_NOEXCEPT
  784. {
  785. return boost::detail::basic_get_deleter<D>( deleter_ );
  786. }
  787. template< class T> void operator()( T* ) BOOST_SP_NOEXCEPT_WITH_ASSERT
  788. {
  789. BOOST_ASSERT( deleter_.use_count() <= 1 );
  790. deleter_.reset();
  791. }
  792. };
  793. } // namespace detail
  794. template<class D, class T> D * get_deleter( shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
  795. {
  796. D * d = boost::detail::basic_get_deleter<D>( p );
  797. if( d == 0 )
  798. {
  799. d = boost::detail::basic_get_local_deleter( d, p );
  800. }
  801. if( d == 0 )
  802. {
  803. boost::detail::esft2_deleter_wrapper *del_wrapper = boost::detail::basic_get_deleter<boost::detail::esft2_deleter_wrapper>(p);
  804. // The following get_deleter method call is fully qualified because
  805. // older versions of gcc (2.95, 3.2.3) fail to compile it when written del_wrapper->get_deleter<D>()
  806. if(del_wrapper) d = del_wrapper->::boost::detail::esft2_deleter_wrapper::get_deleter<D>();
  807. }
  808. return d;
  809. }
  810. // atomic access
  811. #if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
  812. template<class T> inline bool atomic_is_lock_free( shared_ptr<T> const * /*p*/ ) BOOST_SP_NOEXCEPT
  813. {
  814. return false;
  815. }
  816. template<class T> shared_ptr<T> atomic_load( shared_ptr<T> const * p ) BOOST_SP_NOEXCEPT
  817. {
  818. boost::detail::spinlock_pool<2>::scoped_lock lock( p );
  819. return *p;
  820. }
  821. template<class T, class M> inline shared_ptr<T> atomic_load_explicit( shared_ptr<T> const * p, /*memory_order mo*/ M ) BOOST_SP_NOEXCEPT
  822. {
  823. return atomic_load( p );
  824. }
  825. template<class T> void atomic_store( shared_ptr<T> * p, shared_ptr<T> r ) BOOST_SP_NOEXCEPT
  826. {
  827. boost::detail::spinlock_pool<2>::scoped_lock lock( p );
  828. p->swap( r );
  829. }
  830. template<class T, class M> inline void atomic_store_explicit( shared_ptr<T> * p, shared_ptr<T> r, /*memory_order mo*/ M ) BOOST_SP_NOEXCEPT
  831. {
  832. atomic_store( p, r ); // std::move( r )
  833. }
  834. template<class T> shared_ptr<T> atomic_exchange( shared_ptr<T> * p, shared_ptr<T> r ) BOOST_SP_NOEXCEPT
  835. {
  836. boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( p );
  837. sp.lock();
  838. p->swap( r );
  839. sp.unlock();
  840. return r; // return std::move( r )
  841. }
  842. template<class T, class M> shared_ptr<T> inline atomic_exchange_explicit( shared_ptr<T> * p, shared_ptr<T> r, /*memory_order mo*/ M ) BOOST_SP_NOEXCEPT
  843. {
  844. return atomic_exchange( p, r ); // std::move( r )
  845. }
  846. template<class T> bool atomic_compare_exchange( shared_ptr<T> * p, shared_ptr<T> * v, shared_ptr<T> w ) BOOST_SP_NOEXCEPT
  847. {
  848. boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( p );
  849. sp.lock();
  850. if( p->_internal_equiv( *v ) )
  851. {
  852. p->swap( w );
  853. sp.unlock();
  854. return true;
  855. }
  856. else
  857. {
  858. shared_ptr<T> tmp( *p );
  859. sp.unlock();
  860. tmp.swap( *v );
  861. return false;
  862. }
  863. }
  864. 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 ) BOOST_SP_NOEXCEPT
  865. {
  866. return atomic_compare_exchange( p, v, w ); // std::move( w )
  867. }
  868. #endif // !defined(BOOST_SP_NO_ATOMIC_ACCESS)
  869. // hash_value
  870. template< class T > struct hash;
  871. template< class T > std::size_t hash_value( boost::shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
  872. {
  873. return boost::hash< typename boost::shared_ptr<T>::element_type* >()( p.get() );
  874. }
  875. } // namespace boost
  876. // std::hash
  877. #if !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL)
  878. namespace std
  879. {
  880. template<class T> struct hash< ::boost::shared_ptr<T> >
  881. {
  882. std::size_t operator()( ::boost::shared_ptr<T> const & p ) const BOOST_SP_NOEXCEPT
  883. {
  884. return std::hash< typename ::boost::shared_ptr<T>::element_type* >()( p.get() );
  885. }
  886. };
  887. } // namespace std
  888. #endif // #if !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL)
  889. #include <boost/smart_ptr/detail/local_sp_deleter.hpp>
  890. namespace boost
  891. {
  892. namespace detail
  893. {
  894. template<class D, class T> D * basic_get_local_deleter( D *, shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
  895. {
  896. return static_cast<D *>( p._internal_get_local_deleter( BOOST_SP_TYPEID_(local_sp_deleter<D>) ) );
  897. }
  898. template<class D, class T> D const * basic_get_local_deleter( D const *, shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
  899. {
  900. return static_cast<D *>( p._internal_get_local_deleter( BOOST_SP_TYPEID_(local_sp_deleter<D>) ) );
  901. }
  902. } // namespace detail
  903. #if defined(__cpp_deduction_guides)
  904. template<class T> shared_ptr( weak_ptr<T> ) -> shared_ptr<T>;
  905. template<class T, class D> shared_ptr( std::unique_ptr<T, D> ) -> shared_ptr<T>;
  906. #endif
  907. } // namespace boost
  908. #if defined( BOOST_SP_DISABLE_DEPRECATED )
  909. #pragma GCC diagnostic pop
  910. #endif
  911. #endif // #ifndef BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED