shared_ptr.hpp 30 KB

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