#include <ctpointer.h>
Inheritance diagram for CTPointer< T >:
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> |
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;
|
|
|
Empty Constructor. |
|
Constructor with object to be pointing to. |
|
Copy constructor |
|
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.
|
|
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. |
|
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 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.
|
|
Returns a reference to the object pointed to. If the CTPointer is invalid, this throws a CTError. |
|
Inequality operator for the object pointed to. This operator checks whether another pointer and this one are not pointing to the same data.
|
|
Copy operator with another const CTPointer. This operator handles the case where you give another pointer as argument. (like pointer1=pointer2).
|
|
Copy operator with another CTPointer. This operator handles the case where you give another pointer as argument. (like pointer1=pointer2).
|
|
Copy operator with object pointed to.
This operator handles the case where you do something like this:
|
|
Equality operator for the object pointed to. This operator checks whether another pointer and this one are pointing to the same data.
|
|
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.)
|
|
Returns a reference to the object pointed to. If the CTPointer is invalid, this throws a CTError. |
|
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
|
|
|