Xavax C++ Library Class Index   FAQ   Overview

Class xavax::RCObject

RCObject is a base class that works in conjunction with RCPointer 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 counter 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. Any method which modifies an object deriving from RCObject should call one of two methods provided by RCPointer. The modify method should be called by methods which assign values to all members of an object, such as operator=. If the object is shared and the copy-on-write flag is true, modify creates a new object. The modifyCopy method should be called by methods which do not assign values to all members of an object. If the object is shared and the copy-on-write flag is true, modifyCopy creates a copy of the object.

Constructor Summary
RCObject()
         Construct a RCObject.
RCObject(const RCObject&)
         Construct a copy of a RCObject.

Operator Summary
RCObject& operator=(const RCObject&)
         Assignment operator.

Method Summary
void attach()
         Attach a reference to this object.
void copyOnWrite(bool flag)
         Set the copyOnWrite flag.
bool copyOnWrite()
         Return the state of the copyOnWrite flag.
void detach()
         Detach a reference from this object.
int refCount()
         Return the reference count.
void shareable(bool)
         Set the state of the shareable flag.
bool shareable()
         Return true if the object is shareable.
bool shared()
         Return true if the object is shared.

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

Related Classes
Object, RCPointer

Constructor Detail

RCObject

RCObject()
Construct a RCObject.

RCObject

RCObject(const RCObject& src)
Construct a copy of a RCObject.
Parameters:
src - the object to be copied.

Operator Detail

operator=

RCObject& operator=(const RCObject& src)
Copy a RCObject.
Parameters:
src - the object to be copied.

Method Detail

attach

void attach()
Attach a reference to this object and increment the reference counter.

copyOnWrite

void copyOnWrite(bool flag)
Set the state of the copyOnWrite flag to flag.
Parameters:
flag - the new state of the copyOnWrite flag.

copyOnWrite

void copyOnWrite()
Return the state of the copyOnWrite flag.
Returns:
the state of the copyOnWrite flag.

detach

void detach()
Detach a reference from this object and decrement the reference counter. If value of the counter reaches zero, delete this object.

shareable

void shareable(bool flag)
Set the state of the shareable flag.
Parameters:
flag - the new value of the shareable flag.

shareable

bool shareable() const
Returns true if this object is shareable.
Returns:
true if this object is shareable.

shared

bool shared() const
Returns true if this object is shared. An object is shared if the reference count is greater than 1.
Returns:
true if this object is shared.

Example Code

Copyright © 2003 Xavax Inc. -- All Rights Reserved