OBT::GuardedPointerT< T > Class Template Reference

Guarded pointer. More...

#include <OBTWisePointer.h>

Inheritance diagram for OBT::GuardedPointerT< T >:
[legend]
Collaboration diagram for OBT::GuardedPointerT< T >:
[legend]

List of all members.

Public Member Functions

 GuardedPointerT ()
 Default constructor, a null pointer.
 GuardedPointerT (T *o)
 Constructor with a pointer.
 GuardedPointerT (const GuardedPointerT< T > &p)
 Copy constructor.
virtual ~GuardedPointerT ()
bool isNull () const
 Is the pointer null ?
Copy operators



GuardedPointerT< T > & operator= (T *o)
 Copy with a pointer.
GuardedPointerT< T > & operator= (const GuardedPointerT< T > &p)
 Copy with a guarded pointer.
Comparison operators



bool operator== (T *p) const
 Equal with a pointer.
bool operator!= (T *p) const
 Not equal with a pointer.

Access operators



T * _object
 Pointed object, typed version.
T * operator-> () const
 Get the pointed object member.
T & operator* () const
 Get the reference to the pointed object.
 operator T * () const
 Get the pointed object pointer.
virtual void resetPointedObject ()
 Reset the pointed object to null.
void setPointedObject (T *o)
 Set the pointed object to a new value.

Detailed Description

template<class T>
class OBT::GuardedPointerT< T >

Guarded pointer.

Author:
Benoit Chanclou
Parameters:
T the type of the pointed object. The object must be a son of OBT::PointedObject.

A pointer of this type is reset when the pointed object is destroyed. This ensures that you never try to work with a deleted object.

Usage:
Create your class
 class MyObject : public OBT::PointedObject
 {
   ...
 } ;
 *
use it
 OBT::GuardedPointerT< MyObject > a = new MyObject ;
 ...
 OBT::GuardedPointerT< MyObject > b = a ;
 ...
 b->doSomething() ;
 ...
 delete b ; // now both a and b are null
 assert( a.isNull() == true ) ;
 assert( b.isNull() == true ) ;
 ...
 *

Definition at line 80 of file OBTWisePointer.h.


Constructor & Destructor Documentation

template<class T>
OBT::GuardedPointerT< T >::GuardedPointerT (  )  [inline]

Default constructor, a null pointer.

Definition at line 84 of file OBTWisePointer.h.

References OBT::GuardedPointerT< T >::setPointedObject().

00084 : GuardedPointer(), _object( 0 ) { setPointedObject( 0 ) ; }

template<class T>
OBT::GuardedPointerT< T >::GuardedPointerT ( T *  o  )  [inline]

Constructor with a pointer.

Definition at line 86 of file OBTWisePointer.h.

References OBT::GuardedPointerT< T >::setPointedObject().

00086 : GuardedPointer(), _object( 0 ) { setPointedObject( o ) ; }

template<class T>
OBT::GuardedPointerT< T >::GuardedPointerT ( const GuardedPointerT< T > &  p  )  [inline]

Copy constructor.

Definition at line 88 of file OBTWisePointer.h.

References OBT::GuardedPointerT< T >::_object, and OBT::GuardedPointerT< T >::setPointedObject().

00088 : GuardedPointer(), _object( 0 ) { setPointedObject( p._object ) ; }

template<class T>
virtual OBT::GuardedPointerT< T >::~GuardedPointerT (  )  [inline, virtual]

Definition at line 90 of file OBTWisePointer.h.

00090 {}


Member Function Documentation

template<class T>
bool OBT::GuardedPointerT< T >::isNull (  )  const [inline]

Is the pointer null ?

Definition at line 109 of file OBTWisePointer.h.

References OBT::GuardedPointerT< T >::_object.

00109 { return !_object ; }

template<class T>
OBT::GuardedPointerT< T >::operator T * (  )  const [inline]

Get the pointed object pointer.

Definition at line 119 of file OBTWisePointer.h.

References OBT::GuardedPointerT< T >::_object.

00119 { return _object ; }

template<class T>
bool OBT::GuardedPointerT< T >::operator!= ( T *  p  )  const [inline]

Not equal with a pointer.

Definition at line 105 of file OBTWisePointer.h.

00105 { return !( *this == p ) ; }

template<class T>
T& OBT::GuardedPointerT< T >::operator* (  )  const [inline]

Get the reference to the pointed object.

Definition at line 117 of file OBTWisePointer.h.

References OBT::GuardedPointerT< T >::_object.

00117 { return *_object ; }

template<class T>
T* OBT::GuardedPointerT< T >::operator-> (  )  const [inline]

Get the pointed object member.

Definition at line 115 of file OBTWisePointer.h.

References OBT::GuardedPointerT< T >::_object.

00115 { return _object ; }

template<class T>
GuardedPointerT< T >& OBT::GuardedPointerT< T >::operator= ( const GuardedPointerT< T > &  p  )  [inline]

Copy with a guarded pointer.

Definition at line 97 of file OBTWisePointer.h.

References OBT::GuardedPointerT< T >::_object, and OBT::GuardedPointerT< T >::setPointedObject().

00097 { setPointedObject( p._object ) ; return *this ; }

template<class T>
GuardedPointerT< T >& OBT::GuardedPointerT< T >::operator= ( T *  o  )  [inline]

Copy with a pointer.

Definition at line 95 of file OBTWisePointer.h.

References OBT::GuardedPointerT< T >::setPointedObject().

00095 { setPointedObject( o ) ; return *this ; }

template<class T>
bool OBT::GuardedPointerT< T >::operator== ( T *  p  )  const [inline]

Equal with a pointer.

Definition at line 103 of file OBTWisePointer.h.

References OBT::GuardedPointerT< T >::_object.

00103 { return _object == p ; }

template<class T>
virtual void OBT::GuardedPointerT< T >::resetPointedObject (  )  [inline, private, virtual]

Reset the pointed object to null.

Reimplemented from OBT::GuardedPointer.

Definition at line 124 of file OBTWisePointer.h.

References OBT::GuardedPointerT< T >::_object.

template<class T>
void OBT::GuardedPointerT< T >::setPointedObject ( T *  o  )  [inline, private]

Set the pointed object to a new value.

Definition at line 126 of file OBTWisePointer.h.

References OBT::GuardedPointer::_callerObject, OBT::GuardedPointerT< T >::_object, OBT::PointedObject::addPointer(), and OBT::PointedObject::removePointer().

Referenced by OBT::GuardedPointerT< T >::GuardedPointerT(), and OBT::GuardedPointerT< T >::operator=().

00127   {
00128     if( _object != o )
00129     {
00130       // Remove the pointer from the previous object
00131       if( _callerObject ) _callerObject->removePointer( this ) ;
00132       // Set the object
00133       _callerObject = o ;
00134       _object = o ;
00135       // Add the pointer to the object
00136       if( _callerObject ) _callerObject->addPointer( this ) ;
00137     }
00138   }


Member Data Documentation

template<class T>
T* OBT::GuardedPointerT< T >::_object [private]

Generated on 1 Jan 2010 for OBT by  doxygen 1.6.1