local_shared_ptr.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. #ifndef BOOST_SMART_PTR_LOCAL_SHARED_PTR_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_LOCAL_SHARED_PTR_HPP_INCLUDED
  3. // local_shared_ptr.hpp
  4. //
  5. // Copyright 2017 Peter Dimov
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See
  8. // accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // See http://www.boost.org/libs/smart_ptr/ for documentation.
  12. #include <boost/smart_ptr/detail/requires_cxx11.hpp>
  13. #include <boost/smart_ptr/shared_ptr.hpp>
  14. namespace boost
  15. {
  16. template<class T> class local_shared_ptr;
  17. namespace detail
  18. {
  19. template< class E, class Y > inline void lsp_pointer_construct( boost::local_shared_ptr< E > * /*ppx*/, Y * p, boost::detail::local_counted_base * & pn )
  20. {
  21. boost::detail::sp_assert_convertible< Y, E >();
  22. typedef boost::detail::local_sp_deleter< boost::checked_deleter<Y> > D;
  23. boost::shared_ptr<E> p2( p, D() );
  24. D * pd = static_cast< D * >( p2._internal_get_untyped_deleter() );
  25. pd->pn_ = p2._internal_count();
  26. pn = pd;
  27. }
  28. template< class E, class Y > inline void lsp_pointer_construct( boost::local_shared_ptr< E[] > * /*ppx*/, Y * p, boost::detail::local_counted_base * & pn )
  29. {
  30. boost::detail::sp_assert_convertible< Y[], E[] >();
  31. typedef boost::detail::local_sp_deleter< boost::checked_array_deleter<E> > D;
  32. boost::shared_ptr<E[]> p2( p, D() );
  33. D * pd = static_cast< D * >( p2._internal_get_untyped_deleter() );
  34. pd->pn_ = p2._internal_count();
  35. pn = pd;
  36. }
  37. template< class E, std::size_t N, class Y > inline void lsp_pointer_construct( boost::local_shared_ptr< E[N] > * /*ppx*/, Y * p, boost::detail::local_counted_base * & pn )
  38. {
  39. boost::detail::sp_assert_convertible< Y[N], E[N] >();
  40. typedef boost::detail::local_sp_deleter< boost::checked_array_deleter<E> > D;
  41. boost::shared_ptr<E[N]> p2( p, D() );
  42. D * pd = static_cast< D * >( p2._internal_get_untyped_deleter() );
  43. pd->pn_ = p2._internal_count();
  44. pn = pd;
  45. }
  46. template< class E, class P, class D > inline void lsp_deleter_construct( boost::local_shared_ptr< E > * /*ppx*/, P p, D const& d, boost::detail::local_counted_base * & pn )
  47. {
  48. typedef boost::detail::local_sp_deleter<D> D2;
  49. boost::shared_ptr<E> p2( p, D2( d ) );
  50. D2 * pd = static_cast< D2 * >( p2._internal_get_untyped_deleter() );
  51. pd->pn_ = p2._internal_count();
  52. pn = pd;
  53. }
  54. template< class E, class P, class D, class A > inline void lsp_allocator_construct( boost::local_shared_ptr< E > * /*ppx*/, P p, D const& d, A const& a, boost::detail::local_counted_base * & pn )
  55. {
  56. typedef boost::detail::local_sp_deleter<D> D2;
  57. boost::shared_ptr<E> p2( p, D2( d ), a );
  58. D2 * pd = static_cast< D2 * >( p2._internal_get_untyped_deleter() );
  59. pd->pn_ = p2._internal_count();
  60. pn = pd;
  61. }
  62. struct lsp_internal_constructor_tag
  63. {
  64. };
  65. } // namespace detail
  66. //
  67. // local_shared_ptr
  68. //
  69. // as shared_ptr, but local to a thread.
  70. // reference count manipulations are non-atomic.
  71. //
  72. template<class T> class local_shared_ptr
  73. {
  74. private:
  75. typedef local_shared_ptr this_type;
  76. public:
  77. typedef typename boost::detail::sp_element<T>::type element_type;
  78. private:
  79. element_type * px;
  80. boost::detail::local_counted_base * pn;
  81. template<class Y> friend class local_shared_ptr;
  82. public:
  83. // destructor
  84. ~local_shared_ptr() BOOST_SP_NOEXCEPT
  85. {
  86. if( pn )
  87. {
  88. pn->release();
  89. }
  90. }
  91. // constructors
  92. BOOST_CONSTEXPR local_shared_ptr() BOOST_SP_NOEXCEPT : px( 0 ), pn( 0 )
  93. {
  94. }
  95. #if !defined( BOOST_NO_CXX11_NULLPTR )
  96. BOOST_CONSTEXPR local_shared_ptr( boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT : px( 0 ), pn( 0 )
  97. {
  98. }
  99. #endif
  100. // internal constructor, used by make_shared
  101. BOOST_CONSTEXPR local_shared_ptr( boost::detail::lsp_internal_constructor_tag, element_type * px_, boost::detail::local_counted_base * pn_ ) BOOST_SP_NOEXCEPT : px( px_ ), pn( pn_ )
  102. {
  103. }
  104. template<class Y>
  105. explicit local_shared_ptr( Y * p ): px( p ), pn( 0 )
  106. {
  107. boost::detail::lsp_pointer_construct( this, p, pn );
  108. }
  109. template<class Y, class D> local_shared_ptr( Y * p, D d ): px( p ), pn( 0 )
  110. {
  111. boost::detail::lsp_deleter_construct( this, p, d, pn );
  112. }
  113. #if !defined( BOOST_NO_CXX11_NULLPTR )
  114. template<class D> local_shared_ptr( boost::detail::sp_nullptr_t p, D d ): px( p ), pn( 0 )
  115. {
  116. boost::detail::lsp_deleter_construct( this, p, d, pn );
  117. }
  118. #endif
  119. template<class Y, class D, class A> local_shared_ptr( Y * p, D d, A a ): px( p ), pn( 0 )
  120. {
  121. boost::detail::lsp_allocator_construct( this, p, d, a, pn );
  122. }
  123. #if !defined( BOOST_NO_CXX11_NULLPTR )
  124. template<class D, class A> local_shared_ptr( boost::detail::sp_nullptr_t p, D d, A a ): px( p ), pn( 0 )
  125. {
  126. boost::detail::lsp_allocator_construct( this, p, d, a, pn );
  127. }
  128. #endif
  129. // construction from shared_ptr
  130. template<class Y> local_shared_ptr( shared_ptr<Y> const & r,
  131. typename boost::detail::sp_enable_if_convertible<Y, T>::type = boost::detail::sp_empty() )
  132. : px( r.get() ), pn( 0 )
  133. {
  134. boost::detail::sp_assert_convertible< Y, T >();
  135. if( r.use_count() != 0 )
  136. {
  137. pn = new boost::detail::local_counted_impl( r._internal_count() );
  138. }
  139. }
  140. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  141. template<class Y> local_shared_ptr( shared_ptr<Y> && r,
  142. typename boost::detail::sp_enable_if_convertible<Y, T>::type = boost::detail::sp_empty() )
  143. : px( r.get() ), pn( 0 )
  144. {
  145. boost::detail::sp_assert_convertible< Y, T >();
  146. if( r.use_count() != 0 )
  147. {
  148. pn = new boost::detail::local_counted_impl( r._internal_count() );
  149. r.reset();
  150. }
  151. }
  152. #endif
  153. // construction from unique_ptr
  154. #if !defined( BOOST_NO_CXX11_SMART_PTR ) && !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  155. template< class Y, class D >
  156. local_shared_ptr( std::unique_ptr< Y, D > && r,
  157. typename boost::detail::sp_enable_if_convertible<Y, T>::type = boost::detail::sp_empty() )
  158. : px( r.get() ), pn( 0 )
  159. {
  160. boost::detail::sp_assert_convertible< Y, T >();
  161. if( px )
  162. {
  163. pn = new boost::detail::local_counted_impl( shared_ptr<T>( std::move(r) )._internal_count() );
  164. }
  165. }
  166. #endif
  167. template< class Y, class D >
  168. local_shared_ptr( boost::movelib::unique_ptr< Y, D > r ); // !
  169. // : px( r.get() ), pn( new boost::detail::local_counted_impl( shared_ptr<T>( std::move(r) ) ) )
  170. //{
  171. // boost::detail::sp_assert_convertible< Y, T >();
  172. //}
  173. // copy constructor
  174. local_shared_ptr( local_shared_ptr const & r ) BOOST_SP_NOEXCEPT : px( r.px ), pn( r.pn )
  175. {
  176. if( pn )
  177. {
  178. pn->add_ref();
  179. }
  180. }
  181. // move constructor
  182. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  183. local_shared_ptr( local_shared_ptr && r ) BOOST_SP_NOEXCEPT : px( r.px ), pn( r.pn )
  184. {
  185. r.px = 0;
  186. r.pn = 0;
  187. }
  188. #endif
  189. // converting copy constructor
  190. template<class Y> local_shared_ptr( local_shared_ptr<Y> const & r,
  191. typename boost::detail::sp_enable_if_convertible<Y, T>::type = boost::detail::sp_empty() ) BOOST_SP_NOEXCEPT
  192. : px( r.px ), pn( r.pn )
  193. {
  194. boost::detail::sp_assert_convertible< Y, T >();
  195. if( pn )
  196. {
  197. pn->add_ref();
  198. }
  199. }
  200. // converting move constructor
  201. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  202. template<class Y> local_shared_ptr( local_shared_ptr<Y> && r,
  203. typename boost::detail::sp_enable_if_convertible<Y, T>::type = boost::detail::sp_empty() ) BOOST_SP_NOEXCEPT
  204. : px( r.px ), pn( r.pn )
  205. {
  206. boost::detail::sp_assert_convertible< Y, T >();
  207. r.px = 0;
  208. r.pn = 0;
  209. }
  210. #endif
  211. // aliasing
  212. template<class Y>
  213. local_shared_ptr( local_shared_ptr<Y> const & r, element_type * p ) BOOST_SP_NOEXCEPT : px( p ), pn( r.pn )
  214. {
  215. if( pn )
  216. {
  217. pn->add_ref();
  218. }
  219. }
  220. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  221. template<class Y>
  222. local_shared_ptr( local_shared_ptr<Y> && r, element_type * p ) BOOST_SP_NOEXCEPT : px( p ), pn( r.pn )
  223. {
  224. r.px = 0;
  225. r.pn = 0;
  226. }
  227. #endif
  228. // assignment
  229. local_shared_ptr & operator=( local_shared_ptr const & r ) BOOST_SP_NOEXCEPT
  230. {
  231. local_shared_ptr( r ).swap( *this );
  232. return *this;
  233. }
  234. template<class Y> local_shared_ptr & operator=( local_shared_ptr<Y> const & r ) BOOST_SP_NOEXCEPT
  235. {
  236. local_shared_ptr( r ).swap( *this );
  237. return *this;
  238. }
  239. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  240. local_shared_ptr & operator=( local_shared_ptr && r ) BOOST_SP_NOEXCEPT
  241. {
  242. local_shared_ptr( std::move( r ) ).swap( *this );
  243. return *this;
  244. }
  245. template<class Y>
  246. local_shared_ptr & operator=( local_shared_ptr<Y> && r ) BOOST_SP_NOEXCEPT
  247. {
  248. local_shared_ptr( std::move( r ) ).swap( *this );
  249. return *this;
  250. }
  251. #endif
  252. #if !defined( BOOST_NO_CXX11_NULLPTR )
  253. local_shared_ptr & operator=( boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT
  254. {
  255. local_shared_ptr().swap(*this);
  256. return *this;
  257. }
  258. #endif
  259. #if !defined( BOOST_NO_CXX11_SMART_PTR ) && !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  260. template<class Y, class D>
  261. local_shared_ptr & operator=( std::unique_ptr<Y, D> && r )
  262. {
  263. local_shared_ptr( std::move(r) ).swap( *this );
  264. return *this;
  265. }
  266. #endif
  267. template<class Y, class D>
  268. local_shared_ptr & operator=( boost::movelib::unique_ptr<Y, D> r ); // !
  269. // reset
  270. void reset() BOOST_SP_NOEXCEPT
  271. {
  272. local_shared_ptr().swap( *this );
  273. }
  274. template<class Y> void reset( Y * p ) // Y must be complete
  275. {
  276. local_shared_ptr( p ).swap( *this );
  277. }
  278. template<class Y, class D> void reset( Y * p, D d )
  279. {
  280. local_shared_ptr( p, d ).swap( *this );
  281. }
  282. template<class Y, class D, class A> void reset( Y * p, D d, A a )
  283. {
  284. local_shared_ptr( p, d, a ).swap( *this );
  285. }
  286. template<class Y> void reset( local_shared_ptr<Y> const & r, element_type * p ) BOOST_SP_NOEXCEPT
  287. {
  288. local_shared_ptr( r, p ).swap( *this );
  289. }
  290. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  291. template<class Y> void reset( local_shared_ptr<Y> && r, element_type * p ) BOOST_SP_NOEXCEPT
  292. {
  293. local_shared_ptr( std::move( r ), p ).swap( *this );
  294. }
  295. #endif
  296. // accessors
  297. typename boost::detail::sp_dereference< T >::type operator* () const BOOST_SP_NOEXCEPT
  298. {
  299. return *px;
  300. }
  301. typename boost::detail::sp_member_access< T >::type operator-> () const BOOST_SP_NOEXCEPT
  302. {
  303. return px;
  304. }
  305. typename boost::detail::sp_array_access< T >::type operator[] ( std::ptrdiff_t i ) const BOOST_SP_NOEXCEPT_WITH_ASSERT
  306. {
  307. BOOST_ASSERT( px != 0 );
  308. BOOST_ASSERT( i >= 0 && ( i < boost::detail::sp_extent< T >::value || boost::detail::sp_extent< T >::value == 0 ) );
  309. return static_cast< typename boost::detail::sp_array_access< T >::type >( px[ i ] );
  310. }
  311. element_type * get() const BOOST_SP_NOEXCEPT
  312. {
  313. return px;
  314. }
  315. // implicit conversion to "bool"
  316. #include <boost/smart_ptr/detail/operator_bool.hpp>
  317. long local_use_count() const BOOST_SP_NOEXCEPT
  318. {
  319. return pn? pn->local_use_count(): 0;
  320. }
  321. // conversions to shared_ptr, weak_ptr
  322. #if !defined( BOOST_SP_NO_SP_CONVERTIBLE ) && !defined(BOOST_NO_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGS)
  323. template<class Y, class E = typename boost::detail::sp_enable_if_convertible<T,Y>::type> operator shared_ptr<Y>() const BOOST_SP_NOEXCEPT
  324. #else
  325. template<class Y> operator shared_ptr<Y>() const BOOST_SP_NOEXCEPT
  326. #endif
  327. {
  328. boost::detail::sp_assert_convertible<T, Y>();
  329. if( pn )
  330. {
  331. return shared_ptr<Y>( boost::detail::sp_internal_constructor_tag(), px, pn->local_cb_get_shared_count() );
  332. }
  333. else
  334. {
  335. return shared_ptr<Y>();
  336. }
  337. }
  338. #if !defined( BOOST_SP_NO_SP_CONVERTIBLE ) && !defined(BOOST_NO_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGS)
  339. template<class Y, class E = typename boost::detail::sp_enable_if_convertible<T,Y>::type> operator weak_ptr<Y>() const BOOST_SP_NOEXCEPT
  340. #else
  341. template<class Y> operator weak_ptr<Y>() const BOOST_SP_NOEXCEPT
  342. #endif
  343. {
  344. boost::detail::sp_assert_convertible<T, Y>();
  345. if( pn )
  346. {
  347. return shared_ptr<Y>( boost::detail::sp_internal_constructor_tag(), px, pn->local_cb_get_shared_count() );
  348. }
  349. else
  350. {
  351. return weak_ptr<Y>();
  352. }
  353. }
  354. // swap
  355. void swap( local_shared_ptr & r ) BOOST_SP_NOEXCEPT
  356. {
  357. std::swap( px, r.px );
  358. std::swap( pn, r.pn );
  359. }
  360. // owner_before
  361. template<class Y> bool owner_before( local_shared_ptr<Y> const & r ) const BOOST_SP_NOEXCEPT
  362. {
  363. return std::less< boost::detail::local_counted_base* >()( pn, r.pn );
  364. }
  365. // owner_equals
  366. template<class Y> bool owner_equals( local_shared_ptr<Y> const & r ) const BOOST_SP_NOEXCEPT
  367. {
  368. return pn == r.pn;
  369. }
  370. };
  371. template<class T, class U> inline bool operator==( local_shared_ptr<T> const & a, local_shared_ptr<U> const & b ) BOOST_SP_NOEXCEPT
  372. {
  373. return a.get() == b.get();
  374. }
  375. template<class T, class U> inline bool operator!=( local_shared_ptr<T> const & a, local_shared_ptr<U> const & b ) BOOST_SP_NOEXCEPT
  376. {
  377. return a.get() != b.get();
  378. }
  379. #if !defined( BOOST_NO_CXX11_NULLPTR )
  380. template<class T> inline bool operator==( local_shared_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT
  381. {
  382. return p.get() == 0;
  383. }
  384. template<class T> inline bool operator==( boost::detail::sp_nullptr_t, local_shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
  385. {
  386. return p.get() == 0;
  387. }
  388. template<class T> inline bool operator!=( local_shared_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT
  389. {
  390. return p.get() != 0;
  391. }
  392. template<class T> inline bool operator!=( boost::detail::sp_nullptr_t, local_shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
  393. {
  394. return p.get() != 0;
  395. }
  396. #endif
  397. template<class T, class U> inline bool operator==( local_shared_ptr<T> const & a, shared_ptr<U> const & b ) BOOST_SP_NOEXCEPT
  398. {
  399. return a.get() == b.get();
  400. }
  401. template<class T, class U> inline bool operator!=( local_shared_ptr<T> const & a, shared_ptr<U> const & b ) BOOST_SP_NOEXCEPT
  402. {
  403. return a.get() != b.get();
  404. }
  405. template<class T, class U> inline bool operator==( shared_ptr<T> const & a, local_shared_ptr<U> const & b ) BOOST_SP_NOEXCEPT
  406. {
  407. return a.get() == b.get();
  408. }
  409. template<class T, class U> inline bool operator!=( shared_ptr<T> const & a, local_shared_ptr<U> const & b ) BOOST_SP_NOEXCEPT
  410. {
  411. return a.get() != b.get();
  412. }
  413. template<class T, class U> inline bool operator<(local_shared_ptr<T> const & a, local_shared_ptr<U> const & b) BOOST_SP_NOEXCEPT
  414. {
  415. return a.owner_before( b );
  416. }
  417. template<class T> inline void swap( local_shared_ptr<T> & a, local_shared_ptr<T> & b ) BOOST_SP_NOEXCEPT
  418. {
  419. a.swap( b );
  420. }
  421. template<class T, class U> local_shared_ptr<T> static_pointer_cast( local_shared_ptr<U> const & r ) BOOST_SP_NOEXCEPT
  422. {
  423. (void) static_cast< T* >( static_cast< U* >( 0 ) );
  424. typedef typename local_shared_ptr<T>::element_type E;
  425. E * p = static_cast< E* >( r.get() );
  426. return local_shared_ptr<T>( r, p );
  427. }
  428. template<class T, class U> local_shared_ptr<T> const_pointer_cast( local_shared_ptr<U> const & r ) BOOST_SP_NOEXCEPT
  429. {
  430. (void) const_cast< T* >( static_cast< U* >( 0 ) );
  431. typedef typename local_shared_ptr<T>::element_type E;
  432. E * p = const_cast< E* >( r.get() );
  433. return local_shared_ptr<T>( r, p );
  434. }
  435. template<class T, class U> local_shared_ptr<T> dynamic_pointer_cast( local_shared_ptr<U> const & r ) BOOST_SP_NOEXCEPT
  436. {
  437. (void) dynamic_cast< T* >( static_cast< U* >( 0 ) );
  438. typedef typename local_shared_ptr<T>::element_type E;
  439. E * p = dynamic_cast< E* >( r.get() );
  440. return p? local_shared_ptr<T>( r, p ): local_shared_ptr<T>();
  441. }
  442. template<class T, class U> local_shared_ptr<T> reinterpret_pointer_cast( local_shared_ptr<U> const & r ) BOOST_SP_NOEXCEPT
  443. {
  444. (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
  445. typedef typename local_shared_ptr<T>::element_type E;
  446. E * p = reinterpret_cast< E* >( r.get() );
  447. return local_shared_ptr<T>( r, p );
  448. }
  449. #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  450. template<class T, class U> local_shared_ptr<T> static_pointer_cast( local_shared_ptr<U> && r ) BOOST_SP_NOEXCEPT
  451. {
  452. (void) static_cast< T* >( static_cast< U* >( 0 ) );
  453. typedef typename local_shared_ptr<T>::element_type E;
  454. E * p = static_cast< E* >( r.get() );
  455. return local_shared_ptr<T>( std::move(r), p );
  456. }
  457. template<class T, class U> local_shared_ptr<T> const_pointer_cast( local_shared_ptr<U> && r ) BOOST_SP_NOEXCEPT
  458. {
  459. (void) const_cast< T* >( static_cast< U* >( 0 ) );
  460. typedef typename local_shared_ptr<T>::element_type E;
  461. E * p = const_cast< E* >( r.get() );
  462. return local_shared_ptr<T>( std::move(r), p );
  463. }
  464. template<class T, class U> local_shared_ptr<T> dynamic_pointer_cast( local_shared_ptr<U> && r ) BOOST_SP_NOEXCEPT
  465. {
  466. (void) dynamic_cast< T* >( static_cast< U* >( 0 ) );
  467. typedef typename local_shared_ptr<T>::element_type E;
  468. E * p = dynamic_cast< E* >( r.get() );
  469. return p? local_shared_ptr<T>( std::move(r), p ): local_shared_ptr<T>();
  470. }
  471. template<class T, class U> local_shared_ptr<T> reinterpret_pointer_cast( local_shared_ptr<U> && r ) BOOST_SP_NOEXCEPT
  472. {
  473. (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
  474. typedef typename local_shared_ptr<T>::element_type E;
  475. E * p = reinterpret_cast< E* >( r.get() );
  476. return local_shared_ptr<T>( std::move(r), p );
  477. }
  478. #endif // !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  479. // get_pointer() enables boost::mem_fn to recognize local_shared_ptr
  480. template<class T> inline typename local_shared_ptr<T>::element_type * get_pointer( local_shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
  481. {
  482. return p.get();
  483. }
  484. // operator<<
  485. #if !defined(BOOST_NO_IOSTREAM)
  486. template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< ( std::basic_ostream<E, T> & os, local_shared_ptr<Y> const & p )
  487. {
  488. os << p.get();
  489. return os;
  490. }
  491. #endif // !defined(BOOST_NO_IOSTREAM)
  492. // get_deleter
  493. template<class D, class T> D * get_deleter( local_shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
  494. {
  495. return get_deleter<D>( shared_ptr<T>( p ) );
  496. }
  497. // hash_value
  498. template< class T > struct hash;
  499. template< class T > std::size_t hash_value( local_shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
  500. {
  501. return boost::hash< typename local_shared_ptr<T>::element_type* >()( p.get() );
  502. }
  503. } // namespace boost
  504. // std::hash
  505. #if !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL)
  506. namespace std
  507. {
  508. template<class T> struct hash< ::boost::local_shared_ptr<T> >
  509. {
  510. std::size_t operator()( ::boost::local_shared_ptr<T> const & p ) const BOOST_SP_NOEXCEPT
  511. {
  512. return std::hash< typename ::boost::local_shared_ptr<T>::element_type* >()( p.get() );
  513. }
  514. };
  515. } // namespace std
  516. #endif // #if !defined(BOOST_NO_CXX11_HDR_FUNCTIONAL)
  517. #endif // #ifndef BOOST_SMART_PTR_LOCAL_SHARED_PTR_HPP_INCLUDED