src/helpers.hpp
changeset 8259 127e5d73637d
parent 6505 abcb0580d976
child 8334 bca925892b54
equal deleted inserted replaced
8258:e5ded58d6efb 8259:127e5d73637d
   156 template <typename T> void ToggleBitT(T &t, int bit_index)
   156 template <typename T> void ToggleBitT(T &t, int bit_index)
   157 {
   157 {
   158 	t = (T)(t ^ ((T)1 << bit_index));
   158 	t = (T)(t ^ ((T)1 << bit_index));
   159 }
   159 }
   160 
   160 
       
   161 /**
       
   162  * Overflow safe template for integers, i.e. integers that will never overflow
       
   163  * you multiply the maximum value with 2, or add 2, or substract somethng from
       
   164  * the minimum value, etc.
       
   165  * @param T     the type these integers are stored with.
       
   166  * @param T_MAX the maximum value for the integers.
       
   167  * @param T_MIN the minimum value for the integers.
       
   168  */
       
   169 template <class T, T T_MAX, T T_MIN>
       
   170 class OverflowSafeInt
       
   171 {
       
   172 private:
       
   173 	/** The non-overflow safe backend to store the value in. */
       
   174 	T m_value;
       
   175 public:
       
   176 	OverflowSafeInt() : m_value(0) { }
       
   177 
       
   178 	OverflowSafeInt(const OverflowSafeInt& other) { this->m_value = other.m_value; }
       
   179 	OverflowSafeInt(const int64 int_)             { this->m_value = int_; }
       
   180 
       
   181 	FORCEINLINE OverflowSafeInt& operator = (const OverflowSafeInt& other) { this->m_value = other.m_value; return *this; }
       
   182 
       
   183 	FORCEINLINE OverflowSafeInt operator - () const { return OverflowSafeInt(-this->m_value); }
       
   184 
       
   185 	/**
       
   186 	 * Safe implementation of addition.
       
   187 	 * @param other the amount to add
       
   188 	 * @note when the addition would yield more than T_MAX (or less than T_MIN),
       
   189 	 *       it will be T_MAX (respectively T_MIN).
       
   190 	 */
       
   191 	FORCEINLINE OverflowSafeInt& operator += (const OverflowSafeInt& other)
       
   192 	{
       
   193 		if ((T_MAX - myabs(other.m_value)) < myabs(this->m_value) &&
       
   194 				(this->m_value < 0) == (other.m_value < 0)) {
       
   195 			this->m_value = (this->m_value < 0) ? T_MIN : T_MAX ;
       
   196 		} else {
       
   197 			this->m_value += other.m_value;
       
   198 		}
       
   199 		return *this;
       
   200 	}
       
   201 
       
   202 	/* Operators for addition and substraction */
       
   203 	FORCEINLINE OverflowSafeInt  operator +  (const OverflowSafeInt& other) const { OverflowSafeInt result = *this; result += other; return result; }
       
   204 	FORCEINLINE OverflowSafeInt  operator +  (const int              other) const { OverflowSafeInt result = *this; result += (int64)other; return result; }
       
   205 	FORCEINLINE OverflowSafeInt  operator +  (const uint             other) const { OverflowSafeInt result = *this; result += (int64)other; return result; }
       
   206 	FORCEINLINE OverflowSafeInt& operator -= (const OverflowSafeInt& other)       { return *this += (-other); }
       
   207 	FORCEINLINE OverflowSafeInt  operator -  (const OverflowSafeInt& other) const { OverflowSafeInt result = *this; result -= other; return result; }
       
   208 	FORCEINLINE OverflowSafeInt  operator -  (const int              other) const { OverflowSafeInt result = *this; result -= (int64)other; return result; }
       
   209 	FORCEINLINE OverflowSafeInt  operator -  (const uint             other) const { OverflowSafeInt result = *this; result -= (int64)other; return result; }
       
   210 
       
   211 	FORCEINLINE OverflowSafeInt& operator ++ (int) { return *this += 1; }
       
   212 	FORCEINLINE OverflowSafeInt& operator -- (int) { return *this += -1; }
       
   213 
       
   214 	/**
       
   215 	 * Safe implementation of multiplication.
       
   216 	 * @param factor the factor to multiply this with.
       
   217 	 * @note when the multiplication would yield more than T_MAX (or less than T_MIN),
       
   218 	 *       it will be T_MAX (respectively T_MIN).
       
   219 	 */
       
   220 	FORCEINLINE OverflowSafeInt& operator *= (const int factor)
       
   221 	{
       
   222 		if (factor != 0 && (T_MAX / myabs(factor)) < myabs(this->m_value)) {
       
   223 			 this->m_value = ((this->m_value < 0) == (factor < 0)) ? T_MAX : T_MIN ;
       
   224 		} else {
       
   225 			this->m_value *= factor ;
       
   226 		}
       
   227 		return *this;
       
   228 	}
       
   229 
       
   230 	/* Operators for multiplication */
       
   231 	FORCEINLINE OverflowSafeInt operator * (const int64  factor) const { OverflowSafeInt result = *this; result *= factor; return result; }
       
   232 	FORCEINLINE OverflowSafeInt operator * (const int    factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
       
   233 	FORCEINLINE OverflowSafeInt operator * (const uint   factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
       
   234 	FORCEINLINE OverflowSafeInt operator * (const uint16 factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
       
   235 	FORCEINLINE OverflowSafeInt operator * (const byte   factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
       
   236 
       
   237 	/* Operators for division */
       
   238 	FORCEINLINE OverflowSafeInt& operator /= (const int              divisor)       { this->m_value /= divisor; return *this; }
       
   239 	FORCEINLINE OverflowSafeInt  operator /  (const OverflowSafeInt& divisor) const { OverflowSafeInt result = *this; result /= divisor.m_value; return result; }
       
   240 	FORCEINLINE OverflowSafeInt  operator /  (const int              divisor) const { OverflowSafeInt result = *this; result /= divisor; return result; }
       
   241 	FORCEINLINE OverflowSafeInt  operator /  (const uint             divisor) const { OverflowSafeInt result = *this; result /= (int)divisor; return result; }
       
   242 
       
   243 	/* Operators for modulo */
       
   244 	FORCEINLINE OverflowSafeInt& operator %= (const int  divisor)       { this->m_value %= divisor; return *this; }
       
   245 	FORCEINLINE OverflowSafeInt  operator %  (const int  divisor) const { OverflowSafeInt result = *this; result %= divisor; return result; }
       
   246 
       
   247 	/* Operators for shifting */
       
   248 	FORCEINLINE OverflowSafeInt& operator <<= (const int shift)       { this->m_value <<= shift; return *this; }
       
   249 	FORCEINLINE OverflowSafeInt  operator <<  (const int shift) const { OverflowSafeInt result = *this; result <<= shift; return result; }
       
   250 	FORCEINLINE OverflowSafeInt& operator >>= (const int shift)       { this->m_value >>= shift; return *this; }
       
   251 	FORCEINLINE OverflowSafeInt  operator >>  (const int shift) const { OverflowSafeInt result = *this; result >>= shift; return result; }
       
   252 
       
   253 	/* Operators for (in)equality when comparing overflow safe ints */
       
   254 	FORCEINLINE bool operator == (const OverflowSafeInt& other) const { return this->m_value == other.m_value; }
       
   255 	FORCEINLINE bool operator != (const OverflowSafeInt& other) const { return !(*this == other); }
       
   256 	FORCEINLINE bool operator >  (const OverflowSafeInt& other) const { return this->m_value > other.m_value; }
       
   257 	FORCEINLINE bool operator >= (const OverflowSafeInt& other) const { return this->m_value >= other.m_value; }
       
   258 	FORCEINLINE bool operator <  (const OverflowSafeInt& other) const { return !(*this >= other); }
       
   259 	FORCEINLINE bool operator <= (const OverflowSafeInt& other) const { return !(*this > other); }
       
   260 
       
   261 	/* Operators for (in)equality when comparing non-overflow safe ints */
       
   262 	FORCEINLINE bool operator == (const int other) const { return this->m_value == other; }
       
   263 	FORCEINLINE bool operator != (const int other) const { return !(*this == other); }
       
   264 	FORCEINLINE bool operator >  (const int other) const { return this->m_value > other; }
       
   265 	FORCEINLINE bool operator >= (const int other) const { return this->m_value >= other; }
       
   266 	FORCEINLINE bool operator <  (const int other) const { return !(*this >= other); }
       
   267 	FORCEINLINE bool operator <= (const int other) const { return !(*this > other); }
       
   268 
       
   269 	FORCEINLINE operator int64 () const { return this->m_value; }
       
   270 };
       
   271 
       
   272 /* Sometimes we got int64 operator OverflowSafeInt instead of vice versa. Handle that properly */
       
   273 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; }
       
   274 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; }
       
   275 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; }
       
   276 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; }
       
   277 
       
   278 /* Sometimes we got int operator OverflowSafeInt instead of vice versa. Handle that properly */
       
   279 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; }
       
   280 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; }
       
   281 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; }
       
   282 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; }
       
   283 
       
   284 /* Sometimes we got uint operator OverflowSafeInt instead of vice versa. Handle that properly */
       
   285 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; }
       
   286 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; }
       
   287 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; }
       
   288 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; }
       
   289 
       
   290 /* Sometimes we got byte operator OverflowSafeInt instead of vice versa. Handle that properly */
       
   291 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 + a; }
       
   292 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 + a; }
       
   293 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 * a; }
       
   294 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; }
       
   295 
   161 #endif /* HELPERS_HPP */
   296 #endif /* HELPERS_HPP */