00001 #ifndef IOTRREFCOUNT 00002 #define IOTRREFCOUNT 00003 00010 #include <assert.h> 00011 00023 class IotrRefCount { 00024 public: 00028 static int instances; 00029 00031 int refs() { return mRefs; }; 00032 00039 static inline void release( IotrRefCount ** obj ); 00040 00047 static inline void addRef( IotrRefCount * const * obj ); 00048 00050 IotrRefCount() : mRefs(1) { instances++; }; 00051 protected: 00055 virtual ~IotrRefCount() { instances--; }; 00056 private: 00058 int mRefs; 00063 IotrRefCount& operator=( const IotrRefCount & ) 00064 { 00065 return *this; 00066 } 00070 IotrRefCount( const IotrRefCount& ) : mRefs(1) 00071 { 00072 instances++; 00073 } 00074 }; 00075 00076 inline void IotrRefCount::release(IotrRefCount ** obj ) 00077 { 00078 assert( !*obj || (*obj)->mRefs > 0 ); 00079 if( *obj && 0 >= --(*obj)->mRefs ) delete *obj; 00080 *obj = 0; 00081 } 00082 00083 inline void IotrRefCount::addRef( IotrRefCount * const * obj ) 00084 { 00085 assert( !*obj || (*obj)->mRefs > 0 ); 00086 if( *obj ) (*obj)->mRefs++; 00087 } 00088 00095 template <class T> 00096 inline void IotrRelease( T ** obj ) 00097 { 00098 IotrRefCount * objref = *obj; 00099 IotrRefCount::release( &objref ); 00100 00101 *obj = (T*) objref; 00102 } 00103 00112 template <class T> 00113 inline void IotrAddRef( T * const * obj ) 00114 { 00115 IotrRefCount * objref = *obj; 00116 IotrRefCount::addRef( &objref ); 00117 } 00118 00122 #endif