overflowsafe_type.hpp

Go to the documentation of this file.
00001 /* $Id: overflowsafe_type.hpp 16245 2009-05-06 22:21:32Z rubidium $ */
00002 
00005 #ifndef OVERFLOWSAFE_TYPE_HPP
00006 #define OVERFLOWSAFE_TYPE_HPP
00007 
00008 #include "math_func.hpp"
00009 
00018 template <class T, T T_MAX, T T_MIN>
00019 class OverflowSafeInt
00020 {
00021 private:
00023   T m_value;
00024 public:
00025   OverflowSafeInt() : m_value(0) { }
00026 
00027   OverflowSafeInt(const OverflowSafeInt& other) { this->m_value = other.m_value; }
00028   OverflowSafeInt(const int64 int_)             { this->m_value = int_; }
00029 
00030   FORCEINLINE OverflowSafeInt& operator = (const OverflowSafeInt& other) { this->m_value = other.m_value; return *this; }
00031 
00032   FORCEINLINE OverflowSafeInt operator - () const { return OverflowSafeInt(-this->m_value); }
00033 
00040   FORCEINLINE OverflowSafeInt& operator += (const OverflowSafeInt& other)
00041   {
00042     if ((T_MAX - abs(other.m_value)) < abs(this->m_value) &&
00043         (this->m_value < 0) == (other.m_value < 0)) {
00044       this->m_value = (this->m_value < 0) ? T_MIN : T_MAX ;
00045     } else {
00046       this->m_value += other.m_value;
00047     }
00048     return *this;
00049   }
00050 
00051   /* Operators for addition and substraction */
00052   FORCEINLINE OverflowSafeInt  operator +  (const OverflowSafeInt& other) const { OverflowSafeInt result = *this; result += other; return result; }
00053   FORCEINLINE OverflowSafeInt  operator +  (const int              other) const { OverflowSafeInt result = *this; result += (int64)other; return result; }
00054   FORCEINLINE OverflowSafeInt  operator +  (const uint             other) const { OverflowSafeInt result = *this; result += (int64)other; return result; }
00055   FORCEINLINE OverflowSafeInt& operator -= (const OverflowSafeInt& other)       { return *this += (-other); }
00056   FORCEINLINE OverflowSafeInt  operator -  (const OverflowSafeInt& other) const { OverflowSafeInt result = *this; result -= other; return result; }
00057   FORCEINLINE OverflowSafeInt  operator -  (const int              other) const { OverflowSafeInt result = *this; result -= (int64)other; return result; }
00058   FORCEINLINE OverflowSafeInt  operator -  (const uint             other) const { OverflowSafeInt result = *this; result -= (int64)other; return result; }
00059 
00060   FORCEINLINE OverflowSafeInt& operator ++ () { return *this += 1; }
00061   FORCEINLINE OverflowSafeInt& operator -- () { return *this += -1; }
00062   FORCEINLINE OverflowSafeInt operator ++ (int) { OverflowSafeInt org = *this; *this += 1; return org; }
00063   FORCEINLINE OverflowSafeInt operator -- (int) { OverflowSafeInt org = *this; *this += -1; return org; }
00064 
00071   FORCEINLINE OverflowSafeInt& operator *= (const int factor)
00072   {
00073     if (factor != 0 && (T_MAX / abs(factor)) < abs(this->m_value)) {
00074        this->m_value = ((this->m_value < 0) == (factor < 0)) ? T_MAX : T_MIN ;
00075     } else {
00076       this->m_value *= factor ;
00077     }
00078     return *this;
00079   }
00080 
00081   /* Operators for multiplication */
00082   FORCEINLINE OverflowSafeInt operator * (const int64  factor) const { OverflowSafeInt result = *this; result *= factor; return result; }
00083   FORCEINLINE OverflowSafeInt operator * (const int    factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
00084   FORCEINLINE OverflowSafeInt operator * (const uint   factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
00085   FORCEINLINE OverflowSafeInt operator * (const uint16 factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
00086   FORCEINLINE OverflowSafeInt operator * (const byte   factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
00087 
00088   /* Operators for division */
00089   FORCEINLINE OverflowSafeInt& operator /= (const int64            divisor)       { this->m_value /= divisor; return *this; }
00090   FORCEINLINE OverflowSafeInt  operator /  (const OverflowSafeInt& divisor) const { OverflowSafeInt result = *this; result /= divisor.m_value; return result; }
00091   FORCEINLINE OverflowSafeInt  operator /  (const int              divisor) const { OverflowSafeInt result = *this; result /= divisor; return result; }
00092   FORCEINLINE OverflowSafeInt  operator /  (const uint             divisor) const { OverflowSafeInt result = *this; result /= (int)divisor; return result; }
00093 
00094   /* Operators for modulo */
00095   FORCEINLINE OverflowSafeInt& operator %= (const int  divisor)       { this->m_value %= divisor; return *this; }
00096   FORCEINLINE OverflowSafeInt  operator %  (const int  divisor) const { OverflowSafeInt result = *this; result %= divisor; return result; }
00097 
00098   /* Operators for shifting */
00099   FORCEINLINE OverflowSafeInt& operator <<= (const int shift)       { this->m_value <<= shift; return *this; }
00100   FORCEINLINE OverflowSafeInt  operator <<  (const int shift) const { OverflowSafeInt result = *this; result <<= shift; return result; }
00101   FORCEINLINE OverflowSafeInt& operator >>= (const int shift)       { this->m_value >>= shift; return *this; }
00102   FORCEINLINE OverflowSafeInt  operator >>  (const int shift) const { OverflowSafeInt result = *this; result >>= shift; return result; }
00103 
00104   /* Operators for (in)equality when comparing overflow safe ints */
00105   FORCEINLINE bool operator == (const OverflowSafeInt& other) const { return this->m_value == other.m_value; }
00106   FORCEINLINE bool operator != (const OverflowSafeInt& other) const { return !(*this == other); }
00107   FORCEINLINE bool operator >  (const OverflowSafeInt& other) const { return this->m_value > other.m_value; }
00108   FORCEINLINE bool operator >= (const OverflowSafeInt& other) const { return this->m_value >= other.m_value; }
00109   FORCEINLINE bool operator <  (const OverflowSafeInt& other) const { return !(*this >= other); }
00110   FORCEINLINE bool operator <= (const OverflowSafeInt& other) const { return !(*this > other); }
00111 
00112   /* Operators for (in)equality when comparing non-overflow safe ints */
00113   FORCEINLINE bool operator == (const int other) const { return this->m_value == other; }
00114   FORCEINLINE bool operator != (const int other) const { return !(*this == other); }
00115   FORCEINLINE bool operator >  (const int other) const { return this->m_value > other; }
00116   FORCEINLINE bool operator >= (const int other) const { return this->m_value >= other; }
00117   FORCEINLINE bool operator <  (const int other) const { return !(*this >= other); }
00118   FORCEINLINE bool operator <= (const int other) const { return !(*this > other); }
00119 
00120   FORCEINLINE operator int64 () const { return this->m_value; }
00121 };
00122 
00123 /* Sometimes we got int64 operator OverflowSafeInt instead of vice versa. Handle that properly */
00124 template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator + (int64 a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b + a; }
00125 template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator - (int64 a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return -b + a; }
00126 template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator * (int64 a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b * a; }
00127 template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE 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; }
00128 
00129 /* Sometimes we got int operator OverflowSafeInt instead of vice versa. Handle that properly */
00130 template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator + (int   a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b + a; }
00131 template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator - (int   a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return -b + a; }
00132 template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator * (int   a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b * a; }
00133 template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE 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; }
00134 
00135 /* Sometimes we got uint operator OverflowSafeInt instead of vice versa. Handle that properly */
00136 template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator + (uint  a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b + a; }
00137 template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator - (uint  a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return -b + a; }
00138 template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator * (uint  a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b * a; }
00139 template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE 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; }
00140 
00141 /* Sometimes we got byte operator OverflowSafeInt instead of vice versa. Handle that properly */
00142 template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator + (byte  a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b + (uint)a; }
00143 template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator - (byte  a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return -b + (uint)a; }
00144 template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator * (byte  a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b * (uint)a; }
00145 template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE 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; }
00146 
00147 typedef OverflowSafeInt<int64, INT64_MAX, INT64_MIN> OverflowSafeInt64;
00148 
00149 #endif /* OVERFLOWSAFE_TYPE_HPP */

Generated on Sun Sep 13 08:19:15 2009 for OpenTTD by  doxygen 1.5.6