1 #ifndef BOOST_SMART_PTR_LOCAL_SHARED_PTR_HPP_INCLUDED
2 #define BOOST_SMART_PTR_LOCAL_SHARED_PTR_HPP_INCLUDED
4 // local_shared_ptr.hpp
6 // Copyright 2017 Peter Dimov
8 // Distributed under the Boost Software License, Version 1.0. (See
9 // accompanying file LICENSE_1_0.txt or copy at
10 // http://www.boost.org/LICENSE_1_0.txt)
12 // See http://www.boost.org/libs/smart_ptr/ for documentation.
14 #include <boost/smart_ptr/shared_ptr.hpp>
19 template<class T> class local_shared_ptr;
24 template< class E, class Y > inline void lsp_pointer_construct( boost::local_shared_ptr< E > * /*ppx*/, Y * p, boost::detail::local_counted_base * & pn )
26 boost::detail::sp_assert_convertible< Y, E >();
28 typedef boost::detail::local_sp_deleter< boost::checked_deleter<Y> > D;
30 boost::shared_ptr<E> p2( p, D() );
32 D * pd = static_cast< D * >( p2._internal_get_untyped_deleter() );
34 pd->pn_ = p2._internal_count();
39 template< class E, class Y > inline void lsp_pointer_construct( boost::local_shared_ptr< E[] > * /*ppx*/, Y * p, boost::detail::local_counted_base * & pn )
41 boost::detail::sp_assert_convertible< Y[], E[] >();
43 typedef boost::detail::local_sp_deleter< boost::checked_array_deleter<E> > D;
45 boost::shared_ptr<E[]> p2( p, D() );
47 D * pd = static_cast< D * >( p2._internal_get_untyped_deleter() );
49 pd->pn_ = p2._internal_count();
54 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 )
56 boost::detail::sp_assert_convertible< Y[N], E[N] >();
58 typedef boost::detail::local_sp_deleter< boost::checked_array_deleter<E> > D;
60 boost::shared_ptr<E[N]> p2( p, D() );
62 D * pd = static_cast< D * >( p2._internal_get_untyped_deleter() );
64 pd->pn_ = p2._internal_count();
69 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 )
71 typedef boost::detail::local_sp_deleter<D> D2;
73 boost::shared_ptr<E> p2( p, D2( d ) );
75 D2 * pd = static_cast< D2 * >( p2._internal_get_untyped_deleter() );
77 pd->pn_ = p2._internal_count();
82 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 )
84 typedef boost::detail::local_sp_deleter<D> D2;
86 boost::shared_ptr<E> p2( p, D2( d ), a );
88 D2 * pd = static_cast< D2 * >( p2._internal_get_untyped_deleter() );
90 pd->pn_ = p2._internal_count();
95 struct lsp_internal_constructor_tag
104 // as shared_ptr, but local to a thread.
105 // reference count manipulations are non-atomic.
108 template<class T> class local_shared_ptr
112 typedef local_shared_ptr this_type;
116 typedef typename boost::detail::sp_element<T>::type element_type;
121 boost::detail::local_counted_base * pn;
123 template<class Y> friend class local_shared_ptr;
129 ~local_shared_ptr() BOOST_SP_NOEXCEPT
139 BOOST_CONSTEXPR local_shared_ptr() BOOST_SP_NOEXCEPT : px( 0 ), pn( 0 )
143 #if !defined( BOOST_NO_CXX11_NULLPTR )
145 BOOST_CONSTEXPR local_shared_ptr( boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT : px( 0 ), pn( 0 )
151 // internal constructor, used by make_shared
152 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_ )
157 explicit local_shared_ptr( Y * p ): px( p ), pn( 0 )
159 boost::detail::lsp_pointer_construct( this, p, pn );
162 template<class Y, class D> local_shared_ptr( Y * p, D d ): px( p ), pn( 0 )
164 boost::detail::lsp_deleter_construct( this, p, d, pn );
167 #if !defined( BOOST_NO_CXX11_NULLPTR )
169 template<class D> local_shared_ptr( boost::detail::sp_nullptr_t p, D d ): px( p ), pn( 0 )
171 boost::detail::lsp_deleter_construct( this, p, d, pn );
176 template<class Y, class D, class A> local_shared_ptr( Y * p, D d, A a ): px( p ), pn( 0 )
178 boost::detail::lsp_allocator_construct( this, p, d, a, pn );
181 #if !defined( BOOST_NO_CXX11_NULLPTR )
183 template<class D, class A> local_shared_ptr( boost::detail::sp_nullptr_t p, D d, A a ): px( p ), pn( 0 )
185 boost::detail::lsp_allocator_construct( this, p, d, a, pn );
190 // construction from shared_ptr
192 template<class Y> local_shared_ptr( shared_ptr<Y> const & r,
193 typename boost::detail::sp_enable_if_convertible<Y, T>::type = boost::detail::sp_empty() )
194 : px( r.get() ), pn( 0 )
196 boost::detail::sp_assert_convertible< Y, T >();
198 if( r.use_count() != 0 )
200 pn = new boost::detail::local_counted_impl( r._internal_count() );
204 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
206 template<class Y> local_shared_ptr( shared_ptr<Y> && r,
207 typename boost::detail::sp_enable_if_convertible<Y, T>::type = boost::detail::sp_empty() )
208 : px( r.get() ), pn( 0 )
210 boost::detail::sp_assert_convertible< Y, T >();
212 if( r.use_count() != 0 )
214 pn = new boost::detail::local_counted_impl( r._internal_count() );
221 // construction from unique_ptr
223 #if !defined( BOOST_NO_CXX11_SMART_PTR ) && !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
225 template< class Y, class D >
226 local_shared_ptr( std::unique_ptr< Y, D > && r,
227 typename boost::detail::sp_enable_if_convertible<Y, T>::type = boost::detail::sp_empty() )
228 : px( r.get() ), pn( 0 )
230 boost::detail::sp_assert_convertible< Y, T >();
234 pn = new boost::detail::local_counted_impl( shared_ptr<T>( std::move(r) )._internal_count() );
240 template< class Y, class D >
241 local_shared_ptr( boost::movelib::unique_ptr< Y, D > r ); // !
242 // : px( r.get() ), pn( new boost::detail::local_counted_impl( shared_ptr<T>( std::move(r) ) ) )
244 // boost::detail::sp_assert_convertible< Y, T >();
249 local_shared_ptr( local_shared_ptr const & r ) BOOST_SP_NOEXCEPT : px( r.px ), pn( r.pn )
259 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
261 local_shared_ptr( local_shared_ptr && r ) BOOST_SP_NOEXCEPT : px( r.px ), pn( r.pn )
269 // converting copy constructor
271 template<class Y> local_shared_ptr( local_shared_ptr<Y> const & r,
272 typename boost::detail::sp_enable_if_convertible<Y, T>::type = boost::detail::sp_empty() ) BOOST_SP_NOEXCEPT
273 : px( r.px ), pn( r.pn )
275 boost::detail::sp_assert_convertible< Y, T >();
283 // converting move constructor
285 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
287 template<class Y> local_shared_ptr( local_shared_ptr<Y> && r,
288 typename boost::detail::sp_enable_if_convertible<Y, T>::type = boost::detail::sp_empty() ) BOOST_SP_NOEXCEPT
289 : px( r.px ), pn( r.pn )
291 boost::detail::sp_assert_convertible< Y, T >();
302 local_shared_ptr( local_shared_ptr<Y> const & r, element_type * p ) BOOST_SP_NOEXCEPT : px( p ), pn( r.pn )
310 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
313 local_shared_ptr( local_shared_ptr<Y> && r, element_type * p ) BOOST_SP_NOEXCEPT : px( p ), pn( r.pn )
323 local_shared_ptr & operator=( local_shared_ptr const & r ) BOOST_SP_NOEXCEPT
325 local_shared_ptr( r ).swap( *this );
329 template<class Y> local_shared_ptr & operator=( local_shared_ptr<Y> const & r ) BOOST_SP_NOEXCEPT
331 local_shared_ptr( r ).swap( *this );
335 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
337 local_shared_ptr & operator=( local_shared_ptr && r ) BOOST_SP_NOEXCEPT
339 local_shared_ptr( std::move( r ) ).swap( *this );
344 local_shared_ptr & operator=( local_shared_ptr<Y> && r ) BOOST_SP_NOEXCEPT
346 local_shared_ptr( std::move( r ) ).swap( *this );
352 #if !defined( BOOST_NO_CXX11_NULLPTR )
354 local_shared_ptr & operator=( boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT
356 local_shared_ptr().swap(*this);
362 #if !defined( BOOST_NO_CXX11_SMART_PTR ) && !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
364 template<class Y, class D>
365 local_shared_ptr & operator=( std::unique_ptr<Y, D> && r )
367 local_shared_ptr( std::move(r) ).swap( *this );
373 template<class Y, class D>
374 local_shared_ptr & operator=( boost::movelib::unique_ptr<Y, D> r ); // !
378 void reset() BOOST_SP_NOEXCEPT
380 local_shared_ptr().swap( *this );
383 template<class Y> void reset( Y * p ) // Y must be complete
385 local_shared_ptr( p ).swap( *this );
388 template<class Y, class D> void reset( Y * p, D d )
390 local_shared_ptr( p, d ).swap( *this );
393 template<class Y, class D, class A> void reset( Y * p, D d, A a )
395 local_shared_ptr( p, d, a ).swap( *this );
398 template<class Y> void reset( local_shared_ptr<Y> const & r, element_type * p ) BOOST_SP_NOEXCEPT
400 local_shared_ptr( r, p ).swap( *this );
403 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
405 template<class Y> void reset( local_shared_ptr<Y> && r, element_type * p ) BOOST_SP_NOEXCEPT
407 local_shared_ptr( std::move( r ), p ).swap( *this );
414 typename boost::detail::sp_dereference< T >::type operator* () const BOOST_SP_NOEXCEPT
419 typename boost::detail::sp_member_access< T >::type operator-> () const BOOST_SP_NOEXCEPT
424 typename boost::detail::sp_array_access< T >::type operator[] ( std::ptrdiff_t i ) const BOOST_SP_NOEXCEPT_WITH_ASSERT
426 BOOST_ASSERT( px != 0 );
427 BOOST_ASSERT( i >= 0 && ( i < boost::detail::sp_extent< T >::value || boost::detail::sp_extent< T >::value == 0 ) );
429 return static_cast< typename boost::detail::sp_array_access< T >::type >( px[ i ] );
432 element_type * get() const BOOST_SP_NOEXCEPT
437 // implicit conversion to "bool"
438 #include <boost/smart_ptr/detail/operator_bool.hpp>
440 long local_use_count() const BOOST_SP_NOEXCEPT
442 return pn? pn->local_use_count(): 0;
445 // conversions to shared_ptr, weak_ptr
447 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE ) && !defined(BOOST_NO_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGS)
448 template<class Y, class E = typename boost::detail::sp_enable_if_convertible<T,Y>::type> operator shared_ptr<Y>() const BOOST_SP_NOEXCEPT
450 template<class Y> operator shared_ptr<Y>() const BOOST_SP_NOEXCEPT
453 boost::detail::sp_assert_convertible<T, Y>();
457 return shared_ptr<Y>( boost::detail::sp_internal_constructor_tag(), px, pn->local_cb_get_shared_count() );
461 return shared_ptr<Y>();
465 #if !defined( BOOST_SP_NO_SP_CONVERTIBLE ) && !defined(BOOST_NO_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGS)
466 template<class Y, class E = typename boost::detail::sp_enable_if_convertible<T,Y>::type> operator weak_ptr<Y>() const BOOST_SP_NOEXCEPT
468 template<class Y> operator weak_ptr<Y>() const BOOST_SP_NOEXCEPT
471 boost::detail::sp_assert_convertible<T, Y>();
475 return shared_ptr<Y>( boost::detail::sp_internal_constructor_tag(), px, pn->local_cb_get_shared_count() );
479 return weak_ptr<Y>();
485 void swap( local_shared_ptr & r ) BOOST_SP_NOEXCEPT
487 std::swap( px, r.px );
488 std::swap( pn, r.pn );
493 template<class Y> bool owner_before( local_shared_ptr<Y> const & r ) const BOOST_SP_NOEXCEPT
495 return std::less< boost::detail::local_counted_base* >()( pn, r.pn );
499 template<class T, class U> inline bool operator==( local_shared_ptr<T> const & a, local_shared_ptr<U> const & b ) BOOST_SP_NOEXCEPT
501 return a.get() == b.get();
504 template<class T, class U> inline bool operator!=( local_shared_ptr<T> const & a, local_shared_ptr<U> const & b ) BOOST_SP_NOEXCEPT
506 return a.get() != b.get();
509 #if !defined( BOOST_NO_CXX11_NULLPTR )
511 template<class T> inline bool operator==( local_shared_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT
516 template<class T> inline bool operator==( boost::detail::sp_nullptr_t, local_shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
521 template<class T> inline bool operator!=( local_shared_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT
526 template<class T> inline bool operator!=( boost::detail::sp_nullptr_t, local_shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
533 template<class T, class U> inline bool operator==( local_shared_ptr<T> const & a, shared_ptr<U> const & b ) BOOST_SP_NOEXCEPT
535 return a.get() == b.get();
538 template<class T, class U> inline bool operator!=( local_shared_ptr<T> const & a, shared_ptr<U> const & b ) BOOST_SP_NOEXCEPT
540 return a.get() != b.get();
543 template<class T, class U> inline bool operator==( shared_ptr<T> const & a, local_shared_ptr<U> const & b ) BOOST_SP_NOEXCEPT
545 return a.get() == b.get();
548 template<class T, class U> inline bool operator!=( shared_ptr<T> const & a, local_shared_ptr<U> const & b ) BOOST_SP_NOEXCEPT
550 return a.get() != b.get();
553 template<class T, class U> inline bool operator<(local_shared_ptr<T> const & a, local_shared_ptr<U> const & b) BOOST_SP_NOEXCEPT
555 return a.owner_before( b );
558 template<class T> inline void swap( local_shared_ptr<T> & a, local_shared_ptr<T> & b ) BOOST_SP_NOEXCEPT
563 template<class T, class U> local_shared_ptr<T> static_pointer_cast( local_shared_ptr<U> const & r ) BOOST_SP_NOEXCEPT
565 (void) static_cast< T* >( static_cast< U* >( 0 ) );
567 typedef typename local_shared_ptr<T>::element_type E;
569 E * p = static_cast< E* >( r.get() );
570 return local_shared_ptr<T>( r, p );
573 template<class T, class U> local_shared_ptr<T> const_pointer_cast( local_shared_ptr<U> const & r ) BOOST_SP_NOEXCEPT
575 (void) const_cast< T* >( static_cast< U* >( 0 ) );
577 typedef typename local_shared_ptr<T>::element_type E;
579 E * p = const_cast< E* >( r.get() );
580 return local_shared_ptr<T>( r, p );
583 template<class T, class U> local_shared_ptr<T> dynamic_pointer_cast( local_shared_ptr<U> const & r ) BOOST_SP_NOEXCEPT
585 (void) dynamic_cast< T* >( static_cast< U* >( 0 ) );
587 typedef typename local_shared_ptr<T>::element_type E;
589 E * p = dynamic_cast< E* >( r.get() );
590 return p? local_shared_ptr<T>( r, p ): local_shared_ptr<T>();
593 template<class T, class U> local_shared_ptr<T> reinterpret_pointer_cast( local_shared_ptr<U> const & r ) BOOST_SP_NOEXCEPT
595 (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
597 typedef typename local_shared_ptr<T>::element_type E;
599 E * p = reinterpret_cast< E* >( r.get() );
600 return local_shared_ptr<T>( r, p );
603 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
605 template<class T, class U> local_shared_ptr<T> static_pointer_cast( local_shared_ptr<U> && r ) BOOST_SP_NOEXCEPT
607 (void) static_cast< T* >( static_cast< U* >( 0 ) );
609 typedef typename local_shared_ptr<T>::element_type E;
611 E * p = static_cast< E* >( r.get() );
612 return local_shared_ptr<T>( std::move(r), p );
615 template<class T, class U> local_shared_ptr<T> const_pointer_cast( local_shared_ptr<U> && r ) BOOST_SP_NOEXCEPT
617 (void) const_cast< T* >( static_cast< U* >( 0 ) );
619 typedef typename local_shared_ptr<T>::element_type E;
621 E * p = const_cast< E* >( r.get() );
622 return local_shared_ptr<T>( std::move(r), p );
625 template<class T, class U> local_shared_ptr<T> dynamic_pointer_cast( local_shared_ptr<U> && r ) BOOST_SP_NOEXCEPT
627 (void) dynamic_cast< T* >( static_cast< U* >( 0 ) );
629 typedef typename local_shared_ptr<T>::element_type E;
631 E * p = dynamic_cast< E* >( r.get() );
632 return p? local_shared_ptr<T>( std::move(r), p ): local_shared_ptr<T>();
635 template<class T, class U> local_shared_ptr<T> reinterpret_pointer_cast( local_shared_ptr<U> && r ) BOOST_SP_NOEXCEPT
637 (void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
639 typedef typename local_shared_ptr<T>::element_type E;
641 E * p = reinterpret_cast< E* >( r.get() );
642 return local_shared_ptr<T>( std::move(r), p );
645 #endif // !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
647 // get_pointer() enables boost::mem_fn to recognize local_shared_ptr
649 template<class T> inline typename local_shared_ptr<T>::element_type * get_pointer( local_shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
656 #if !defined(BOOST_NO_IOSTREAM)
658 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 )
664 #endif // !defined(BOOST_NO_IOSTREAM)
668 template<class D, class T> D * get_deleter( local_shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
670 return get_deleter<D>( shared_ptr<T>( p ) );
675 template< class T > struct hash;
677 template< class T > std::size_t hash_value( local_shared_ptr<T> const & p ) BOOST_SP_NOEXCEPT
679 return boost::hash< typename local_shared_ptr<T>::element_type* >()( p.get() );
684 #endif // #ifndef BOOST_SMART_PTR_LOCAL_SHARED_PTR_HPP_INCLUDED