00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef _QT_COUNTER_H
00022 #define _QT_COUNTER_H
00023
00024 #undef UTILISE_PTHREAD
00025
00026 #ifdef UTILISE_PTHREAD
00027 #include <pthread.h>
00028 #else
00029 #include <qmutex.h>
00030 #endif
00031
00039 class QCounter
00040 {
00041 int theCount;
00042 #ifdef UTILISE_PTHREAD
00043 pthread_mutex_t theMutex;
00044 #else
00045 QMutex theMutex;
00046 #endif
00047 public:
00054 const int operator++();
00055
00062 const int operator++(int);
00063
00070 const int operator--();
00071
00078 const int operator--(int);
00079
00087 const int operator+=(const int i);
00088
00096 const int operator-=(const int i);
00097
00104 const int operator=(const int i);
00105
00115 operator const int() const;
00116
00122 QCounter(const int i = 0);
00123
00127 ~QCounter();
00128 };
00129
00130 #ifdef UTILISE_PTHREAD
00131 inline const int QCounter::operator++() { pthread_mutex_lock(&theMutex); int ret = (++theCount); pthread_mutex_unlock(&theMutex); return ret; }
00132 inline const int QCounter::operator++(int) { pthread_mutex_lock(&theMutex); int ret = (theCount++); pthread_mutex_unlock(&theMutex); return ret; }
00133 inline const int QCounter::operator--() { pthread_mutex_lock(&theMutex); int ret = (--theCount); pthread_mutex_unlock(&theMutex); return ret; }
00134 inline const int QCounter::operator--(int) { pthread_mutex_lock(&theMutex); int ret = (theCount--); pthread_mutex_unlock(&theMutex); return ret; }
00135 inline const int QCounter::operator+=(const int i) { pthread_mutex_lock(&theMutex); int ret = (theCount += i); pthread_mutex_unlock(&theMutex); return ret; }
00136 inline const int QCounter::operator-=(const int i) { pthread_mutex_lock(&theMutex); int ret = (theCount -= i); pthread_mutex_unlock(&theMutex); return ret; }
00137 inline const int QCounter::operator=(const int i) { pthread_mutex_lock(&theMutex); int ret = (theCount = i); pthread_mutex_unlock(&theMutex); return ret; }
00138 inline QCounter::operator const int() const { return theCount; }
00139 inline QCounter::QCounter(const int i) : theCount(i) { pthread_mutex_init(&theMutex, NULL); }
00140 inline QCounter::~QCounter() { pthread_mutex_destroy(&theMutex); }
00141 #else
00142 inline const int QCounter::operator++() { QMutexLocker lock(&theMutex); return ++theCount; }
00143 inline const int QCounter::operator++(int) { QMutexLocker lock(&theMutex); return theCount++; }
00144 inline const int QCounter::operator--() { QMutexLocker lock(&theMutex); return --theCount; }
00145 inline const int QCounter::operator--(int) { QMutexLocker lock(&theMutex); return theCount--; }
00146 inline const int QCounter::operator+=(const int i) { QMutexLocker lock(&theMutex); return theCount += i; }
00147 inline const int QCounter::operator-=(const int i) { QMutexLocker lock(&theMutex); return theCount -= i; }
00148 inline const int QCounter::operator=(const int i) { QMutexLocker lock(&theMutex); return theCount = i; }
00149 inline QCounter::operator const int() const { return theCount; }
00150 inline QCounter::QCounter(const int i) : theCount(i) {}
00151 inline QCounter::~QCounter() {}
00152 #endif
00153
00154 #endif