/* -*- C++ -*- */ // $Id$ // Refcounted_Auto_Ptr.i #include "Synch_T.h" template inline int ACE_Refcounted_Auto_Ptr_Rep::count (void) const { ACE_GUARD_RETURN (ACE_LOCK, guard, ACE_const_cast (ACE_LOCK &, this->lock_), 0); return this->ref_count_; } template inline int ACE_Refcounted_Auto_Ptr::count (void) const { return this->rep_->count (); } template inline int ACE_Refcounted_Auto_Ptr_Rep::null (void) const { ACE_GUARD_RETURN (ACE_LOCK, guard, ACE_const_cast (ACE_LOCK&, this->lock_), 0); return this->ptr_.get() == 0; } template inline int ACE_Refcounted_Auto_Ptr::null (void) const { return this->rep_->null (); } template inline ACE_Refcounted_Auto_Ptr_Rep * ACE_Refcounted_Auto_Ptr_Rep::internal_create (X *p) { ACE_Refcounted_Auto_Ptr_Rep *temp = 0; ACE_NEW_RETURN (temp, (ACE_Refcounted_Auto_Ptr_Rep) (p), 0); return temp; } template inline ACE_Refcounted_Auto_Ptr_Rep * ACE_Refcounted_Auto_Ptr_Rep::create (X *p) { // Yes set ref count to zero. ACE_Refcounted_Auto_Ptr_Rep *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 inline ACE_Refcounted_Auto_Ptr_Rep * ACE_Refcounted_Auto_Ptr_Rep::attach (ACE_Refcounted_Auto_Ptr_Rep*& rep) { ACE_ASSERT (rep != 0); ACE_GUARD_RETURN (ACE_LOCK, guard, rep->lock_, rep); ++rep->ref_count_; return rep; } template inline void ACE_Refcounted_Auto_Ptr_Rep::detach (ACE_Refcounted_Auto_Ptr_Rep*& rep) { ACE_ASSERT (rep != 0); ACE_Refcounted_Auto_Ptr_Rep *rep_del = 0; { ACE_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 inline void ACE_Refcounted_Auto_Ptr_Rep::assign (ACE_Refcounted_Auto_Ptr_Rep*& rep, ACE_Refcounted_Auto_Ptr_Rep* new_rep) { ACE_ASSERT (rep != 0); ACE_ASSERT (new_rep != 0); ACE_Refcounted_Auto_Ptr_Rep *old = 0; { // detached old last for exception safety ACE_GUARD (ACE_LOCK, guard, rep->lock_); old = rep; rep = new_rep; if (old->ref_count_-- > 0) return; } // The lock is released before deleting old rep object below. delete old; } template inline ACE_Refcounted_Auto_Ptr_Rep::ACE_Refcounted_Auto_Ptr_Rep (X *p) : ptr_ (p), ref_count_ (0) { } template inline ACE_Refcounted_Auto_Ptr_Rep::~ACE_Refcounted_Auto_Ptr_Rep (void) { } template inline X * ACE_Refcounted_Auto_Ptr_Rep::release (void) { ACE_GUARD_RETURN (ACE_LOCK, guard, this->lock_, 0); return this->ptr_.release (); } template inline void ACE_Refcounted_Auto_Ptr_Rep::reset (X *p) { ACE_GUARD (ACE_LOCK, guard, this->lock_); this->ptr_.reset (p); } template inline X * ACE_Refcounted_Auto_Ptr_Rep::get (void) { ACE_GUARD_RETURN (ACE_LOCK, guard, this->lock_, 0); return this->ptr_.get (); } template inline ACE_Refcounted_Auto_Ptr::ACE_Refcounted_Auto_Ptr (X *p) : rep_ (AUTO_REFCOUNTED_PTR_REP::create (p)) { } template inline ACE_Refcounted_Auto_Ptr::ACE_Refcounted_Auto_Ptr (const ACE_Refcounted_Auto_Ptr &r) : rep_ (AUTO_REFCOUNTED_PTR_REP::attach (((ACE_Refcounted_Auto_Ptr &) r).rep_)) { } template inline ACE_Refcounted_Auto_Ptr::~ACE_Refcounted_Auto_Ptr (void) { AUTO_REFCOUNTED_PTR_REP::detach (rep_); } template inline int ACE_Refcounted_Auto_Ptr::operator== (const ACE_Refcounted_Auto_Ptr &r) const { return r.rep_ == this->rep_; } template inline int ACE_Refcounted_Auto_Ptr::operator!= (const ACE_Refcounted_Auto_Ptr &r) const { return r.rep_ != this->rep_; } template inline X * ACE_Refcounted_Auto_Ptr::operator-> (void) const { return this->rep_->get(); } template inline X & ACE_Refcounted_Auto_Ptr::operator *() const { return *this->rep_->get (); } template inline X* ACE_Refcounted_Auto_Ptr::get (void) { // We return the ACE_Future_rep. return this->rep_->get (); } template inline X * ACE_Refcounted_Auto_Ptr::release (void) { return this->rep_->release (); } template inline void ACE_Refcounted_Auto_Ptr::reset (X *p) { this->rep_->reset (p); } template inline void ACE_Refcounted_Auto_Ptr::operator = (const ACE_Refcounted_Auto_Ptr &rhs) { // assignment: // // bind to the same as . // This will work if &r == this, by first increasing the ref count ACE_Refcounted_Auto_Ptr &r = (ACE_Refcounted_Auto_Ptr &) rhs; AUTO_REFCOUNTED_PTR_REP::assign (this->rep_, AUTO_REFCOUNTED_PTR_REP::attach (r.rep_)); }