Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

MCRefPtr Class Template Reference

Template class for managing reference counted pointers. More...

#include <mcllib/MCRefPtr.h>

Inheritance diagram for MCRefPtr:

MCRefCountAccessorTmpl MCRefCountAccessorBase MCRefPtrAttacher MCRefPtrDetacher List of all members.

Public Member Functions

 MCRefPtr (const MCRefPtr &r)
 Copy constructor.
 MCRefPtr (T *pHdl=0)
 Attach to an MCRefCount derived object.
 operator MCRefPtr () const
 Conversion operator to help with const-ness.
 operator T * () const
 Get the contained object.
T * operator-> () const
 Dereference the contained object.
bool operator< (const MCRefPtr< T > &t) const
 Less-than operator for use in STL containers etc.
MCRefPtroperator= (const MCRefPtr &r)
 Assignment operator.
 ~MCRefPtr ()
 Detach from the object.

Protected Member Functions

void attach (T *pHdl)
void detach ()

Protected Attributes

T * m_pHdl
 Pointer to the MCRefCount derived implementation handle.

Detailed Description

template<typename T, MCThreadModel::EModel MODEL = MCThreadModel::THRMODELMULTI>
class mcllib::MCRefPtr< T, MODEL >

Template class for managing reference counted pointers.

In this implementation, the reference count is maintained within the data being pointed to rather than in a separate reference counted pointer. This means that the reference count and the data are intimately bound and cannot become disassociated (a problem with the alternative approach of using a separate ref count pointer). However the disadvantage is that the contained class must be derived from MCRefCount. This is not typically a problem.

Note that the class manipulates the reference count in a thread safe manner by default so it is safe to pass reference counted objects between threads using MCRefPtr. Where this is not required, greater performance can be obtained by using MCThreadModel::THRMODELSINGLE. This model uses reference counting that is not thread safe but is about 10-15 times faster than the thread safe reference counting. The single threaded model is about 5 times slower than not using reference counting at all (adopting some other strategy).

It is preferable to return const MCRefPtr<T>& from a method rather than MCRefPtr<T> to avoid the increment/decrement of the counter.


Constructor & Destructor Documentation

MCRefPtr T *  pHdl = 0  )  [inline]
 

Attach to an MCRefCount derived object.

Parameters:
pHdl the object to attach to. The reference count for the object is incremented.

~MCRefPtr  )  [inline]
 

Detach from the object.

The object's reference count is decremented. If it reaches zero the object is deleted.

MCRefPtr const MCRefPtr< T, MODEL > &  r  )  [inline]
 

Copy constructor.

Parameters:
r the reference counted object to copy. Attaches another reference to the object referenced by r (i.e. increments the reference count of the handle in r)


Member Function Documentation

operator T *  )  const [inline]
 

Get the contained object.

Returns:
the contained object

T* operator->  )  const [inline]
 

Dereference the contained object.

Returns:
the contained object

bool operator< const MCRefPtr< T > &  t  )  const [inline]
 

Less-than operator for use in STL containers etc.

Invokes the less-than operator on the contained object addresses not on the the values that the object refers to.

Parameters:
t the other reference pointer to compare against
Returns:
true if the address of the object contained in this is less-than the adress of the object contained in t.

MCRefPtr& operator= const MCRefPtr< T, MODEL > &  r  )  [inline]
 

Assignment operator.

Parameters:
r the reference counted object to assign from. Safely attach another reference to the object reference by r and detach from the object currently being referenced. At completion this reference pointer references the same object as r. Takes care not to overwrite itself or to adjust the reference counts if both this and r already refer to the same object.


Generated on Wed Jan 12 19:05:49 2005 for MCLLIB by  doxygen 1.3.9.1