1 #ifndef BOOST_SMART_PTR_DETAIL_SHARED_COUNT_HPP_INCLUDED
2 #define BOOST_SMART_PTR_DETAIL_SHARED_COUNT_HPP_INCLUDED
4 // MS compatible compilers support #pragma once
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
11 // detail/shared_count.hpp
13 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
14 // Copyright 2004-2005 Peter Dimov
16 // Distributed under the Boost Software License, Version 1.0. (See
17 // accompanying file LICENSE_1_0.txt or copy at
18 // http://www.boost.org/LICENSE_1_0.txt)
21 #if defined(__BORLANDC__) && !defined(__clang__)
22 # pragma warn -8027 // Functions containing try are not expanded inline
25 #include <boost/smart_ptr/bad_weak_ptr.hpp>
26 #include <boost/smart_ptr/detail/sp_counted_base.hpp>
27 #include <boost/smart_ptr/detail/sp_counted_impl.hpp>
28 #include <boost/smart_ptr/detail/sp_disable_deprecated.hpp>
29 #include <boost/smart_ptr/detail/sp_noexcept.hpp>
30 #include <boost/checked_delete.hpp>
31 #include <boost/throw_exception.hpp>
32 #include <boost/core/addressof.hpp>
33 #include <boost/config.hpp>
34 #include <boost/config/workaround.hpp>
35 #include <boost/cstdint.hpp>
36 #include <memory> // std::auto_ptr
37 #include <functional> // std::less
38 #include <cstddef> // std::size_t
40 #ifdef BOOST_NO_EXCEPTIONS
41 # include <new> // std::bad_alloc
44 #if defined( BOOST_SP_DISABLE_DEPRECATED )
45 #pragma GCC diagnostic push
46 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
55 template< class T, class D > class unique_ptr;
57 } // namespace movelib
62 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
64 int const shared_count_id = 0x2C35F101;
65 int const weak_count_id = 0x298C38A4;
69 struct sp_nothrow_tag {};
71 template< class D > struct sp_inplace_tag
75 template< class T > class sp_reference_wrapper
79 explicit sp_reference_wrapper( T & t): t_( boost::addressof( t ) )
83 template< class Y > void operator()( Y * p ) const
93 template< class D > struct sp_convert_reference
98 template< class D > struct sp_convert_reference< D& >
100 typedef sp_reference_wrapper< D > type;
103 template<class T> std::size_t sp_hash_pointer( T* p ) BOOST_NOEXCEPT
105 boost::uintptr_t v = reinterpret_cast<boost::uintptr_t>( p );
107 // match boost::hash<T*>
108 return static_cast<std::size_t>( v + ( v >> 3 ) );
117 sp_counted_base * pi_;
119 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
123 friend class weak_count;
127 BOOST_CONSTEXPR shared_count() BOOST_SP_NOEXCEPT: pi_(0)
128 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
129 , id_(shared_count_id)
134 BOOST_CONSTEXPR explicit shared_count( sp_counted_base * pi ) BOOST_SP_NOEXCEPT: pi_( pi )
135 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
136 , id_(shared_count_id)
141 template<class Y> explicit shared_count( Y * p ): pi_( 0 )
142 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
143 , id_(shared_count_id)
146 #ifndef BOOST_NO_EXCEPTIONS
150 pi_ = new sp_counted_impl_p<Y>( p );
154 boost::checked_delete( p );
160 pi_ = new sp_counted_impl_p<Y>( p );
164 boost::checked_delete( p );
165 boost::throw_exception( std::bad_alloc() );
171 #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, <= 1200 )
172 template<class Y, class D> shared_count( Y * p, D d ): pi_(0)
174 template<class P, class D> shared_count( P p, D d ): pi_(0)
176 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
177 , id_(shared_count_id)
180 #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, <= 1200 )
183 #ifndef BOOST_NO_EXCEPTIONS
187 pi_ = new sp_counted_impl_pd<P, D>(p, d);
197 pi_ = new sp_counted_impl_pd<P, D>(p, d);
202 boost::throw_exception(std::bad_alloc());
208 #if !defined( BOOST_NO_FUNCTION_TEMPLATE_ORDERING )
210 template< class P, class D > shared_count( P p, sp_inplace_tag<D> ): pi_( 0 )
211 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
212 , id_(shared_count_id)
215 #ifndef BOOST_NO_EXCEPTIONS
219 pi_ = new sp_counted_impl_pd< P, D >( p );
223 D::operator_fn( p ); // delete p
229 pi_ = new sp_counted_impl_pd< P, D >( p );
233 D::operator_fn( p ); // delete p
234 boost::throw_exception( std::bad_alloc() );
237 #endif // #ifndef BOOST_NO_EXCEPTIONS
240 #endif // !defined( BOOST_NO_FUNCTION_TEMPLATE_ORDERING )
242 template<class P, class D, class A> shared_count( P p, D d, A a ): pi_( 0 )
243 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
244 , id_(shared_count_id)
247 typedef sp_counted_impl_pda<P, D, A> impl_type;
249 #if !defined( BOOST_NO_CXX11_ALLOCATOR )
251 typedef typename std::allocator_traits<A>::template rebind_alloc< impl_type > A2;
255 typedef typename A::template rebind< impl_type >::other A2;
261 #ifndef BOOST_NO_EXCEPTIONS
265 pi_ = a2.allocate( 1 );
266 ::new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
274 a2.deallocate( static_cast< impl_type* >( pi_ ), 1 );
282 pi_ = a2.allocate( 1 );
286 ::new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
291 boost::throw_exception( std::bad_alloc() );
297 #if !defined( BOOST_NO_FUNCTION_TEMPLATE_ORDERING )
299 template< class P, class D, class A > shared_count( P p, sp_inplace_tag< D >, A a ): pi_( 0 )
300 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
301 , id_(shared_count_id)
304 typedef sp_counted_impl_pda< P, D, A > impl_type;
306 #if !defined( BOOST_NO_CXX11_ALLOCATOR )
308 typedef typename std::allocator_traits<A>::template rebind_alloc< impl_type > A2;
312 typedef typename A::template rebind< impl_type >::other A2;
318 #ifndef BOOST_NO_EXCEPTIONS
322 pi_ = a2.allocate( 1 );
323 ::new( static_cast< void* >( pi_ ) ) impl_type( p, a );
331 a2.deallocate( static_cast< impl_type* >( pi_ ), 1 );
339 pi_ = a2.allocate( 1 );
343 ::new( static_cast< void* >( pi_ ) ) impl_type( p, a );
348 boost::throw_exception( std::bad_alloc() );
351 #endif // #ifndef BOOST_NO_EXCEPTIONS
354 #endif // !defined( BOOST_NO_FUNCTION_TEMPLATE_ORDERING )
356 #ifndef BOOST_NO_AUTO_PTR
358 // auto_ptr<Y> is special cased to provide the strong guarantee
361 explicit shared_count( std::auto_ptr<Y> & r ): pi_( new sp_counted_impl_p<Y>( r.get() ) )
362 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
363 , id_(shared_count_id)
366 #ifdef BOOST_NO_EXCEPTIONS
370 boost::throw_exception(std::bad_alloc());
380 #if !defined( BOOST_NO_CXX11_SMART_PTR )
382 template<class Y, class D>
383 explicit shared_count( std::unique_ptr<Y, D> & r ): pi_( 0 )
384 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
385 , id_(shared_count_id)
388 typedef typename sp_convert_reference<D>::type D2;
390 D2 d2( static_cast<D&&>( r.get_deleter() ) );
391 pi_ = new sp_counted_impl_pd< typename std::unique_ptr<Y, D>::pointer, D2 >( r.get(), d2 );
393 #ifdef BOOST_NO_EXCEPTIONS
397 boost::throw_exception( std::bad_alloc() );
407 template<class Y, class D>
408 explicit shared_count( boost::movelib::unique_ptr<Y, D> & r ): pi_( 0 )
409 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
410 , id_(shared_count_id)
413 typedef typename sp_convert_reference<D>::type D2;
415 D2 d2( r.get_deleter() );
416 pi_ = new sp_counted_impl_pd< typename boost::movelib::unique_ptr<Y, D>::pointer, D2 >( r.get(), d2 );
418 #ifdef BOOST_NO_EXCEPTIONS
422 boost::throw_exception( std::bad_alloc() );
430 ~shared_count() /*BOOST_SP_NOEXCEPT*/
432 if( pi_ != 0 ) pi_->release();
433 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
438 shared_count(shared_count const & r) BOOST_SP_NOEXCEPT: pi_(r.pi_)
439 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
440 , id_(shared_count_id)
443 if( pi_ != 0 ) pi_->add_ref_copy();
446 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
448 shared_count(shared_count && r) BOOST_SP_NOEXCEPT: pi_(r.pi_)
449 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
450 , id_(shared_count_id)
458 explicit shared_count(weak_count const & r); // throws bad_weak_ptr when r.use_count() == 0
459 shared_count( weak_count const & r, sp_nothrow_tag ) BOOST_SP_NOEXCEPT; // constructs an empty *this when r.use_count() == 0
461 shared_count & operator= (shared_count const & r) BOOST_SP_NOEXCEPT
463 sp_counted_base * tmp = r.pi_;
467 if( tmp != 0 ) tmp->add_ref_copy();
468 if( pi_ != 0 ) pi_->release();
475 void swap(shared_count & r) BOOST_SP_NOEXCEPT
477 sp_counted_base * tmp = r.pi_;
482 long use_count() const BOOST_SP_NOEXCEPT
484 return pi_ != 0? pi_->use_count(): 0;
487 bool unique() const BOOST_SP_NOEXCEPT
489 return use_count() == 1;
492 bool empty() const BOOST_SP_NOEXCEPT
497 bool operator==( shared_count const & r ) const BOOST_SP_NOEXCEPT
502 bool operator==( weak_count const & r ) const BOOST_SP_NOEXCEPT;
504 bool operator<( shared_count const & r ) const BOOST_SP_NOEXCEPT
506 return std::less<sp_counted_base *>()( pi_, r.pi_ );
509 bool operator<( weak_count const & r ) const BOOST_SP_NOEXCEPT;
511 void * get_deleter( sp_typeinfo_ const & ti ) const BOOST_SP_NOEXCEPT
513 return pi_? pi_->get_deleter( ti ): 0;
516 void * get_local_deleter( sp_typeinfo_ const & ti ) const BOOST_SP_NOEXCEPT
518 return pi_? pi_->get_local_deleter( ti ): 0;
521 void * get_untyped_deleter() const BOOST_SP_NOEXCEPT
523 return pi_? pi_->get_untyped_deleter(): 0;
526 std::size_t hash_value() const BOOST_SP_NOEXCEPT
528 return sp_hash_pointer( pi_ );
537 sp_counted_base * pi_;
539 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
543 friend class shared_count;
547 BOOST_CONSTEXPR weak_count() BOOST_SP_NOEXCEPT: pi_(0)
548 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
554 weak_count(shared_count const & r) BOOST_SP_NOEXCEPT: pi_(r.pi_)
555 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
559 if(pi_ != 0) pi_->weak_add_ref();
562 weak_count(weak_count const & r) BOOST_SP_NOEXCEPT: pi_(r.pi_)
563 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
567 if(pi_ != 0) pi_->weak_add_ref();
572 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
574 weak_count(weak_count && r) BOOST_SP_NOEXCEPT: pi_(r.pi_)
575 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
584 ~weak_count() /*BOOST_SP_NOEXCEPT*/
586 if(pi_ != 0) pi_->weak_release();
587 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
592 weak_count & operator= (shared_count const & r) BOOST_SP_NOEXCEPT
594 sp_counted_base * tmp = r.pi_;
598 if(tmp != 0) tmp->weak_add_ref();
599 if(pi_ != 0) pi_->weak_release();
606 weak_count & operator= (weak_count const & r) BOOST_SP_NOEXCEPT
608 sp_counted_base * tmp = r.pi_;
612 if(tmp != 0) tmp->weak_add_ref();
613 if(pi_ != 0) pi_->weak_release();
620 void swap(weak_count & r) BOOST_SP_NOEXCEPT
622 sp_counted_base * tmp = r.pi_;
627 long use_count() const BOOST_SP_NOEXCEPT
629 return pi_ != 0? pi_->use_count(): 0;
632 bool empty() const BOOST_SP_NOEXCEPT
637 bool operator==( weak_count const & r ) const BOOST_SP_NOEXCEPT
642 bool operator==( shared_count const & r ) const BOOST_SP_NOEXCEPT
647 bool operator<( weak_count const & r ) const BOOST_SP_NOEXCEPT
649 return std::less<sp_counted_base *>()( pi_, r.pi_ );
652 bool operator<( shared_count const & r ) const BOOST_SP_NOEXCEPT
654 return std::less<sp_counted_base *>()( pi_, r.pi_ );
657 std::size_t hash_value() const BOOST_SP_NOEXCEPT
659 return sp_hash_pointer( pi_ );
663 inline shared_count::shared_count( weak_count const & r ): pi_( r.pi_ )
664 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
665 , id_(shared_count_id)
668 if( pi_ == 0 || !pi_->add_ref_lock() )
670 boost::throw_exception( boost::bad_weak_ptr() );
674 inline shared_count::shared_count( weak_count const & r, sp_nothrow_tag ) BOOST_SP_NOEXCEPT: pi_( r.pi_ )
675 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
676 , id_(shared_count_id)
679 if( pi_ != 0 && !pi_->add_ref_lock() )
685 inline bool shared_count::operator==( weak_count const & r ) const BOOST_SP_NOEXCEPT
690 inline bool shared_count::operator<( weak_count const & r ) const BOOST_SP_NOEXCEPT
692 return std::less<sp_counted_base *>()( pi_, r.pi_ );
695 } // namespace detail
699 #if defined( BOOST_SP_DISABLE_DEPRECATED )
700 #pragma GCC diagnostic pop
703 #if defined(__BORLANDC__) && !defined(__clang__)
704 # pragma warn .8027 // Functions containing try are not expanded inline
707 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SHARED_COUNT_HPP_INCLUDED