00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #ifndef OVERFLOWSAFE_TYPE_HPP
00013 #define OVERFLOWSAFE_TYPE_HPP
00014
00015
00024 template <class T, T T_MAX, T T_MIN>
00025 class OverflowSafeInt
00026 {
00027 private:
00029 T m_value;
00030 public:
00031 OverflowSafeInt() : m_value(0) { }
00032
00033 OverflowSafeInt(const OverflowSafeInt& other) { this->m_value = other.m_value; }
00034 OverflowSafeInt(const int64 int_) { this->m_value = int_; }
00035
00036 inline OverflowSafeInt& operator = (const OverflowSafeInt& other) { this->m_value = other.m_value; return *this; }
00037
00038 inline OverflowSafeInt operator - () const { return OverflowSafeInt(-this->m_value); }
00039
00046 inline OverflowSafeInt& operator += (const OverflowSafeInt& other)
00047 {
00048 if ((T_MAX - abs(other.m_value)) < abs(this->m_value) &&
00049 (this->m_value < 0) == (other.m_value < 0)) {
00050 this->m_value = (this->m_value < 0) ? T_MIN : T_MAX ;
00051 } else {
00052 this->m_value += other.m_value;
00053 }
00054 return *this;
00055 }
00056
00057
00058 inline OverflowSafeInt operator + (const OverflowSafeInt& other) const { OverflowSafeInt result = *this; result += other; return result; }
00059 inline OverflowSafeInt operator + (const int other) const { OverflowSafeInt result = *this; result += (int64)other; return result; }
00060 inline OverflowSafeInt operator + (const uint other) const { OverflowSafeInt result = *this; result += (int64)other; return result; }
00061 inline OverflowSafeInt& operator -= (const OverflowSafeInt& other) { return *this += (-other); }
00062 inline OverflowSafeInt operator - (const OverflowSafeInt& other) const { OverflowSafeInt result = *this; result -= other; return result; }
00063 inline OverflowSafeInt operator - (const int other) const { OverflowSafeInt result = *this; result -= (int64)other; return result; }
00064 inline OverflowSafeInt operator - (const uint other) const { OverflowSafeInt result = *this; result -= (int64)other; return result; }
00065
00066 inline OverflowSafeInt& operator ++ () { return *this += 1; }
00067 inline OverflowSafeInt& operator -- () { return *this += -1; }
00068 inline OverflowSafeInt operator ++ (int) { OverflowSafeInt org = *this; *this += 1; return org; }
00069 inline OverflowSafeInt operator -- (int) { OverflowSafeInt org = *this; *this += -1; return org; }
00070
00077 inline OverflowSafeInt& operator *= (const int factor)
00078 {
00079 if (factor != 0 && (T_MAX / abs(factor)) < abs(this->m_value)) {
00080 this->m_value = ((this->m_value < 0) == (factor < 0)) ? T_MAX : T_MIN ;
00081 } else {
00082 this->m_value *= factor ;
00083 }
00084 return *this;
00085 }
00086
00087
00088 inline OverflowSafeInt operator * (const int64 factor) const { OverflowSafeInt result = *this; result *= factor; return result; }
00089 inline OverflowSafeInt operator * (const int factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
00090 inline OverflowSafeInt operator * (const uint factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
00091 inline OverflowSafeInt operator * (const uint16 factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
00092 inline OverflowSafeInt operator * (const byte factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
00093
00094
00095 inline OverflowSafeInt& operator /= (const int64 divisor) { this->m_value /= divisor; return *this; }
00096 inline OverflowSafeInt operator / (const OverflowSafeInt& divisor) const { OverflowSafeInt result = *this; result /= divisor.m_value; return result; }
00097 inline OverflowSafeInt operator / (const int divisor) const { OverflowSafeInt result = *this; result /= divisor; return result; }
00098 inline OverflowSafeInt operator / (const uint divisor) const { OverflowSafeInt result = *this; result /= (int)divisor; return result; }
00099
00100
00101 inline OverflowSafeInt& operator %= (const int divisor) { this->m_value %= divisor; return *this; }
00102 inline OverflowSafeInt operator % (const int divisor) const { OverflowSafeInt result = *this; result %= divisor; return result; }
00103
00104
00105 inline OverflowSafeInt& operator <<= (const int shift) { this->m_value <<= shift; return *this; }
00106 inline OverflowSafeInt operator << (const int shift) const { OverflowSafeInt result = *this; result <<= shift; return result; }
00107 inline OverflowSafeInt& operator >>= (const int shift) { this->m_value >>= shift; return *this; }
00108 inline OverflowSafeInt operator >> (const int shift) const { OverflowSafeInt result = *this; result >>= shift; return result; }
00109
00110
00111 inline bool operator == (const OverflowSafeInt& other) const { return this->m_value == other.m_value; }
00112 inline bool operator != (const OverflowSafeInt& other) const { return !(*this == other); }
00113 inline bool operator > (const OverflowSafeInt& other) const { return this->m_value > other.m_value; }
00114 inline bool operator >= (const OverflowSafeInt& other) const { return this->m_value >= other.m_value; }
00115 inline bool operator < (const OverflowSafeInt& other) const { return !(*this >= other); }
00116 inline bool operator <= (const OverflowSafeInt& other) const { return !(*this > other); }
00117
00118
00119 inline bool operator == (const int other) const { return this->m_value == other; }
00120 inline bool operator != (const int other) const { return !(*this == other); }
00121 inline bool operator > (const int other) const { return this->m_value > other; }
00122 inline bool operator >= (const int other) const { return this->m_value >= other; }
00123 inline bool operator < (const int other) const { return !(*this >= other); }
00124 inline bool operator <= (const int other) const { return !(*this > other); }
00125
00126 inline operator int64 () const { return this->m_value; }
00127 };
00128
00129
00130 template <class T, int64 T_MAX, int64 T_MIN> inline OverflowSafeInt<T, T_MAX, T_MIN> operator + (int64 a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b + a; }
00131 template <class T, int64 T_MAX, int64 T_MIN> inline OverflowSafeInt<T, T_MAX, T_MIN> operator - (int64 a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return -b + a; }
00132 template <class T, int64 T_MAX, int64 T_MIN> inline OverflowSafeInt<T, T_MAX, T_MIN> operator * (int64 a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b * a; }
00133 template <class T, int64 T_MAX, int64 T_MIN> inline OverflowSafeInt<T, T_MAX, T_MIN> operator / (int64 a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return (OverflowSafeInt<T, T_MAX, T_MIN>)a / (int)b; }
00134
00135
00136 template <class T, int64 T_MAX, int64 T_MIN> inline OverflowSafeInt<T, T_MAX, T_MIN> operator + (int a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b + a; }
00137 template <class T, int64 T_MAX, int64 T_MIN> inline OverflowSafeInt<T, T_MAX, T_MIN> operator - (int a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return -b + a; }
00138 template <class T, int64 T_MAX, int64 T_MIN> inline OverflowSafeInt<T, T_MAX, T_MIN> operator * (int a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b * a; }
00139 template <class T, int64 T_MAX, int64 T_MIN> inline OverflowSafeInt<T, T_MAX, T_MIN> operator / (int a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return (OverflowSafeInt<T, T_MAX, T_MIN>)a / (int)b; }
00140
00141
00142 template <class T, int64 T_MAX, int64 T_MIN> inline OverflowSafeInt<T, T_MAX, T_MIN> operator + (uint a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b + a; }
00143 template <class T, int64 T_MAX, int64 T_MIN> inline OverflowSafeInt<T, T_MAX, T_MIN> operator - (uint a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return -b + a; }
00144 template <class T, int64 T_MAX, int64 T_MIN> inline OverflowSafeInt<T, T_MAX, T_MIN> operator * (uint a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b * a; }
00145 template <class T, int64 T_MAX, int64 T_MIN> inline OverflowSafeInt<T, T_MAX, T_MIN> operator / (uint a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return (OverflowSafeInt<T, T_MAX, T_MIN>)a / (int)b; }
00146
00147
00148 template <class T, int64 T_MAX, int64 T_MIN> inline OverflowSafeInt<T, T_MAX, T_MIN> operator + (byte a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b + (uint)a; }
00149 template <class T, int64 T_MAX, int64 T_MIN> inline OverflowSafeInt<T, T_MAX, T_MIN> operator - (byte a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return -b + (uint)a; }
00150 template <class T, int64 T_MAX, int64 T_MIN> inline OverflowSafeInt<T, T_MAX, T_MIN> operator * (byte a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b * (uint)a; }
00151 template <class T, int64 T_MAX, int64 T_MIN> inline OverflowSafeInt<T, T_MAX, T_MIN> operator / (byte a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return (OverflowSafeInt<T, T_MAX, T_MIN>)a / (int)b; }
00152
00153 typedef OverflowSafeInt<int64, INT64_MAX, INT64_MIN> OverflowSafeInt64;
00154
00155 #endif