12 #ifndef OVERFLOWSAFE_TYPE_HPP 13 #define OVERFLOWSAFE_TYPE_HPP 25 template <
class T, T T_MAX, T T_MIN>
50 (this->m_value < 0) == (other.
m_value < 0)) {
51 this->m_value = (this->m_value < 0) ? T_MIN : T_MAX ;
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 ;
83 this->m_value *= factor ;
96 inline OverflowSafeInt& operator /= (
const int64 divisor) { this->m_value /= divisor;
return *
this; }
102 inline OverflowSafeInt& operator %= (
const int divisor) { this->m_value %= divisor;
return *
this; }
106 inline OverflowSafeInt& operator <<= (
const int shift) { this->m_value <<= shift;
return *
this; }
108 inline OverflowSafeInt& operator >>= (
const int shift) { this->m_value >>= shift;
return *
this; }
113 inline bool operator != (
const OverflowSafeInt& other)
const {
return !(*
this == other); }
116 inline bool operator < (
const OverflowSafeInt& other)
const {
return !(*
this >= other); }
117 inline bool operator <= (
const OverflowSafeInt& 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 == 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); }
127 inline operator int64 ()
const {
return this->
m_value; }
T m_value
The non-overflow safe backend to store the value in.
OverflowSafeInt & operator*=(const int factor)
Safe implementation of multiplication.
static T abs(const T a)
Returns the absolute value of (scalar) variable.
OverflowSafeInt & operator+=(const OverflowSafeInt &other)
Safe implementation of addition.
Overflow safe template for integers, i.e.