Homepage | Demos | Overview | Downloads | Tutorials | Reference | Credits |
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 00016 //! destructor - will std::cout a warning if still has references 00017 virtual ~ReferenceCounter() { 00018 if(references>0) 00019 std::cout << "*** WARNING RefCounter was deleted with " << references << " references" << std::endl; 00020 } 00021 00022 //! adds one to references 00023 virtual void AddReference() { references++; } 00024 //! subtracts one from references AND DELETES the object IF ZERO 00025 virtual void RemoveReference() { 00026 if(--references==0) { 00027 if(RC_autodelete) 00028 delete this; 00029 } else if(references==(unsigned int)-1) 00030 std::cout << "*** WARNING RefCounter went negative" << std::endl; 00031 } 00032 //! returns the number of references 00033 /*! @return references */ 00034 virtual unsigned int GetReferences() { return references; } 00035 00036 //! if true, next time a RemoveReference() causes references to hit 0, the object will delete itself 00037 void SetAutoDelete(bool b) {RC_autodelete=b;} 00038 00039 bool GetAutoDelete() { return RC_autodelete; } //!< returns RC_autodelete 00040 00041 protected: 00042 //! the current number of references 00043 unsigned int references; 00044 00045 //! prevents deletion when counter hits 0 00046 bool RC_autodelete; 00047 }; 00048 00049 /* 00050 #include "Behaviors/BehaviorBase.h" 00051 00052 const char* findname(ReferenceCounter* x) { 00053 BehaviorBase* beh=dynamic_cast<BehaviorBase*>(x); 00054 if(beh==NULL) { 00055 static char s[100]; 00056 sprintf(s,"Uknown @ %x",x); 00057 return s; 00058 } else { 00059 static char s2[100]; 00060 sprintf(s2," @ %x",x); 00061 return (beh->getName()+s2).c_str(); 00062 } 00063 } 00064 00065 */ 00066 00067 /*! @file 00068 * @brief Defines the ReferenceCounter base class, which allows for automatic memory deallocation 00069 * @author ejt (Creator) 00070 * 00071 * $Author: ejt $ 00072 * $Name: tekkotsu-1_4_1 $ 00073 * $Revision: 1.3 $ 00074 * $State: Exp $ 00075 * $Date: 2003/06/12 18:06:11 $ 00076 */ 00077 00078 #endif 00079
Tekkotsu v1.4 |
Generated Sat Jul 19 00:06:31 2003 by Doxygen 1.3.2 |