Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

ReferenceCounter.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_ReferenceCounter_h_
00003 #define INCLUDED_ReferenceCounter_h_
00004 
00005 #include <iostream>
00006 
00007 //class ReferenceCounter;
00008 //const char* findname(ReferenceCounter* x); // !< tests if @a x is a behavior and outputs its name, otherwise outputs its address
00009 
00010 //! Performs simple reference counting, will delete the object when removing the last reference
00011 class ReferenceCounter {
00012  public:
00013   //! constructor
00014   ReferenceCounter() : references(0),RC_autodelete(true) {}
00015   //! copy constructor - uses autodelete setting of @a rc, but references will still start at 0
00016   ReferenceCounter(const ReferenceCounter& rc) : references(0),RC_autodelete(rc.RC_autodelete) {}
00017   //! assignment operator - does nothing because the reference count shouldn't be copied
00018   ReferenceCounter& operator=(const ReferenceCounter& /*rc*/) {return *this;}
00019 
00020   //! destructor - will std::cout a warning if still has references
00021   virtual ~ReferenceCounter() {
00022     if(references>0)
00023       std::cout << "*** WARNING RefCounter was deleted with " << references << " references" << std::endl;
00024   }
00025 
00026   //! adds one to #references
00027   virtual void AddReference() { references++; }
00028   //! subtracts one from #references AND DELETES the object IF ZERO
00029   virtual void RemoveReference() {
00030     if(--references==0) {
00031       if(RC_autodelete)
00032         delete this;
00033     } else if(references==(unsigned int)-1)
00034       std::cout << "*** WARNING RefCounter went negative" << std::endl;
00035   }
00036   //! returns the number of references
00037   /*! @return references */
00038   virtual unsigned int GetReferences() const { return references; }
00039 
00040   //! if true, next time a RemoveReference() causes #references to hit 0, the object will delete itself
00041   void SetAutoDelete(bool b) {RC_autodelete=b;}
00042 
00043   bool GetAutoDelete() { return RC_autodelete; } //!< returns RC_autodelete
00044   
00045  protected:
00046   //! the current number of references
00047   unsigned int references;
00048 
00049   //! if false, prevents deletion when counter hits 0
00050   bool RC_autodelete;
00051 };
00052 
00053 /*
00054 #include "Behaviors/BehaviorBase.h"
00055 
00056 const char* findname(ReferenceCounter* x) {
00057   BehaviorBase* beh=dynamic_cast<BehaviorBase*>(x);
00058   if(beh==NULL) {
00059     static char s[100];
00060     sprintf(s,"Uknown @ %x",x);
00061     return s;
00062   } else {
00063     static char s2[100];
00064     sprintf(s2," @ %x",x);
00065     return (beh->getName()+s2).c_str();
00066   }
00067 }
00068 
00069 */
00070 
00071 /*! @file
00072  * @brief Defines the ReferenceCounter base class, which allows for automatic memory deallocation
00073  * @author ejt (Creator)
00074  *
00075  * $Author: ejt $
00076  * $Name: tekkotsu-2_4_1 $
00077  * $Revision: 1.8 $
00078  * $State: Exp $
00079  * $Date: 2005/02/02 18:22:29 $
00080  */
00081 
00082 #endif
00083 

Tekkotsu v2.4.1
Generated Tue Aug 16 16:32:48 2005 by Doxygen 1.4.4