Loading...
Searching...
No Matches
SPtrHolder< T > Class Template Reference
casa package (libcasa_casa) » Utilities_module

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-> () const
| Make it possible to use -> on the pointer object.
T * | ptr ()
| Get the pointer for use.
const T * | ptr () const

## Private Member Functions

| SPtrHolder (const SPtrHolder< T > &other)
| SPrtHolder cannot be copied.
SPtrHolder< T > & | operator= (const SPtrHolder< T > &other)

## Private Attributes

T * | itsPtr

## Detailed Description

template<class T>
class SPtrHolder< T >

Hold and delete pointers not deleted by object destructors.

### Intended use:

Public interface

### Review Status

Test programs:
  tPtrHolder

### Prerequisite

- module Exceptions

### Synopsis

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

### Example

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
SPtrHolder::ptr
T * ptr()
Get the pointer for use.
Definition PtrHolder.h:238
SPtrHolder::SPtrHolder
SPtrHolder(T *ptr=0)
Construct an SPtrHolder from a pointer which MUST have been allocated from new, since the destructor ...
Definition PtrHolder.h:205

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'

### Motivation

std::auto_ptr 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 196 of file PtrHolder.h.

## Constructor & Destructor Documentation

## SPtrHolder() [1/2]

template<class T>

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 205 of file PtrHolder.h.

References itsPtr, and ptr().

Referenced by operator=(), and SPtrHolder().

## ~SPtrHolder()

template<class T>

SPtrHolder< T >::~SPtrHolder | ( |  | ) |

inline

Definition at line 208 of file PtrHolder.h.

References itsPtr.

## SPtrHolder() [2/2]

template<class T>

SPtrHolder< T >::SPtrHolder | ( | const SPtrHolder< T > & | other | ) |

private

SPrtHolder cannot be copied.

References SPtrHolder().

## Member Function Documentation

## operator*() [1/2]

template<class T>

T & SPtrHolder< T >::operator* | ( |  | ) |

inline

Make it possible to dereference the pointer object.

Definition at line 226 of file PtrHolder.h.

References itsPtr.

## operator*() [2/2]

template<class T>

const T & SPtrHolder< T >::operator* | ( |  | ) | const

inline

Definition at line 228 of file PtrHolder.h.

References itsPtr.

## operator->()

template<class T>

T * SPtrHolder< T >::operator-> | ( |  | ) | const

inline

Make it possible to use -> on the pointer object.

Definition at line 233 of file PtrHolder.h.

References itsPtr.

## operator=()

template<class T>

SPtrHolder< T > & SPtrHolder< T >::operator= | ( | const SPtrHolder< T > & | other | ) |

private

References SPtrHolder().

## ptr() [1/2]

template<class T>

T * SPtrHolder< T >::ptr | ( |  | ) |

inline

Get the pointer for use.

Definition at line 238 of file PtrHolder.h.

References itsPtr.

Referenced by reset(), SPtrHolder(), and transfer().

## ptr() [2/2]

template<class T>

const T * SPtrHolder< T >::ptr | ( |  | ) | const

inline

Definition at line 240 of file PtrHolder.h.

References itsPtr.

## release()

template<class T>

void SPtrHolder< T >::release | ( |  | ) |

inline

Release the pointer.

Definition at line 221 of file PtrHolder.h.

References itsPtr.

## reset()

template<class T>

void SPtrHolder< T >::reset | ( | T * | ptr | ) |

inline

Reset the pointer.

Definition at line 212 of file PtrHolder.h.

References itsPtr, and ptr().

## transfer()

template<class T>

T * 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 217 of file PtrHolder.h.

References itsPtr, and ptr().

## Member Data Documentation

## itsPtr

template<class T>

T* SPtrHolder< T >::itsPtr

private

Definition at line 252 of file PtrHolder.h.

Referenced by operator*(), operator*(), operator->(), ptr(), ptr(), release(), reset(), SPtrHolder(), transfer(), and ~SPtrHolder().

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

- casa/Utilities/PtrHolder.h