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

CTBsharedObjPtr.icc

Go to the documentation of this file.
00001 
00006 /*----------------------------------------------------------------------------*/
00007 /* C Tool Box: Designed and implemented by:                                   */
00008 /*    Walter F.J. Mueller   Gesellschaft fuer Schwerionenforschung (GSI)      */
00009 /*                          Planckstrasse 1, D-64291 Darmstadt, Germany       */
00010 /*                  Email:  W.F.J.Mueller@gsi.de                              */
00011 /*                  WWW:    http://www-kp3.gsi.de/www/kp3/people/mueller.html */
00012 /*------------------------------------------+---------------------------------*/
00042 #include <assert.h>
00043 
00044 //------------------------------------------+-----------------------------------
00046 
00047 template <class T>
00048 inline CTBsharedCObjPtr<T>::CTBsharedCObjPtr()
00049   : mp_obj(0),
00050     mp_refcount(0)
00051 {}
00052 
00053 //------------------------------------------+-----------------------------------
00055 
00060 template <class T>
00061 inline CTBsharedCObjPtr<T>::CTBsharedCObjPtr(const CTBsharedCObjPtr<T>& rhs)
00062   : mp_obj(rhs.mp_obj),
00063     mp_refcount(rhs.mp_refcount)
00064 {
00065   if (mp_refcount) mp_refcount->Increment();
00066 }
00067 
00068 //------------------------------------------+-----------------------------------
00070 
00078 template <class T>
00079 inline CTBsharedCObjPtr<T>::CTBsharedCObjPtr(T* p)
00080   : mp_obj(p),
00081     mp_refcount(new CTBreferenceCounter)
00082 {}
00083 
00084 //------------------------------------------+-----------------------------------
00086 
00092 template <class T>
00093 inline CTBsharedCObjPtr<T>::~CTBsharedCObjPtr()
00094 {
00095   Clear();
00096 }
00097 
00098 //------------------------------------------+-----------------------------------
00100 
00106 template <class T>
00107 inline bool CTBsharedCObjPtr<T>::IsUnique() const
00108 {
00109   return (mp_refcount) ? mp_refcount->IsUnique() : true;
00110 }
00111 
00112 //------------------------------------------+-----------------------------------
00114 
00119 template <class T>
00120 inline bool CTBsharedCObjPtr<T>::IsShared() const
00121 {
00122   return (mp_refcount) ? mp_refcount->IsShared() : false;
00123 }
00124 
00125 //------------------------------------------+-----------------------------------
00127 
00132 template <class T>
00133 inline CTBint CTBsharedCObjPtr<T>::ShareCount() const
00134 {
00135   return (mp_refcount) ? *mp_refcount : 0;
00136 }
00137 
00138 //------------------------------------------+-----------------------------------
00140 
00141 template <class T>
00142 inline bool CTBsharedCObjPtr<T>::Equal(const CTBsharedCObjPtr<T>& rhs) const
00143 {
00144   return mp_obj == rhs.mp_obj;
00145 }
00146 
00147 //------------------------------------------+-----------------------------------
00149 
00150 template <class T>
00151 inline void CTBsharedCObjPtr<T>::ToStream(ostream& os) const
00152 {
00153   os << (void*) mp_obj;
00154   return;
00155 }
00156 
00157 //------------------------------------------+-----------------------------------
00159 
00164 template <class T>
00165 inline void CTBsharedCObjPtr<T>::Clear()
00166 {
00167   if (mp_refcount) {                        // refering to an object ?
00168     if (mp_refcount->Decrement()) {         // last reference ?
00169       delete mp_obj;                        // then delete object
00170       delete mp_refcount;                   // and reference counter
00171     }
00172     mp_obj      = 0;
00173     mp_refcount = 0;
00174   }
00175   return;
00176 }
00177 
00178 //------------------------------------------+-----------------------------------
00180 
00184 template <class T>
00185 inline CTBsharedCObjPtr<T>& CTBsharedCObjPtr<T>::operator=(
00186                              const CTBsharedCObjPtr<T>& rhs)
00187 {
00188   Clear();
00189   mp_obj      = rhs.mp_obj;
00190   mp_refcount = rhs.mp_refcount;
00191   if (mp_refcount) mp_refcount->Increment();
00192   return *this;
00193 }
00194 
00195 //------------------------------------------+-----------------------------------
00197 
00198 template <class T>
00199 inline CTBsharedCObjPtr<T>& CTBsharedCObjPtr<T>::operator=(T* rhs)
00200 {
00201   Clear();
00202   mp_obj      = rhs;
00203   mp_refcount = new CTBreferenceCounter;
00204   return *this;
00205 }
00206 
00207 //------------------------------------------+-----------------------------------
00209 
00210 template <class T>
00211 inline const T& CTBsharedCObjPtr<T>::operator*() const
00212 {
00213   return *mp_obj;
00214 }
00215 
00216 //------------------------------------------+-----------------------------------
00218 
00219 template <class T>
00220 inline const T* CTBsharedCObjPtr<T>::operator->() const
00221 {
00222   return mp_obj;
00223 }
00224 
00225 //------------------------------------------+-----------------------------------
00227 
00228 template <class T>
00229 inline bool CTBsharedCObjPtr<T>::operator!() const
00230 {
00231   return !mp_obj;
00232 }
00233 
00234 //------------------------------------------+-----------------------------------
00236 
00237 template <class T>
00238 inline CTBsharedCObjPtr<T>::operator void* () const
00239 {
00240   return (void*) mp_obj;
00241 }
00242 
00243 //------------------------------------------+-----------------------------------
00249 template <class T>
00250 inline bool operator==(const CTBsharedCObjPtr<T>& lhs,
00251                        const CTBsharedCObjPtr<T>& rhs)
00252 {
00253   return lhs.Equal(rhs);
00254 }
00255 
00256 //------------------------------------------+-----------------------------------
00262 template <class T>
00263 inline ostream& operator<<(ostream& os, const CTBsharedCObjPtr<T>& obj)
00264 {
00265   obj.ToStream(os);
00266   return os;
00267 }
00268 
00269 //##############################################################################
00270 
00277 //------------------------------------------+-----------------------------------
00279 
00280 template <class T>
00281 inline CTBsharedObjPtr<T>::CTBsharedObjPtr()
00282   : CTBsharedCObjPtr<T>()
00283 {}
00284 
00285 //------------------------------------------+-----------------------------------
00287 
00292 template <class T>
00293 inline CTBsharedObjPtr<T>::CTBsharedObjPtr(const CTBsharedObjPtr<T>& rhs)
00294   : CTBsharedCObjPtr<T>(rhs)
00295 {}
00296 
00297 //------------------------------------------+-----------------------------------
00299 
00307 template <class T>
00308 inline CTBsharedObjPtr<T>::CTBsharedObjPtr(T* p)
00309   : CTBsharedCObjPtr<T>(p)
00310 {}
00311 
00312 //------------------------------------------+-----------------------------------
00314 
00315 template <class T>
00316 inline CTBsharedObjPtr<T>& CTBsharedObjPtr<T>::operator=(T* rhs)
00317 {
00318   CTBsharedCObjPtr<T>::operator=(rhs);
00319   return *this;
00320 }
00321 
00322 //------------------------------------------+-----------------------------------
00324 
00325 template <class T>
00326 inline T& CTBsharedObjPtr<T>::operator*() const
00327 {
00328   return *mp_obj;
00329 }
00330 
00331 //------------------------------------------+-----------------------------------
00333 
00334 template <class T>
00335 inline T* CTBsharedObjPtr<T>::operator->() const
00336 {
00337   return mp_obj;
00338 }
00339 

Generated at Fri Oct 24 18:11:30 2003 for CTBbase by doxygen1.2.9-20010812 written by Dimitri van Heesch, © 1997-2001