00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef __UT_Counter__
00023 #define __UT_Counter__
00024
00025 #include "UT_API.h"
00026
00027 #ifdef UT_DEBUG
00028
00029 #include <stdio.h>
00030 #include <SYS/SYS_AtomicInt.h>
00031
00032 #if defined(WIN32)
00033 typedef int UT_CounterIntType;
00034 #else
00035 #include <SYS/SYS_AtomicIntImpl.h>
00036 typedef SYS_AtomicInt32 UT_CounterIntType;
00037 #endif
00038
00039 class UT_API UT_Counter {
00040 public:
00041 explicit UT_Counter(const char *msg)
00042 {
00043 myMessage = msg;
00044 #if defined(WIN32)
00045 myCount = myPeak = 0;
00046 #else
00047 myCount.set(0);
00048 myPeak.set(0);
00049 #endif
00050 }
00051 ~UT_Counter()
00052 {
00053 if ((int32)myPeak > 0)
00054 {
00055 printf("%s: %d [%d peak, %d increments]\n", myMessage,
00056 (int32)myCount, (int32)myPeak,
00057 (int32)myIncrements);
00058 }
00059 }
00060
00061 UT_Counter &operator+=(int v)
00062 {
00063 #if defined(WIN32)
00064 myCount += v;
00065 if (myCount > myPeak)
00066 myPeak = myCount;
00067 #else
00068 myPeak.maximum(myCount.add(v));
00069 if (v > 0)
00070 myIncrements.add(v);
00071 #endif
00072 return *this;
00073 }
00074 UT_Counter &operator-=(int v) { return operator+=(-v); }
00075 UT_Counter &operator++(int) { return operator+=(1); }
00076 UT_Counter &operator--(int) { return operator+=(-1); }
00077 #if defined(WIN32)
00078 void reset() { myCount = 0; }
00079 #else
00080 void reset() { myCount.set(0); }
00081 #endif
00082
00083 private:
00084 UT_CounterIntType myCount;
00085 UT_CounterIntType myPeak;
00086 SYS_AtomicInt32 myIncrements;
00087 const char *myMessage;
00088 };
00089
00090 #else // UT_DEBUG
00091
00092
00093
00094 class UT_API UT_Counter {
00095 public:
00096 UT_Counter(const char *) {}
00097 ~UT_Counter() {}
00098
00099 UT_Counter &operator+=(int) { return *this; }
00100 UT_Counter &operator++(int) { return *this; }
00101 UT_Counter &operator-=(int) { return *this; }
00102 UT_Counter &operator--(int) { return *this; }
00103 void reset() { }
00104 };
00105
00106 #endif // UT_DEBUG
00107 #endif