PIPS-NLP
Public Member Functions | Static Public Member Functions | Protected Attributes | List of all members
SmartPointer< T > Class Template Reference

#include <SmartPointer.h>

Public Member Functions

 SmartPointer ()
 
 ~SmartPointer ()
 
 operator T * ()
 
T * ptr () const
 
 SmartPointer (T *t)
 
 SmartPointer (const SmartPointer< T > &sp)
 
T & operator* ()
 
const T & operator* () const
 
SmartPointer< T > & operator= (const SmartPointer< T > &sp)
 
T * operator-> ()
 
const T * operator-> () const
 

Static Public Member Functions

static void bind (SmartPointer< T > &sp, T **obj)
 
static void referTo (SmartPointer< T > &sp, T *obj)
 

Protected Attributes

T * obj
 

Detailed Description

template<class T>
class SmartPointer< T >

A class whose instances act like pointers that manage their reference count automatically.

Use smart pointers when you wish to keep a reference to a instance of a subclass of IotrRefCount. Do not use a smart pointer to refer to an object passed in as a parameter to a routine, unless you intend to keep the reference after the routine has exited.

Instances of this class are created by value. In other words definitions such as

SmartPointer<T> t; 

are encouraged, whereas pointers to objects of type SmartPointer<T> should not be used.

Method calls through a smart pointer look like calls to a regular pointer. In other words

class T {
public:
void myMethod();
};
// ...
{
// ...
t->myMethod();
}

is the preferred way to call T::myMethod through a smart pointer.

SmartPointers can be assigned a pointer to an object through the function SpReferTo(). It is never necessary to release a a SmartPointer, this will be done automatically whenever the SmartPointer goes out of scope. It is not possible, or necessary, to call IotrRelease() or IotrAddRef() on a SmartPointer.

Assignments between SmartPointers should work with the same semantics as assigment of traditional pointers.

//...
t = s;

or

SmartPointer<T> sp(new T);

or

T * t = new T;
sp = SmartPointer<T>(t); // or sp = (SmartPointer<T>) t;

So long as implicit conversion

sp = t;

is disallowed. SmartPointers should generally not be in the definition of parameters to routines or as return values. Use traditional pointers for these. Automatic conversion allows SmartPointers to be a "drop-in" replacement to traditional pointers when calling routines.

class T { // ...
};
// ...
void myRoutine( T * t );
//...
{
// ...
myRoutine t );
}

When returning pointers from a function, be sure to use SpAsPointer() to obtain the traditional pointer to the object.

class T { // ...
};
// ...
T * myFunction( T * t );
{
// ...
return SpAsPointer( t );
}
See also
IotrRefCount
ReferenceCounting

Constructor & Destructor Documentation

template<class T>
SmartPointer< T >::SmartPointer ( )
inline

Default constructor; creates a SmartPointer referring to nothing.

template<class T>
SmartPointer< T >::SmartPointer ( const SmartPointer< T > &  sp)
inline

Copy constructor; creates a new smart pointer referring to the same object. Increments the reference count of the object.

Parameters
span existing reference to an object. This reference may be nil.
template<class T>
SmartPointer< T >::~SmartPointer ( )
inline

Destructor; release the object (through a call to IotrRelease()) if the object is not nil.

template<class T>
SmartPointer< T >::SmartPointer ( T *  t)
inline

Allow EXPLICIT conversion from a (T*) to a SmartPointer to T. Implicit conversion is not allowed, because this could have subtle, unintended consequences.

Member Function Documentation

template<class T>
static void SmartPointer< T >::bind ( SmartPointer< T > &  sp,
T **  obj 
)
inlinestatic

Converts a traditional pointer to a smart pointer, destroying the original pointer.

template<class T>
SmartPointer< T >::operator T * ( )
inline

Automatically convert this object to a traditional pointer to an object that may be passed in as a paramter to function or method calls. For most other purposes SmartPointer::ptr() is more appropriate

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

Dereferencing operation. Return a reference to the object.

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

Send a message to, or access a data memeber of, the object to which this is a reference.

template<class T>
const T* SmartPointer< T >::operator-> ( ) const
inline
template<class T>
SmartPointer<T>& SmartPointer< T >::operator= ( const SmartPointer< T > &  sp)
inline

Assignment operator; cause this SmartPointer to refer to the same object that sp refers to.

Parameters
span existing reference to an object. This reference may be nil.
template<class T>
T* SmartPointer< T >::ptr ( ) const
inline

Call SpAsPointer() instead; return a traditional pointer to the underlying object.

template<class T>
static void SmartPointer< T >::referTo ( SmartPointer< T > &  sp,
T *  obj 
)
inlinestatic

Call SpReferTo() instead of this method; make a SmartPointer refer to the same object as a traditional pointer.

Member Data Documentation

template<class T>
T* SmartPointer< T >::obj
protected

a traditional pointer to the object being referenced.


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