# overflowsafe_type.hpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    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   FORCEINLINE OverflowSafeInt& operator = (const OverflowSafeInt& other) { this->m_value = other.m_value; return *this; }
    00037
    00038   FORCEINLINE OverflowSafeInt operator - () const { return OverflowSafeInt(-this->m_value); }
    00039
    00046   FORCEINLINE 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   /* Operators for addition and substraction */
    00058   FORCEINLINE OverflowSafeInt  operator +  (const OverflowSafeInt& other) const { OverflowSafeInt result = *this; result += other; return result; }
    00059   FORCEINLINE OverflowSafeInt  operator +  (const int              other) const { OverflowSafeInt result = *this; result += (int64)other; return result; }
    00060   FORCEINLINE OverflowSafeInt  operator +  (const uint             other) const { OverflowSafeInt result = *this; result += (int64)other; return result; }
    00061   FORCEINLINE OverflowSafeInt& operator -= (const OverflowSafeInt& other)       { return *this += (-other); }
    00062   FORCEINLINE OverflowSafeInt  operator -  (const OverflowSafeInt& other) const { OverflowSafeInt result = *this; result -= other; return result; }
    00063   FORCEINLINE OverflowSafeInt  operator -  (const int              other) const { OverflowSafeInt result = *this; result -= (int64)other; return result; }
    00064   FORCEINLINE OverflowSafeInt  operator -  (const uint             other) const { OverflowSafeInt result = *this; result -= (int64)other; return result; }
    00065
    00066   FORCEINLINE OverflowSafeInt& operator ++ () { return *this += 1; }
    00067   FORCEINLINE OverflowSafeInt& operator -- () { return *this += -1; }
    00068   FORCEINLINE OverflowSafeInt operator ++ (int) { OverflowSafeInt org = *this; *this += 1; return org; }
    00069   FORCEINLINE OverflowSafeInt operator -- (int) { OverflowSafeInt org = *this; *this += -1; return org; }
    00070
    00077   FORCEINLINE 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   /* Operators for multiplication */
    00088   FORCEINLINE OverflowSafeInt operator * (const int64  factor) const { OverflowSafeInt result = *this; result *= factor; return result; }
    00089   FORCEINLINE OverflowSafeInt operator * (const int    factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
    00090   FORCEINLINE OverflowSafeInt operator * (const uint   factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
    00091   FORCEINLINE OverflowSafeInt operator * (const uint16 factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
    00092   FORCEINLINE OverflowSafeInt operator * (const byte   factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
    00093
    00094   /* Operators for division */
    00095   FORCEINLINE OverflowSafeInt& operator /= (const int64            divisor)       { this->m_value /= divisor; return *this; }
    00096   FORCEINLINE OverflowSafeInt  operator /  (const OverflowSafeInt& divisor) const { OverflowSafeInt result = *this; result /= divisor.m_value; return result; }
    00097   FORCEINLINE OverflowSafeInt  operator /  (const int              divisor) const { OverflowSafeInt result = *this; result /= divisor; return result; }
    00098   FORCEINLINE OverflowSafeInt  operator /  (const uint             divisor) const { OverflowSafeInt result = *this; result /= (int)divisor; return result; }
    00099
    00100   /* Operators for modulo */
    00101   FORCEINLINE OverflowSafeInt& operator %= (const int  divisor)       { this->m_value %= divisor; return *this; }
    00102   FORCEINLINE OverflowSafeInt  operator %  (const int  divisor) const { OverflowSafeInt result = *this; result %= divisor; return result; }
    00103
    00104   /* Operators for shifting */
    00105   FORCEINLINE OverflowSafeInt& operator <<= (const int shift)       { this->m_value <<= shift; return *this; }
    00106   FORCEINLINE OverflowSafeInt  operator <<  (const int shift) const { OverflowSafeInt result = *this; result <<= shift; return result; }
    00107   FORCEINLINE OverflowSafeInt& operator >>= (const int shift)       { this->m_value >>= shift; return *this; }
    00108   FORCEINLINE OverflowSafeInt  operator >>  (const int shift) const { OverflowSafeInt result = *this; result >>= shift; return result; }
    00109
    00110   /* Operators for (in)equality when comparing overflow safe ints */
    00111   FORCEINLINE bool operator == (const OverflowSafeInt& other) const { return this->m_value == other.m_value; }
    00112   FORCEINLINE bool operator != (const OverflowSafeInt& other) const { return !(*this == other); }
    00113   FORCEINLINE bool operator >  (const OverflowSafeInt& other) const { return this->m_value > other.m_value; }
    00114   FORCEINLINE bool operator >= (const OverflowSafeInt& other) const { return this->m_value >= other.m_value; }
    00115   FORCEINLINE bool operator <  (const OverflowSafeInt& other) const { return !(*this >= other); }
    00116   FORCEINLINE bool operator <= (const OverflowSafeInt& other) const { return !(*this > other); }
    00117
    00118   /* Operators for (in)equality when comparing non-overflow safe ints */
    00119   FORCEINLINE bool operator == (const int other) const { return this->m_value == other; }
    00120   FORCEINLINE bool operator != (const int other) const { return !(*this == other); }
    00121   FORCEINLINE bool operator >  (const int other) const { return this->m_value > other; }
    00122   FORCEINLINE bool operator >= (const int other) const { return this->m_value >= other; }
    00123   FORCEINLINE bool operator <  (const int other) const { return !(*this >= other); }
    00124   FORCEINLINE bool operator <= (const int other) const { return !(*this > other); }
    00125
    00126   FORCEINLINE operator int64 () const { return this->m_value; }
    00127 };
    00128
    00129 /* Sometimes we got int64 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 + (int64 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 - (int64 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 * (int64 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 / (int64 a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return (OverflowSafeInt<T, T_MAX, T_MIN>)a / (int)b; }
    00134
    00135 /* Sometimes we got int 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 + (int   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 - (int   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 * (int   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 / (int   a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return (OverflowSafeInt<T, T_MAX, T_MIN>)a / (int)b; }
    00140
    00141 /* Sometimes we got uint 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 + (uint  a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b + a; }
    00143 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; }
    00144 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; }
    00145 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; }
    00146
    00147 /* Sometimes we got byte operator OverflowSafeInt instead of vice versa. Handle that properly */
    00148 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; }
    00149 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; }
    00150 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; }
    00151 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; }
    00152
    00153 typedef OverflowSafeInt<int64, INT64_MAX, INT64_MIN> OverflowSafeInt64;
    00154
    00155 #endif /* OVERFLOWSAFE_TYPE_HPP */