casa
$Rev:20696$
|
Hold and delete pointers not deleted by object destructors. More...
#include <PtrHolder.h>
Public Member Functions | |
SPtrHolder (T *ptr=0) | |
Construct an SPtrHolder from a pointer which MUST have been allocated from new , since the destructor will After the pointer is placed into the holder, the user should not manually delete the pointer unless the transfer function is called. | |
~SPtrHolder () | |
void | reset (T *ptr) |
Reset the pointer. | |
T * | transfer () |
Transfer ownership of the pointer. | |
void | release () |
Release the pointer. | |
T & | operator* () |
Make it possible to dereference the pointer object. | |
const T & | operator* () const |
T * | operator-> () |
Make it possible to use -> on the pointer object. | |
const T * | operator-> () const |
T * | ptr () |
Get the pointer for use. | |
const T * | ptr () const |
Private Member Functions | |
SPtrHolder (const SPtrHolder< T > &other) | |
SPtrHolder< T > & | operator= (const SPtrHolder< T > &other) |
Private Attributes | |
T * | itsPtr |
Hold and delete pointers not deleted by object destructors.
Public interface
SPtrHolder
s hold allocated pointers to non-array objects which should be deleted when an exception is thrown. SPtrHolder is similar to PtrHolder, but easier to use and only valid for pointer to a single object, thus not to a C-array of objects.
void func(Table *ptr); // some other function that takes a pointer // ..\. // True below means it's an array, False (the default) would mean // a singleton object. SPtrHolder<Int> iholder(new Table(...)); func(iholder); // converts automatically to ptr Table* tab = iholder.transfer(); // transfer ownership
If an exception is thrown in function func
, the Table will be deleted automatically. After the function call, the ownership is tranfered back to the 'user'
std::autoptr
is harder to use and its future is unclear.
PtrHolder
is not fully inlined and has C-array overhead. Furthermore the automatic conversion to a T* is dangerous, because the programmer may not be aware that the pointer is maybe taken over.
Definition at line 194 of file PtrHolder.h.
casa::SPtrHolder< T >::SPtrHolder | ( | T * | ptr = 0 | ) | [inline, explicit] |
Construct an SPtrHolder
from a pointer which MUST have been allocated from new
, since the destructor will After the pointer is placed into the holder, the user should not manually delete the pointer unless the transfer function is called.
The pointer must also only be put into one holder to avoid double deletion.
Definition at line 203 of file PtrHolder.h.
casa::SPtrHolder< T >::~SPtrHolder | ( | ) | [inline] |
Definition at line 206 of file PtrHolder.h.
References casa::SPtrHolder< T >::itsPtr.
casa::SPtrHolder< T >::SPtrHolder | ( | const SPtrHolder< T > & | other | ) | [private] |
SPrtHolder cannot be copied.
T& casa::SPtrHolder< T >::operator* | ( | ) | [inline] |
Make it possible to dereference the pointer object.
Definition at line 224 of file PtrHolder.h.
References casa::SPtrHolder< T >::itsPtr.
const T& casa::SPtrHolder< T >::operator* | ( | ) | const [inline] |
Definition at line 226 of file PtrHolder.h.
References casa::SPtrHolder< T >::itsPtr.
T* casa::SPtrHolder< T >::operator-> | ( | ) | [inline] |
Make it possible to use -> on the pointer object.
Definition at line 232 of file PtrHolder.h.
References casa::SPtrHolder< T >::itsPtr.
const T* casa::SPtrHolder< T >::operator-> | ( | ) | const [inline] |
Definition at line 234 of file PtrHolder.h.
References casa::SPtrHolder< T >::itsPtr.
SPtrHolder<T>& casa::SPtrHolder< T >::operator= | ( | const SPtrHolder< T > & | other | ) | [private] |
T* casa::SPtrHolder< T >::ptr | ( | ) | [inline] |
Get the pointer for use.
Definition at line 240 of file PtrHolder.h.
References casa::SPtrHolder< T >::itsPtr.
Referenced by casa::SPtrHolder< T >::reset(), and casa::SPtrHolder< T >::transfer().
const T* casa::SPtrHolder< T >::ptr | ( | ) | const [inline] |
Definition at line 242 of file PtrHolder.h.
References casa::SPtrHolder< T >::itsPtr.
void casa::SPtrHolder< T >::release | ( | ) | [inline] |
Release the pointer.
Definition at line 219 of file PtrHolder.h.
References casa::SPtrHolder< T >::itsPtr.
void casa::SPtrHolder< T >::reset | ( | T * | ptr | ) | [inline] |
Reset the pointer.
Definition at line 210 of file PtrHolder.h.
References casa::SPtrHolder< T >::itsPtr, and casa::SPtrHolder< T >::ptr().
T* casa::SPtrHolder< T >::transfer | ( | ) | [inline] |
Transfer ownership of the pointer.
I.e. return the pointer and set it to 0 in the object.
Definition at line 215 of file PtrHolder.h.
References casa::SPtrHolder< T >::itsPtr, and casa::SPtrHolder< T >::ptr().
T* casa::SPtrHolder< T >::itsPtr [private] |
Definition at line 254 of file PtrHolder.h.
Referenced by casa::SPtrHolder< T >::operator*(), casa::SPtrHolder< T >::operator->(), casa::SPtrHolder< T >::ptr(), casa::SPtrHolder< T >::release(), casa::SPtrHolder< T >::reset(), casa::SPtrHolder< T >::transfer(), and casa::SPtrHolder< T >::~SPtrHolder().