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