local_shared_ptr.hpp 17 KB

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