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
00051 class Referenced: public RWMutex
00052 {
00053
00054 private:
00056 int m_count;
00058 bool m_disposeable;
00059
00060 protected:
00075 Referenced( int count = 1, bool dispose = true ):
00076 RWMutex()
00077 {
00078 #ifndef NO_ASSERTIONS
00079 assert ( count > 0 );
00080 #endif
00081 m_count = count > 1 ? count : 1;
00082 m_disposeable = dispose;
00083 }
00084
00086 virtual ~Referenced() {}
00087
00088 public:
00089
00094 void incRef() {
00095 lockWrite();
00096 if (m_count > 0 )
00097 m_count++;
00098 unlock();
00099 }
00100
00106 void decRef() {
00107 bool destroy = false;
00108 lockWrite();
00109 if ( m_disposeable && m_count > 0 ) m_count--;
00110 if ( m_count <= 0 ) destroy = true;
00111 unlock();
00112
00113
00114
00115
00116
00117 if ( destroy ) delete this;
00118 }
00119
00148 int references() { return m_count; }
00149
00154 void incRefUnlocked() { m_count++; }
00155
00163 void setRefUnlocked( int count )
00164 {
00165 #ifndef NO_ASSERTIONS
00166 assert( count > 0 );
00167 #endif
00168 m_count = count;
00169 }
00170
00171 };
00172
00173 }
00174
00175 #endif
00176