Tekkotsu Homepage | Demos | Overview | Downloads | Dev. Resources | Reference | Credits |
ReferenceCounter.hGo to the documentation of this file.00001 //-*-c++-*- 00002 #ifndef INCLUDED_ReferenceCounter_h_ 00003 #define INCLUDED_ReferenceCounter_h_ 00004 00005 #include <iostream> 00006 00007 //! Performs simple reference counting, will delete the object when removing the last reference 00008 /*! Remember to call SetAutoDelete(false) if you 00009 * instantiate a subclass on the stack instead of the heap -- don't want to try to free memory 00010 * on the stack if/when last reference is removed! (The stack limits the allocation of the behavior 00011 * to the current scope, so reference counting is moot.) 00012 * 00013 * @todo It would be nice if there was a way for ReferenceCounter to automatically know whether it 00014 * has been allocated on the stack... is that possible? 00015 */ 00016 class ReferenceCounter { 00017 public: 00018 //! constructor 00019 ReferenceCounter() : references(0),RC_autodelete(true) {} 00020 //! copy constructor - uses autodelete setting of @a rc, but references will still start at 0 00021 ReferenceCounter(const ReferenceCounter& rc) : references(0),RC_autodelete(rc.RC_autodelete) {} 00022 //! assignment operator - does nothing because the reference count shouldn't be copied 00023 ReferenceCounter& operator=(const ReferenceCounter& /*rc*/) {return *this;} 00024 00025 //! destructor - will std::cout a warning if still has references 00026 virtual ~ReferenceCounter() { 00027 if(references>0) 00028 std::cout << "*** WARNING RefCounter was deleted with " << references << " references" << std::endl; 00029 } 00030 00031 //! adds one to #references 00032 virtual void AddReference() { references++; } 00033 //! subtracts one from #references AND DELETES the object IF ZERO (and RC_autodelete is set) 00034 /*! @return true if the last reference was removed, false otherwise */ 00035 virtual bool RemoveReference() { 00036 if(--references==0) { 00037 if(RC_autodelete) 00038 delete this; 00039 return true; 00040 } else if(references==(unsigned int)-1) { 00041 std::cout << "*** WARNING RefCounter went negative" << std::endl; 00042 return true; 00043 } else 00044 return false; 00045 } 00046 //! returns the number of references 00047 /*! @return references */ 00048 virtual unsigned int GetReferences() const { return references; } 00049 00050 //! if true, next time a RemoveReference() causes #references to hit 0, the object will delete itself 00051 void SetAutoDelete(bool b) {RC_autodelete=b;} 00052 00053 bool GetAutoDelete() { return RC_autodelete; } //!< returns RC_autodelete 00054 00055 protected: 00056 //! the current number of references 00057 unsigned int references; 00058 00059 //! if false, prevents deletion when counter hits 0 00060 bool RC_autodelete; 00061 }; 00062 00063 /* 00064 #include "Behaviors/BehaviorBase.h" 00065 00066 const char* findname(ReferenceCounter* x) { 00067 BehaviorBase* beh=dynamic_cast<BehaviorBase*>(x); 00068 if(beh==NULL) { 00069 static char s[100]; 00070 sprintf(s,"Uknown @ %x",x); 00071 return s; 00072 } else { 00073 static char s2[100]; 00074 sprintf(s2," @ %x",x); 00075 return (beh->getName()+s2).c_str(); 00076 } 00077 } 00078 00079 */ 00080 00081 /*! @file 00082 * @brief Defines the ReferenceCounter base class, which allows for automatic memory deallocation 00083 * @author ejt (Creator) 00084 * 00085 * $Author: ejt $ 00086 * $Name: tekkotsu-4_0 $ 00087 * $Revision: 1.10 $ 00088 * $State: Exp $ 00089 * $Date: 2006/10/03 19:13:16 $ 00090 */ 00091 00092 #endif 00093 |
Tekkotsu v4.0 |
Generated Thu Nov 22 00:54:55 2007 by Doxygen 1.5.4 |