Refcounted_Auto_Ptr.inl

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // $Id: Refcounted_Auto_Ptr.inl 81179 2008-03-31 19:00:29Z iliyan $
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 long
00011 ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::count (void) const
00012 {
00013   return this->ref_count_.value();
00014 }
00015 
00016 template <class X, class ACE_LOCK> inline long
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 bool
00023 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::null (void) const
00024 {
00025   return (this->rep_ == 0 || this->rep_->get () == 0);
00026 }
00027 
00028 template <class X, class ACE_LOCK> inline ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *
00029 ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::internal_create (X *p)
00030 {
00031   ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *temp = 0;
00032   ACE_NEW_RETURN (temp,
00033                   (ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>) (p),
00034                   0);
00035   return temp;
00036 }
00037 
00038 template <class X, class ACE_LOCK> inline ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *
00039 ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::create (X *p)
00040 {
00041   // Yes set ref count to zero.
00042   ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *temp = internal_create (p);
00043 #if defined (ACE_NEW_THROWS_EXCEPTIONS)
00044   if (temp == 0)
00045     ACE_throw_bad_alloc;
00046 #else
00047   ACE_ASSERT (temp != 0);
00048 #endif /* ACE_NEW_THROWS_EXCEPTIONS */
00049    return temp;
00050 }
00051 
00052 template <class X, class ACE_LOCK> inline ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *
00053 ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::attach (ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>*& rep)
00054 {
00055   if (rep == 0)
00056     return 0;
00057 
00058   ++rep->ref_count_;
00059 
00060   return rep;
00061 }
00062 
00063 template <class X, class ACE_LOCK> inline void
00064 ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::detach (ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>*& rep)
00065 {
00066   if (rep == 0)
00067     return;
00068 
00069   if (rep->ref_count_-- == 0)
00070     delete rep;
00071 }
00072 
00073 template <class X, class ACE_LOCK> inline
00074 ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::ACE_Refcounted_Auto_Ptr_Rep (X *p)
00075   : ptr_ (p),
00076     ref_count_ (0)
00077 {
00078 }
00079 
00080 template <class X, class ACE_LOCK> inline
00081 ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::~ACE_Refcounted_Auto_Ptr_Rep (void)
00082 {
00083 }
00084 
00085 template <class X, class ACE_LOCK> inline X *
00086 ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK>::get (void) const
00087 {
00088   return this->ptr_.get ();
00089 }
00090 
00091 template <class X, class ACE_LOCK> inline
00092 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::ACE_Refcounted_Auto_Ptr (X *p)
00093   : rep_ (AUTO_REFCOUNTED_PTR_REP::create (p))
00094 {
00095 }
00096 
00097 template <class X, class ACE_LOCK> inline
00098 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::ACE_Refcounted_Auto_Ptr (const ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &r)
00099   : rep_ (AUTO_REFCOUNTED_PTR_REP::attach (((ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &) r).rep_))
00100 {
00101 }
00102 
00103 template <class X, class ACE_LOCK> inline bool
00104 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::operator== (const ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &r) const
00105 {
00106   return r.rep_ == this->rep_;
00107 }
00108 
00109 template <class X, class ACE_LOCK> inline bool
00110 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::operator!= (const ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &r) const
00111 {
00112   return r.rep_ != this->rep_;
00113 }
00114 
00115 template <class X, class ACE_LOCK> inline X *
00116 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::operator-> (void) const
00117 {
00118     return this->rep_->get();
00119 }
00120 
00121 template<class X, class ACE_LOCK> inline X &
00122 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::operator *() const
00123 {
00124   return *this->rep_->get ();
00125 }
00126 
00127 template<class X, class ACE_LOCK> inline bool
00128 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::operator !() const
00129 {
00130   return this->rep_->get () == 0;
00131 }
00132 
00133 template<class X, class ACE_LOCK> inline
00134 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::operator bool () const
00135 {
00136   return this->rep_->get () != 0;
00137 }
00138 
00139 template <class X, class ACE_LOCK> inline X*
00140 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::get (void) const
00141 {
00142   // We return the ACE_Future_rep.
00143   return this->rep_->get ();
00144 }
00145 
00146 template<class X, class ACE_LOCK> inline X *
00147 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::release (void)
00148 {
00149   X *p = this->get ();
00150   AUTO_REFCOUNTED_PTR_REP::detach (this->rep_);
00151   this->rep_ = 0;
00152   return p;
00153 }
00154 
00155 template<class X, class ACE_LOCK> inline void
00156 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::reset (X *p)
00157 {
00158   // Avoid deleting the underlying auto_ptr if assigning the same actual
00159   // pointer value.
00160   if (this->get () == p)
00161     return;
00162 
00163   AUTO_REFCOUNTED_PTR_REP *old_rep = this->rep_;
00164   if ((this->rep_ = AUTO_REFCOUNTED_PTR_REP::create (p)) != 0)
00165     AUTO_REFCOUNTED_PTR_REP::detach (old_rep);
00166   else
00167     this->rep_ = old_rep;
00168   return;
00169 }
00170 
00171 template <class X, class ACE_LOCK> inline void
00172 ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>::operator = (const ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &rhs)
00173 {
00174   //  bind <this> to the same <ACE_Refcounted_Auto_Ptr_Rep> as <r>.
00175   AUTO_REFCOUNTED_PTR_REP *old_rep = this->rep_;
00176   if (rhs.rep_ != 0)
00177     {
00178       this->rep_ = AUTO_REFCOUNTED_PTR_REP::attach
00179         (const_cast<ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>& > (rhs).rep_);
00180       if (this->rep_ != 0)
00181         AUTO_REFCOUNTED_PTR_REP::detach (old_rep);
00182     }
00183   else    // Assign a 0 rep to this
00184     {
00185       AUTO_REFCOUNTED_PTR_REP::detach (old_rep);
00186       this->rep_ = 0;
00187     }
00188 }
00189 
00190 ACE_END_VERSIONED_NAMESPACE_DECL

Generated on Tue Feb 2 17:18:42 2010 for ACE by  doxygen 1.4.7