#include <smart_ptr.h>
CSmartPtr<A> a0, a1; A *p0; a0= new A; // Ok. RefCount==1. p0= a0; // Ok, cast operator. object still owned by a0. a1= p0; // Ok!! RefCount==2. Object owned by a0 and a1; // At destruction, a1 unref(), then a0 unref() and delete the object.
The ref counter cannot be put directly in the smartptr since the preceding behavior must be supported and inheritance must be supported too. Here, if A is a base class of B, Pa and Pb are smartptr of a and b respectively, then Pa=Pb
; is a valid operation. But, doing this, you may ensure that you have a virtual dtor(), since dtor() Pa may call ~A() (or you may ensure that Pa won't destruct A, which it sound much more as a normal pointer :) ).
Sample:
class A : public CRefCount { public: A() {puts("A()");} virtual ~A() {puts("~A()");} }; class B : public A { public: B() {puts("B()");} ~B() {puts("~B()");} }; void testPtr() { CSmartPtr<A> a0,a1,a2; CSmartPtr<B> b0; a0= new A; a1= a0; a1= new A; a2= a1; a1=NULL; b0= new B; a0=b0; printf("%d\n", (A*)NULL==a0); printf("%d\n", b0!=a0); printf("%d\n", (A*)NULL==a1); printf("%d\n", a2!=a0); } *
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. p= rp; // result: p==NULL. }
PERFORMANCE WARNING! operator=() are about 10 times slower than normal pointers. For local use, prefer cast the smartptr to a normal Ptr.
Nevrax France
Definition at line 185 of file smart_ptr.h.
Public Member Functions | |
CSmartPtr (const CSmartPtr ©) | |
Copy constructor. | |
CSmartPtr (T *p) | |
Attach a ptr to a SmartPtr. | |
CSmartPtr () | |
Init a NULL Ptr. | |
sint | getNbRef () |
bool | isNull () const |
returns if there's no object pointed by this SmartPtr. | |
T & | operator * (void) const |
Indirection operator. Doesn't check NULL. | |
operator T * (void) const | |
Cast operator. | |
T * | operator-> (void) const |
Selection operator. Doesn't check NULL. | |
bool | operator< (const CSmartPtr &p) const |
operator<. Compare the pointers. | |
CSmartPtr & | operator= (const CSmartPtr &p) |
operator=. Giving a NULL pointer is a valid operation. | |
CSmartPtr & | operator= (T *p) |
operator=. Giving a NULL pointer is a valid operation. | |
std::string | toString () |
~CSmartPtr () | |
Release the SmartPtr. | |
Private Attributes | |
T * | Ptr |
|
Init a NULL Ptr.
Definition at line 191 of file smart_ptr.h.
00191 { Ptr=NULL; SMART_TRACE("ctor()"); } |
|
Attach a ptr to a SmartPtr.
Definition at line 193 of file smart_ptr.h.
00193 { Ptr=p; if(Ptr) Ptr->crefs++; SMART_TRACE("ctor(T*)"); } |
|
Copy constructor.
Definition at line 195 of file smart_ptr.h.
00195 { Ptr=copy.Ptr; if(Ptr) Ptr->crefs++; SMART_TRACE("ctor(Copy)"); } |
|
Release the SmartPtr.
Definition at line 69 of file smart_ptr_inline.h. References SMART_TRACE.
00070 { 00071 SMART_TRACE("dtor()"); 00072 00073 if(Ptr) 00074 { 00075 if (--(Ptr->crefs) == 0) 00076 delete Ptr; 00077 Ptr=NULL; 00078 } 00079 } |
|
Definition at line 216 of file smart_ptr.h.
|
|
returns if there's no object pointed by this SmartPtr.
Definition at line 207 of file smart_ptr.h.
00207 { return Ptr==NULL; } |
|
Indirection operator. Doesn't check NULL.
Definition at line 203 of file smart_ptr.h.
00203 { SMART_TRACE("ope*()"); return *Ptr; } |
|
Cast operator.
Definition at line 201 of file smart_ptr.h.
00201 { SMART_TRACE("castT*()"); return Ptr; } |
|
Selection operator. Doesn't check NULL.
Definition at line 205 of file smart_ptr.h.
00205 { SMART_TRACE("ope->()"); return Ptr; } |
|
operator<. Compare the pointers.
Definition at line 105 of file smart_ptr_inline.h. References NLMISC::CSmartPtr< T >::Ptr, and SMART_INLINE.
00106 { 00107 return Ptr<p.Ptr; 00108 } |
|
operator=. Giving a NULL pointer is a valid operation.
Definition at line 100 of file smart_ptr_inline.h. References NLMISC::CSmartPtr< T >::operator=(), NLMISC::CSmartPtr< T >::Ptr, and SMART_INLINE.
00101 { 00102 return operator=(p.Ptr); 00103 } |
|
operator=. Giving a NULL pointer is a valid operation.
Definition at line 81 of file smart_ptr_inline.h. References SMART_INLINE, and SMART_TRACE. Referenced by NLMISC::CSmartPtr< T >::operator=().
00082 { 00083 SMART_TRACE("ope=(T*)Start"); 00084 00085 // Implicit manage auto-assignation. 00086 if(p) 00087 p->crefs++; 00088 if(Ptr) 00089 { 00090 if (--(Ptr->crefs) == 0) 00091 delete Ptr; 00092 } 00093 Ptr = p; 00094 00095 SMART_TRACE("ope=(T*)End"); 00096 00097 return *this; 00098 } |
|
Definition at line 219 of file smart_ptr.h. Referenced by NLMISC::CSmartPtr< CSkeletonShape >::toString().
|
|
Definition at line 187 of file smart_ptr.h. Referenced by NLMISC::CSmartPtr< CSkeletonShape >::CSmartPtr(), NLMISC::CSmartPtr< T >::operator<(), and NLMISC::CSmartPtr< T >::operator=(). |