Smart pointer template class. More...
#include <OBTSmartPointer.h>
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.
| |
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) |
Smart pointer template class.
Definition at line 16 of file OBTSmartPointer.h.
typedef TPointeeType OBT::SmartPointer< TPointeeType >::pointee_type |
Definition at line 21 of file OBTSmartPointer.h.
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 }
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 }
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 }
OBT::SmartPointer< TPointeeType >::~SmartPointer | ( | ) | [inline, virtual] |
destructor
Definition at line 296 of file OBTSmartPointer.h.
References OBT::SmartPointer< TPointeeType >::_pointee, OBT::SmartPointer< TPointeeType >::_referenceCounter, and OBT::SmartPointer< TPointeeType >::decreaseCounter().
00297 { 00298 decreaseCounter() ; 00299 _pointee = NULL ; 00300 _referenceCounter = NULL ; 00301 }
OBT::SmartPointer< TPointeeType >::SmartPointer | ( | TPointeeType * | pointer, | |
unsigned long | instance | |||
) | [private] |
constructor explicitly setting the instance ID, used by smartObjectsMap
pointer | to the pointee | |
instance | instance ID |
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 }
unsigned int OBT::SmartPointer< TPointeeType >::getReferenceCount | ( | ) | const [inline] |
Definition at line 493 of file OBTSmartPointer.h.
References OBT::SmartPointer< TPointeeType >::_referenceCounter.
00494 { 00495 return *_referenceCounter ; 00496 }
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 }
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 }
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 }
bool OBT::SmartPointer< TPointeeType >::operator! | ( | ) | const [inline] |
enables if (!sp) .
..
Definition at line 403 of file OBTSmartPointer.h.
References OBT::SmartPointer< TPointeeType >::_pointee.
00404 { 00405 return !_pointee ; 00406 }
bool OBT::SmartPointer< TPointeeType >::operator!= | ( | const SmartPointer< U > & | rhs | ) | const [inline] |
rhs | right operand |
Definition at line 463 of file OBTSmartPointer.h.
References OBT::SmartPointer< TPointeeType >::_pointee.
00464 { 00465 return _pointee != rhs._pointee ; 00466 }
TPointeeType & OBT::SmartPointer< TPointeeType >::operator* | ( | ) | const [inline] |
allows a smart pointer object to act as though it were the actual encapsulated pointee when dereferenced.
Definition at line 393 of file OBTSmartPointer.h.
References OBT::SmartPointer< TPointeeType >::_pointee.
00394 { 00395 return *_pointee ; 00396 }
TPointeeType * OBT::SmartPointer< TPointeeType >::operator-> | ( | ) | const [inline] |
allows a smart pointer object to act as though it were the actual encapsulated pointee through indirection.
Definition at line 329 of file OBTSmartPointer.h.
References OBT::SmartPointer< TPointeeType >::_pointee.
00330 { 00331 return _pointee ; 00332 }
const SmartPointer< TPointeeType > & OBT::SmartPointer< TPointeeType >::operator= | ( | const SmartPointer< TPointeeType > & | pointer | ) | [inline] |
assignment operator
pointer | value to be assigned |
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 }
bool OBT::SmartPointer< TPointeeType >::operator== | ( | const SmartPointer< U > & | rhs | ) | const [inline] |
rhs | right operand |
Definition at line 434 of file OBTSmartPointer.h.
References OBT::SmartPointer< TPointeeType >::_pointee.
00435 { 00436 return _pointee == rhs._pointee ; 00437 }
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
pointer | to be casted |
bool operator!= | ( | const TPointeeType * | lhs, | |
const SmartPointer< TPointeeType > & | rhs | |||
) | [friend] |
lhs | left operand | |
rhs | right operand |
Definition at line 208 of file OBTSmartPointer.h.
bool operator!= | ( | const SmartPointer< TPointeeType > & | lhs, | |
const TPointeeType * | rhs | |||
) | [friend] |
lhs | left operand | |
rhs | right operand |
Definition at line 198 of file OBTSmartPointer.h.
bool operator!= | ( | const U * | lhs, | |
const SmartPointer< TPointeeType > & | rhs | |||
) | [friend] |
lhs | left operand | |
rhs | right operand |
bool operator!= | ( | const SmartPointer< TPointeeType > & | lhs, | |
const U * | rhs | |||
) | [friend] |
lhs | left operand | |
rhs | right operand |
bool operator== | ( | const TPointeeType * | lhs, | |
const SmartPointer< TPointeeType > & | rhs | |||
) | [friend] |
lhs | left operand | |
rhs | right operand |
Definition at line 188 of file OBTSmartPointer.h.
bool operator== | ( | const SmartPointer< TPointeeType > & | lhs, | |
const TPointeeType * | rhs | |||
) | [friend] |
lhs | left operand | |
rhs | right operand |
Definition at line 178 of file OBTSmartPointer.h.
bool operator== | ( | const U * | lhs, | |
const SmartPointer< TPointeeType > & | rhs | |||
) | [friend] |
lhs | left operand | |
rhs | right operand |
bool operator== | ( | const SmartPointer< TPointeeType > & | lhs, | |
const U * | rhs | |||
) | [friend] |
lhs | left operand | |
rhs | right operand |
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
pointer | to be casted |
TPointeeType* OBT::SmartPointer< TPointeeType >::_pointee [private] |
stores a copy of TPointeeType for each aspect
Definition at line 252 of file OBTSmartPointer.h.
Referenced by OBT::SmartPointer< TPointeeType >::decreaseCounter(), OBT::SmartPointer< TPointeeType >::operator TPointeeType *(), OBT::SmartPointer< TPointeeType >::operator void *(), OBT::SmartPointer< TPointeeType >::operator!(), OBT::SmartPointer< TPointeeType >::operator!=(), OBT::operator!=(), OBT::SmartPointer< TPointeeType >::operator*(), OBT::SmartPointer< TPointeeType >::operator->(), OBT::SmartPointer< TPointeeType >::operator=(), OBT::SmartPointer< TPointeeType >::operator==(), OBT::operator==(), and OBT::SmartPointer< TPointeeType >::~SmartPointer().
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().