Exponent::Basics::CCountedObject Class Reference

Inheritance diagram for Exponent::Basics::CCountedObject:

Exponent::Basics::ICountedObject Exponent::Audio::CAudioBuffer Exponent::Audio::TAudioBuffer< TypeName > Exponent::Audio::TAudioFile< TypeName > Exponent::Basics::CBool Exponent::Basics::CDouble Exponent::Basics::CFloat Exponent::Basics::CLong Exponent::Basics::CPascalString Exponent::Basics::CString Exponent::Basics::CStringTokeniser Exponent::Collections::TMatrix< TypeName > Exponent::Collections::TPointerCollection< TypeName > Exponent::GUI::Basics::CColour Exponent::GUI::Controls::CMenuItem Exponent::GUI::Events::CActionEvent Exponent::GUI::Events::CKeyboardEvent Exponent::GUI::Events::CMenuEvent Exponent::GUI::Events::CMouseEvent Exponent::GUI::Fonts::CFont Exponent::GUI::Fonts::CFontReference Exponent::GUI::Windowing::CCursor Exponent::Host::CFileInformation Exponent::MathTools::CDimension Exponent::MathTools::CParameter Exponent::MathTools::CPoint Exponent::MathTools::TComplex< TypeName > Exponent::Midi::CMidiEvent Exponent::Midi::CMidiEventList Exponent::Music::CTimeSignature Exponent::Threading::CCriticalSection Exponent::Vst::CVstEffect Exponent::Vst::CVstTempo List of all members.

Detailed Description

Reference counted object.

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:

  1. You can pass annonymous pointers to objects that take CCountedObjects as pointers for example
                someObjectThatTakesCCountedObjects->addPointer(new CCountedObject);
    
  2. You must always make sure that when dealing with CCountedObjects you always use the references / dereferenced system, otherwise you will end up with hanging pointers and lost memory / memory leaks
  3. Never, ever do something like this, as you will end up deleting an non dynamically assigned object:
                CMyCountedObject object;
                // THIS IS VERY BAD!!! DONT DO THIS!!!
                someObjectThatTakesCCountedObjects->addPointer(&object);
    

Date:
07/09/2004
Author:
Paul Chana
Version:
1.0.0 Initial version
Note:
All contents of this source code are copyright 2005 Exp Digital Uk.
This source file is covered by the licence conditions of the Infinity API. You should have recieved a copy
with the source code. If you didnt, please refer to http://www.expdigital.co.uk All content is the Intellectual property of Exp Digital Uk.
Certain sections of this code may come from other sources. They are credited where applicable.
If you have comments, suggestions or bug reports please visit http://support.expdigital.co.uk
Id
CCountedObject.hpp,v 1.5 2007/02/08 21:06:44 paul Exp

Definition at line 96 of file CCountedObject.hpp.

Public Member Functions

Protected Attributes


Constructor & Destructor Documentation

Exponent::Basics::CCountedObject::CCountedObject (  ) 

Default construction

Exponent::Basics::CCountedObject::CCountedObject ( const char *  className  ) 

Construction with a class name

Parameters:
className The name of the class

virtual Exponent::Basics::CCountedObject::~CCountedObject (  )  [virtual]

Destruction


Member Function Documentation

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

Parameters:
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

Return values:
long The reference count

Implements Exponent::Basics::ICountedObject.

virtual void Exponent::Basics::CCountedObject::referenced (  )  [virtual]

Increment the reference count

Implements Exponent::Basics::ICountedObject.


Member Data Documentation

long Exponent::Basics::CCountedObject::m_referenceCount [protected]

How many things point to this item?

Definition at line 155 of file CCountedObject.hpp.


Infinity API - Exponent::Basics::CCountedObject Class Reference generated on 7 Mar 2007