Refcounted_Auto_Ptr.inl

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Refcounted_Auto_Ptr.inl,v 4.3 2005/10/28 16:14:55 ossama Exp
00004 
00005 #include "ace/Guard_T.h"
00006 #include "ace/Log_Msg.h"
00007 
00008 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00009 
00010 template <class X, class ACE_LOCK> inline int
00011 ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::count (void) const
00012 {
00013   ACE_GUARD_RETURN (ACE_LOCK, guard, this->lock_, 0);
00014   return this->ref_count_;
00015 }
00016 
00017 template <class X, class ACE_LOCK> inline int
00018 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::count (void) const
00019 {
00020   return this->rep_->count ();
00021 }
00022 
00023 template <class X, class ACE_LOCK> inline int
00024 ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::null (void) const
00025 {
00026   ACE_GUARD_RETURN (ACE_LOCK, guard, 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, 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 bool
00163 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::operator== (const ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &r) const
00164 {
00165   return r.rep_ == this->rep_;
00166 }
00167 
00168 template <class X, class ACE_LOCK> inline bool
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 X *
00175 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::operator-> (void) const
00176 {
00177     return this->rep_->get();
00178 }
00179 
00180 template<class X, class ACE_LOCK> inline X &
00181 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::operator *() 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>::get (void) const
00188 {
00189   // We return the ACE_Future_rep.
00190   return this->rep_->get ();
00191 }
00192 
00193 template<class X, class ACE_LOCK> inline X *
00194 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::release (void)
00195 {
00196   return this->rep_->release ();
00197 }
00198 
00199 template<class X, class ACE_LOCK> inline void
00200 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::reset (X *p)
00201 {
00202   this->rep_->reset (p);
00203 }
00204 
00205 template <class X, class ACE_LOCK> inline void
00206 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::operator = (const ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &rhs)
00207 {
00208   // assignment:
00209   //
00210   //  bind <this> to the same <ACE_Refcounted_Auto_Ptr_Rep> as <r>.
00211 
00212   // This will work if &r == this, by first increasing the ref count
00213   ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &r = (ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &) rhs;
00214   AUTO_REFCOUNTED_PTR_REP::assign (this->rep_,
00215                                    AUTO_REFCOUNTED_PTR_REP::attach (r.rep_));
00216 }
00217 
00218 ACE_END_VERSIONED_NAMESPACE_DECL

Generated on Thu Nov 9 09:42:01 2006 for ACE by doxygen 1.3.6