#include <smart_ptr.h>
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).
Definition at line 256 of file smart_ptr.h.
Public Member Functions | |
CRefPtr (const CRefPtr ©) | |
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. | |
CRefPtr & | operator= (const CRefPtr ©) |
operator=. Giving a NULL pointer is a valid operation. | |
CRefPtr & | operator= (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::CPtrInfo * | pinfo |
T * | Ptr |
|
Init a NULL Ptr.
Definition at line 153 of file smart_ptr_inline.h. References REF_TRACE.
|
|
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 } |
|
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.
|
|
Release the RefPtr.
Definition at line 185 of file smart_ptr_inline.h. References REF_TRACE, and NLMISC::CRefPtr< T >::unRef().
|
|
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().
|
|
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.
|
|
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.
|
|
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.
|
|
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 } |
|
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 } |
|
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 } |
|
Definition at line 259 of file smart_ptr.h. Referenced by NLMISC::CRefPtr< T >::CRefPtr(), and NLMISC::CRefPtr< T >::operator=(). |
|
Definition at line 260 of file smart_ptr.h. |