Main Page   Class Hierarchy   Alphabetical List   Compound List   Compound Members  

util::SmartPtr< T > Class Template Reference

#include <SmartPtr.h>

Inheritance diagram for util::SmartPtr< T >:

util::Object List of all members.

Detailed Description

template<class T>
class util::SmartPtr< T >

A smart pointer class.

SmartPtr provides much the same functionality as its STL counterpart auto_ptr. The main difference is that it is derived from Object. It also has some sophisticated features that are missing from auto_ptr.


Public Methods

 SmartPtr (const SmartPtr &other)
 Copy a SmartPtr instance.

 SmartPtr (T *ptr=NULL, bool owner=true, bool array=false)
 Initialize the internal pointer with ptr.

 ~SmartPtr ()
 Destroy a SmartPtr instance and delete the the internal pointer if it is owned by *this.

void reset (T *ptr=NULL)
 Set the internal pointer to a value.

T * release () const
 Release the ownership of the internal pointer and return it.

T * get ()
 Return the internal pointer.

const T * get () const
 Return the internal pointer.

 operator T * ()
 Cast this SmartPtr to a pointer to the type of the internal pointer.

 operator const T * () const
 Cast this SmartPtr to a pointer to the type of the internal pointer.

 operator T & ()
 Cast this SmartPtr to a reference of the type of the internal pointer.

 operator const T & () const
 Cast this SmartPtr to a reference of the type of the internal pointer.

T & operator * ()
 Get a reference to the object pointed to by the internal pointer.

T * operator-> ()
 Return the internal pointer.

const T * operator-> () const
 Return the internal pointer.

SmartPtr & operator= (T *ptr)
 Set the internal pointer to a value.

SmartPtr & operator= (const SmartPtr &other)
 Copy another SmartPtr instance.

bool operator== (const SmartPtr< T > &other) const
 Compare two SmartPtrs.

void setOwner (bool owner)
 Set the ownership of the internal pointer.

bool isArray () const
 Check whether the internal pointer is an array.

void setArray (bool array)
 Set the type of the internal pointer.


Constructor & Destructor Documentation

template<class T>
util::SmartPtr< T >::SmartPtr const SmartPtr< T > &    other [inline]
 

Copy a SmartPtr instance.

If other owns its internal pointer, the ownership of the pointer is transferred to *this.

template<class T>
util::SmartPtr< T >::SmartPtr T *    ptr = NULL,
bool    owner = true,
bool    array = false
[inline, explicit]
 

Initialize the internal pointer with ptr.

If owner is true (as it is by default), the new SmartPtr instance becomes the owner of the pointer, and deletes it automatically if its ownership is not explicitly released. If array is true, then delete[] is used instead of delete when the pointer needs to be destroyed.


Member Function Documentation

template<class T>
bool util::SmartPtr< T >::isArray   const [inline]
 

Check whether the internal pointer is an array.

If it is, delete[] is used instead of delete when destroying the pointer.

template<class T>
util::SmartPtr< T >::operator const T &   const [inline]
 

Cast this SmartPtr to a reference of the type of the internal pointer.

(Const version)

template<class T>
util::SmartPtr< T >::operator const T *   const [inline]
 

Cast this SmartPtr to a pointer to the type of the internal pointer.

This method simply returns the intenal pointer. (Const version)

template<class T>
util::SmartPtr< T >::operator T *   [inline]
 

Cast this SmartPtr to a pointer to the type of the internal pointer.

This method simply returns the intenal pointer.

template<class T>
const T* util::SmartPtr< T >::operator->   const [inline]
 

Return the internal pointer.

Const version.

template<class T>
SmartPtr& util::SmartPtr< T >::operator= const SmartPtr< T > &    other [inline]
 

Copy another SmartPtr instance.

If *this is owner of its internal pointer, it is deleted first. If other is owner of its internal pointer, the ownership is transferred to *this. As a result, both SmartPtr instances point to the same memory location.

template<class T>
SmartPtr& util::SmartPtr< T >::operator= T *    ptr [inline]
 

Set the internal pointer to a value.

See also:
reset(T*)

template<class T>
bool util::SmartPtr< T >::operator== const SmartPtr< T > &    other const [inline]
 

Compare two SmartPtrs.

Returns true if and only if the internal pointers are the same.

template<class T>
T* util::SmartPtr< T >::release   const [inline]
 

Release the ownership of the internal pointer and return it.

The internal pointer remains usable, but it is no longer owned by *this.

template<class T>
void util::SmartPtr< T >::reset T *    ptr = NULL [inline]
 

Set the internal pointer to a value.

If the current pointer is owned by *this, it is deleted. *this becomes the owner of the given pointer. The type (i.e. array/not array) of the internal pointer is assumed to remain intact.

template<class T>
void util::SmartPtr< T >::setArray bool    array [inline]
 

Set the type of the internal pointer.

Parameters:
array  if true, delete[] is used instead of delete (the default) when destroying the internal pointer

template<class T>
void util::SmartPtr< T >::setOwner bool    owner [inline]
 

Set the ownership of the internal pointer.

Parameters:
owner  if true, this SmartPtr is allowed to release its internal pointer


The documentation for this class was generated from the following file:
Documentation generated on 11.09.2003 with Doxygen.
The documentation is copyrighted material.
Copyright © Topi Mäenpää 2003. All rights reserved.