Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Refcounted_Auto_Ptr.i

Go to the documentation of this file.
00001 /* -*- C++ -*- */
00002 // $Id: Refcounted_Auto_Ptr.i,v 1.1.1.2 2003/02/21 18:36:32 chad Exp $
00003 
00004 // Refcounted_Auto_Ptr.i
00005 
00006 #include "Synch_T.h"
00007 #include "ace/Log_Msg.h"
00008 
00009 template <class X, class ACE_LOCK> inline int
00010 ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::count (void) const
00011 {
00012   ACE_GUARD_RETURN (ACE_LOCK, guard, ACE_const_cast (ACE_LOCK &, this->lock_), 0);
00013   return this->ref_count_;
00014 }
00015 
00016 template <class X, class ACE_LOCK> inline int
00017 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::count (void) const
00018 {
00019   return this->rep_->count ();
00020 }
00021 
00022 template <class X, class ACE_LOCK> inline int
00023 ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::null (void) const
00024 {
00025   ACE_GUARD_RETURN (ACE_LOCK, guard,
00026                     ACE_const_cast (ACE_LOCK&, this->lock_), 0);
00027 
00028   return this->ptr_.get() == 0;
00029 }
00030 
00031 template <class X, class ACE_LOCK> inline int
00032 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::null (void) const
00033 {
00034   return this->rep_->null ();
00035 }
00036 
00037 template <class X, class ACE_LOCK> inline ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *
00038 ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::internal_create (X *p)
00039 {
00040   ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *temp = 0;
00041   ACE_NEW_RETURN (temp,
00042                   (ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>) (p),
00043                   0);
00044   return temp;
00045 }
00046 
00047 template <class X, class ACE_LOCK> inline ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *
00048 ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::create (X *p)
00049 {
00050   // Yes set ref count to zero.
00051   ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *temp = internal_create (p);
00052 #if defined (ACE_NEW_THROWS_EXCEPTIONS)
00053   if (temp == 0)
00054     ACE_throw_bad_alloc;
00055 #else
00056   ACE_ASSERT (temp != 0);
00057 #endif /* ACE_NEW_THROWS_EXCEPTIONS */
00058    return temp;
00059 }
00060 
00061 template <class X, class ACE_LOCK> inline ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *
00062 ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::attach (ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>*& rep)
00063 {
00064   ACE_ASSERT (rep != 0);
00065 
00066   ACE_GUARD_RETURN (ACE_LOCK, guard, rep->lock_, rep);
00067 
00068   ++rep->ref_count_;
00069 
00070   return rep;
00071 }
00072 
00073 template <class X, class ACE_LOCK> inline void
00074 ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::detach (ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>*& rep)
00075 {
00076   ACE_ASSERT (rep != 0);
00077   ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *rep_del = 0;
00078 
00079   {
00080     ACE_GUARD (ACE_LOCK, guard, rep->lock_);
00081 
00082     if (rep->ref_count_-- == 0)
00083       // Since rep contains the lock held by the ACE_Guard, the guard
00084       // needs to be released before freeing the memory holding the
00085       // lock. So save the pointer to free, then release, then free.
00086       rep_del = rep;
00087   }  // Release the lock
00088   if (0 != rep_del)
00089     delete rep;
00090 }
00091 
00092 template <class X, class ACE_LOCK> inline void
00093 ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::assign (ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>*& rep,
00094                                                   ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>* new_rep)
00095 {
00096   ACE_ASSERT (rep != 0);
00097   ACE_ASSERT (new_rep != 0);
00098 
00099   ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *old = 0;
00100   {
00101     // detached old last for exception safety
00102     ACE_GUARD (ACE_LOCK, guard, rep->lock_);
00103     old = rep;
00104     rep = new_rep;
00105  
00106     if (old->ref_count_-- > 0)
00107       return;
00108  
00109   } // The lock is released before deleting old rep object below.
00110  
00111   delete old;
00112 }
00113 
00114 template <class X, class ACE_LOCK> inline
00115 ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::ACE_Refcounted_Auto_Ptr_Rep (X *p)
00116   : ptr_ (p),
00117     ref_count_ (0)
00118 {
00119 }
00120 
00121 template <class X, class ACE_LOCK> inline
00122 ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::~ACE_Refcounted_Auto_Ptr_Rep (void)
00123 {
00124 }
00125 
00126 template<class X, class ACE_LOCK> inline X *
00127 ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::release (void)
00128 {
00129   ACE_GUARD_RETURN (ACE_LOCK, guard, this->lock_, 0);
00130 
00131   return this->ptr_.release ();
00132 }
00133 
00134 template<class X, class ACE_LOCK> inline void
00135 ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::reset (X *p)
00136 {
00137   ACE_GUARD (ACE_LOCK, guard, this->lock_);
00138 
00139   this->ptr_.reset (p);
00140 }
00141 
00142 template <class X, class ACE_LOCK> inline X *
00143 ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::get (void) const
00144 {
00145   ACE_GUARD_RETURN (ACE_LOCK, guard, ACE_const_cast (ACE_LOCK &, this->lock_), 0);
00146 
00147   return this->ptr_.get ();
00148 }
00149 
00150 template <class X, class ACE_LOCK> inline
00151 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::ACE_Refcounted_Auto_Ptr (X *p)
00152   : rep_ (AUTO_REFCOUNTED_PTR_REP::create (p))
00153 {
00154 }
00155 
00156 template <class X, class ACE_LOCK> inline
00157 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::ACE_Refcounted_Auto_Ptr (const ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &r)
00158   : rep_ (AUTO_REFCOUNTED_PTR_REP::attach (((ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &) r).rep_))
00159 {
00160 }
00161 
00162 template <class X, class ACE_LOCK> inline
00163 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::~ACE_Refcounted_Auto_Ptr (void)
00164 {
00165   AUTO_REFCOUNTED_PTR_REP::detach (rep_);
00166 }
00167 
00168 template <class X, class ACE_LOCK> inline int
00169 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::operator== (const ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &r) const
00170 {
00171   return r.rep_ == this->rep_;
00172 }
00173 
00174 template <class X, class ACE_LOCK> inline int
00175 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::operator!= (const ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &r) const
00176 {
00177   return r.rep_ != this->rep_;
00178 }
00179 
00180 template <class X, class ACE_LOCK> inline X *
00181 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::operator-> (void) const
00182 {
00183     return this->rep_->get();
00184 }
00185 
00186 template<class X, class ACE_LOCK> inline X &
00187 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::operator *() const
00188 {
00189   return *this->rep_->get ();
00190 }
00191 
00192 template <class X, class ACE_LOCK> inline X*
00193 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::get (void) const
00194 {
00195   // We return the ACE_Future_rep.
00196   return this->rep_->get ();
00197 }
00198 
00199 template<class X, class ACE_LOCK> inline X *
00200 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::release (void)
00201 {
00202   return this->rep_->release ();
00203 }
00204 
00205 template<class X, class ACE_LOCK> inline void
00206 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::reset (X *p)
00207 {
00208   this->rep_->reset (p);
00209 }
00210 
00211 template <class X, class ACE_LOCK> inline void
00212 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::operator = (const ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &rhs)
00213 {
00214   // assignment:
00215   //
00216   //  bind <this> to the same <ACE_Refcounted_Auto_Ptr_Rep> as <r>.
00217 
00218   // This will work if &r == this, by first increasing the ref count
00219   ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &r = (ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &) rhs;
00220   AUTO_REFCOUNTED_PTR_REP::assign (this->rep_,
00221                                    AUTO_REFCOUNTED_PTR_REP::attach (r.rep_));
00222 }
00223 

Generated on Mon Jun 16 11:21:03 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002