Xavax C++ Library Class Index   FAQ   Overview

Class xavax::RCPointer<T>

RCPointer<T> is a template for smart pointer classes that work in conjunction with RCObject to implement a reference counting mechanism. RCObject contains a reference counter which is initialized to zero when the object is created. When a pointer is constructed, it attaches to the object causing the pointer to increment. When the pointer is deleted or goes out of scope, it detaches from the object causing the counter to decrement. When the counter decrements to zero, the object automatically deletes itself.

Copy semantics for RCObject are controlled by a shareable flag which defaults to true and is set by the shareable method. When a RCObject is shareable, new references increment the reference count and share the object. If a RCObject is not shareable, a new copy of the object is created for each new reference.

RCObject works in conjunction with RCPointer to implement a copy-on-write mechanism. Before a derived class method modifies an object, it must call one of two methods provided by RCPointer. The modify method should be called when an object will be overwritten, which is usually the case in an assignment operator. If the object is shared, the modify method will create a new object. The modifyCopy method should be called when an object will be modified. If the object is shared, the modifyCopy method will create a new copy of the object.

Constructor Summary
RCPointer<T>(T*)
         Construct a RCPointer<T>.
RCPointer<T>(const RCPointer&)
         Construct a copy of a RCPointer<T>.

Operator Summary
RCPointer<T>& operator=(const RCPointer<T>&)
         Assignment operator.
bool operator==(const RCPointer<T>&)
         Test for equality.
T* operator->() const
         Return the pointer cast as a pointer to T.
T& operator*() const
         Return the dereferenced pointer cast as a reference to T.

Method Summary
void modify()
         Prepare to modify the referenced object.
void modifyCopy()
         Prepare to modify a copy of the referenced object.

Methods Inherited From Object
cast<T>, cast<T>, classRecord, ClassRecord, clone, hashCode

Related Classes
Object, RCObject

Constructor Detail

RCPointer<T>

RCPointer<T>()
Construct a RCPointer<T>.

RCPointer<T>

RCPointer<T>(const RCPointer<T>& src)
Construct a copy of a RCPointer<T>.
Parameters:
src - the object to be copied.

Operator Detail

operator=

RCPointer<T>&
operator=(const RCPointer<T>& src)
Copy a RCPointer<T>.
Parameters:
src - the pointer to be copied.

operator==

bool operator==(const RCPointer<T>& rhs)
Compare two RCPointers for equality.
Parameters:
src - the right hand side of the == operator.

operator->

T* operator->()
Return the pointer cast as a pointer to T.

operator*

T& operator*()
Return the dereferenced pointer cast as a reference to T.
Throws:
NullPointerException - if the underlying pointer to T is null.

Method Detail

modify

void modify()
Prepare to modify an object. If the current object is shared, detach from it, create a new object, and attach to the new object.

modifyCopy

void modifyCopy()
Prepare to modify an object. If the current object is shared, detach from it, create a copy of the object, and attach to the new object.

Example Code

Copyright © 2003 Xavax Inc. -- All Rights Reserved