Inheritance diagram for Exponent::Basics::CCountedObject:
A reference counted class. CCountedObjects should never be deleted
directly, you should always call dereference on them. Dereference reduces
the reference count, when this is 0 or below, the object is deleted
In a default construction, CCountedObjects are not referenced
If you need to use the object after creation, you should make sure
that you make a call to referenced. If this call is not made
you cannot guarantee that the object will still be existance
// To use effectively do something along these lines // First we construct an object. This *does not* increment the reference count. // Even if you are the creator of the object you do not automatically count as wanting to reference it // This allows you to pass new objects in to arrays etc without having to define them properly first.. // However it does rely on *YOU* remembering to referece the object if you are going to use it! // Objects are automatically deleted when the reference count reaches 0 CCountedObject *object = new CCountedObject; // We are going to be processing with this, so we want to make sure that the object has a reference to it object->referenced(); // [Ref Count : 1] // Lets say we pass it to another object that is also going to do something with it CSomeObject *someObject = new CSomeObject(object); // Increments reference count of object by calling referenced on it [Ref Count : 2] // Here is what the constructor would look like: // CSomeObject::CSomeObject(CCountedObject *object) { EXCHANGE_COUNTED_OBJECT(m_object, object); } // Do some process someObject->doSomeProcess(); // Now delete someObject delete someObject; // Decrements the reference count to object by calling dereference on it [Ref Count : 1] // Here is what the destructor would look like: // CSomeObject::~CSomeObject() { FORGET_COUNTED_OBJECT(m_object); } // Object is now still in existance.. we add it to another object CSomeObject *someObject2 = new CSomeObject(object); // Again increments reference count [Ref Count : 2] // Now we dont want our object any more object->derefenced(); // Notice we dont delete the object, we just forget it! [Ref Count : 1] // The object is still 'alive' inside someObject2, so we can process someObject->doSomeProcess(); // Now delete someObject. This actually deletes object delete someObject; // Decrements the reference count to object, which makes [Ref Count : 0] and calls the destructor of object // object is now destoryed correctly
All of this has several important implications:
someObjectThatTakesCCountedObjects->addPointer(new CCountedObject);
CMyCountedObject object;
// THIS IS VERY BAD!!! DONT DO THIS!!!
someObjectThatTakesCCountedObjects->addPointer(&object);
Definition at line 96 of file CCountedObject.hpp.
Exponent::Basics::CCountedObject::CCountedObject | ( | ) |
Default construction
Exponent::Basics::CCountedObject::CCountedObject | ( | const char * | className | ) |
Construction with a class name
className | The name of the class |
virtual Exponent::Basics::CCountedObject::~CCountedObject | ( | ) | [virtual] |
Destruction
virtual void Exponent::Basics::CCountedObject::dereference | ( | ) | [virtual] |
Decrement the reference count. Object is deleted if reference count is <= 0
Implements Exponent::Basics::ICountedObject.
virtual void Exponent::Basics::CCountedObject::getObjectDescription | ( | char * | string, | |
const long | size | |||
) | const [virtual] |
Get a description of the object
string | On return is filled with the description | |
size | The size of the stirng |
Implements Exponent::Basics::ICountedObject.
Reimplemented in Exponent::Midi::CMidiEvent, Exponent::Midi::CMidiEventList, Exponent::Basics::CBool, Exponent::Basics::CDouble, Exponent::Basics::CFloat, Exponent::Basics::CLong, Exponent::Basics::CString, Exponent::MathTools::CPoint, Exponent::GUI::Basics::CAlphaColour, and Exponent::GUI::Basics::CColour.
virtual long Exponent::Basics::CCountedObject::getReferenceCount | ( | ) | const [virtual] |
Get the reference count
long | The reference count |
Implements Exponent::Basics::ICountedObject.
virtual void Exponent::Basics::CCountedObject::referenced | ( | ) | [virtual] |
Increment the reference count
Implements Exponent::Basics::ICountedObject.
long Exponent::Basics::CCountedObject::m_referenceCount [protected] |
How many things point to this item?
Definition at line 155 of file CCountedObject.hpp.