shared_ptr.hpp 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  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/detail/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. } // namespace boost
  854. #if defined( BOOST_SP_DISABLE_DEPRECATED )
  855. #pragma GCC diagnostic pop
  856. #endif
  857. #endif // #ifndef BOOST_SMART_PTR_SHARED_PTR_HPP_INCLUDED