diff options
author | William R. Otte <wotte@dre.vanderbilt.edu> | 2006-07-24 15:50:30 +0000 |
---|---|---|
committer | William R. Otte <wotte@dre.vanderbilt.edu> | 2006-07-24 15:50:30 +0000 |
commit | c44379cc7d9c7aa113989237ab0f56db12aa5219 (patch) | |
tree | 66a84b20d47f2269d8bdc6e0323f338763424d3a /ACE/ace/Refcounted_Auto_Ptr.inl | |
parent | 3aff90f4a822fcf5d902bbfbcc9fa931d6191a8c (diff) | |
download | ATCD-c44379cc7d9c7aa113989237ab0f56db12aa5219.tar.gz |
Repo restructuring
Diffstat (limited to 'ACE/ace/Refcounted_Auto_Ptr.inl')
-rw-r--r-- | ACE/ace/Refcounted_Auto_Ptr.inl | 191 |
1 files changed, 191 insertions, 0 deletions
diff --git a/ACE/ace/Refcounted_Auto_Ptr.inl b/ACE/ace/Refcounted_Auto_Ptr.inl new file mode 100644 index 00000000000..aa4e3914638 --- /dev/null +++ b/ACE/ace/Refcounted_Auto_Ptr.inl @@ -0,0 +1,191 @@ +// -*- C++ -*- +// +// $Id$ + +#include "ace/Guard_T.h" +#include "ace/Log_Msg.h" + +ACE_BEGIN_VERSIONED_NAMESPACE_DECL + +template <class X, class ACE_LOCK> inline int +ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::count (void) const +{ + ACE_READ_GUARD_RETURN (ACE_LOCK, guard, this->lock_, 0); + return this->ref_count_; +} + +template <class X, class ACE_LOCK> inline int +ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::count (void) const +{ + return this->rep_->count (); +} + +template <class X, class ACE_LOCK> inline bool +ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::null (void) const +{ + return (this->rep_ == 0 || this->rep_->get () == 0); +} + +template <class X, class ACE_LOCK> inline ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> * +ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::internal_create (X *p) +{ + ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *temp = 0; + ACE_NEW_RETURN (temp, + (ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>) (p), + 0); + return temp; +} + +template <class X, class ACE_LOCK> inline ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> * +ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::create (X *p) +{ + // Yes set ref count to zero. + ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *temp = internal_create (p); +#if defined (ACE_NEW_THROWS_EXCEPTIONS) + if (temp == 0) + ACE_throw_bad_alloc; +#else + ACE_ASSERT (temp != 0); +#endif /* ACE_NEW_THROWS_EXCEPTIONS */ + return temp; +} + +template <class X, class ACE_LOCK> inline ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> * +ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::attach (ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>*& rep) +{ + if (rep == 0) + return 0; + + ACE_WRITE_GUARD_RETURN (ACE_LOCK, guard, rep->lock_, 0); + + ++rep->ref_count_; + + return rep; +} + +template <class X, class ACE_LOCK> inline void +ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::detach (ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>*& rep) +{ + if (rep == 0) + return; + + ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *rep_del = 0; + { + ACE_WRITE_GUARD (ACE_LOCK, guard, rep->lock_); + + if (rep->ref_count_-- == 0) + // Since rep contains the lock held by the ACE_Guard, the guard + // needs to be released before freeing the memory holding the + // lock. So save the pointer to free, then release, then free. + rep_del = rep; + } // Release the lock + if (0 != rep_del) + delete rep; +} + +template <class X, class ACE_LOCK> inline +ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::ACE_Refcounted_Auto_Ptr_Rep (X *p) + : ptr_ (p), + ref_count_ (0) +{ +} + +template <class X, class ACE_LOCK> inline +ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::~ACE_Refcounted_Auto_Ptr_Rep (void) +{ +} + +template <class X, class ACE_LOCK> inline X * +ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::get (void) const +{ + return this->ptr_.get (); +} + +template <class X, class ACE_LOCK> inline +ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::ACE_Refcounted_Auto_Ptr (X *p) + : rep_ (AUTO_REFCOUNTED_PTR_REP::create (p)) +{ +} + +template <class X, class ACE_LOCK> inline +ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::ACE_Refcounted_Auto_Ptr (const ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &r) + : rep_ (AUTO_REFCOUNTED_PTR_REP::attach (((ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &) r).rep_)) +{ +} + +template <class X, class ACE_LOCK> inline bool +ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::operator== (const ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &r) const +{ + return r.rep_ == this->rep_; +} + +template <class X, class ACE_LOCK> inline bool +ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::operator!= (const ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &r) const +{ + return r.rep_ != this->rep_; +} + +template <class X, class ACE_LOCK> inline X * +ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::operator-> (void) const +{ + return this->rep_->get(); +} + +template<class X, class ACE_LOCK> inline X & +ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::operator *() const +{ + return *this->rep_->get (); +} + +template <class X, class ACE_LOCK> inline X* +ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::get (void) const +{ + // We return the ACE_Future_rep. + return this->rep_->get (); +} + +template<class X, class ACE_LOCK> inline X * +ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::release (void) +{ + X *p = this->get (); + AUTO_REFCOUNTED_PTR_REP::detach (this->rep_); + this->rep_ = 0; + return p; +} + +template<class X, class ACE_LOCK> inline void +ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::reset (X *p) +{ + // Avoid deleting the underlying auto_ptr if assigning the same actual + // pointer value. + if (this->get () == p) + return; + + AUTO_REFCOUNTED_PTR_REP *old_rep = this->rep_; + if ((this->rep_ = AUTO_REFCOUNTED_PTR_REP::create (p)) != 0) + AUTO_REFCOUNTED_PTR_REP::detach (old_rep); + else + this->rep_ = old_rep; + return; +} + +template <class X, class ACE_LOCK> inline void +ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::operator = (const ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &rhs) +{ + // bind <this> to the same <ACE_Refcounted_Auto_Ptr_Rep> as <r>. + AUTO_REFCOUNTED_PTR_REP *old_rep = this->rep_; + if (rhs.rep_ != 0) + { + this->rep_ = AUTO_REFCOUNTED_PTR_REP::attach + (const_cast<ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>& > (rhs).rep_); + if (this->rep_ != 0) + AUTO_REFCOUNTED_PTR_REP::detach (old_rep); + } + else // Assign a 0 rep to this + { + AUTO_REFCOUNTED_PTR_REP::detach (old_rep); + this->rep_ = 0; + } +} + +ACE_END_VERSIONED_NAMESPACE_DECL |