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

HBCI::Pointer< T > Class Template Reference

A smart pointer template class. More...

#include <pointer.h>

Inheritance diagram for HBCI::Pointer< T >:

Inheritance graph
[legend]
List of all members.

Public Methods

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

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

Protected Methods

virtual void _deleteObject (void *p)
 Pointer (const PointerBase &p)

Friends

class PointerCastBase< T >

Detailed Description

template<class T>
class HBCI::Pointer< 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 Pointer to the same object is created, the reference counter is incremented. Each time a Pointer 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:

Pointer<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<openhbci@aquamaniac.de>


Constructor & Destructor Documentation

template<class T>
HBCI::Pointer< T >::Pointer const PointerBase   p [inline, protected]
 

template<class T>
HBCI::Pointer< T >::Pointer   [inline]
 

Empty Constructor.

template<class T>
HBCI::Pointer< T >::Pointer T *    obj [inline]
 

Constructor with object to be pointing to.

template<class T>
HBCI::Pointer< T >::Pointer const Pointer< T > &    p [inline]
 

Copy constructor

template<class T>
virtual HBCI::Pointer< T >::~Pointer   [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<openhbci@aquamaniac.de>


Member Function Documentation

template<class T>
virtual void HBCI::Pointer< 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>
Pointer<U> HBCI::Pointer< T >::cast   const [inline]
 

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

This method returns a type-safe casted Pointer 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;

 Pointer<type_X> pX;
 Pointer<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 HBCI::Error is thrown. Also, if you call this method on an invalid Pointer, a HBCI::Error is thrown.

Author:
Martin Preuss<openhbci@aquamaniac.de>

template<class T>
T& HBCI::Pointer< T >::operator *   const [inline]
 

Returns a reference to the object pointed to.

If the Pointer is invalid, this throws a HBCI::Error.

template<class T>
bool HBCI::Pointer< T >::operator!= const Pointer< 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<openhbci@aquamaniac.de>

template<class T>
void HBCI::Pointer< T >::operator= const Pointer< T > &    p [inline]
 

Copy operator with another const Pointer.

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

Author:
Martin Preuss<openhbci@aquamaniac.de>

template<class T>
void HBCI::Pointer< T >::operator= Pointer< T > &    p [inline]
 

Copy operator with another Pointer.

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

Author:
Martin Preuss<openhbci@aquamaniac.de>

template<class T>
void HBCI::Pointer< 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<openhbci@aquamaniac.de>

template<class T>
bool HBCI::Pointer< T >::operator== const Pointer< 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<openhbci@aquamaniac.de>

template<class T>
virtual T* HBCI::Pointer< T >::ptr   const [inline, virtual]
 

Returns a raw pointer to the stored data.

If you can continue using only Pointer'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 Pointer pointing to it, please never delete the object returned. The last remaining Pointer's will take care of deletion.

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

Author:
Martin Preuss<openhbci@aquamaniac.de>

template<class T>
T& HBCI::Pointer< T >::ref   const [inline]
 

Returns a reference to the object pointed to.

If the Pointer is invalid, this throws a HBCI::Error.

template<class T>
virtual int HBCI::Pointer< T >::refCount   const [inline, virtual]
 

Reimplemented from HBCI::PointerBase.

template<class T>
bool HBCI::Pointer< T >::sharingData const Pointer< 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 Pointer has been copied to other Pointer's. But as soon as some "raw C pointers" have been assigned to different Pointer, 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<openhbci@aquamaniac.de>


Friends And Related Function Documentation

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


The documentation for this class was generated from the following file:
Generated on Mon Jun 23 13:41:17 2003 for openhbci by doxygen1.2.17