casacore
|
Hold and delete pointers not deleted by object destructors. More...
#include <PtrHolder.h>
Public Member Functions | |
PtrHolder () | |
The default constructor uses a null pointer. More... | |
PtrHolder (T *pointer, Bool isCArray=False) | |
Construct a PtrHolder from a pointer which MUST have been allocated from new , since the destructor will call delete on it. More... | |
~PtrHolder () | |
void | set (T *pointer, Bool isCarray=False, Bool deleteCurrentPtr=True) |
Set the pointer to a new value. More... | |
void | clear (Bool deleteCurrentPtr=True) |
Set the current pointer to null; if deletePtr is True (the default), then the current pointer is deleted first. More... | |
T * | ptr () |
Release the pointer for use. More... | |
const T * | ptr () const |
operator T * () | |
Attempt to automatically release a pointer when required. More... | |
operator T * () const | |
Hold and delete pointers not deleted by object destructors.
Public interface
PtrHolder
s hold allocated pointers which should be deleted when an exception is thrown. Exceptions only call destructors of objects. Thus, for example, storage allocated in a global function (outside of an object)is not deleted. A PtrHolder
solves this problem: it merely holds the pointer and deletes it when it is destroyed itself, e.g. when an exception is thrown or when the function exits normally.
Avoid leaks when throwing/catching exceptions.
Definition at line 81 of file PtrHolder.h.
casacore::PtrHolder< T >::PtrHolder | ( | ) |
The default constructor uses a null pointer.
casacore::PtrHolder< T >::PtrHolder | ( | T * | pointer, |
Bool | isCArray = False |
||
) |
Construct a PtrHolder
from a pointer which MUST have been allocated from new
, since the destructor will call delete
on it.
If the pointer is to an array, i.e. allocated with operator new[]
, then isCarray
should be set to True. (This parameter is required because C-arrays need to be deleted with delete[]
.)
After the pointer is placed into the holder, the user should not manually delete the pointer; the PtrHolder
object will do that, unless set()
or clear()
is called with deleteCurrentPtr
set to False. The pointer must also only be put into one holder to avoid double deletion.
casacore::PtrHolder< T >::~PtrHolder | ( | ) |
void casacore::PtrHolder< T >::clear | ( | Bool | deleteCurrentPtr = True | ) |
Set the current pointer to null; if deletePtr
is True (the default), then the current pointer is deleted first.
|
inline |
Attempt to automatically release a pointer when required.
If the compiler can't figure it out, you can use the ptr()
member function directly.
Definition at line 124 of file PtrHolder.h.
|
inline |
Definition at line 125 of file PtrHolder.h.
|
inline |
Release the pointer for use.
Definition at line 117 of file PtrHolder.h.
|
inline |
Definition at line 118 of file PtrHolder.h.
void casacore::PtrHolder< T >::set | ( | T * | pointer, |
Bool | isCarray = False , |
||
Bool | deleteCurrentPtr = True |
||
) |
Set the pointer to a new value.
If deleteCurrentPtr
is True (the default), then delete the existing pointer first. If isCarray
is True, then the new pointer is assumed to have been allocated with new[]
.