00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef WT_REFERENCED_H
00022 #define WT_REFERENCED_H
00023
00024 #include <wefts_rwmutex.h>
00025 #ifndef NO_ASSERTIONS
00026 #include <cassert>
00027 #endif
00028
00029
00030 namespace Wefts {
00031
00048 class Referenced: public RWMutex
00049 {
00050
00051 private:
00053 int m_count;
00055 bool m_disposeable;
00056
00057 protected:
00072 Referenced( int count = 1, bool dispose = true ):
00073 RWMutex()
00074 {
00075 #ifndef NO_ASSERTIONS
00076 assert ( count > 0 );
00077 #endif
00078 m_count = count > 1 ? count : 1;
00079 m_disposeable = dispose;
00080 }
00081
00083 virtual ~Referenced() {}
00084
00085 public:
00086
00091 void incRef() {
00092 lockWrite();
00093 if (m_count > 0 )
00094 m_count++;
00095 unlock();
00096 }
00097
00103 void decRef() {
00104 bool destroy = false;
00105 lockWrite();
00106 if ( m_disposeable && m_count > 0 ) m_count--;
00107 if ( m_count <= 0 ) destroy = true;
00108 unlock();
00109
00110
00111
00112
00113
00114 if ( destroy ) delete this;
00115 }
00116
00145 int references() { return m_count; }
00146
00151 void incRefUnlocked() { m_count++; }
00152
00160 void setRefUnlocked( int count )
00161 {
00162 #ifndef NO_ASSERTIONS
00163 assert( count > 0 );
00164 #endif
00165 m_count = count;
00166 }
00167
00168 };
00169
00170 }
00171
00172 #endif
00173