00001 #ifndef __JCounter_HPP__
00002 #define __JCounter_HPP__
00003
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <iostream.h>
00007 #include <string.h>
00008 #include <math.h>
00009 #include <assert.h>
00010
00011 #define BOOL bool
00012 #define ULONG unsigned long
00013
00014 inline ULONG MAX( register ULONG x, register ULONG y )
00015 {
00016 return ( x > y ? x : y );
00017 }
00018
00019
00020 class JCounter {
00021 public:
00022
00023 inline JCounter( register ULONG initial = 0, register ULONG maximum = ~0 );
00024
00025
00026 inline JCounter( const JCounter& src );
00027
00028
00029 inline virtual ~JCounter();
00030
00031
00032 inline const JCounter& operator = ( const JCounter& src );
00033 inline const JCounter& operator = ( register ULONG value );
00034
00035
00036 inline const JCounter& operator += ( const JCounter& src );
00037 inline const JCounter& operator -= ( const JCounter& src );
00038 inline const JCounter& operator += ( register ULONG v );
00039 inline const JCounter& operator -= ( register ULONG v );
00040
00041
00042 inline const JCounter& operator ++ ( );
00043 inline const JCounter& operator -- ( );
00044
00045
00046 inline const JCounter& operator ++ ( int );
00047 inline const JCounter& operator -- ( int );
00048
00049
00050 inline const JCounter& operator ~ ();
00051
00052
00053 inline const JCounter& operator ! ();
00054
00055
00056 inline ULONG operator() ();
00057
00058
00059 inline BOOL operator == ( const JCounter& src );
00060 inline BOOL operator != ( const JCounter& src );
00061 inline BOOL operator > ( const JCounter& src );
00062 inline BOOL operator >= ( const JCounter& src );
00063 inline BOOL operator < ( const JCounter& src );
00064 inline BOOL operator <= ( const JCounter& src );
00065
00066 inline BOOL operator == ( register ULONG v );
00067 inline BOOL operator != ( register ULONG v );
00068 inline BOOL operator > ( register ULONG v );
00069 inline BOOL operator >= ( register ULONG v );
00070 inline BOOL operator < ( register ULONG v );
00071 inline BOOL operator <= ( register ULONG v );
00072
00073
00074 inline JCounter operator + ( const JCounter& right );
00075 inline JCounter operator - ( const JCounter& right );
00076 inline JCounter operator * ( const JCounter& right );
00077 inline JCounter operator / ( const JCounter& right );
00078
00079 inline JCounter operator + ( register ULONG right );
00080 inline JCounter operator - ( register ULONG right );
00081 inline JCounter operator * ( register ULONG right );
00082 inline JCounter operator / ( register ULONG right );
00083
00084
00085
00086
00087
00088
00089 friend ostream& operator << (ostream& os, JCounter& right )
00090 {
00091 return os << right.count;
00092 }
00093
00094 friend istream& operator >> (istream& is, JCounter& right )
00095 {
00096 return is >> right.count;
00097 }
00098
00099
00100
00101
00102
00103
00104 inline friend BOOL operator == ( register ULONG left, const JCounter& right ) { return ( left == right.count ); }
00105 inline friend BOOL operator != ( register ULONG left, const JCounter& right ) { return ( left != right.count ); }
00106 inline friend BOOL operator > ( register ULONG left, const JCounter& right ) { return ( left > right.count ); }
00107 inline friend BOOL operator >= ( register ULONG left, const JCounter& right ) { return ( left >= right.count ); }
00108 inline friend BOOL operator < ( register ULONG left, const JCounter& right ) { return ( left < right.count ); }
00109 inline friend BOOL operator <= ( register ULONG left, const JCounter& right ) { return ( left <= right.count ); }
00110
00111
00112 inline friend JCounter JCounter::operator + ( register ULONG left, const JCounter& right )
00113 {
00114 register ULONG val = verify( ( _int64 )left + ( _int64 )right.count, right.max );
00115 return JCounter( val );
00116 }
00117
00118 inline friend JCounter JCounter::operator - ( register ULONG left, const JCounter& right )
00119 {
00120 register ULONG val = verify( ( _int64 )left - ( _int64 )right.count, right.max );
00121 return JCounter( val );
00122 }
00123
00124 inline friend JCounter JCounter::operator * ( register ULONG left, const JCounter& right )
00125 {
00126 register ULONG val = verify( ( _int64 )left * ( _int64 )right.count, right.max );
00127 return JCounter( val );
00128 }
00129
00130 inline friend JCounter JCounter::operator / ( register ULONG left, const JCounter& right )
00131 {
00132 if ( right.count == 0 ) return JCounter( right.max );
00133 register ULONG val = verify( ( _int64 )left / ( _int64 )right.count, right.max );
00134 return JCounter( val );
00135 }
00136
00137
00138
00139 inline operator ULONG () const
00140 {
00141 return count;
00142 }
00143
00144
00145 inline void setMax( register ULONG maximum )
00146 {
00147 max = maximum;
00148 }
00149
00150 inline void set( register ULONG value )
00151 {
00152 count = verify( (_int64)value );
00153 }
00154
00155 protected:
00156
00157 inline void JCounter::check()
00158 {
00159 count = ( ( count >= max ) ? max : count );
00160 }
00161
00162 ULONG JCounter::verify( _int64 value )
00163 {
00164 if ( value <= 0 ) return 0;
00165 if ( value >= max ) return max;
00166
00167 return (ULONG)value;
00168 }
00169
00170 static ULONG JCounter::verify( _int64 value, register ULONG maximum )
00171 {
00172 if ( value <= 0 ) return 0;
00173 if ( value >= maximum ) return maximum;
00174
00175 return (ULONG)value;
00176 }
00177
00178
00179 JCounter JCounter::add( register ULONG right, register ULONG maximum )
00180 {
00181 register ULONG val = verify( ( _int64 )count + ( _int64 )right, maximum );
00182 return JCounter( val );
00183 }
00184
00185 JCounter JCounter::substract( register ULONG right, register ULONG maximum )
00186 {
00187 register ULONG val = verify( ( _int64 )count - ( _int64 )right, maximum );
00188 return JCounter( val );
00189 }
00190
00191 JCounter JCounter::multiply( register ULONG right, register ULONG maximum )
00192 {
00193 register ULONG val = verify( ( _int64 )count * ( _int64 )right, maximum );
00194 return JCounter( val );
00195 }
00196
00197 JCounter JCounter::divideBy( register ULONG right, register ULONG maximum )
00198 {
00199 if ( right == 0 ) return JCounter( maximum );
00200 register ULONG val = verify( ( _int64 )count / ( _int64 )right, maximum );
00201 return JCounter( val );
00202 }
00203
00204 private:
00205 ULONG count;
00206 ULONG max;
00207
00208 };
00209
00210 #endif