00001 #ifndef _EH_0_RefCounting_H_ // -*-c++-*- 00002 00003 /* 00004 * Copyright 1991-2002 Eric M. Hopper <hopper@omnifarious.org> 00005 * 00006 * This program is free software; you can redistribute it and/or modify it 00007 * under the terms of the GNU Lesser General Public License as published 00008 * by the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, but 00012 * WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00019 */ 00020 00021 /* $Header: /home/hopper/src/cvs/C++/LCore/LCore/RefCounting.h,v 1.6 2002/09/10 12:54:56 hopper Exp $ */ 00022 00023 // For a log, see ../ChangeLog 00024 // 00025 // Revision 1.2 1997/05/12 16:27:53 hopper 00026 // Fixed ReferenceCounting::NumReferences to be a const method, as befits 00027 // an accessor. 00028 // 00029 // Revision 1.1 1996/03/26 16:23:02 hopper 00030 // Moved ReferenceCounting class over from Container library. 00031 // Fixed up class numbering system so LCore uses a seperate library number 00032 // from the container library. 00033 // 00034 // Revision 1.2 1996/02/22 03:56:03 hopper 00035 // Fixed to be close to the new 'standard' class format. 00036 // Fixed to be less open to subclasses. 00037 // 00038 // Revision 1.1.1.1 1996/02/20 02:32:52 hopper 00039 // Imported into CVS 00040 // 00041 // Revision 0.2 1994/10/30 04:49:32 hopper 00042 // Moved various things into the new LCore library. 00043 // 00044 // Revision 0.1 1994/07/21 05:38:24 hopper 00045 // Genesis! 00046 // 00047 00048 #ifdef __GNUG__ 00049 # pragma interface 00050 #endif 00051 00052 #ifndef _LCORE_Protocol_H_ 00053 # include <LCore/Protocol.h> 00054 #endif 00055 00056 #define _EH_0_RefCounting_H_ 00057 00058 namespace strmod { 00059 namespace lcore { 00060 00061 /** \class ReferenceCounting RefCounting.h LCore/RefCounting.h 00062 * A base mixin class for reference counted things. 00063 * 00064 * There are many ways to handle reference counting as a general garbage 00065 * collection technique in C++. This helps implement one of the more efficient, 00066 * but also somewhat invasive ones. This one has the reference count stored 00067 * inside the class itself. This means the class holds data that really doesn't 00068 * have much to do with the class, but some classes are designed to be used in 00069 * ways where you know reference counting will be needed and useful as a garbage 00070 * collection technique. 00071 * 00072 * This class has an associated smart pointer class, RefCountPtr, that can be 00073 * used to point at instances of this class. The smart pointer class will track 00074 * references for you. But, there is no requirement to use it if you want to do 00075 * your reference counting in some other way. 00076 */ 00077 class ReferenceCounting : virtual public Protocol 00078 { 00079 public: 00080 static const LCore_ClassIdent identifier; 00081 00082 //! Initialize with a reference count 00083 ReferenceCounting(U4Byte refs) : refcounter(refs) {} 00084 //! Not much to do on destruction 00085 virtual ~ReferenceCounting() {} 00086 00087 virtual int AreYouA(const ClassIdent &cid) const { 00088 return((identifier == cid) || Protocol::AreYouA(cid)); 00089 } 00090 00091 //! Add a reference to the count. 00092 void AddReference() { refcounter++; } 00093 /** Remove a reference from the count and stop the counter from rolling over backwards 00094 * 00095 * Note that this class does not consider it it's job to actually delete 00096 * itself when the reference count goes to 0. That job is more properly left 00097 * to the smart pointer class. 00098 */ 00099 void DelReference() { if (refcounter) refcounter--; } 00100 //! How many references? 00101 U4Byte NumReferences() const { return(refcounter); } 00102 00103 protected: 00104 inline virtual const ClassIdent *i_GetIdent() const; 00105 00106 //! Add more than 1 reference 00107 void AddReferences(U4Byte num) { refcounter += num; } 00108 //! Remove more than 1 reference, again with protection from rolling over backwards. 00109 inline void DelReferences(U4Byte num); 00110 00111 private: 00112 mutable U4Byte refcounter; 00113 }; 00114 00115 inline const ClassIdent *ReferenceCounting::i_GetIdent() const 00116 { 00117 return(&identifier); 00118 } 00119 00120 inline void ReferenceCounting::DelReferences(U4Byte num) 00121 { 00122 if (refcounter > num) 00123 refcounter -= num; 00124 else 00125 refcounter = 0; 00126 } 00127 00128 } // namespace lcore 00129 } // namespace strmod 00130 00131 #endif
1.3-rc1