Main Page Class Hierarchy Alphabetical List Compound List File List Compound Members File Members
EkConstSmartPtr Class Template ReferenceSmart Point implementation based on reference counting.
More...
#include <EkSmartPtr.h>
Inheritance diagram for EkConstSmartPtr: [legend]Collaboration diagram for EkConstSmartPtr:[legend]List of all members.
Public Methods |
| EkConstSmartPtr (void) |
| Void constructor sets the ptr to NULL. More...
|
| ~EkConstSmartPtr (void) |
| Destructor - Calls deleteRef if ptr is not NULL. More...
|
| EkConstSmartPtr (const EkConstSmartPtr< obj > &sp) |
| Create a new reference to the object. Calls addRef if ptr != NULL. More...
|
| EkConstSmartPtr (const obj *objPtr) |
| constructor. More...
|
EkConstSmartPtr< obj > & | operator= (const EkConstSmartPtr< obj > &sp) |
| Point this object at a new spot. Calls deleteRef for current obj, and addRef for new Object... Assuming they aren't null. More...
|
EkConstSmartPtr< obj > & | operator= (const obj *objPtr) |
| assignment operator. More...
|
const obj * | operator-> (void) const |
| Member selection through pointer. More...
|
const obj * | ptr (void) const |
| Get the raw pointer value be careful not to delete the pointer. More...
|
const obj & | operator * (void) const |
| Dereference operator. More...
|
bool | operator== (const EkConstSmartPtr< obj > &nSP) const |
| Equal operator. More...
|
bool | operator== (const obj *objPtr) const |
| Another equal operator. More...
|
bool | operator!= (const EkConstSmartPtr< obj > &nSP) const |
| Not equal operator. More...
|
bool | operator!= (const obj *objPtr) const |
| Another not equal operator. More...
|
bool | operator< (const EkConstSmartPtr< obj > &nsp) const |
| Less than operator. More...
|
bool | operator< (const obj *optr) const |
| Another less than operator. More...
|
| operator bool (void) const |
| Convert to bool. Allow for code like: if(ptr). More...
|
Protected Attributes |
obj * | mPtr |
Detailed Description
template<class obj>
class EkConstSmartPtr< 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> |
EkConstSmartPtr< obj >::EkConstSmartPtr |
( |
void |
|
) |
[inline] |
|
|
Void constructor sets the ptr to NULL.
|
template<class obj> |
EkConstSmartPtr< obj >::~EkConstSmartPtr |
( |
void |
|
) |
[inline] |
|
|
Destructor - Calls deleteRef if ptr is not NULL.
|
template<class obj> |
EkConstSmartPtr< obj >::EkConstSmartPtr |
( |
const EkConstSmartPtr< obj > & |
sp |
) |
[inline] |
|
|
Create a new reference to the object. Calls addRef if ptr != NULL.
|
template<class obj> |
EkConstSmartPtr< obj >::EkConstSmartPtr |
( |
const obj * |
objPtr |
) |
[inline] |
|
Member Function Documentation
template<class obj> |
const obj & EkConstSmartPtr< obj >::operator * |
( |
void |
|
) |
const [inline] |
|
|
Dereference operator.
Reimplemented in EkSmartPtr. |
template<class obj> |
EkConstSmartPtr< obj >::operator bool |
( |
void |
|
) |
const [inline] |
|
|
Convert to bool. Allow for code like: if(ptr).
|
template<class obj> |
bool EkConstSmartPtr< obj >::operator!= |
( |
const obj * |
objPtr |
) |
const [inline] |
|
|
Another not equal operator.
|
template<class obj> |
bool EkConstSmartPtr< obj >::operator!= |
( |
const EkConstSmartPtr< obj > & |
nSP |
) |
const [inline] |
|
template<class obj> |
const obj * EkConstSmartPtr< obj >::operator-> |
( |
void |
|
) |
const [inline] |
|
|
Member selection through pointer.
Reimplemented in EkSmartPtr. |
template<class obj> |
bool EkConstSmartPtr< obj >::operator< |
( |
const obj * |
optr |
) |
const [inline] |
|
|
Another less than operator.
|
template<class obj> |
bool EkConstSmartPtr< obj >::operator< |
( |
const EkConstSmartPtr< obj > & |
nsp |
) |
const [inline] |
|
template<class obj> |
EkConstSmartPtr< obj > & EkConstSmartPtr< obj >::operator= |
( |
const obj * |
objPtr |
) |
[inline] |
|
template<class obj> |
EkConstSmartPtr< obj > & EkConstSmartPtr< obj >::operator= |
( |
const EkConstSmartPtr< 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 EkConstSmartPtr< obj >::operator== |
( |
const obj * |
objPtr |
) |
const [inline] |
|
template<class obj> |
bool EkConstSmartPtr< obj >::operator== |
( |
const EkConstSmartPtr< obj > & |
nSP |
) |
const [inline] |
|
template<class obj> |
const obj * EkConstSmartPtr< obj >::ptr |
( |
void |
|
) |
const [inline] |
|
|
Get the raw pointer value be careful not to delete the pointer.
Reimplemented in EkSmartPtr. |
Member Data Documentation
template<class obj> |
obj* EkConstSmartPtr::mPtr [protected]
|
|
The documentation for this class was generated from the following file:
|