casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Private Member Functions | Private Attributes | List of all members
SPtrHolder< T > Class Template Reference

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. More...
 
 ~SPtrHolder ()
 
void reset (T *ptr)
 Reset the pointer. More...
 
T * transfer ()
 Transfer ownership of the pointer. More...
 
void release ()
 Release the pointer. More...
 
T & operator* ()
 Make it possible to dereference the pointer object. More...
 
const T & operator* () const
 
T * operator-> () const
 Make it possible to use -> on the pointer object. More...
 
T * ptr ()
 Get the pointer for use. More...
 
const T * ptr () const
 

Private Member Functions

 SPtrHolder (const SPtrHolder< T > &other)
 SPrtHolder cannot be copied. More...
 
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

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

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

Constructor & Destructor Documentation

template<class T>
SPtrHolder< T >::SPtrHolder ( T *  ptr = 0)
inlineexplicit

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

template<class T>
SPtrHolder< T >::~SPtrHolder ( )
inline

Definition at line 209 of file PtrHolder.h.

template<class T>
SPtrHolder< T >::SPtrHolder ( const SPtrHolder< T > &  other)
private

SPrtHolder cannot be copied.

Member Function Documentation

template<class T>
T& SPtrHolder< T >::operator* ( )
inline

Make it possible to dereference the pointer object.

Definition at line 227 of file PtrHolder.h.

template<class T>
const T& SPtrHolder< T >::operator* ( ) const
inline

Definition at line 229 of file PtrHolder.h.

template<class T>
T* SPtrHolder< T >::operator-> ( ) const
inline

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

Definition at line 234 of file PtrHolder.h.

template<class T>
SPtrHolder<T>& SPtrHolder< T >::operator= ( const SPtrHolder< T > &  other)
private
template<class T>
T* SPtrHolder< T >::ptr ( )
inline

Get the pointer for use.

Definition at line 239 of file PtrHolder.h.

template<class T>
const T* SPtrHolder< T >::ptr ( ) const
inline

Definition at line 241 of file PtrHolder.h.

template<class T>
void SPtrHolder< T >::release ( )
inline

Release the pointer.

Definition at line 222 of file PtrHolder.h.

template<class T>
void SPtrHolder< T >::reset ( T *  ptr)
inline

Reset the pointer.

Definition at line 213 of file PtrHolder.h.

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

Member Data Documentation

template<class T>
T* SPtrHolder< T >::itsPtr
private

Definition at line 253 of file PtrHolder.h.


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