Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Public Member Functions | Protected Attributes | List of all members
drwnSmartPointer< T > Class Template Reference

Implements a shared pointer interface to avoid the need to deep copy constant (shared) objects. More...

Public Member Functions

 drwnSmartPointer ()
 default constructor (NULL smart pointer)
 
 drwnSmartPointer (T *obj, bool bOwner=true)
 create a smart pointer of type T to obj
 
 drwnSmartPointer (const drwnSmartPointer< T > &p)
 copy constructor
 
drwnSmartPointer< T > & operator= (const drwnSmartPointer< T > &p)
 assignment operator
 
bool operator== (const drwnSmartPointer< T > &p)
 compare two smart pointers for equality
 
bool operator!= (const drwnSmartPointer< T > &p)
 compare two smart pointers for inequality
 
bool operator== (const T *o)
 compare smart pointers with object pointer for equality
 
bool operator!= (const T *o)
 compare smart pointers with object pointer for inequality
 
T * operator-> ()
 de-reference the smart pointer
 
const T * operator-> () const
 de-reference the smart pointer
 
 operator T * ()
 de-reference the smart pointer
 
 operator const T * () const
 de-reference the smart pointer
 

Protected Attributes

T * _objPtr
 pointer to the shared object
 
unsigned * _refCount
 number of drwnSmartPointer objects that share _objPtr
 
bool _bOwner
 true if drwnSmartPointer is responsible for destroying _objPtr
 

Detailed Description

template<typename T>
class drwnSmartPointer< T >

Implements a shared pointer interface to avoid the need to deep copy constant (shared) objects.

Example use:

drwnSmartPointer<TImage> A(new TImage());
DRWN_ASSERT((C == A) && (B == A));
C->showImage();

Unlike the STL's auto_ptr, it is safe to use smart pointers in STL containers.

By default the smart pointer takes ownership of the object (and will delete it when the reference count reaches zero). However, the smart pointer can also be configured to access statically allocated objects (e.g. on the stack). In this case the object is not deleted, but care must be taken to ensure that the object does not go out of scope before all smart pointers have been destroyed. Here's an example:

TImage I;

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