Main Page Class Hierarchy Alphabetical List Compound List File List Compound Members File Members
EkConstCompSmartPtr Class Template ReferenceSmart Point implementation based on reference counting.
More...
#include <EkCompSmartPtr.h>
Inheritance diagram for EkConstCompSmartPtr: [legend]Collaboration diagram for EkConstCompSmartPtr:[legend]List of all members.
Detailed Description
template<class obj>
class EkConstCompSmartPtr< obj >
Smart Point implementation based on reference counting.
These classes are reference-counted and have shallow copy semantics. Memory management can be a tedious and error prone process. The idea behind smart pointers is to handle the majority of the book work for the user.
This smart pointer implementation is built on the notion of reference counting objects. This means that when someone new starts to point at an object they increment a use count variable in the object they are pointing at. When they cease to point at that object they decrement the use count variable. If the use count variable ever goes to zero the object should be deleted as it is no-longer being referenced by anyone.
Reference counting has been used for many years to manage shared objects. However with the advent of C++ and operator overloading it has become possible to hide most tedious aspects of reference counting in a 'pointer' class. This 'pointer' class attempts to mimic the behaviour of a regular C++ pointer through operator overloading. In the proper places (construction, deletion, and assignment) it calls the addRef, and deleteRef methods of the object it is pointing at.
The required methods are defined by class EkRefCount. Because EkConstSmartPtr and EkSmartPtr are templated it is not required that the object they point to be subclasses of EkRefCount (although that is a quick way to obtain the needed functionality).
In certain cases the user may wish to reimplement addRef/deleteRef to handle tricky situations.
In general a EkSmartPtr can be treated the same as a regular pointer. However it should never be used to point at a stack based object (otherwise a double delete will happen). Similarly, it should never be used to point at a static object, for it will attempt to call "delete" on that object.
Note that EkConstSmartPtr is a base class for EkSmartPtr this is so that proper constness symantics can be followed. Just like a char* can be converted to a const char * a EkSmartPtr<foo> can be converted to a EkConstSmartPtr<foo> (the constness refers to the object being pointed at, not the EkSmartPtr object).
Note: The template argument for EkSmartPtr and EkConstSmartPtr must be a class that provides the following member functions:
void addRef(void) - add one to the use count for the object. bool deleteRef(void) - subtract one from the use count for the object If the object should be deleted return non-zero.
This requirement may be satisfied by basing the class on EkRefCount
Constructor & Destructor Documentation
template<class obj> |
EkConstCompSmartPtr< obj >::EkConstCompSmartPtr |
( |
void |
|
) |
[inline] |
|
|
Void constructor sets the ptr to NULL.
|
template<class obj> |
EkConstCompSmartPtr< obj >::~EkConstCompSmartPtr |
( |
void |
|
) |
[inline] |
|
|
Destructor - Calls deleteRef if ptr is not NULL.
|
template<class obj> |
EkConstCompSmartPtr< obj >::EkConstCompSmartPtr |
( |
const EkConstCompSmartPtr< obj > & |
sp |
) |
[inline] |
|
|
Create a new reference to the object. Calls addRef if ptr != NULL.
|
template<class obj> |
EkConstCompSmartPtr< obj >::EkConstCompSmartPtr |
( |
const obj * |
objPtr |
) |
[inline] |
|
Member Function Documentation
template<class obj> |
void EkConstCompSmartPtr< obj >::addRef |
( |
void |
|
) |
const [inline, protected] |
|
template<class obj> |
void EkConstCompSmartPtr< obj >::deleteRef |
( |
void |
|
) |
const [inline, protected] |
|
template<class obj> |
const obj & EkConstCompSmartPtr< obj >::operator * |
( |
void |
|
) |
const [inline] |
|
template<class obj> |
EkConstCompSmartPtr< obj >::operator bool |
( |
void |
|
) |
const [inline] |
|
|
Convert to bool. Allow for code like: if(ptr).
|
template<class obj> |
bool EkConstCompSmartPtr< obj >::operator!= |
( |
const obj * |
objPtr |
) |
const [inline] |
|
|
Another not equal operator.
|
template<class obj> |
bool EkConstCompSmartPtr< obj >::operator!= |
( |
const EkConstCompSmartPtr< obj > & |
nSP |
) |
const [inline] |
|
template<class obj> |
const obj * EkConstCompSmartPtr< obj >::operator-> |
( |
void |
|
) |
const [inline] |
|
template<class obj> |
bool EkConstCompSmartPtr< obj >::operator< |
( |
const obj * |
optr |
) |
const [inline] |
|
|
Another less than operator.
|
template<class obj> |
bool EkConstCompSmartPtr< obj >::operator< |
( |
const EkConstCompSmartPtr< obj > & |
nsp |
) |
const [inline] |
|
template<class obj> |
EkConstCompSmartPtr< obj > & EkConstCompSmartPtr< obj >::operator= |
( |
const obj * |
objPtr |
) |
[inline] |
|
template<class obj> |
EkConstCompSmartPtr< obj > & EkConstCompSmartPtr< obj >::operator= |
( |
const EkConstCompSmartPtr< obj > & |
sp |
) |
[inline] |
|
|
Point this object at a new spot. Calls deleteRef for current obj, and addRef for new Object... Assuming they aren't null.
|
template<class obj> |
bool EkConstCompSmartPtr< obj >::operator== |
( |
const obj * |
objPtr |
) |
const [inline] |
|
template<class obj> |
bool EkConstCompSmartPtr< obj >::operator== |
( |
const EkConstCompSmartPtr< obj > & |
nSP |
) |
const [inline] |
|
template<class obj> |
const obj * EkConstCompSmartPtr< obj >::ptr |
( |
void |
|
) |
const [inline] |
|
|
Get the raw pointer value be careful not to delete the pointer.
Reimplemented in EkCompSmartPtr. |
Member Data Documentation
template<class obj> |
obj* EkConstCompSmartPtr::mPtr [protected]
|
|
The documentation for this class was generated from the following file:
|