# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

NLMISC::CSmartPtr Class Template Reference

Standard SmartPtr class. More...

#include <smart_ptr.h>

List of all members.

Public Methods

 CSmartPtr ()
 Init a NULL Ptr. More...

 CSmartPtr (T *p)
 Attach a ptr to a SmartPtr. More...

 CSmartPtr (const CSmartPtr &copy)
 Copy constructor. More...

 ~CSmartPtr ()
 Release the SmartPtr. More...

 operator T * (void) const
 Cast operator. More...

T & operator * (void) const
 Indirection operator. Doesn't check NULL. More...

T * operator-> (void) const
 Selection operator. Doesn't check NULL. More...

CSmartPtr & operator= (T *p)
 operator=. Giving a NULL pointer is a valid operation. More...

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

bool operator< (const CSmartPtr &p) const
 operator<. Compare the pointers. More...

sint getNbRef ()

Private Attributes

T * Ptr


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 178 of file smart_ptr.h.


Constructor & Destructor Documentation

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

Init a NULL Ptr.

Definition at line 184 of file smart_ptr.h.

Referenced by NLMISC::CSmartPtr< CSkeletonShape >::CSmartPtr.

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

Attach a ptr to a SmartPtr.

Definition at line 186 of file smart_ptr.h.

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

Copy constructor.

Definition at line 188 of file smart_ptr.h.

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

Release the SmartPtr.

Definition at line 66 of file smart_ptr_inline.h.

References Ptr, and SMART_TRACE.


Member Function Documentation

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

Definition at line 207 of file smart_ptr.h.

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

Indirection operator. Doesn't check NULL.

Definition at line 196 of file smart_ptr.h.

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

Cast operator.

Definition at line 194 of file smart_ptr.h.

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

Selection operator. Doesn't check NULL.

Definition at line 198 of file smart_ptr.h.

template<class T>
SMART_INLINE bool NLMISC::CSmartPtr< T >::operator< const CSmartPtr< T > &    p const
 

operator<. Compare the pointers.

Definition at line 102 of file smart_ptr_inline.h.

References Ptr, and SMART_INLINE.

template<class T>
SMART_INLINE CSmartPtr< T > & NLMISC::CSmartPtr< T >::operator= const CSmartPtr< T > &    p
 

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

Definition at line 97 of file smart_ptr_inline.h.

References operator=, and SMART_INLINE.

template<class T>
SMART_INLINE CSmartPtr< T > & NLMISC::CSmartPtr< T >::operator= T *    p
 

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

Definition at line 78 of file smart_ptr_inline.h.

References Ptr, SMART_INLINE, and SMART_TRACE.

Referenced by operator=.


Member Data Documentation

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

Definition at line 180 of file smart_ptr.h.

Referenced by NLMISC::CSmartPtr< CSkeletonShape >::CSmartPtr, NLMISC::CSmartPtr< CSkeletonShape >::getNbRef, NLMISC::CSmartPtr< CSkeletonShape >::operator *, NLMISC::CSmartPtr< CSkeletonShape >::operator T *, NLMISC::CSmartPtr< CSkeletonShape >::operator->, operator<, operator=, and ~CSmartPtr.


The documentation for this class was generated from the following files: