NLMISC::CSmartPtr< T > Class Template Reference

#include <smart_ptr.h>


Detailed Description

template<class T>
class NLMISC::CSmartPtr< T >

Standard SmartPtr class. T Must derive from CRefCount. Once a normal ptr is assigned to a SmartPtr, the smartptr will own this pointer, and delete it when no other smartptr reference the object (with a reference couting scheme). The following code works, since the object himself must herit from CRefCount, and so hold the refcount.
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.

See also:
CRefPtr
Author:
Lionel Berenguier

Nevrax France

Date:
2000

Definition at line 185 of file smart_ptr.h.

Public Member Functions

 CSmartPtr (const CSmartPtr &copy)
 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.

CSmartPtroperator= (const CSmartPtr &p)
 operator=. Giving a NULL pointer is a valid operation.

CSmartPtroperator= (T *p)
 operator=. Giving a NULL pointer is a valid operation.

std::string toString ()
 ~CSmartPtr ()
 Release the SmartPtr.


Private Attributes

T * Ptr


Constructor & Destructor Documentation

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

Init a NULL Ptr.

Definition at line 191 of file smart_ptr.h.

00191 { Ptr=NULL; SMART_TRACE("ctor()"); }

template<class T>
NLMISC::CSmartPtr< T >::CSmartPtr T *  p  )  [inline]
 

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*)"); }

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

Copy constructor.

Definition at line 195 of file smart_ptr.h.

00195 { Ptr=copy.Ptr; if(Ptr) Ptr->crefs++; SMART_TRACE("ctor(Copy)"); }

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

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 }


Member Function Documentation

template<class T>
sint NLMISC::CSmartPtr< T >::getNbRef  )  [inline]
 

Definition at line 216 of file smart_ptr.h.

00216 { if(Ptr) return Ptr->crefs; else return 0; }

template<class T>
bool NLMISC::CSmartPtr< T >::isNull  )  const [inline]
 

returns if there's no object pointed by this SmartPtr.

Definition at line 207 of file smart_ptr.h.

00207 { return Ptr==NULL; }

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

Indirection operator. Doesn't check NULL.

Definition at line 203 of file smart_ptr.h.

00203 { SMART_TRACE("ope*()"); return *Ptr; }

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

Cast operator.

Definition at line 201 of file smart_ptr.h.

00201 { SMART_TRACE("castT*()"); return Ptr; }

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

Selection operator. Doesn't check NULL.

Definition at line 205 of file smart_ptr.h.

00205 { SMART_TRACE("ope->()"); return Ptr; }

template<class T>
bool NLMISC::CSmartPtr< T >::operator< const CSmartPtr< T > &  p  )  const [inline]
 

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 }

template<class T>
CSmartPtr< T > & NLMISC::CSmartPtr< T >::operator= const CSmartPtr< T > &  p  )  [inline]
 

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 }

template<class T>
CSmartPtr< T > & NLMISC::CSmartPtr< T >::operator= T *  p  )  [inline]
 

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 }

template<class T>
std::string NLMISC::CSmartPtr< T >::toString  )  [inline]
 

Definition at line 219 of file smart_ptr.h.

Referenced by NLMISC::CSmartPtr< CSkeletonShape >::toString().

00219 { if(Ptr) return toString(*Ptr); else return "<null>"; }


Field Documentation

template<class T>
T* NLMISC::CSmartPtr< T >::Ptr [private]
 

Definition at line 187 of file smart_ptr.h.

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


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