Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

ReferenceCounter Class Reference

#include <ReferenceCounter.h>

Inheritance diagram for ReferenceCounter:

List of all members.


Detailed Description

Performs simple reference counting, will delete the object when removing the last reference.

Remember to call SetAutoDelete(false) if you instantiate a subclass on the stack instead of the heap -- don't want to try to free memory on the stack if/when last reference is removed! (The stack limits the allocation of the behavior to the current scope, so reference counting is moot.)

Todo:
It would be nice if there was a way for ReferenceCounter to automatically know whether it has been allocated on the stack... is that possible?

Definition at line 16 of file ReferenceCounter.h.


Public Member Functions

 ReferenceCounter ()
 constructor
 ReferenceCounter (const ReferenceCounter &rc)
 copy constructor - uses autodelete setting of rc, but references will still start at 0
ReferenceCounteroperator= (const ReferenceCounter &)
 assignment operator - does nothing because the reference count shouldn't be copied
virtual ~ReferenceCounter ()
 destructor - will std::cout a warning if still has references
virtual void AddReference ()
 adds one to references
virtual bool RemoveReference ()
 subtracts one from references AND DELETES the object IF ZERO (and RC_autodelete is set)
virtual unsigned int GetReferences () const
 returns the number of references
void SetAutoDelete (bool b)
 if true, next time a RemoveReference() causes references to hit 0, the object will delete itself
bool GetAutoDelete ()
 returns RC_autodelete

Protected Attributes

unsigned int references
 the current number of references
bool RC_autodelete
 if false, prevents deletion when counter hits 0

Constructor & Destructor Documentation

ReferenceCounter::ReferenceCounter (  )  [inline]

constructor

Definition at line 19 of file ReferenceCounter.h.

ReferenceCounter::ReferenceCounter ( const ReferenceCounter rc  )  [inline]

copy constructor - uses autodelete setting of rc, but references will still start at 0

Definition at line 21 of file ReferenceCounter.h.

virtual ReferenceCounter::~ReferenceCounter (  )  [inline, virtual]

destructor - will std::cout a warning if still has references

Definition at line 26 of file ReferenceCounter.h.


Member Function Documentation

ReferenceCounter& ReferenceCounter::operator= ( const ReferenceCounter  )  [inline]

assignment operator - does nothing because the reference count shouldn't be copied

Definition at line 23 of file ReferenceCounter.h.

Referenced by BehaviorBase::operator=().

virtual bool ReferenceCounter::RemoveReference (  )  [inline, virtual]

virtual unsigned int ReferenceCounter::GetReferences (  )  const [inline, virtual]

returns the number of references

Returns:
references

Definition at line 48 of file ReferenceCounter.h.

void ReferenceCounter::SetAutoDelete ( bool  b  )  [inline]

if true, next time a RemoveReference() causes references to hit 0, the object will delete itself

Definition at line 51 of file ReferenceCounter.h.

Referenced by BehaviorBase::~BehaviorBase(), and FreeMemReportControl::~FreeMemReportControl().

bool ReferenceCounter::GetAutoDelete (  )  [inline]

returns RC_autodelete

Definition at line 53 of file ReferenceCounter.h.


Member Data Documentation

if false, prevents deletion when counter hits 0

Definition at line 60 of file ReferenceCounter.h.

Referenced by GetAutoDelete(), RemoveReference(), and SetAutoDelete().


The documentation for this class was generated from the following file:

Tekkotsu v4.0
Generated Thu Nov 22 00:58:38 2007 by Doxygen 1.5.4