Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

CTPointer< T > Class Template Reference
[This and That]

A smart pointer template class. More...

#include <ctpointer.h>

Inheritance diagram for CTPointer< T >:

CTPointerBase List of all members.

Public Member Functions

 CTPointer ()
 CTPointer (T *obj)
 CTPointer (const CTPointer< T > &p)
virtual ~CTPointer ()
Copy Operators
void operator= (T *obj)
void operator= (CTPointer< T > &p)
void operator= (const CTPointer< T > &p)
Object Access
T & ref () const
T & operator * () const
virtual T * ptr () const
Type cast
template<class U> CTPointer< U > cast () const
 Returns a type-safe casted CTPointer of the given type.

Equality
bool operator== (const CTPointer< T > &p) const
bool operator!= (const CTPointer< T > &p) const
bool sharingData (const CTPointer< T > &p) const

Protected Member Functions

virtual void _deleteObject (void *p)
 CTPointer (const CTPointerBase &p)

Friends

class CTPointerCastBase< T >

Detailed Description

template<class T>
class CTPointer< T >

A smart pointer template class.

This class serves as a smart pointer class that is used in OpenHBCI to avoid memory leaks. It does automatic reference counting for the objects pointed to, like so: Each time a new CTPointer to the same object is created, the reference counter is incremented. Each time a CTPointer to an object is deleted, the reference counter is decremented. When the reference counter reaches zero, the object is deleted.

Use it instead of normal pointers, for example: instead of

structXYZ *pointer;
pointer = new structXYZ;

use this one:

CTPointer<structXYZ> pointer;
pointer = new structXYZ;

You can access the data easily by using the "*" operator, e.g:

structXYZ xyz = *pointer;

To access members of the object, either use the "*" operator or the ref() method:

a = (*pointer).a; or
b = pointer.ref().a;

Author:
Martin Preuss<martin@libchipcard.de>


Constructor & Destructor Documentation

template<class T>
CTPointer< T >::CTPointer const CTPointerBase p  )  [inline, protected]
 

template<class T>
CTPointer< T >::CTPointer  )  [inline]
 

Empty Constructor.

template<class T>
CTPointer< T >::CTPointer T *  obj  )  [inline]
 

Constructor with object to be pointing to.

template<class T>
CTPointer< T >::CTPointer const CTPointer< T > &  p  )  [inline]
 

Copy constructor

template<class T>
virtual CTPointer< T >::~CTPointer  )  [inline, virtual]
 

Destructor.

If this one gets called, it automagically decrements the usage counter of the object pointed to. If it reaches zero, then no other pointer points to the object and the object will be deleted.

Author:
Martin Preuss<martin@libchipcard.de>


Member Function Documentation

template<class T>
virtual void CTPointer< T >::_deleteObject void *  p  )  [inline, protected, virtual]
 

This method actually deletes the object. Since the base class does not know the type of this object, we have to make this method virtual. The template class MUST override this.

template<class T>
template<class U>
CTPointer<U> CTPointer< T >::cast  )  const [inline]
 

Returns a type-safe casted CTPointer of the given type.

This method returns a type-safe casted CTPointer of the given type. This obeys the same rules as a dynamic_cast<TARGET_TYPE>, and in fact internally a dynamic_cast is used.

Use it like this:

class type_X; class type_Y : public type_X;

CTPointer<type_X> pX; CTPointer<type_Y> pY = new type_Y; pX = pY.cast<type_X>();

The casting fails if it is impossibe to safely cast the "type_Y" to "type_X". In that case, an CTError is thrown. Also, if you call this method on an invalid CTPointer, a CTError is thrown.

Author:
Martin Preuss<martin@libchipcard.de>

template<class T>
T& CTPointer< T >::operator *  )  const [inline]
 

Returns a reference to the object pointed to.

If the CTPointer is invalid, this throws a CTError.

template<class T>
bool CTPointer< T >::operator!= const CTPointer< T > &  p  )  const [inline]
 

Inequality operator for the object pointed to.

This operator checks whether another pointer and this one are not pointing to the same data.

Author:
Martin Preuss<martin@libchipcard.de>

template<class T>
void CTPointer< T >::operator= const CTPointer< T > &  p  )  [inline]
 

Copy operator with another const CTPointer.

This operator handles the case where you give another pointer as argument. (like pointer1=pointer2).

Author:
Martin Preuss<martin@libchipcard.de>

template<class T>
void CTPointer< T >::operator= CTPointer< T > &  p  )  [inline]
 

Copy operator with another CTPointer.

This operator handles the case where you give another pointer as argument. (like pointer1=pointer2).

Author:
Martin Preuss<martin@libchipcard.de>

template<class T>
void CTPointer< T >::operator= T *  obj  )  [inline]
 

Copy operator with object pointed to.

This operator handles the case where you do something like this:
pointer=new structXYZ;

Author:
Martin Preuss<martin@libchipcard.de>

template<class T>
bool CTPointer< T >::operator== const CTPointer< T > &  p  )  const [inline]
 

Equality operator for the object pointed to.

This operator checks whether another pointer and this one are pointing to the same data.

Author:
Martin Preuss<martin@libchipcard.de>

template<class T>
virtual T* CTPointer< T >::ptr  )  const [inline, virtual]
 

Returns a raw pointer to the stored data.

If you can continue using only CTPointer's, you should not really need to use this. This method is necessary if and only if you need to use a "raw C pointer" of the object pointed to.

So if you need to use this method while there is still a CTPointer pointing to it, please never delete the object returned. The last remaining CTPointer's will take care of deletion.

On the other hand, if you need to use this pointer longer than the last CTPointer would exist, then either try to keep a CTPointer around long enough, or you need to consider setting CTPointerBase::setAutoDelete appropriately. (Because if the last CTPointer stops pointing to an object, then that object will get deleted unless CTPointerBase::setAutoDelete was changed.)

Author:
Martin Preuss<martin@libchipcard.de>

template<class T>
T& CTPointer< T >::ref  )  const [inline]
 

Returns a reference to the object pointed to.

If the CTPointer is invalid, this throws a CTError.

template<class T>
bool CTPointer< T >::sharingData const CTPointer< T > &  p  )  const [inline]
 

Checks whether both pointers share their data object.

This method checks whether another pointer and this one share the same internal data object and thus also the same data pointed to. This is a stronger condition than operator==. This method returns true only if one CTPointer has been copied to other CTPointer's. But as soon as some "raw C pointers" have been assigned to different CTPointer, this method would return false. In that latter case, the operator== would still return true, so that is why the operator== is more likely to be useful.

Author:
Martin Preuss<martin@libchipcard.de>


Friends And Related Function Documentation

template<class T>
friend class CTPointerCastBase< T > [friend]
 


The documentation for this class was generated from the following file:
Generated on Mon Jan 5 20:56:28 2004 for libchipcard by doxygen 1.3.4