The Micro C++ Library
micxx::RefCountPtr< T > Class Template Reference

The RefCountPtr template is used by SharedPtr to store a shared pointer that keeps a reference counter. More...

#include <micxx.hxx>

List of all members.

Public Member Functions

 RefCountPtr (T *pointer=0)
 Create a new RefCountPtr instance to pointer pointer. The reference counter is initialized with 1.
virtual ~RefCountPtr ()
 Delete the stored pointer if is not 0.
virtual RefCountPtr< T > & operator++ ()
 Increment the reference counter by 1.
virtual RefCountPtr< T > & operator-- ()
 Decrement the reference counter by 1. If the reference counter is decremented to 0, the stored pointer is deleted.
virtual bool operator! ()
 Check, if the reference counter is 0.
virtual RefCountPtr< T > & operator= (T *pointer)
 Replace the old stored pointer with new pointer. The old stored pointer is deleted and the reference counter is reset to 1.
virtual operator T * ()
virtual operator const T * () const
virtual T * release ()
 Replace the old stored pointer with 0. The old stored pointer is returned and not deleted.

Detailed Description

template<typename T>
class micxx::RefCountPtr< T >

The RefCountPtr template is used by SharedPtr to store a shared pointer that keeps a reference counter.

A RefCountPtr maintains a pointer of type T* and a reference counter. The reference counter could be incremented, decremented or checked against 0. RefCountPtr itself only checks and guarantees, that the stored pointer is deleted either

  • by the destructor,
  • if the reference counter is decremented to 0, or
  • if a new pointer is assigned to this instance, in which case also the reference counter is reset to 1.
Author:
Norbert Klose
Date:
December, 2006

Constructor & Destructor Documentation

template<typename T >
micxx::RefCountPtr< T >::RefCountPtr ( T *  pointer = 0)

Create a new RefCountPtr instance to pointer pointer. The reference counter is initialized with 1.

Parameters:
pointerThe pointer to be stored, or 0.
                                        :
    referenceCounter(1),
    pointer(apointer)
{
}
template<typename T >
micxx::RefCountPtr< T >::~RefCountPtr ( ) [virtual]

Delete the stored pointer if is not 0.

{
    __MICXXTRY__
    if (pointer)
    {
        delete pointer;
        pointer = 0;
    }
    __MICXXEND__
}

Member Function Documentation

template<typename T >
micxx::RefCountPtr< T >::operator const T * ( ) const [virtual]
Returns:
The stored pointer as const.
{
    return pointer;
}
template<typename T >
micxx::RefCountPtr< T >::operator T * ( ) [virtual]
Returns:
The stored pointer.
{
    return pointer;
}
template<typename T >
bool micxx::RefCountPtr< T >::operator! ( ) [virtual]

Check, if the reference counter is 0.

Returns:
true, if the reference counter is 0
false, otherwise
{
    return !referenceCounter;
}
template<typename T >
RefCountPtr< T > & micxx::RefCountPtr< T >::operator++ ( ) [virtual]

Increment the reference counter by 1.

Returns:
A reference to *this
{
    ++referenceCounter;
    return *this;
}
template<typename T >
RefCountPtr< T > & micxx::RefCountPtr< T >::operator-- ( ) [virtual]

Decrement the reference counter by 1. If the reference counter is decremented to 0, the stored pointer is deleted.

Returns:
A reference to *this
{
    __MICXXTRY__
    if (referenceCounter)
    {
        --referenceCounter;
        if (!referenceCounter && pointer)
        {
            delete pointer;
            pointer = 0;
        }
    }
    return *this;
    __MICXXEND__
}
template<typename T >
RefCountPtr< T > & micxx::RefCountPtr< T >::operator= ( T *  pointer) [virtual]

Replace the old stored pointer with new pointer. The old stored pointer is deleted and the reference counter is reset to 1.

Returns:
A reference to *this.
{
    __MICXXTRY__
    if (pointer)
    {
        delete pointer;
    }
    referenceCounter = 1;
    pointer = apointer;
    return *this;
    __MICXXEND__
}
template<typename T >
T * micxx::RefCountPtr< T >::release ( ) [virtual]

Replace the old stored pointer with 0. The old stored pointer is returned and not deleted.

Returns:
The old stored pointer.
{
    T * apointer = pointer;
    referenceCounter = 1;
    pointer = 0;
    return apointer;
}

The documentation for this class was generated from the following file: