OpenTTD Source: src/core/overflowsafe_type.hpp Source File

OpenTTD Source 1.11.0-RC1

overflowsafe_type.hpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
7
10 #ifndef OVERFLOWSAFE_TYPE_HPP
11 #define OVERFLOWSAFE_TYPE_HPP
12
13 #include "math_func.hpp"
14
23 template <class T, T T_MAX, T T_MIN>
24 class OverflowSafeInt
25 {
26 private:
28 T m_value;
29 public:
30 OverflowSafeInt() : m_value(0) { }
31
32 OverflowSafeInt(const OverflowSafeInt& other) { this->m_value = other.m_value; }
33 OverflowSafeInt(const int64 int_) { this->m_value = int_; }
34
35 inline OverflowSafeInt& operator = (const OverflowSafeInt& other) { this->m_value = other.m_value; return *this; }
36
37 inline OverflowSafeInt operator - () const { return OverflowSafeInt(-this->m_value); }
38
45 inline OverflowSafeInt& operator += (const OverflowSafeInt& other)
46 {
47 if ((T_MAX - abs(other.m_value)) < abs(this->m_value) &&
48 (this->m_value < 0) == (other.m_value < 0)) {
49 this->m_value = (this->m_value < 0) ? T_MIN : T_MAX ;
50 } else {
51 this->m_value += other.m_value;
52 }
53 return *this;
54 }
55
56 /* Operators for addition and subtraction */
57 inline OverflowSafeInt operator + (const OverflowSafeInt& other) const { OverflowSafeInt result = *this; result += other; return result; }
58 inline OverflowSafeInt operator + (const int other) const { OverflowSafeInt result = *this; result += (int64)other; return result; }
59 inline OverflowSafeInt operator + (const uint other) const { OverflowSafeInt result = *this; result += (int64)other; return result; }
60 inline OverflowSafeInt& operator -= (const OverflowSafeInt& other) { return *this += (-other); }
61 inline OverflowSafeInt operator - (const OverflowSafeInt& other) const { OverflowSafeInt result = *this; result -= other; return result; }
62 inline OverflowSafeInt operator - (const int other) const { OverflowSafeInt result = *this; result -= (int64)other; return result; }
63 inline OverflowSafeInt operator - (const uint other) const { OverflowSafeInt result = *this; result -= (int64)other; return result; }
64
65 inline OverflowSafeInt& operator ++ () { return *this += 1; }
66 inline OverflowSafeInt& operator -- () { return *this += -1; }
67 inline OverflowSafeInt operator ++ (int) { OverflowSafeInt org = *this; *this += 1; return org; }
68 inline OverflowSafeInt operator -- (int) { OverflowSafeInt org = *this; *this += -1; return org; }
69
76 inline OverflowSafeInt& operator *= (const int factor)
77 {
78 if (factor != 0 && (T_MAX / abs(factor)) < abs(this->m_value)) {
79 this->m_value = ((this->m_value < 0) == (factor < 0)) ? T_MAX : T_MIN ;
80 } else {
81 this->m_value *= factor ;
82 }
83 return *this;
84 }
85
86 /* Operators for multiplication */
87 inline OverflowSafeInt operator * (const int64 factor) const { OverflowSafeInt result = *this; result *= factor; return result; }
88 inline OverflowSafeInt operator * (const int factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
89 inline OverflowSafeInt operator * (const uint factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
90 inline OverflowSafeInt operator * (const uint16 factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
91 inline OverflowSafeInt operator * (const byte factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
92
93 /* Operators for division */
94 inline OverflowSafeInt& operator /= (const int64 divisor) { this->m_value /= divisor; return *this; }
95 inline OverflowSafeInt operator / (const OverflowSafeInt& divisor) const { OverflowSafeInt result = *this; result /= divisor.m_value; return result; }
96 inline OverflowSafeInt operator / (const int divisor) const { OverflowSafeInt result = *this; result /= divisor; return result; }
97 inline OverflowSafeInt operator / (const uint divisor) const { OverflowSafeInt result = *this; result /= (int)divisor; return result; }
98
99 /* Operators for modulo */
100 inline OverflowSafeInt& operator %= (const int divisor) { this->m_value %= divisor; return *this; }
101 inline OverflowSafeInt operator % (const int divisor) const { OverflowSafeInt result = *this; result %= divisor; return result; }
102
103 /* Operators for shifting */
104 inline OverflowSafeInt& operator <<= (const int shift) { this->m_value <<= shift; return *this; }
105 inline OverflowSafeInt operator << (const int shift) const { OverflowSafeInt result = *this; result <<= shift; return result; }
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
109 /* Operators for (in)equality when comparing overflow safe ints */
110 inline bool operator == (const OverflowSafeInt& other) const { return this->m_value == other.m_value; }
111 inline bool operator != (const OverflowSafeInt& other) const { return !(*this == other); }
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->m_value >= other.m_value; }
114 inline bool operator < (const OverflowSafeInt& other) const { return !(*this >= other); }
115 inline bool operator <= (const OverflowSafeInt& other) const { return !(*this > other); }
116
117 /* Operators for (in)equality when comparing non-overflow safe ints */
118 inline bool operator == (const int other) const { return this->m_value == other; }
119 inline bool operator != (const int other) const { return !(*this == other); }
120 inline bool operator > (const int other) const { return this->m_value > other; }
121 inline bool operator >= (const int other) const { return this->m_value >= other; }
122 inline bool operator < (const int other) const { return !(*this >= other); }
123 inline bool operator <= (const int other) const { return !(*this > other); }
124
125 inline operator int64 () const { return this->m_value; }
126 };
127
128 /* Sometimes we got int64 operator OverflowSafeInt instead of vice versa. Handle that properly */
129 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; }
130 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; }
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 (OverflowSafeInt<T, T_MAX, T_MIN>)a / (int)b; }
133
134 /* Sometimes we got int operator OverflowSafeInt instead of vice versa. Handle that properly */
135 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; }
136 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; }
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 (OverflowSafeInt<T, T_MAX, T_MIN>)a / (int)b; }
139
140 /* Sometimes we got uint operator OverflowSafeInt instead of vice versa. Handle that properly */
141 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; }
142 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; }
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 (OverflowSafeInt<T, T_MAX, T_MIN>)a / (int)b; }
145
146 /* Sometimes we got byte operator OverflowSafeInt instead of vice versa. Handle that properly */
147 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; }
148 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; }
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 (OverflowSafeInt<T, T_MAX, T_MIN>)a / (int)b; }
151
152 typedef OverflowSafeInt<int64, INT64_MAX, INT64_MIN> OverflowSafeInt64;
153 typedef OverflowSafeInt<int32, INT32_MAX, INT32_MIN> OverflowSafeInt32;
154
155 #endif /* OVERFLOWSAFE_TYPE_HPP */
math_func.hpp
OverflowSafeInt::operator*=
OverflowSafeInt & operator*=(const int factor)
Safe implementation of multiplication.
Definition: overflowsafe_type.hpp:76
OverflowSafeInt::operator+=
OverflowSafeInt & operator+=(const OverflowSafeInt &other)
Safe implementation of addition.
Definition: overflowsafe_type.hpp:45
abs
static T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition: math_func.hpp:21
OverflowSafeInt
Overflow safe template for integers, i.e.
Definition: overflowsafe_type.hpp:24
OverflowSafeInt::m_value
T m_value
The non-overflow safe backend to store the value in.
Definition: overflowsafe_type.hpp:28