# 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::CRefPtr Template Class Reference

CRefPtr: an handle on a ptr. More...

#include <smart_ptr.h>

Collaboration diagram for NLMISC::CRefPtr:

Collaboration graph
[legend]
List of all members.

Public Methods

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

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

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

 ~CRefPtr (void)
 Release the RefPtr. More...

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

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

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

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

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

void kill ()
 kill/delete the object pointed by the pointer, and inform the other RefPtr of this. More...


Private Methods

void unRef () const

Private Attributes

CRefCount::CPtrInfopinfo
T* Ptr

Detailed Description

template<class T> template class NLMISC::CRefPtr

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


Constructor & Destructor Documentation

template<classT>
NLMISC::CRefPtr< T >::CRefPtr<T> ( ) [inline]
 

Init a NULL Ptr.

Definition at line 152 of file smart_ptr_inline.h.

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

Attach a ptr to a RefPtr.

Definition at line 159 of file smart_ptr_inline.h.

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

Copy constructor.

Definition at line 176 of file smart_ptr_inline.h.

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

Release the RefPtr.

Definition at line 184 of file smart_ptr_inline.h.


Member Function Documentation

template<classT>
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 240 of file smart_ptr_inline.h.

template<classT>
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 271 of file smart_ptr_inline.h.

template<classT>
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 259 of file smart_ptr_inline.h.

template<classT>
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 276 of file smart_ptr_inline.h.

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

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

Definition at line 222 of file smart_ptr_inline.h.

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

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

Definition at line 195 of file smart_ptr_inline.h.

template<classT>
SMART_INLINE void NLMISC::CRefPtr< T >::unRef ( ) const [private]
 

Definition at line 120 of file smart_ptr_inline.h.


Member Data Documentation

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

Definition at line 247 of file smart_ptr.h.

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

Definition at line 246 of file smart_ptr.h.


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