# Reference Count Class

Topics
Overview | Functions
Overloaded Operators

Overview

Reference counting is a technique used to ensure the safe deletion or modification of an object when a copy of the object exists. In C++ a temporary copy of an object is created when an object is passed by value as an argument to a function or returned by value from a function. If the object allocates memory when it is created, all the copies will share the piece of memory. The same situation can also occur if a class does not define its own copy constructor. In this case the compiler will generate a copy constructor. A compiler generated copy constructor copies each data member one by one. If any data members are pointers, then only the value of the pointer is copied causing all the copies to point to the same memory location. If the class contains a destructor, then the same piece of memory will be freed multiple times when the object that allocated the memory is destroyed. Reference counted objects, count the number of times an object is referenced to a copy of itself. If a copy exists in memory, then the object cannot be safely destroyed or modified without performing some other operation.

A reference count can be embedded directly into an object or be indirectly associated with an object. The implementation presented here works by embedding a reference count with each object. Each time the object is referenced by another entity a counter is incremented. When the entity is finished with the object the counter is decremented. When the counter goes to zero, it means that no entities are referencing the object, so it can be safely deleted or modified.

Counted Object Class:
In order to create reference counted objects, a class must inherit the CountedObject class to embed a reference count into each object:

    class CountedObject
    {
    public:
      CountedObject() { refcount = 1; }
      virtual ~CountedObject() { } // Destructor provided for virtuality

    public:
      void IncRefCount() { refcount++; }
      void DecRefCount() { refcount--; }
      int UnReferenced() { return refcount == 0; } // Not Referenced
      unsigned Count() { return refcount; }  // Returns refcount
      void Release() { refcount = 0; }       // Reset refcount

    protected:
      unsigned refcount;
    };

The RefCount class is used to handle pointers to reference counted objects. It works by storing pointers to objects in containers, rather than the objects themselves. Reference counted pointers work like normal pointers, except they keep track of the number of references to the objects they point to. To set up a reference counted pointer to an object, allocate and initialize the object on the heap using the new operator, and then pass a pointer to the object in a call to the RefCount constructor. See the RefCount example program for further details.

NOTE

: The RefCount class is implemented as a template class that can be used with any data type. In order to avoid portability problems with template classes, the template version can be disabled at compile time with a preprocessor directive. For any applications that require reference counting and cannot use the template version of the RefCount class, define the __NOT_USING_TEMPLATE_CLASS__ macro and directly code this class for the data type.

Functions

RefCount<TYPE>::RefCount(TYPE *ptr = 0)

- Class constructor that initializes the container. TYPE is a protected data member assumed to be a class having the IncRefCount(), DecRefCount(), and Referenced() member functions inherited from the CountedObject class.

RefCount<TYPE>::~RefCount()

- Class destructor responsible for decrementing the reference count if the container is not pointing an object and deleting the container if it is no longer referenced.

RefCount<TYPE>::RefCount(const RefCount<TYPE> &ptr)

- Class copy constructor called when a RefCount pointer is passed by value or copied. Like normal pointers only the pointer is copied, not the object pointed to. The copying that occurs at this point is recorded by incrementing the reference count.

void RefCount<TYPE>::operator=(const RefCount<TYPE> &ptr)

- Class assignment operator using share semantics. No copies of the container are made. Instead the new container is bound to the same data as the old one, after the old container is released. This assignment operator does not allow chain assignments.

void RefCount<TYPE>::operator=(int)

- Class assignment operator used to set the object pointer to zero or null. NOTE: int is a dummy parameter.

void RefCount<TYPE>::Bind(const RefCount<TYPE> &ptr)

- Protected member function used to bind the object to the same object that ptr is bound to. If the pointer is not equal to zero, the reference count will be incremented.

void RefCount<TYPE>::Unbind()

- Protected member function used to decrement the reference count if the container is not pointing to an object. This function will do nothing if the pointer equals zero. The container will be deleted if its reference count equals zero.

void RefCount<TYPE>::NewBinding(const RefCount<TYPE> &ptr)

- Protected member function used to give the reference counted pointer a new binding (the same as ptr.)

Overloaded Operators

TYPE &RefCount<TYPE>::operator*() const

- Overloaded indirection (*) operator that returns the object at the address pointed to . This allows reference counted pointers to be de-referenced like ordinary pointers

TYPE *RefCount<TYPE>::operator->() const

- Overloaded arrow (->) operator that returns the container's address.

int RefCount<TYPE>::operator!() const

- Overloaded NOT (!) operator used to test whether a RefCount is null. Returns true if the pointer equals zero.

RefCount<TYPE>::operator int()

- Conversion function used to test whether a RefCount is null. Returns true if the pointer is not equal to zero.

friend int operator==(const RefCount<TYPE> &a, const RefCount<TYPE> &b)

- Friend function used to overload the "equal to" (==) operator relative to the RefCount class. Returns true if the pointers are equal.

friend int operator!=(const RefCount<TYPE> &a, const RefCount<TYPE> &b)

- Friend function used to overload the "not equal to" (!=) operator relative to the RefCount class. Returns true if the pointers are not equal.

End Of Document