shared_ptr.hpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  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 __GNUC__ == 2 && __GNUC_MINOR__ <= 96
  649. // Resolve the ambiguity between our op!= and the one in rel_ops
  650. template<class T> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<T> const & b) BOOST_SP_NOEXCEPT
  651. {
  652. return a.get() != b.get();
  653. }
  654. #endif
  655. #if !defined( BOOST_NO_CXX11_NULLPTR )
  656. template<class T> inline bool operator==( shared_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT
  657. {
  658. return p.get() == 0;
  659. }
  660. template<class T> inline bool operator==( boost::detail::sp_nullptr_t, shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
  661. {
  662. return p.get() == 0;
  663. }
  664. template<class T> inline bool operator!=( shared_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT
  665. {
  666. return p.get() != 0;
  667. }
  668. template<class T> inline bool operator!=( boost::detail::sp_nullptr_t, shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
  669. {
  670. return p.get() != 0;
  671. }
  672. #endif
  673. template<class T, class U> inline bool operator<(shared_ptr<T> const & a, shared_ptr<U> const & b) BOOST_SP_NOEXCEPT
  674. {
  675. return a.owner_before( b );
  676. }
  677. template<class T> inline void swap(shared_ptr<T> & a, shared_ptr<T> & b) BOOST_SP_NOEXCEPT
  678. {
  679. a.swap(b);
  680. }
  681. template<class T, class U> shared_ptr<T> static_pointer_cast( shared_ptr<U> const & r ) BOOST_SP_NOEXCEPT
  682. {
  683. (void) static_cast< T* >( static_cast< U* >( 0 ) );
  684. typedef typename shared_ptr<T>::element_type E;
  685. E * p = static_cast< E* >( r.get() );
  686. return shared_ptr<T>( r, p );
  687. }
  688. template<class T, class U> shared_ptr<T> const_pointer_cast( shared_ptr<U> const & r ) BOOST_SP_NOEXCEPT
  689. {
  690. (void) const_cast< T* >( static_cast< U* >( 0 ) );
  691. typedef typename shared_ptr<T>::element_type E;
  692. E * p = const_cast< E* >( r.get() );
  693. return shared_ptr<T>( r, p );
  694. }
  695. template<class T, class U> shared_ptr<T> dynamic_pointer_cast( shared_ptr<U> const & r ) BOOST_SP_NOEXCEPT
  696. {
  697. (void) dynamic_cast< T* >( static_cast< U* >( 0 ) );
  698. typedef typename shared_ptr<T>::element_type E;
  699. E * p = dynamic_cast< E* >( r.get() );
  700. return p? shared_ptr<T>( r, p ): shared_ptr<T>();
  701. }
  702. template<class T, class U> shared_ptr<T> reinterpret_pointer_cast( shared_ptr<U> const & r ) BOOST_SP_NOEXCEPT
  703. {
  704. (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
  705. typedef typename shared_ptr<T>::element_type E;
  706. E * p = reinterpret_cast< E* >( r.get() );
  707. return shared_ptr<T>( r, p );
  708. }
  709. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  710. template<class T, class U> shared_ptr<T> static_pointer_cast( shared_ptr<U> && r ) BOOST_SP_NOEXCEPT
  711. {
  712. (void) static_cast< T* >( static_cast< U* >( 0 ) );
  713. typedef typename shared_ptr<T>::element_type E;
  714. E * p = static_cast< E* >( r.get() );
  715. return shared_ptr<T>( std::move(r), p );
  716. }
  717. template<class T, class U> shared_ptr<T> const_pointer_cast( shared_ptr<U> && r ) BOOST_SP_NOEXCEPT
  718. {
  719. (void) const_cast< T* >( static_cast< U* >( 0 ) );
  720. typedef typename shared_ptr<T>::element_type E;
  721. E * p = const_cast< E* >( r.get() );
  722. return shared_ptr<T>( std::move(r), p );
  723. }
  724. template<class T, class U> shared_ptr<T> dynamic_pointer_cast( shared_ptr<U> && r ) BOOST_SP_NOEXCEPT
  725. {
  726. (void) dynamic_cast< T* >( static_cast< U* >( 0 ) );
  727. typedef typename shared_ptr<T>::element_type E;
  728. E * p = dynamic_cast< E* >( r.get() );
  729. return p? shared_ptr<T>( std::move(r), p ): shared_ptr<T>();
  730. }
  731. template<class T, class U> shared_ptr<T> reinterpret_pointer_cast( shared_ptr<U> && r ) BOOST_SP_NOEXCEPT
  732. {
  733. (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
  734. typedef typename shared_ptr<T>::element_type E;
  735. E * p = reinterpret_cast< E* >( r.get() );
  736. return shared_ptr<T>( std::move(r), p );
  737. }
  738. #endif // !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  739. // get_pointer() enables boost::mem_fn to recognize shared_ptr
  740. template<class T> inline typename shared_ptr<T>::element_type * get_pointer(shared_ptr<T> const & p) BOOST_SP_NOEXCEPT
  741. {
  742. return p.get();
  743. }
  744. // operator<<
  745. #if !defined(BOOST_NO_IOSTREAM)
  746. #if defined(BOOST_NO_TEMPLATED_IOSTREAMS) || ( defined(__GNUC__) && (__GNUC__ < 3) )
  747. template<class Y> std::ostream & operator<< (std::ostream & os, shared_ptr<Y> const & p)
  748. {
  749. os << p.get();
  750. return os;
  751. }
  752. #else
  753. // in STLport's no-iostreams mode no iostream symbols can be used
  754. #ifndef _STLP_NO_IOSTREAMS
  755. # if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300 && __SGI_STL_PORT)
  756. // MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
  757. using std::basic_ostream;
  758. template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, shared_ptr<Y> const & p)
  759. # else
  760. template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, shared_ptr<Y> const & p)
  761. # endif
  762. {
  763. os << p.get();
  764. return os;
  765. }
  766. #endif // _STLP_NO_IOSTREAMS
  767. #endif // __GNUC__ < 3
  768. #endif // !defined(BOOST_NO_IOSTREAM)
  769. // get_deleter
  770. namespace detail
  771. {
  772. template<class D, class T> D * basic_get_deleter( shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
  773. {
  774. return static_cast<D *>( p._internal_get_deleter(BOOST_SP_TYPEID_(D)) );
  775. }
  776. template<class D, class T> D * basic_get_local_deleter( D *, shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT;
  777. template<class D, class T> D const * basic_get_local_deleter( D const *, shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT;
  778. class esft2_deleter_wrapper
  779. {
  780. private:
  781. shared_ptr<void const volatile> deleter_;
  782. public:
  783. esft2_deleter_wrapper() BOOST_SP_NOEXCEPT
  784. {
  785. }
  786. template< class T > void set_deleter( shared_ptr<T> const & deleter ) BOOST_SP_NOEXCEPT
  787. {
  788. deleter_ = deleter;
  789. }
  790. template<typename D> D* get_deleter() const BOOST_SP_NOEXCEPT
  791. {
  792. return boost::detail::basic_get_deleter<D>( deleter_ );
  793. }
  794. template< class T> void operator()( T* ) BOOST_SP_NOEXCEPT_WITH_ASSERT
  795. {
  796. BOOST_ASSERT( deleter_.use_count() <= 1 );
  797. deleter_.reset();
  798. }
  799. };
  800. } // namespace detail
  801. template<class D, class T> D * get_deleter( shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
  802. {
  803. D * d = boost::detail::basic_get_deleter<D>( p );
  804. if( d == 0 )
  805. {
  806. d = boost::detail::basic_get_local_deleter( d, p );
  807. }
  808. if( d == 0 )
  809. {
  810. boost::detail::esft2_deleter_wrapper *del_wrapper = boost::detail::basic_get_deleter<boost::detail::esft2_deleter_wrapper>(p);
  811. // The following get_deleter method call is fully qualified because
  812. // older versions of gcc (2.95, 3.2.3) fail to compile it when written del_wrapper->get_deleter<D>()
  813. if(del_wrapper) d = del_wrapper->::boost::detail::esft2_deleter_wrapper::get_deleter<D>();
  814. }
  815. return d;
  816. }
  817. // atomic access
  818. #if !defined(BOOST_SP_NO_ATOMIC_ACCESS)
  819. template<class T> inline bool atomic_is_lock_free( shared_ptr<T> const * /*p*/ ) BOOST_SP_NOEXCEPT
  820. {
  821. return false;
  822. }
  823. template<class T> shared_ptr<T> atomic_load( shared_ptr<T> const * p ) BOOST_SP_NOEXCEPT
  824. {
  825. boost::detail::spinlock_pool<2>::scoped_lock lock( p );
  826. return *p;
  827. }
  828. template<class T, class M> inline shared_ptr<T> atomic_load_explicit( shared_ptr<T> const * p, /*memory_order mo*/ M ) BOOST_SP_NOEXCEPT
  829. {
  830. return atomic_load( p );
  831. }
  832. template<class T> void atomic_store( shared_ptr<T> * p, shared_ptr<T> r ) BOOST_SP_NOEXCEPT
  833. {
  834. boost::detail::spinlock_pool<2>::scoped_lock lock( p );
  835. p->swap( r );
  836. }
  837. 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
  838. {
  839. atomic_store( p, r ); // std::move( r )
  840. }
  841. template<class T> shared_ptr<T> atomic_exchange( shared_ptr<T> * p, shared_ptr<T> r ) BOOST_SP_NOEXCEPT
  842. {
  843. boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( p );
  844. sp.lock();
  845. p->swap( r );
  846. sp.unlock();
  847. return r; // return std::move( r )
  848. }
  849. 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
  850. {
  851. return atomic_exchange( p, r ); // std::move( r )
  852. }
  853. template<class T> bool atomic_compare_exchange( shared_ptr<T> * p, shared_ptr<T> * v, shared_ptr<T> w ) BOOST_SP_NOEXCEPT
  854. {
  855. boost::detail::spinlock & sp = boost::detail::spinlock_pool<2>::spinlock_for( p );
  856. sp.lock();
  857. if( p->_internal_equiv( *v ) )
  858. {
  859. p->swap( w );
  860. sp.unlock();
  861. return true;
  862. }
  863. else
  864. {
  865. shared_ptr<T> tmp( *p );
  866. sp.unlock();
  867. tmp.swap( *v );
  868. return false;
  869. }
  870. }
  871. 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
  872. {
  873. return atomic_compare_exchange( p, v, w ); // std::move( w )
  874. }
  875. #endif // !defined(BOOST_SP_NO_ATOMIC_ACCESS)
  876. // hash_value
  877. template< class T > struct hash;
  878. template< class T > std::size_t hash_value( boost::shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
  879. {
  880. return boost::hash< typename boost::shared_ptr<T>::element_type* >()( p.get() );
  881. }
  882. } // namespace boost
  883. // std::hash
  884. #if !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL)
  885. namespace std
  886. {
  887. template<class T> struct hash< ::boost::shared_ptr<T> >
  888. {
  889. std::size_t operator()( ::boost::shared_ptr<T> const & p ) const BOOST_SP_NOEXCEPT
  890. {
  891. return std::hash< typename ::boost::shared_ptr<T>::element_type* >()( p.get() );
  892. }
  893. };
  894. } // namespace std
  895. #endif // #if !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL)
  896. #include <boost/smart_ptr/detail/local_sp_deleter.hpp>
  897. namespace boost
  898. {
  899. namespace detail
  900. {
  901. template<class D, class T> D * basic_get_local_deleter( D *, shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
  902. {
  903. return static_cast<D *>( p._internal_get_local_deleter( BOOST_SP_TYPEID_(local_sp_deleter<D>) ) );
  904. }
  905. template<class D, class T> D const * basic_get_local_deleter( D const *, shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
  906. {
  907. return static_cast<D *>( p._internal_get_local_deleter( BOOST_SP_TYPEID_(local_sp_deleter<D>) ) );
  908. }
  909. } // namespace detail
  910. #if defined(__cpp_deduction_guides)
  911. template<class T> shared_ptr( weak_ptr<T> ) -> shared_ptr<T>;
  912. template<class T, class D> shared_ptr( std::unique_ptr<T, D> ) -> shared_ptr<T>;
  913. #endif
  914. } // namespace boost
  915. #if defined( BOOST_SP_DISABLE_DEPRECATED )
  916. #pragma GCC diagnostic pop
  917. #endif
  918. #endif // #ifndef BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED