qcounter.h

00001 /***************************************************************************
00002  *   Copyright (C) 2004 by Gav Wood                                        *
00003  *   gav@kde.org                                                           *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
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

Generated on Fri Nov 10 21:58:26 2006 for Exscalibar by  doxygen 1.5.1