00001 /* OOQP * 00002 * Authors: E. Michael Gertz, Stephen J. Wright * 00003 * (C) 2001 University of Chicago. See Copyright Notification in OOQP */ 00004 00005 #ifndef IOTRREFCOUNT 00006 #define IOTRREFCOUNT 00007 00019 #include <assert.h> 00020 00025 class IotrRefCount { 00026 public: 00030 static int instances; 00031 00033 int refs() { return mRefs; }; 00034 00041 static inline void release( IotrRefCount ** obj ); 00042 00049 static inline void addRef( IotrRefCount * const * obj ); 00050 00052 IotrRefCount() : mRefs(1) { instances++; }; 00053 protected: 00057 virtual ~IotrRefCount() { instances--; }; 00058 private: 00060 int mRefs; 00065 IotrRefCount& operator=( const IotrRefCount & ) 00066 { 00067 // mRefs = mRefs; 00068 return *this; 00069 } 00073 IotrRefCount( const IotrRefCount& ) 00074 { 00075 instances++; 00076 mRefs = 1; 00077 } 00078 }; 00079 00080 inline void IotrRefCount::release(IotrRefCount ** obj ) 00081 { 00082 assert( !*obj || (*obj)->mRefs > 0 ); 00083 if( *obj && 0 >= --(*obj)->mRefs ) delete *obj; 00084 *obj = 0; 00085 } 00086 00087 inline void IotrRefCount::addRef( IotrRefCount * const * obj ) 00088 { 00089 assert( !*obj || (*obj)->mRefs > 0 ); 00090 if( *obj ) (*obj)->mRefs++; 00091 } 00092 00099 template <class T> 00100 inline void IotrRelease( T ** obj ) 00101 { 00102 IotrRefCount * objref = *obj; 00103 IotrRefCount::release( &objref ); 00104 00105 *obj = (T*) objref; 00106 } 00107 00116 template <class T> 00117 inline void IotrAddRef( T * const * obj ) 00118 { 00119 IotrRefCount * objref = *obj; 00120 IotrRefCount::addRef( &objref ); 00121 } 00122 00126 #endif