NLMISC::CRefPtr< T > Class Template Reference

#include <smart_ptr.h>


Detailed Description

template<class T>
class NLMISC::CRefPtr< T >

CRefPtr: an handle on a ptr. T Must derive from CRefCount. If you use CRefPtr, you can kill the object simply by calling delete (T*)RefPtr, or the kill() method. All other CRefPtr which point to it can know if it has been deleted. (but you must be sure that this ptr is not handle by a SmartPtr, of course...)

SmartPtr are compatible with RefPtr. A ptr may be link to a CRefPtr and a CSmartPtr. As example, when the CSmartPtr will destroy him, CRefPtr will be informed... Sample:

void foo() { A *p; CSmartPtr<A> sp; CRefPtr<A> rp; p= new A; sp= p; // OK. p is now owned by sp and will be deleted by sp. rp= p; // OK. rp handle p. sp= NULL; // Destruction. p deleted. rp automatically informed. if(rp==NULL) thisIsGood(); // rp==NULL. }

PERFORMANCE WARNING! operator=() are about 10 times slower than normal pointers. So use them wisely. For local use, prefer cast the refptr to a normal Ptr. Also, an object used with a CRefPtr will allocate a small PtrInfo (one only per object, not per ptr).

See also:
CSmartPtr

Definition at line 256 of file smart_ptr.h.

Public Member Functions

 CRefPtr (const CRefPtr &copy)
 Copy constructor.

 CRefPtr (T *v)
 Attach a ptr to a RefPtr.

 CRefPtr ()
 Init a NULL Ptr.

void kill ()
T & operator * (void) const
 Indirection operator. Doesn't test if ptr has been deleted somewhere, and doesn't check NULL.

 operator T * () const
 Cast operator. Check if the object has been deleted somewhere, and return NULL if this is the case.

T * operator-> (void) const
 Selection operator. Doesn't test if ptr has been deleted somewhere, and doesn't check NULL.

CRefPtroperator= (const CRefPtr &copy)
 operator=. Giving a NULL pointer is a valid operation.

CRefPtroperator= (T *v)
 operator=. Giving a NULL pointer is a valid operation.

 ~CRefPtr (void)
 Release the RefPtr.


Private Member Functions

void unRef () const

Private Attributes

CRefCount::CPtrInfopinfo
T * Ptr


Constructor & Destructor Documentation

template<class T>
NLMISC::CRefPtr< T >::CRefPtr  )  [inline]
 

Init a NULL Ptr.

Definition at line 153 of file smart_ptr_inline.h.

References REF_TRACE.

00154 { 
00155         pinfo= &CRefCount::NullPtrInfo;
00156         Ptr= NULL;
00157 
00158         REF_TRACE("Smart()");
00159 }

template<class T>
NLMISC::CRefPtr< T >::CRefPtr T *  v  )  [inline]
 

Attach a ptr to a RefPtr.

Definition at line 160 of file smart_ptr_inline.h.

References REF_TRACE, NLMISC::CRefCount::CPtrInfo::RefCount, and v.

00161 {
00162         Ptr= v;
00163     if(v)
00164         {
00165                 // If no CRefPtr handles v, create a pinfo ref...
00166                 if(v->pinfo->IsNullPtrInfo)
00167                         v->pinfo=new CRefCount::CPtrInfo(v);
00168                 pinfo=v->pinfo;
00169                 // v is now used by this.
00170                 pinfo->RefCount++;
00171         }
00172         else
00173                 pinfo= &CRefCount::NullPtrInfo;
00174 
00175         REF_TRACE("Smart(T*)");
00176 }

template<class T>
NLMISC::CRefPtr< T >::CRefPtr const CRefPtr< T > &  copy  )  [inline]
 

Copy constructor.

Definition at line 177 of file smart_ptr_inline.h.

References NLMISC::CRefPtr< T >::pinfo, NLMISC::CRefCount::CPtrInfo::Ptr, REF_TRACE, and NLMISC::CRefCount::CPtrInfo::RefCount.

00178 {
00179         pinfo=copy.pinfo;
00180         pinfo->RefCount++;
00181         Ptr= (T*)pinfo->Ptr;
00182 
00183         REF_TRACE("SmartCopy()");
00184 }

template<class T>
NLMISC::CRefPtr< T >::~CRefPtr void   )  [inline]
 

Release the RefPtr.

Definition at line 185 of file smart_ptr_inline.h.

References REF_TRACE, and NLMISC::CRefPtr< T >::unRef().

00186 {
00187         REF_TRACE("~Smart()");
00188 
00189         unRef();
00190         pinfo= &CRefCount::NullPtrInfo;
00191         Ptr= NULL;
00192 }


Member Function Documentation

template<class T>
void NLMISC::CRefPtr< T >::kill  ) 
 

kill/delete the object pointed by the pointer, and inform the other RefPtr of this. "rp.kill()" and "delete (T*)rp" do the same thing, except that rp NULLity is updated with kill(). RefPtr which point to the same object could know if the object is valid, by just testing it ( by an implicit call to the cast operator to T*). But any calls to operator->() or operator*() will have unpredictible effects (may crash... :) ).

Definition at line 241 of file smart_ptr_inline.h.

References NLMISC::CRefCount::CPtrInfo::Ptr, REF_TRACE, and NLMISC::CRefPtr< T >::unRef().

00242 {
00243         REF_TRACE("SmartKill");
00244 
00245         T       *ptr= (T*)pinfo->Ptr;
00246 
00247         // First, release the refptr.
00248         unRef();
00249         pinfo= &CRefCount::NullPtrInfo;
00250         Ptr= NULL;
00251 
00252         // Then delete the pointer.
00253         if(ptr)
00254                 delete ptr;
00255 }

template<class T>
T & NLMISC::CRefPtr< T >::operator * void   )  const [inline]
 

Indirection operator. Doesn't test if ptr has been deleted somewhere, and doesn't check NULL.

Definition at line 272 of file smart_ptr_inline.h.

References REF_TRACE.

00273 { 
00274         REF_TRACE("Smart *()");
00275         return *Ptr; 
00276 }

template<class T>
NLMISC::CRefPtr< T >::operator T *  )  const [inline]
 

Cast operator. Check if the object has been deleted somewhere, and return NULL if this is the case.

Definition at line 260 of file smart_ptr_inline.h.

References NLMISC::CRefCount::CPtrInfo::Ptr, and REF_TRACE.

00261 {
00262         REF_TRACE("SmartCast T*()");
00263 
00264         // Refresh Ptr.
00265         Ptr= (T*)pinfo->Ptr;
00266         return Ptr;
00267 }

template<class T>
T * NLMISC::CRefPtr< T >::operator-> void   )  const [inline]
 

Selection operator. Doesn't test if ptr has been deleted somewhere, and doesn't check NULL.

Definition at line 277 of file smart_ptr_inline.h.

References REF_TRACE.

00278 { 
00279         REF_TRACE("Smart ->()");
00280         return Ptr;  
00281 }

template<class T>
CRefPtr< T > & NLMISC::CRefPtr< T >::operator= const CRefPtr< T > &  copy  ) 
 

operator=. Giving a NULL pointer is a valid operation.

Definition at line 223 of file smart_ptr_inline.h.

References NLMISC::CRefPtr< T >::pinfo, NLMISC::CRefCount::CPtrInfo::Ptr, REF_TRACE, NLMISC::CRefCount::CPtrInfo::RefCount, and NLMISC::CRefPtr< T >::unRef().

00224 {
00225         REF_TRACE("ope=(Smart)Start");
00226 
00227         // The auto equality test is implicitly done by upcounting first "copy", then downcounting "this".
00228         copy.pinfo->RefCount++;
00229         unRef();
00230         pinfo=copy.pinfo;
00231         // Must Refresh the ptr.
00232         Ptr= (T*)pinfo->Ptr;
00233 
00234         REF_TRACE("ope=(Smart)End");
00235         return *this;
00236 }

template<class T>
CRefPtr< T > & NLMISC::CRefPtr< T >::operator= T *  v  ) 
 

operator=. Giving a NULL pointer is a valid operation.

Definition at line 196 of file smart_ptr_inline.h.

References REF_TRACE, NLMISC::CRefPtr< T >::unRef(), and v.

00197 {
00198         REF_TRACE("ope=(T*)Start");
00199 
00200 
00201         Ptr= v;
00202         if(v)
00203         {
00204                 // If no CRefPtr handles v, create a pinfo ref...
00205                 if(v->pinfo->IsNullPtrInfo)
00206                         v->pinfo=new CRefCount::CPtrInfo(v);
00207                 // The auto equality test is implicitly done by upcounting first "v", then downcounting "this".
00208                 v->pinfo->RefCount++;
00209                 unRef();
00210                 pinfo= v->pinfo;
00211         }
00212         else
00213         {
00214                 unRef();
00215                 pinfo= &CRefCount::NullPtrInfo;
00216         }
00217 
00218 
00219         REF_TRACE("ope=(T*)End");
00220 
00221         return *this;
00222 }

template<class T>
void NLMISC::CRefPtr< T >::unRef  )  const [inline, private]
 

Definition at line 121 of file smart_ptr_inline.h.

References NLMISC::CRefCount::CPtrInfo::IsNullPtrInfo, NLMISC::CRefCount::CPtrInfo::Ptr, NLMISC::CRefCount::CPtrInfo::RefCount, and SMART_INLINE.

Referenced by NLMISC::CRefPtr< T >::kill(), NLMISC::CRefPtr< T >::operator=(), and NLMISC::CRefPtr< T >::~CRefPtr().

00122 {
00123         pinfo->RefCount--;
00124         if(pinfo->RefCount==0)
00125         {
00126                 // In CRefPtr, Never delete the object.
00127 
00128                 // We may be in the case that this==NullPtrInfo, and our NullPtrInfo has done a total round. Test it.
00129                 if(pinfo->IsNullPtrInfo)
00130                 {
00131                         // This should not happens, but I'm not sure :) ...
00132                         // Reset the NullPtrInfo to a middle round.
00133                         pinfo->RefCount= 0x7FFFFFFF;
00134                 }
00135                 else
00136                 {
00137                         // If the CRefPtr still point to a valid object.
00138                         if(pinfo->Ptr)
00139                         {
00140                                 // Inform the Object that no more CRefPtr points on it.
00141                                 ((T*)(pinfo->Ptr))->pinfo= &CRefCount::NullPtrInfo;
00142                         }
00143                         // Then delete the pinfo.
00144                         delete pinfo;
00145                 }
00146 
00147         }
00148 }


Field Documentation

template<class T>
CRefCount::CPtrInfo* NLMISC::CRefPtr< T >::pinfo [private]
 

Definition at line 259 of file smart_ptr.h.

Referenced by NLMISC::CRefPtr< T >::CRefPtr(), and NLMISC::CRefPtr< T >::operator=().

template<class T>
T* NLMISC::CRefPtr< T >::Ptr [mutable, private]
 

Definition at line 260 of file smart_ptr.h.


The documentation for this class was generated from the following files:
Generated on Tue Mar 16 13:31:38 2004 for NeL by doxygen 1.3.6