OBT::SmartPointer< TPointeeType > Class Template Reference

Smart pointer template class. More...

#include <OBTSmartPointer.h>

Collaboration diagram for OBT::SmartPointer< TPointeeType >:
[legend]

List of all members.

Public Types

typedef TPointeeType pointee_type

Public Member Functions

 SmartPointer ()
 default constructor
 SmartPointer (TPointeeType *pointer)
 constructor
 SmartPointer (const SmartPointer< TPointeeType > &SmartPointer)
 copy constructor
virtual ~SmartPointer ()
 destructor
const SmartPointer
< TPointeeType > & 
operator= (const SmartPointer< TPointeeType > &pointer)
 assignment operator
TPointeeType * operator-> () const
 allows a smart pointer object to act as though it were the actual encapsulated pointee through indirection.
TPointeeType & operator* () const
 allows a smart pointer object to act as though it were the actual encapsulated pointee when dereferenced.
bool operator! () const
 enables if (!sp) .
 operator TPointeeType * ()
 user-defined conversion to TPointeeType*
 operator void * ()
 added conversion to void*, to prevent delete from compiling and becomes ambiguous
unsigned int getReferenceCount () const

Private Member Functions

 SmartPointer (TPointeeType *pointer, unsigned long instance)
 constructor explicitly setting the instance ID, used by smartObjectsMap
void increaseCounter ()
 increase the reference counter
void decreaseCounter ()
 decrease the reference counter

Private Attributes

unsigned int * _referenceCounter
 Reference counting tracks the number of smart pointers that point to the same object.
TPointeeType * _pointee
 stores a copy of TPointeeType for each aspect

Friends

template<class TTo , class TFrom >
const SmartPointer< TTo > & staticCast (const SmartPointer< TFrom > &from)
 the StaticCast function permits to explicitly cast a smart pointer to a pointee to a smart pointer to a related pointee
template<class TTo , class TFrom >
SmartPointer< TTo > & constCast (const SmartPointer< TFrom > &from)
 the ConstCast function permits to explicitly cast a const smart pointer to a pointee to a non-const smart pointer to a related pointee
non-templated comparison operators

why should we keep the nontemplated operators the ones that take the pointee type? They never get a chance to match, because the template matches any pointer type, including the pointee type itself.

The rule that "never" actually means "almost never" applies here, too. In the test if (sp == 0), the compiler tries the following matches.

  • The templated operators. They don't match because zero is not a pointer type. A literal zero can be implicitly converted to a pointer type, but template matching does not include conversions.
  • The nontemplated operators. After eliminating the templated operators, the compiler tries the nontemplated ones. One of these operators kicks in through an implicit conversion from the literal zero to the pointee type. Had the nontemplated operators not existed, the test would have been an error. In conclusion, we need both the nontemplated and the templated comparison operators.


bool operator== (const SmartPointer< TPointeeType > &lhs, const TPointeeType *rhs)
bool operator== (const TPointeeType *lhs, const SmartPointer< TPointeeType > &rhs)
bool operator!= (const SmartPointer< TPointeeType > &lhs, const TPointeeType *rhs)
bool operator!= (const TPointeeType *lhs, const SmartPointer< TPointeeType > &rhs)

template comparison operators

if you provide an automatic conversion to the pointee type, there still is the risk of ambiguities.

The templated operators are "greedy" in the sense that they match comparisons with any pointer type whatsoever, thus consuming the ambiguity.



template<class U >
bool operator== (const SmartPointer< U > &rhs) const
template<class U >
bool operator!= (const SmartPointer< U > &rhs) const
template<class TPointeeType , class U >
bool operator== (const SmartPointer< TPointeeType > &lhs, const U *rhs)
template<class U , class TPointeeType >
bool operator== (const U *lhs, const SmartPointer< TPointeeType > &rhs)
template<class U >
bool operator!= (const SmartPointer< TPointeeType > &lhs, const U *rhs)
template<class U >
bool operator!= (const U *lhs, const SmartPointer< TPointeeType > &rhs)

Detailed Description

template<class TPointeeType>
class OBT::SmartPointer< TPointeeType >

Smart pointer template class.

Author:
Michaël Rouillé <michael.rouille@gmail.com>

Definition at line 16 of file OBTSmartPointer.h.


Member Typedef Documentation

template<class TPointeeType>
typedef TPointeeType OBT::SmartPointer< TPointeeType >::pointee_type

Definition at line 21 of file OBTSmartPointer.h.


Constructor & Destructor Documentation

template<class TPointeeType >
OBT::SmartPointer< TPointeeType >::SmartPointer (  )  [inline]

default constructor

Definition at line 259 of file OBTSmartPointer.h.

References OBT::SmartPointer< TPointeeType >::increaseCounter().

00260                 :
00261         _referenceCounter( NULL ),
00262         _pointee( NULL )
00263         {
00264                 increaseCounter() ;
00265         }

template<class TPointeeType >
OBT::SmartPointer< TPointeeType >::SmartPointer ( TPointeeType *  pointer  )  [inline]

constructor

Definition at line 271 of file OBTSmartPointer.h.

References OBT::SmartPointer< TPointeeType >::increaseCounter().

00272                 :
00273         _referenceCounter( NULL ),
00274         _pointee( pointer )
00275         {
00276                 increaseCounter() ;
00277         }

template<class TPointeeType >
OBT::SmartPointer< TPointeeType >::SmartPointer ( const SmartPointer< TPointeeType > &  SmartPointer  )  [inline]

copy constructor

Definition at line 283 of file OBTSmartPointer.h.

00284                 :
00285         // initialise the reference counter to NULL, it will be set in the operator=
00286         _referenceCounter( NULL ),
00287         _pointee( NULL )
00288         {
00289                 *this = SmartPointer ;
00290         }

template<class TPointeeType >
OBT::SmartPointer< TPointeeType >::~SmartPointer (  )  [inline, virtual]
template<class TPointeeType>
OBT::SmartPointer< TPointeeType >::SmartPointer ( TPointeeType *  pointer,
unsigned long  instance 
) [private]

constructor explicitly setting the instance ID, used by smartObjectsMap

Parameters:
pointer to the pointee
instance instance ID

Member Function Documentation

template<class TPointeeType >
void OBT::SmartPointer< TPointeeType >::decreaseCounter (  )  [inline, private]

decrease the reference counter

Definition at line 354 of file OBTSmartPointer.h.

References OBT::SmartPointer< TPointeeType >::_pointee, and OBT::SmartPointer< TPointeeType >::_referenceCounter.

Referenced by OBT::SmartPointer< TPointeeType >::operator=(), and OBT::SmartPointer< TPointeeType >::~SmartPointer().

00355         {
00356                 if ( _referenceCounter != NULL )
00357                 {
00358                         --*_referenceCounter ;
00359                         // if no more SmartPointer points to the object, we delete it
00360                         if ( *_referenceCounter == 0 )
00361                         {
00362                                 delete _pointee ;
00363                                 delete _referenceCounter ;
00364                         }
00365                 }
00366         }

template<class TPointeeType >
unsigned int OBT::SmartPointer< TPointeeType >::getReferenceCount (  )  const [inline]
Returns:
the number of references to the pointer

Definition at line 493 of file OBTSmartPointer.h.

References OBT::SmartPointer< TPointeeType >::_referenceCounter.

00494         {
00495                 return *_referenceCounter ;
00496         }

template<class TPointeeType >
void OBT::SmartPointer< TPointeeType >::increaseCounter (  )  [inline, private]

increase the reference counter

Definition at line 339 of file OBTSmartPointer.h.

References OBT::SmartPointer< TPointeeType >::_referenceCounter.

Referenced by OBT::SmartPointer< TPointeeType >::operator=(), and OBT::SmartPointer< TPointeeType >::SmartPointer().

00340         {
00341                 if ( _referenceCounter == NULL )
00342                 {
00343                         // initialise the SmartPointer reference counter
00344                         _referenceCounter = new unsigned int( 0 ) ;
00345                 }
00346                 ++*_referenceCounter ;
00347         }

template<class TPointeeType >
OBT::SmartPointer< TPointeeType >::operator TPointeeType * (  )  [inline]

user-defined conversion to TPointeeType*

Definition at line 473 of file OBTSmartPointer.h.

References OBT::SmartPointer< TPointeeType >::_pointee.

00474         {
00475                 return _pointee ;
00476         }

template<class TPointeeType >
OBT::SmartPointer< TPointeeType >::operator void * (  )  [inline]

added conversion to void*, to prevent delete from compiling and becomes ambiguous

Definition at line 483 of file OBTSmartPointer.h.

References OBT::SmartPointer< TPointeeType >::_pointee.

00484         {
00485                 return _pointee ;
00486         }

template<class TPointeeType >
bool OBT::SmartPointer< TPointeeType >::operator! (  )  const [inline]

enables if (!sp) .

..

Returns:
the result of the SmartPointer validity test

Definition at line 403 of file OBTSmartPointer.h.

References OBT::SmartPointer< TPointeeType >::_pointee.

00404         {
00405                 return !_pointee ;
00406         }

template<class TPointeeType >
template<class U >
bool OBT::SmartPointer< TPointeeType >::operator!= ( const SmartPointer< U > &  rhs  )  const [inline]
Parameters:
rhs right operand
Returns:
result of the inequality test between rhs and this

Definition at line 463 of file OBTSmartPointer.h.

References OBT::SmartPointer< TPointeeType >::_pointee.

00464         {
00465                 return _pointee != rhs._pointee ;
00466         }

template<class TPointeeType >
TPointeeType & OBT::SmartPointer< TPointeeType >::operator* (  )  const [inline]

allows a smart pointer object to act as though it were the actual encapsulated pointee when dereferenced.

Returns:
a reference to the copy of TPointeeType for the current aspect

Definition at line 393 of file OBTSmartPointer.h.

References OBT::SmartPointer< TPointeeType >::_pointee.

00394         {
00395                 return *_pointee ;
00396         }

template<class TPointeeType >
TPointeeType * OBT::SmartPointer< TPointeeType >::operator-> (  )  const [inline]

allows a smart pointer object to act as though it were the actual encapsulated pointee through indirection.

Returns:
the pointee

Definition at line 329 of file OBTSmartPointer.h.

References OBT::SmartPointer< TPointeeType >::_pointee.

00330         {
00331                 return _pointee ;
00332         }

template<class TPointeeType >
const SmartPointer< TPointeeType > & OBT::SmartPointer< TPointeeType >::operator= ( const SmartPointer< TPointeeType > &  pointer  )  [inline]

assignment operator

Parameters:
pointer value to be assigned
Returns:
the assigned SmartPointer

Definition at line 308 of file OBTSmartPointer.h.

References OBT::SmartPointer< TPointeeType >::_pointee, OBT::SmartPointer< TPointeeType >::_referenceCounter, OBT::SmartPointer< TPointeeType >::decreaseCounter(), and OBT::SmartPointer< TPointeeType >::increaseCounter().

00309         {
00310                 if ( this != &pointer )
00311                 {
00312                         // if the SmartPointer has not been initialised by a copy constructor
00313                         // we must decrease the old reference counter instance
00314                         decreaseCounter() ;
00315                         // initialise the reference counter to the assignment value
00316                         _referenceCounter = pointer._referenceCounter ;
00317                         // copy the pointers to the copies of T
00318                         _pointee = pointer._pointee ;
00319                         increaseCounter() ;
00320                 }
00321                 return( *this ) ;
00322         }

template<class TPointeeType >
template<class U >
bool OBT::SmartPointer< TPointeeType >::operator== ( const SmartPointer< U > &  rhs  )  const [inline]
Parameters:
rhs right operand
Returns:
result of the equality test between rhs and this

Definition at line 434 of file OBTSmartPointer.h.

References OBT::SmartPointer< TPointeeType >::_pointee.

00435         {
00436                 return _pointee == rhs._pointee ;
00437         }


Friends And Related Function Documentation

template<class TPointeeType>
template<class TTo , class TFrom >
SmartPointer< TTo >& constCast ( const SmartPointer< TFrom > &  from  )  [friend]

the ConstCast function permits to explicitly cast a const smart pointer to a pointee to a non-const smart pointer to a related pointee

Parameters:
pointer to be casted
Returns:
an casted smart pointer
template<class TPointeeType>
bool operator!= ( const TPointeeType *  lhs,
const SmartPointer< TPointeeType > &  rhs 
) [friend]
Parameters:
lhs left operand
rhs right operand
Returns:
result of the equality test between lhs and rhs

Definition at line 208 of file OBTSmartPointer.h.

00209                 {
00210                         return lhs != rhs._pointee ;
00211                 }

template<class TPointeeType>
bool operator!= ( const SmartPointer< TPointeeType > &  lhs,
const TPointeeType *  rhs 
) [friend]
Parameters:
lhs left operand
rhs right operand
Returns:
result of the equality test between lhs and rhs

Definition at line 198 of file OBTSmartPointer.h.

00199                 {
00200                         return lhs._pointee != rhs ;
00201                 }

template<class TPointeeType>
template<class U >
bool operator!= ( const U *  lhs,
const SmartPointer< TPointeeType > &  rhs 
) [friend]
Parameters:
lhs left operand
rhs right operand
Returns:
result of the inequality test between lhs and rhs
template<class TPointeeType>
template<class U >
bool operator!= ( const SmartPointer< TPointeeType > &  lhs,
const U *  rhs 
) [friend]
Parameters:
lhs left operand
rhs right operand
Returns:
result of the inequality test between lhs and rhs
template<class TPointeeType>
bool operator== ( const TPointeeType *  lhs,
const SmartPointer< TPointeeType > &  rhs 
) [friend]
Parameters:
lhs left operand
rhs right operand
Returns:
result of the equality test between lhs and rhs

Definition at line 188 of file OBTSmartPointer.h.

00189                 {
00190                         return lhs == rhs._pointee ;
00191                 }

template<class TPointeeType>
bool operator== ( const SmartPointer< TPointeeType > &  lhs,
const TPointeeType *  rhs 
) [friend]
Parameters:
lhs left operand
rhs right operand
Returns:
result of the equality test between lhs and rhs

Definition at line 178 of file OBTSmartPointer.h.

00179                 {
00180                         return lhs._pointee == rhs ;
00181                 }

template<class TPointeeType>
template<class U , class TPointeeType >
bool operator== ( const U *  lhs,
const SmartPointer< TPointeeType > &  rhs 
) [friend]
Parameters:
lhs left operand
rhs right operand
Returns:
result of the equality test between lhs and rhs
template<class TPointeeType>
template<class TPointeeType , class U >
bool operator== ( const SmartPointer< TPointeeType > &  lhs,
const U *  rhs 
) [friend]
Parameters:
lhs left operand
rhs right operand
Returns:
result of the equality test between lhs and rhs
template<class TPointeeType>
template<class TTo , class TFrom >
const SmartPointer< TTo >& staticCast ( const SmartPointer< TFrom > &  from  )  [friend]

the StaticCast function permits to explicitly cast a smart pointer to a pointee to a smart pointer to a related pointee

Implicit or explicit C++ cast should not be used with the SmartPointer as the behavior will not be similar to a standard C++ pointer

Parameters:
pointer to be casted
Returns:
an casted smart pointer

Member Data Documentation

template<class TPointeeType>
TPointeeType* OBT::SmartPointer< TPointeeType >::_pointee [private]
template<class TPointeeType>
unsigned int* OBT::SmartPointer< TPointeeType >::_referenceCounter [private]

Reference counting tracks the number of smart pointers that point to the same object.

Reference counting is the most popular ownership strategy used with smart pointers. When that number goes to zero, the pointee object is deleted. This strategy works very well if you don't break certain rules for instance, you should not keep standard C++ pointers and smart pointers to the same object.

Definition at line 249 of file OBTSmartPointer.h.

Referenced by OBT::SmartPointer< TPointeeType >::decreaseCounter(), OBT::SmartPointer< TPointeeType >::getReferenceCount(), OBT::SmartPointer< TPointeeType >::increaseCounter(), OBT::SmartPointer< TPointeeType >::operator=(), and OBT::SmartPointer< TPointeeType >::~SmartPointer().


Generated on 1 Jan 2010 for OBT by  doxygen 1.6.1