casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Public Member Functions | Private Member Functions | Private Attributes
casa::PtrHolder< T > Class Template Reference

Hold and delete pointers not deleted by object destructors. More...

#include <PtrHolder.h>

List of all members.

Public Member Functions

 PtrHolder ()
 The default constructor uses a null pointer.
 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.
 ~PtrHolder ()
void set (T *pointer, Bool isCarray=False, Bool deleteCurrentPtr=True)
 Set the pointer to a new value.
void clear (Bool deleteCurrentPtr=True)
 Set the current pointer to null; if deletePtr is True (the default), then the current pointer is deleted first.
T * ptr ()
 Release the pointer for use.
const T * ptr () const
 operator T * ()
 Attempt to automatically release a pointer when required.
 operator const T * () const
Bool isCArray () const
 See if the pointer points to a C-array.

Private Member Functions

 PtrHolder (const PtrHolder< T > &other)
PtrHolder< T > & operator= (const PtrHolder< T > &other)
void delete_pointer_if_necessary ()

Private Attributes

T * ptr_p
Bool isCarray_p

Detailed Description

template<class T>
class casa::PtrHolder< T >

Hold and delete pointers not deleted by object destructors.

Intended use:

Public interface

Review Status

Reviewed By:
troberts
Date Reviewed:
1995/07/29
Test programs:
tPtrHolder

Prerequisite

Synopsis

PtrHolders 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.

Example

       void func(Int *ptr); // some other function that takes a pointer
       // ..\.
       // True below means it's an array, False (the default) would mean
       // a singleton object.
       PtrHolder<Int> iholder(new Int[10000], True);
       func(iholder);                           // converts automatically to ptr
       (iholder.ptr() + 5) = 11;                // use pointer explicitly
       some_function_that_throws_exception();   // pointer is deleted

Motivation

Avoid leaks when throwing/catching exceptions.

To Do

Definition at line 81 of file PtrHolder.h.


Constructor & Destructor Documentation

template<class T>
casa::PtrHolder< T >::PtrHolder ( )

The default constructor uses a null pointer.

template<class T>
casa::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.

template<class T>
casa::PtrHolder< T >::~PtrHolder ( )
template<class T>
casa::PtrHolder< T >::PtrHolder ( const PtrHolder< T > &  other) [private]

Member Function Documentation

template<class T>
void casa::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.

template<class T>
void casa::PtrHolder< T >::delete_pointer_if_necessary ( ) [private]
template<class T>
Bool casa::PtrHolder< T >::isCArray ( ) const [inline]

See if the pointer points to a C-array.

Definition at line 130 of file PtrHolder.h.

template<class T>
casa::PtrHolder< T >::operator const T * ( ) const [inline]

Definition at line 126 of file PtrHolder.h.

References casa::PtrHolder< T >::ptr_p.

template<class T>
casa::PtrHolder< T >::operator T * ( ) [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 125 of file PtrHolder.h.

References casa::PtrHolder< T >::ptr_p.

template<class T>
PtrHolder<T>& casa::PtrHolder< T >::operator= ( const PtrHolder< T > &  other) [private]
template<class T>
T* casa::PtrHolder< T >::ptr ( ) [inline]

Release the pointer for use.

Definition at line 117 of file PtrHolder.h.

template<class T>
const T* casa::PtrHolder< T >::ptr ( ) const [inline]

Definition at line 118 of file PtrHolder.h.

template<class T>
void casa::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[].


Member Data Documentation

template<class T>
Bool casa::PtrHolder< T >::isCarray_p [private]

Definition at line 146 of file PtrHolder.h.

Referenced by casa::PtrHolder< Measure >::isCArray().

template<class T>
T* casa::PtrHolder< T >::ptr_p [private]

The documentation for this class was generated from the following file: