SmartPointer< T > Class Template Reference
[Memory handling]

List of all members.

Public Member Functions

 SmartPointer ()
 SmartPointer (const SmartPointer< T > &tt)
 SmartPointer (T *t, const char *id=0)
 ~SmartPointer ()
SmartPointer< T > & operator= (T *tt)
SmartPointer< T > & operator= (const SmartPointer< T > &tt)
 operator T * () const
T & operator* () const
T * operator-> () const
void swap (SmartPointer< T > &tt)
void swap (T *&tt)
unsigned int memory_consumption () const

Private Attributes

T * t
const char *const id


Detailed Description

template<typename T>
class SmartPointer< T >

Smart pointers avoid destruction of an object in use. They can be used just like a pointer (i.e. using the * and -> operators and through casting) but make sure that the object pointed to is not deleted in the course of use of the pointer by signalling the pointee its use.

Objects pointed to should inherit Subscriptor or must implement the same functionality. Null pointers are an exception from this rule and are allowed, too.

SmartPointer does NOT implement any memory handling! Especially, deleting a SmartPointer does not delete the object. Writing

 SmartPointer<T> dont_do_this = new T;
is a sure way to program a memory leak! The secure version is
 T* p = new T;
 {
   SmartPointer<T> t(p, "mypointer");
   ...
 }
 delete p;

Note that a smart pointer can handle constness of an object, i.e. a SmartPointer<const ABC> really behaves as if it were a pointer to a constant object (disallowing write access when dereferenced), while SmartPointer<ABC> is a mutable pointer.

Author:
Guido Kanschat, Wolfgang Bangerth, 1998 - 2005

Constructor & Destructor Documentation

template<typename T >
SmartPointer< T >::SmartPointer (  )  [inline]

Standard constructor for null pointer.

Deprecated:
Since this constructor will leave SmartPointer::id empty, it should be avoided wherever possible and replaced by SmartPointer(0,id).

template<typename T>
SmartPointer< T >::SmartPointer ( const SmartPointer< T > &  tt  )  [inline]

References SmartPointer< T >::t.

template<typename T>
SmartPointer< T >::SmartPointer ( T *  t,
const char *  id = 0 
) [inline]

Constructor taking a normal pointer. If possible, i.e. if the pointer is not a null pointer, the constructor subscribes to the given object to lock it, i.e. to prevent its destruction before the end of its use.

The id is used in the call to Subscriptor::subscribe(typeid(*this).name()) and by ~SmartPointer() in the call to Subscriptor::unsubscribe().

template<typename T >
SmartPointer< T >::~SmartPointer (  )  [inline]

Destructor, removing the subscription.

References SmartPointer< T >::t.


Member Function Documentation

template<typename T>
SmartPointer< T > & SmartPointer< T >::operator= ( T *  tt  )  [inline]

Assignment operator for normal pointers. The pointer subscribes to the new object automatically and unsubscribes to an old one if it exists. It will not try to subscribe to a null-pointer, but stilll delete the old subscription.

References SmartPointer< T >::t.

template<typename T>
SmartPointer< T > & SmartPointer< T >::operator= ( const SmartPointer< T > &  tt  )  [inline]

Assignment operator for SmartPointer. The pointer subscribes to the new object automatically and unsubscribes to an old one if it exists.

References LAPACKSupport::T, and SmartPointer< T >::t.

template<typename T >
SmartPointer< T >::operator T * (  )  const [inline]

Conversion to normal pointer.

References SmartPointer< T >::t.

template<typename T >
T & SmartPointer< T >::operator* (  )  const [inline]

Dereferencing operator.

References SmartPointer< T >::t.

template<typename T >
T * SmartPointer< T >::operator-> (  )  const [inline]

Dereferencing operator.

References SmartPointer< T >::t.

template<typename T>
void SmartPointer< T >::swap ( SmartPointer< T > &  tt  )  [inline]

Exchange the pointers of this object and the argument. Since both the objects to which is pointed are subscribed to before and after, we do not have to change their subscription counters.

Note that this function (with two arguments) and the respective functions where one of the arguments is a pointer and the other one is a C-style pointer are implemented in global namespace.

References SmartPointer< T >::t.

Referenced by swap(), and SmartPointer< T >::swap().

template<typename T>
void SmartPointer< T >::swap ( T *&  tt  )  [inline]

Swap pointers between this object and the pointer given. As this releases the object pointed to presently, we reduce its subscription count by one, and increase it at the object which we will point to in the future.

Note that we indeed need a reference of a pointer, as we want to change the pointer variable which we are given.

References SmartPointer< T >::swap(), and SmartPointer< T >::t.

template<typename T >
unsigned int SmartPointer< T >::memory_consumption (  )  const [inline]

Return an estimate of the amount of memory (in bytes) used by this class. Note in particular, that this only includes the amount of memory used by this object, not by the object pointed to.

Referenced by MGMatrix< MATRIX, VECTOR >::memory_consumption().


Member Data Documentation

template<typename T>
T* SmartPointer< T >::t [private]

Pointer to the object we want to subscribt to. Since it is often necessary to follow this pointer when debugging, we have deliberately chosen a short name.

Referenced by SmartPointer< T >::operator T *(), SmartPointer< T >::operator*(), SmartPointer< T >::operator->(), SmartPointer< T >::operator=(), SmartPointer< T >::SmartPointer(), SmartPointer< T >::swap(), and SmartPointer< T >::~SmartPointer().

template<typename T>
const char* const SmartPointer< T >::id [private]

The identification for the subscriptor.


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

deal.II documentation generated on Sat Aug 15 16:52:17 2009 by doxygen 1.5.9