OpenTTD
overflowsafe_type.hpp
Go to the documentation of this file.
1 /* $Id: overflowsafe_type.hpp 26685 2014-07-12 17:04:14Z alberth $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #ifndef OVERFLOWSAFE_TYPE_HPP
13 #define OVERFLOWSAFE_TYPE_HPP
14 
15 #include "math_func.hpp"
16 
25 template <class T, T T_MAX, T T_MIN>
27 {
28 private:
31 public:
32  OverflowSafeInt() : m_value(0) { }
33 
34  OverflowSafeInt(const OverflowSafeInt& other) { this->m_value = other.m_value; }
35  OverflowSafeInt(const int64 int_) { this->m_value = int_; }
36 
37  inline OverflowSafeInt& operator = (const OverflowSafeInt& other) { this->m_value = other.m_value; return *this; }
38 
39  inline OverflowSafeInt operator - () const { return OverflowSafeInt(-this->m_value); }
40 
48  {
49  if ((T_MAX - abs(other.m_value)) < abs(this->m_value) &&
50  (this->m_value < 0) == (other.m_value < 0)) {
51  this->m_value = (this->m_value < 0) ? T_MIN : T_MAX ;
52  } else {
53  this->m_value += other.m_value;
54  }
55  return *this;
56  }
57 
58  /* Operators for addition and subtraction */
59  inline OverflowSafeInt operator + (const OverflowSafeInt& other) const { OverflowSafeInt result = *this; result += other; return result; }
60  inline OverflowSafeInt operator + (const int other) const { OverflowSafeInt result = *this; result += (int64)other; return result; }
61  inline OverflowSafeInt operator + (const uint other) const { OverflowSafeInt result = *this; result += (int64)other; return result; }
62  inline OverflowSafeInt& operator -= (const OverflowSafeInt& other) { return *this += (-other); }
63  inline OverflowSafeInt operator - (const OverflowSafeInt& other) const { OverflowSafeInt result = *this; result -= other; return result; }
64  inline OverflowSafeInt operator - (const int other) const { OverflowSafeInt result = *this; result -= (int64)other; return result; }
65  inline OverflowSafeInt operator - (const uint other) const { OverflowSafeInt result = *this; result -= (int64)other; return result; }
66 
67  inline OverflowSafeInt& operator ++ () { return *this += 1; }
68  inline OverflowSafeInt& operator -- () { return *this += -1; }
69  inline OverflowSafeInt operator ++ (int) { OverflowSafeInt org = *this; *this += 1; return org; }
70  inline OverflowSafeInt operator -- (int) { OverflowSafeInt org = *this; *this += -1; return org; }
71 
78  inline OverflowSafeInt& operator *= (const int factor)
79  {
80  if (factor != 0 && (T_MAX / abs(factor)) < abs(this->m_value)) {
81  this->m_value = ((this->m_value < 0) == (factor < 0)) ? T_MAX : T_MIN ;
82  } else {
83  this->m_value *= factor ;
84  }
85  return *this;
86  }
87 
88  /* Operators for multiplication */
89  inline OverflowSafeInt operator * (const int64 factor) const { OverflowSafeInt result = *this; result *= factor; return result; }
90  inline OverflowSafeInt operator * (const int factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
91  inline OverflowSafeInt operator * (const uint factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
92  inline OverflowSafeInt operator * (const uint16 factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
93  inline OverflowSafeInt operator * (const byte factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
94 
95  /* Operators for division */
96  inline OverflowSafeInt& operator /= (const int64 divisor) { this->m_value /= divisor; return *this; }
97  inline OverflowSafeInt operator / (const OverflowSafeInt& divisor) const { OverflowSafeInt result = *this; result /= divisor.m_value; return result; }
98  inline OverflowSafeInt operator / (const int divisor) const { OverflowSafeInt result = *this; result /= divisor; return result; }
99  inline OverflowSafeInt operator / (const uint divisor) const { OverflowSafeInt result = *this; result /= (int)divisor; return result; }
100 
101  /* Operators for modulo */
102  inline OverflowSafeInt& operator %= (const int divisor) { this->m_value %= divisor; return *this; }
103  inline OverflowSafeInt operator % (const int divisor) const { OverflowSafeInt result = *this; result %= divisor; return result; }
104 
105  /* Operators for shifting */
106  inline OverflowSafeInt& operator <<= (const int shift) { this->m_value <<= shift; return *this; }
107  inline OverflowSafeInt operator << (const int shift) const { OverflowSafeInt result = *this; result <<= shift; return result; }
108  inline OverflowSafeInt& operator >>= (const int shift) { this->m_value >>= shift; return *this; }
109  inline OverflowSafeInt operator >> (const int shift) const { OverflowSafeInt result = *this; result >>= shift; return result; }
110 
111  /* Operators for (in)equality when comparing overflow safe ints */
112  inline bool operator == (const OverflowSafeInt& other) const { return this->m_value == other.m_value; }
113  inline bool operator != (const OverflowSafeInt& other) const { return !(*this == other); }
114  inline bool operator > (const OverflowSafeInt& other) const { return this->m_value > other.m_value; }
115  inline bool operator >= (const OverflowSafeInt& other) const { return this->m_value >= other.m_value; }
116  inline bool operator < (const OverflowSafeInt& other) const { return !(*this >= other); }
117  inline bool operator <= (const OverflowSafeInt& other) const { return !(*this > other); }
118 
119  /* Operators for (in)equality when comparing non-overflow safe ints */
120  inline bool operator == (const int other) const { return this->m_value == other; }
121  inline bool operator != (const int other) const { return !(*this == other); }
122  inline bool operator > (const int other) const { return this->m_value > other; }
123  inline bool operator >= (const int other) const { return this->m_value >= other; }
124  inline bool operator < (const int other) const { return !(*this >= other); }
125  inline bool operator <= (const int other) const { return !(*this > other); }
126 
127  inline operator int64 () const { return this->m_value; }
128 };
129 
130 /* Sometimes we got int64 operator OverflowSafeInt instead of vice versa. Handle that properly */
131 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; }
132 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; }
133 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; }
134 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; }
135 
136 /* Sometimes we got int operator OverflowSafeInt instead of vice versa. Handle that properly */
137 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; }
138 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; }
139 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; }
140 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; }
141 
142 /* Sometimes we got uint operator OverflowSafeInt instead of vice versa. Handle that properly */
143 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; }
144 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; }
145 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; }
146 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; }
147 
148 /* Sometimes we got byte operator OverflowSafeInt instead of vice versa. Handle that properly */
149 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; }
150 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; }
151 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; }
152 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; }
153 
156 
157 #endif /* OVERFLOWSAFE_TYPE_HPP */
T m_value
The non-overflow safe backend to store the value in.
Integer math functions.
OverflowSafeInt & operator*=(const int factor)
Safe implementation of multiplication.
static T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition: math_func.hpp:83
OverflowSafeInt & operator+=(const OverflowSafeInt &other)
Safe implementation of addition.
Overflow safe template for integers, i.e.