Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

ReferenceCounter Class Reference

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

#include <ReferenceCounter.h>

Inheritance diagram for ReferenceCounter:

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 17 of file ReferenceCounter.h.

List of all members.

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 (needed in case of stack allocation or if the subclass is a direct member variable of another class)

Constructor & Destructor Documentation

ReferenceCounter::ReferenceCounter (  ) 

constructor

Definition at line 20 of file ReferenceCounter.h.

ReferenceCounter::ReferenceCounter ( const ReferenceCounter rc  ) 

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

Definition at line 22 of file ReferenceCounter.h.

virtual ReferenceCounter::~ReferenceCounter (  )  [virtual]

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

Definition at line 27 of file ReferenceCounter.h.


Member Function Documentation

bool ReferenceCounter::getAutoDelete (  ) 

returns RC_autodelete

Definition at line 54 of file ReferenceCounter.h.

Referenced by BehaviorBase::start().

virtual unsigned int ReferenceCounter::getReferences (  )  const [virtual]

returns the number of references

Returns:
references

Definition at line 49 of file ReferenceCounter.h.

ReferenceCounter& ReferenceCounter::operator= ( const ReferenceCounter  ) 

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

Definition at line 24 of file ReferenceCounter.h.

void ReferenceCounter::setAutoDelete ( bool  b  ) 

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

Definition at line 52 of file ReferenceCounter.h.

Referenced by FreeMemReportControl::init(), BehaviorBase::start(), XWalkEdit::XWalkEdit(), and BehaviorBase::~BehaviorBase().


Member Data Documentation

if false, prevents deletion when counter hits 0 (needed in case of stack allocation or if the subclass is a direct member variable of another class)

Definition at line 61 of file ReferenceCounter.h.

Referenced by getAutoDelete(), removeReference(), and setAutoDelete().

unsigned int ReferenceCounter::references [protected]

the current number of references

Definition at line 58 of file ReferenceCounter.h.

Referenced by addReference(), getReferences(), removeReference(), and ~ReferenceCounter().


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

Tekkotsu v5.1CVS
Generated Mon May 9 04:59:15 2016 by Doxygen 1.6.3