# overflowsafe_type.hpp

Go to the documentation of this file.

    00001 /* $Id: overflowsafe_type.hpp 17248 2009-08-21 20:21:05Z rubidium $ */
    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 #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   FORCEINLINE OverflowSafeInt& operator = (const OverflowSafeInt& other) { this->m_value = other.m_value; return *this; }
    00038
    00039   FORCEINLINE OverflowSafeInt operator - () const { return OverflowSafeInt(-this->m_value); }
    00040
    00047   FORCEINLINE 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   /* Operators for addition and substraction */
    00059   FORCEINLINE OverflowSafeInt  operator +  (const OverflowSafeInt& other) const { OverflowSafeInt result = *this; result += other; return result; }
    00060   FORCEINLINE OverflowSafeInt  operator +  (const int              other) const { OverflowSafeInt result = *this; result += (int64)other; return result; }
    00061   FORCEINLINE OverflowSafeInt  operator +  (const uint             other) const { OverflowSafeInt result = *this; result += (int64)other; return result; }
    00062   FORCEINLINE OverflowSafeInt& operator -= (const OverflowSafeInt& other)       { return *this += (-other); }
    00063   FORCEINLINE OverflowSafeInt  operator -  (const OverflowSafeInt& other) const { OverflowSafeInt result = *this; result -= other; return result; }
    00064   FORCEINLINE OverflowSafeInt  operator -  (const int              other) const { OverflowSafeInt result = *this; result -= (int64)other; return result; }
    00065   FORCEINLINE OverflowSafeInt  operator -  (const uint             other) const { OverflowSafeInt result = *this; result -= (int64)other; return result; }
    00066
    00067   FORCEINLINE OverflowSafeInt& operator ++ () { return *this += 1; }
    00068   FORCEINLINE OverflowSafeInt& operator -- () { return *this += -1; }
    00069   FORCEINLINE OverflowSafeInt operator ++ (int) { OverflowSafeInt org = *this; *this += 1; return org; }
    00070   FORCEINLINE OverflowSafeInt operator -- (int) { OverflowSafeInt org = *this; *this += -1; return org; }
    00071
    00078   FORCEINLINE 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   /* Operators for multiplication */
    00089   FORCEINLINE OverflowSafeInt operator * (const int64  factor) const { OverflowSafeInt result = *this; result *= factor; return result; }
    00090   FORCEINLINE OverflowSafeInt operator * (const int    factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
    00091   FORCEINLINE OverflowSafeInt operator * (const uint   factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
    00092   FORCEINLINE OverflowSafeInt operator * (const uint16 factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
    00093   FORCEINLINE OverflowSafeInt operator * (const byte   factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
    00094
    00095   /* Operators for division */
    00096   FORCEINLINE OverflowSafeInt& operator /= (const int64            divisor)       { this->m_value /= divisor; return *this; }
    00097   FORCEINLINE OverflowSafeInt  operator /  (const OverflowSafeInt& divisor) const { OverflowSafeInt result = *this; result /= divisor.m_value; return result; }
    00098   FORCEINLINE OverflowSafeInt  operator /  (const int              divisor) const { OverflowSafeInt result = *this; result /= divisor; return result; }
    00099   FORCEINLINE OverflowSafeInt  operator /  (const uint             divisor) const { OverflowSafeInt result = *this; result /= (int)divisor; return result; }
    00100
    00101   /* Operators for modulo */
    00102   FORCEINLINE OverflowSafeInt& operator %= (const int  divisor)       { this->m_value %= divisor; return *this; }
    00103   FORCEINLINE OverflowSafeInt  operator %  (const int  divisor) const { OverflowSafeInt result = *this; result %= divisor; return result; }
    00104
    00105   /* Operators for shifting */
    00106   FORCEINLINE OverflowSafeInt& operator <<= (const int shift)       { this->m_value <<= shift; return *this; }
    00107   FORCEINLINE OverflowSafeInt  operator <<  (const int shift) const { OverflowSafeInt result = *this; result <<= shift; return result; }
    00108   FORCEINLINE OverflowSafeInt& operator >>= (const int shift)       { this->m_value >>= shift; return *this; }
    00109   FORCEINLINE OverflowSafeInt  operator >>  (const int shift) const { OverflowSafeInt result = *this; result >>= shift; return result; }
    00110
    00111   /* Operators for (in)equality when comparing overflow safe ints */
    00112   FORCEINLINE bool operator == (const OverflowSafeInt& other) const { return this->m_value == other.m_value; }
    00113   FORCEINLINE bool operator != (const OverflowSafeInt& other) const { return !(*this == other); }
    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->m_value >= other.m_value; }
    00116   FORCEINLINE bool operator <  (const OverflowSafeInt& other) const { return !(*this >= other); }
    00117   FORCEINLINE bool operator <= (const OverflowSafeInt& other) const { return !(*this > other); }
    00118
    00119   /* Operators for (in)equality when comparing non-overflow safe ints */
    00120   FORCEINLINE bool operator == (const int other) const { return this->m_value == other; }
    00121   FORCEINLINE bool operator != (const int other) const { return !(*this == other); }
    00122   FORCEINLINE bool operator >  (const int other) const { return this->m_value > other; }
    00123   FORCEINLINE bool operator >= (const int other) const { return this->m_value >= other; }
    00124   FORCEINLINE bool operator <  (const int other) const { return !(*this >= other); }
    00125   FORCEINLINE bool operator <= (const int other) const { return !(*this > other); }
    00126
    00127   FORCEINLINE operator int64 () const { return this->m_value; }
    00128 };
    00129
    00130 /* Sometimes we got int64 operator OverflowSafeInt instead of vice versa. Handle that properly */
    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 b * a; }
    00134 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; }
    00135
    00136 /* Sometimes we got int operator OverflowSafeInt instead of vice versa. Handle that properly */
    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 b * a; }
    00140 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; }
    00141
    00142 /* Sometimes we got uint operator OverflowSafeInt instead of vice versa. Handle that properly */
    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 b * a; }
    00146 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; }
    00147
    00148 /* Sometimes we got byte operator OverflowSafeInt instead of vice versa. Handle that properly */
    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 b * (uint)a; }
    00152 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; }
    00153
    00154 typedef OverflowSafeInt<int64, INT64_MAX, INT64_MIN> OverflowSafeInt64;
    00155
    00156 #endif /* OVERFLOWSAFE_TYPE_HPP */