123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- #ifndef __DAHUA_MEMORY_SHAREDPTR_COUNT_H__
- #define __DAHUA_MEMORY_SHAREDPTR_COUNT_H__
- #include <memory> // std::auto_ptr
- #include <functional> // std::less
- #include <new> // std::bad_alloc
- #include <typeinfo> // std::type_info in get_deleter
- #include "sp_counted_impl.hpp"
- #include "checked_delete.hpp"
- namespace Dahua {
- namespace Memory {
- namespace Detail {
- struct sp_nothrow_tag {};
- class weak_count;
- class shared_count
- {
- private:
-
- sp_counted_base * pi_;
- friend class weak_count;
- public:
- shared_count(): pi_(0)
- {
- }
- template<class Y> explicit shared_count( Y * p ): pi_( 0 )
- {
- try
- {
- pi_ = new sp_counted_impl_p<Y>( p );
- }
- catch(...)
- {
- Detail::checked_delete( p );
- throw;
- }
- }
- template<class P, class D> shared_count( P p, D d ): pi_(0)
- {
- try
- {
- pi_ = new sp_counted_impl_pd<P, D>(p, d);
- }
- catch(...)
- {
- d(p); // delete p
- throw;
- }
- }
-
- template<class P, class D, class A> shared_count( P p, D d, A a ): pi_( 0 )
- {
- typedef sp_counted_impl_pda<P, D, A> impl_type;
- typedef typename A::template rebind< impl_type >::other A2;
- A2 a2( a );
- try
- {
- pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );
- new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
- }
- catch(...)
- {
- d( p );
- if( pi_ != 0 )
- {
- a2.deallocate( static_cast< impl_type* >( pi_ ), 1 );
- }
- throw;
- }
- }
- // auto_ptr<Y> is special cased to provide the strong guarantee
- template<class Y>
- explicit shared_count( std::auto_ptr<Y> & r ): pi_( new sp_counted_impl_p<Y>( r.get() ) )
- {
- assert(pi_ != 0);
- r.release();
- }
- ~shared_count() // nothrow
- {
- if( pi_ != 0 ) pi_->release();
- }
- shared_count(shared_count const & r): pi_(r.pi_) // nothrow
- {
- if( pi_ != 0 ) pi_->add_ref_copy();
- }
- explicit shared_count(weak_count const & r); // throws bad_weak_ptr when r.use_count() == 0
- shared_count & operator= (shared_count const & r) // nothrow
- {
- sp_counted_base * tmp = r.pi_;
- if( tmp != pi_ )
- {
- if( tmp != 0 ) tmp->add_ref_copy();
- if( pi_ != 0 ) pi_->release();
- pi_ = tmp;
- }
- return *this;
- }
- void swap(shared_count & r) // nothrow
- {
- sp_counted_base * tmp = r.pi_;
- r.pi_ = pi_;
- pi_ = tmp;
- }
- long use_count() const // nothrow
- {
- return pi_ != 0? pi_->use_count(): 0;
- }
- bool unique() const // nothrow
- {
- return use_count() == 1;
- }
- bool empty() const // nothrow
- {
- return pi_ == 0;
- }
- friend inline bool operator==(shared_count const & a, shared_count const & b)
- {
- return a.pi_ == b.pi_;
- }
- friend inline bool operator<(shared_count const & a, shared_count const & b)
- {
- return std::less<sp_counted_base *>()( a.pi_, b.pi_ );
- }
- void * get_deleter( std::type_info const & ti ) const
- {
- return pi_? pi_->get_deleter( ti ): 0;
- }
- };
- class weak_count
- {
- private:
- sp_counted_base * pi_;
- friend class shared_count;
- public:
- weak_count(): pi_(0) // nothrow
- {
- }
- weak_count(shared_count const & r): pi_(r.pi_) // nothrow
- {
- if(pi_ != 0) pi_->weak_add_ref();
- }
- weak_count(weak_count const & r): pi_(r.pi_) // nothrow
- {
- if(pi_ != 0) pi_->weak_add_ref();
- }
- ~weak_count() // nothrow
- {
- if(pi_ != 0) pi_->weak_release();
- }
- weak_count & operator= (shared_count const & r) // nothrow
- {
- sp_counted_base * tmp = r.pi_;
- if( tmp != pi_ )
- {
- if(tmp != 0) tmp->weak_add_ref();
- if(pi_ != 0) pi_->weak_release();
- pi_ = tmp;
- }
- return *this;
- }
- weak_count & operator= (weak_count const & r) // nothrow
- {
- sp_counted_base * tmp = r.pi_;
- if( tmp != pi_ )
- {
- if(tmp != 0) tmp->weak_add_ref();
- if(pi_ != 0) pi_->weak_release();
- pi_ = tmp;
- }
- return *this;
- }
- void swap(weak_count & r) // nothrow
- {
- sp_counted_base * tmp = r.pi_;
- r.pi_ = pi_;
- pi_ = tmp;
- }
- long use_count() const // nothrow
- {
- return pi_ != 0? pi_->use_count(): 0;
- }
- bool empty() const // nothrow
- {
- return pi_ == 0;
- }
- friend inline bool operator==(weak_count const & a, weak_count const & b)
- {
- return a.pi_ == b.pi_;
- }
- friend inline bool operator<(weak_count const & a, weak_count const & b)
- {
- return std::less<sp_counted_base *>()(a.pi_, b.pi_);
- }
- };
- inline shared_count::shared_count( weak_count const & r ): pi_( r.pi_ )
- {
- if( pi_ != 0 && !pi_->add_ref_lock() )
- {
- pi_ = 0;
- }
- }
- } // namespace Detail
- } // namespace Memory
- } // namespace Dahua
- #endif // __DAHUA_MEMORY_SHAREDPTR_COUNT_H__
|