src/helpers.hpp
branchnoai
changeset 9722 ebf0ece7d8f6
parent 6505 abcb0580d976
equal deleted inserted replaced
9721:9a27928bcd5e 9722:ebf0ece7d8f6
    36 	T t = a;
    36 	T t = a;
    37 	a = b;
    37 	a = b;
    38 	b = t;
    38 	b = t;
    39 }
    39 }
    40 
    40 
    41 
       
    42 /** returns the absolute value of (scalar) variable. @note assumes variable to be signed */
       
    43 template <typename T> static inline T myabs(T a) { return a < (T)0 ? -a : a; }
       
    44 /** returns the (absolute) difference between two (scalar) variables */
       
    45 template <typename T> static inline T delta(T a, T b) { return a < b ? b - a : a - b; }
       
    46 
    41 
    47 /** Some enums need to have allowed incrementing (i.e. StationClassID) */
    42 /** Some enums need to have allowed incrementing (i.e. StationClassID) */
    48 #define DECLARE_POSTFIX_INCREMENT(type) \
    43 #define DECLARE_POSTFIX_INCREMENT(type) \
    49 	FORCEINLINE type operator ++(type& e, int) \
    44 	FORCEINLINE type operator ++(type& e, int) \
    50 	{ \
    45 	{ \
   134 	{
   129 	{
   135 		m_val = (storage_type)e; return *this;
   130 		m_val = (storage_type)e; return *this;
   136 	}
   131 	}
   137 
   132 
   138 	/** postfix ++ operator on tiny type */
   133 	/** postfix ++ operator on tiny type */
   139 	FORCEINLINE TinyEnumT& operator ++ (int)
   134 	FORCEINLINE TinyEnumT operator ++ (int)
       
   135 	{
       
   136 		TinyEnumT org = *this;
       
   137 		if (++m_val >= end) m_val -= (storage_type)(end - begin);
       
   138 		return org;
       
   139 	}
       
   140 
       
   141 	/** prefix ++ operator on tiny type */
       
   142 	FORCEINLINE TinyEnumT& operator ++ ()
   140 	{
   143 	{
   141 		if (++m_val >= end) m_val -= (storage_type)(end - begin);
   144 		if (++m_val >= end) m_val -= (storage_type)(end - begin);
   142 		return *this;
   145 		return *this;
   143 	}
   146 	}
   144 };
   147 };
   145 
   148 
   146 template <typename T> void ClrBitT(T &t, int bit_index)
   149 /**
   147 {
   150  * Overflow safe template for integers, i.e. integers that will never overflow
   148 	t = (T)(t & ~((T)1 << bit_index));
   151  * you multiply the maximum value with 2, or add 2, or substract somethng from
   149 }
   152  * the minimum value, etc.
   150 
   153  * @param T     the type these integers are stored with.
   151 template <typename T> void SetBitT(T &t, int bit_index)
   154  * @param T_MAX the maximum value for the integers.
   152 {
   155  * @param T_MIN the minimum value for the integers.
   153 	t = (T)(t | ((T)1 << bit_index));
   156  */
   154 }
   157 template <class T, T T_MAX, T T_MIN>
   155 
   158 class OverflowSafeInt
   156 template <typename T> void ToggleBitT(T &t, int bit_index)
   159 {
   157 {
   160 private:
   158 	t = (T)(t ^ ((T)1 << bit_index));
   161 	/** The non-overflow safe backend to store the value in. */
   159 }
   162 	T m_value;
       
   163 public:
       
   164 	OverflowSafeInt() : m_value(0) { }
       
   165 
       
   166 	OverflowSafeInt(const OverflowSafeInt& other) { this->m_value = other.m_value; }
       
   167 	OverflowSafeInt(const int64 int_)             { this->m_value = int_; }
       
   168 
       
   169 	FORCEINLINE OverflowSafeInt& operator = (const OverflowSafeInt& other) { this->m_value = other.m_value; return *this; }
       
   170 
       
   171 	FORCEINLINE OverflowSafeInt operator - () const { return OverflowSafeInt(-this->m_value); }
       
   172 
       
   173 	/**
       
   174 	 * Safe implementation of addition.
       
   175 	 * @param other the amount to add
       
   176 	 * @note when the addition would yield more than T_MAX (or less than T_MIN),
       
   177 	 *       it will be T_MAX (respectively T_MIN).
       
   178 	 */
       
   179 	FORCEINLINE OverflowSafeInt& operator += (const OverflowSafeInt& other)
       
   180 	{
       
   181 		if ((T_MAX - abs(other.m_value)) < abs(this->m_value) &&
       
   182 				(this->m_value < 0) == (other.m_value < 0)) {
       
   183 			this->m_value = (this->m_value < 0) ? T_MIN : T_MAX ;
       
   184 		} else {
       
   185 			this->m_value += other.m_value;
       
   186 		}
       
   187 		return *this;
       
   188 	}
       
   189 
       
   190 	/* Operators for addition and substraction */
       
   191 	FORCEINLINE OverflowSafeInt  operator +  (const OverflowSafeInt& other) const { OverflowSafeInt result = *this; result += other; return result; }
       
   192 	FORCEINLINE OverflowSafeInt  operator +  (const int              other) const { OverflowSafeInt result = *this; result += (int64)other; return result; }
       
   193 	FORCEINLINE OverflowSafeInt  operator +  (const uint             other) const { OverflowSafeInt result = *this; result += (int64)other; return result; }
       
   194 	FORCEINLINE OverflowSafeInt& operator -= (const OverflowSafeInt& other)       { return *this += (-other); }
       
   195 	FORCEINLINE OverflowSafeInt  operator -  (const OverflowSafeInt& other) const { OverflowSafeInt result = *this; result -= other; return result; }
       
   196 	FORCEINLINE OverflowSafeInt  operator -  (const int              other) const { OverflowSafeInt result = *this; result -= (int64)other; return result; }
       
   197 	FORCEINLINE OverflowSafeInt  operator -  (const uint             other) const { OverflowSafeInt result = *this; result -= (int64)other; return result; }
       
   198 
       
   199 	FORCEINLINE OverflowSafeInt& operator ++ () { return *this += 1; }
       
   200 	FORCEINLINE OverflowSafeInt& operator -- () { return *this += -1; }
       
   201 	FORCEINLINE OverflowSafeInt operator ++ (int) { OverflowSafeInt org = *this; *this += 1; return org; }
       
   202 	FORCEINLINE OverflowSafeInt operator -- (int) { OverflowSafeInt org = *this; *this += -1; return org; }
       
   203 
       
   204 	/**
       
   205 	 * Safe implementation of multiplication.
       
   206 	 * @param factor the factor to multiply this with.
       
   207 	 * @note when the multiplication would yield more than T_MAX (or less than T_MIN),
       
   208 	 *       it will be T_MAX (respectively T_MIN).
       
   209 	 */
       
   210 	FORCEINLINE OverflowSafeInt& operator *= (const int factor)
       
   211 	{
       
   212 		if (factor != 0 && (T_MAX / abs(factor)) < abs(this->m_value)) {
       
   213 			 this->m_value = ((this->m_value < 0) == (factor < 0)) ? T_MAX : T_MIN ;
       
   214 		} else {
       
   215 			this->m_value *= factor ;
       
   216 		}
       
   217 		return *this;
       
   218 	}
       
   219 
       
   220 	/* Operators for multiplication */
       
   221 	FORCEINLINE OverflowSafeInt operator * (const int64  factor) const { OverflowSafeInt result = *this; result *= factor; return result; }
       
   222 	FORCEINLINE OverflowSafeInt operator * (const int    factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
       
   223 	FORCEINLINE OverflowSafeInt operator * (const uint   factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
       
   224 	FORCEINLINE OverflowSafeInt operator * (const uint16 factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
       
   225 	FORCEINLINE OverflowSafeInt operator * (const byte   factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; }
       
   226 
       
   227 	/* Operators for division */
       
   228 	FORCEINLINE OverflowSafeInt& operator /= (const int              divisor)       { this->m_value /= divisor; return *this; }
       
   229 	FORCEINLINE OverflowSafeInt  operator /  (const OverflowSafeInt& divisor) const { OverflowSafeInt result = *this; result /= divisor.m_value; return result; }
       
   230 	FORCEINLINE OverflowSafeInt  operator /  (const int              divisor) const { OverflowSafeInt result = *this; result /= divisor; return result; }
       
   231 	FORCEINLINE OverflowSafeInt  operator /  (const uint             divisor) const { OverflowSafeInt result = *this; result /= (int)divisor; return result; }
       
   232 
       
   233 	/* Operators for modulo */
       
   234 	FORCEINLINE OverflowSafeInt& operator %= (const int  divisor)       { this->m_value %= divisor; return *this; }
       
   235 	FORCEINLINE OverflowSafeInt  operator %  (const int  divisor) const { OverflowSafeInt result = *this; result %= divisor; return result; }
       
   236 
       
   237 	/* Operators for shifting */
       
   238 	FORCEINLINE OverflowSafeInt& operator <<= (const int shift)       { this->m_value <<= shift; return *this; }
       
   239 	FORCEINLINE OverflowSafeInt  operator <<  (const int shift) const { OverflowSafeInt result = *this; result <<= shift; return result; }
       
   240 	FORCEINLINE OverflowSafeInt& operator >>= (const int shift)       { this->m_value >>= shift; return *this; }
       
   241 	FORCEINLINE OverflowSafeInt  operator >>  (const int shift) const { OverflowSafeInt result = *this; result >>= shift; return result; }
       
   242 
       
   243 	/* Operators for (in)equality when comparing overflow safe ints */
       
   244 	FORCEINLINE bool operator == (const OverflowSafeInt& other) const { return this->m_value == other.m_value; }
       
   245 	FORCEINLINE bool operator != (const OverflowSafeInt& other) const { return !(*this == other); }
       
   246 	FORCEINLINE bool operator >  (const OverflowSafeInt& other) const { return this->m_value > other.m_value; }
       
   247 	FORCEINLINE bool operator >= (const OverflowSafeInt& other) const { return this->m_value >= other.m_value; }
       
   248 	FORCEINLINE bool operator <  (const OverflowSafeInt& other) const { return !(*this >= other); }
       
   249 	FORCEINLINE bool operator <= (const OverflowSafeInt& other) const { return !(*this > other); }
       
   250 
       
   251 	/* Operators for (in)equality when comparing non-overflow safe ints */
       
   252 	FORCEINLINE bool operator == (const int other) const { return this->m_value == other; }
       
   253 	FORCEINLINE bool operator != (const int other) const { return !(*this == other); }
       
   254 	FORCEINLINE bool operator >  (const int other) const { return this->m_value > other; }
       
   255 	FORCEINLINE bool operator >= (const int other) const { return this->m_value >= other; }
       
   256 	FORCEINLINE bool operator <  (const int other) const { return !(*this >= other); }
       
   257 	FORCEINLINE bool operator <= (const int other) const { return !(*this > other); }
       
   258 
       
   259 	FORCEINLINE operator int64 () const { return this->m_value; }
       
   260 };
       
   261 
       
   262 /* Sometimes we got int64 operator OverflowSafeInt instead of vice versa. Handle that properly */
       
   263 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; }
       
   264 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; }
       
   265 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; }
       
   266 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; }
       
   267 
       
   268 /* Sometimes we got int operator OverflowSafeInt instead of vice versa. Handle that properly */
       
   269 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; }
       
   270 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; }
       
   271 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; }
       
   272 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; }
       
   273 
       
   274 /* Sometimes we got uint operator OverflowSafeInt instead of vice versa. Handle that properly */
       
   275 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; }
       
   276 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; }
       
   277 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; }
       
   278 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; }
       
   279 
       
   280 /* Sometimes we got byte operator OverflowSafeInt instead of vice versa. Handle that properly */
       
   281 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; }
       
   282 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; }
       
   283 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; }
       
   284 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; }
   160 
   285 
   161 #endif /* HELPERS_HPP */
   286 #endif /* HELPERS_HPP */