rubidium@5587: /* $Id$ */ rubidium@5587: belugas@6179: /** @file helpers.hpp */ belugas@6179: rubidium@5587: #ifndef HELPERS_HPP rubidium@5587: #define HELPERS_HPP rubidium@5587: rubidium@5587: #include "macros.h" rubidium@5587: rubidium@5587: /** When allocating using malloc/calloc in C++ it is usually needed to cast the return value rubidium@5587: * from void* to the proper pointer type. Another alternative would be MallocT<> as follows */ KUDr@5609: template FORCEINLINE T* MallocT(size_t num_elements) rubidium@5587: { KUDr@5609: T *t_ptr = (T*)malloc(num_elements * sizeof(T)); KUDr@5609: return t_ptr; rubidium@5587: } rubidium@5587: /** When allocating using malloc/calloc in C++ it is usually needed to cast the return value rubidium@5587: * from void* to the proper pointer type. Another alternative would be MallocT<> as follows */ KUDr@5609: template FORCEINLINE T* CallocT(size_t num_elements) rubidium@5587: { KUDr@5609: T *t_ptr = (T*)calloc(num_elements, sizeof(T)); KUDr@5609: return t_ptr; rubidium@5587: } rubidium@5587: /** When allocating using malloc/calloc in C++ it is usually needed to cast the return value rubidium@5587: * from void* to the proper pointer type. Another alternative would be MallocT<> as follows */ KUDr@5609: template FORCEINLINE T* ReallocT(T* t_ptr, size_t num_elements) rubidium@5587: { KUDr@5609: t_ptr = (T*)realloc(t_ptr, num_elements * sizeof(T)); KUDr@5609: return t_ptr; rubidium@5587: } rubidium@5587: rubidium@5587: tron@5733: /** type safe swap operation */ tron@5733: template void Swap(T& a, T& b) rubidium@5587: { tron@5733: T t = a; tron@5733: a = b; tron@5733: b = t; rubidium@5587: } rubidium@5587: rubidium@5587: rubidium@5587: /** Some enums need to have allowed incrementing (i.e. StationClassID) */ rubidium@5587: #define DECLARE_POSTFIX_INCREMENT(type) \ rubidium@5587: FORCEINLINE type operator ++(type& e, int) \ rubidium@5587: { \ rubidium@5587: type e_org = e; \ rubidium@5587: e = (type)((int)e + 1); \ rubidium@5587: return e_org; \ rubidium@5587: } \ rubidium@5587: FORCEINLINE type operator --(type& e, int) \ rubidium@5587: { \ rubidium@5587: type e_org = e; \ rubidium@5587: e = (type)((int)e - 1); \ rubidium@5587: return e_org; \ rubidium@5587: } rubidium@5587: rubidium@5587: rubidium@5587: rubidium@5587: /** Operators to allow to work with enum as with type safe bit set in C++ */ rubidium@5587: # define DECLARE_ENUM_AS_BIT_SET(mask_t) \ rubidium@5587: FORCEINLINE mask_t operator | (mask_t m1, mask_t m2) {return (mask_t)((int)m1 | m2);} \ rubidium@5587: FORCEINLINE mask_t operator & (mask_t m1, mask_t m2) {return (mask_t)((int)m1 & m2);} \ rubidium@5587: FORCEINLINE mask_t operator ^ (mask_t m1, mask_t m2) {return (mask_t)((int)m1 ^ m2);} \ rubidium@5587: FORCEINLINE mask_t& operator |= (mask_t& m1, mask_t m2) {m1 = m1 | m2; return m1;} \ rubidium@5587: FORCEINLINE mask_t& operator &= (mask_t& m1, mask_t m2) {m1 = m1 & m2; return m1;} \ rubidium@5587: FORCEINLINE mask_t& operator ^= (mask_t& m1, mask_t m2) {m1 = m1 ^ m2; return m1;} \ rubidium@5587: FORCEINLINE mask_t operator ~(mask_t m) {return (mask_t)(~(int)m);} rubidium@5587: rubidium@5587: rubidium@5587: /** Informative template class exposing basic enumeration properties used by several rubidium@5587: * other templates below. Here we have only forward declaration. For each enum type KUDr@6129: * we will create specialization derived from MakeEnumPropsT<>. KUDr@6130: * i.e.: KUDr@6130: * template <> struct EnumPropsT : MakeEnumPropsT {}; KUDr@6130: * followed by: KUDr@6130: * typedef TinyEnumT TrackByte; KUDr@6129: */ rubidium@5587: template struct EnumPropsT; rubidium@5587: rubidium@5587: /** Helper template class that makes basic properties of given enumeration type visible rubidium@5587: * from outsize. It is used as base class of several EnumPropsT specializations each KUDr@6129: * dedicated to one of commonly used enumeration types. KUDr@6129: * @param Tenum_t enumeration type that you want to describe KUDr@6129: * @param Tstorage_t what storage type would be sufficient (i.e. byte) KUDr@6129: * @param Tbegin first valid value from the contiguous range (i.e. TRACK_BEGIN) KUDr@6129: * @param Tend one past the last valid value from the contiguous range (i.e. TRACK_END) KUDr@6129: * @param Tinvalid value used as invalid value marker (i.e. INVALID_TRACK) KUDr@6129: */ rubidium@5587: template rubidium@5587: struct MakeEnumPropsT { rubidium@5587: typedef Tenum_t type; ///< enum type (i.e. Trackdir) rubidium@5587: typedef Tstorage_t storage; ///< storage type (i.e. byte) rubidium@5587: static const Tenum_t begin = Tbegin; ///< lowest valid value (i.e. TRACKDIR_BEGIN) rubidium@5587: static const Tenum_t end = Tend; ///< one after the last valid value (i.e. TRACKDIR_END) rubidium@5587: static const Tenum_t invalid = Tinvalid; ///< what value is used as invalid value (i.e. INVALID_TRACKDIR) rubidium@5587: }; rubidium@5587: rubidium@5587: rubidium@5587: rubidium@5587: /** In some cases we use byte or uint16 to store values that are defined as enum. It is rubidium@5587: * necessary in order to control the sizeof() such values. Some compilers make enum rubidium@5587: * the same size as int (4 or 8 bytes instead of 1 or 2). As a consequence the strict rubidium@5587: * compiler type-checking causes errors like: rubidium@5587: * 'HasPowerOnRail' : cannot convert parameter 1 from 'byte' to 'RailType' when rubidium@5587: * u->u.rail.railtype is passed as argument or type RailType. In such cases it is better rubidium@5587: * to teach the compiler that u->u.rail.railtype is to be treated as RailType. */ rubidium@5587: template struct TinyEnumT; rubidium@5587: rubidium@5587: /** The general declaration of TinyEnumT<> (above) */ rubidium@5587: template struct TinyEnumT rubidium@5587: { rubidium@5587: typedef Tenum_t enum_type; ///< expose our enumeration type (i.e. Trackdir) to outside rubidium@5587: typedef EnumPropsT Props; ///< make easier access to our enumeration propeties rubidium@5587: typedef typename Props::storage storage_type; ///< small storage type rubidium@5587: static const enum_type begin = Props::begin; ///< enum beginning (i.e. TRACKDIR_BEGIN) rubidium@5587: static const enum_type end = Props::end; ///< enum end (i.e. TRACKDIR_END) rubidium@5587: static const enum_type invalid = Props::invalid;///< invalid value (i.e. INVALID_TRACKDIR) rubidium@5587: rubidium@5587: storage_type m_val; ///< here we hold the actual value in small (i.e. byte) form rubidium@5587: rubidium@5587: /** Cast operator - invoked then the value is assigned to the Tenum_t type */ rubidium@5587: FORCEINLINE operator enum_type () const rubidium@5587: { rubidium@5587: return (enum_type)m_val; rubidium@5587: } rubidium@5587: rubidium@5587: /** Assignment operator (from Tenum_t type) */ rubidium@5587: FORCEINLINE TinyEnumT& operator = (enum_type e) rubidium@5587: { rubidium@5587: m_val = (storage_type)e; return *this; rubidium@5587: } rubidium@5587: rubidium@5587: /** postfix ++ operator on tiny type */ truelight@7838: FORCEINLINE TinyEnumT operator ++ (int) truelight@7838: { truelight@7838: TinyEnumT org = *this; truelight@7838: if (++m_val >= end) m_val -= (storage_type)(end - begin); truelight@7838: return org; truelight@7838: } truelight@7838: truelight@7838: /** prefix ++ operator on tiny type */ truelight@7838: FORCEINLINE TinyEnumT& operator ++ () rubidium@5587: { rubidium@5587: if (++m_val >= end) m_val -= (storage_type)(end - begin); rubidium@5587: return *this; rubidium@5587: } rubidium@5587: }; rubidium@5587: rubidium@7763: /** rubidium@7763: * Overflow safe template for integers, i.e. integers that will never overflow rubidium@7763: * you multiply the maximum value with 2, or add 2, or substract somethng from rubidium@7763: * the minimum value, etc. rubidium@7763: * @param T the type these integers are stored with. rubidium@7763: * @param T_MAX the maximum value for the integers. rubidium@7763: * @param T_MIN the minimum value for the integers. rubidium@7763: */ rubidium@7763: template rubidium@7763: class OverflowSafeInt rubidium@7763: { rubidium@7763: private: rubidium@7763: /** The non-overflow safe backend to store the value in. */ rubidium@7763: T m_value; rubidium@7763: public: rubidium@7763: OverflowSafeInt() : m_value(0) { } rubidium@7763: rubidium@7763: OverflowSafeInt(const OverflowSafeInt& other) { this->m_value = other.m_value; } rubidium@7763: OverflowSafeInt(const int64 int_) { this->m_value = int_; } rubidium@7763: rubidium@7763: FORCEINLINE OverflowSafeInt& operator = (const OverflowSafeInt& other) { this->m_value = other.m_value; return *this; } rubidium@7763: rubidium@7763: FORCEINLINE OverflowSafeInt operator - () const { return OverflowSafeInt(-this->m_value); } rubidium@7763: rubidium@7763: /** rubidium@7763: * Safe implementation of addition. rubidium@7763: * @param other the amount to add rubidium@7763: * @note when the addition would yield more than T_MAX (or less than T_MIN), rubidium@7763: * it will be T_MAX (respectively T_MIN). rubidium@7763: */ rubidium@7763: FORCEINLINE OverflowSafeInt& operator += (const OverflowSafeInt& other) rubidium@7763: { skidd13@7923: if ((T_MAX - abs(other.m_value)) < abs(this->m_value) && rubidium@7763: (this->m_value < 0) == (other.m_value < 0)) { rubidium@7763: this->m_value = (this->m_value < 0) ? T_MIN : T_MAX ; rubidium@7763: } else { rubidium@7763: this->m_value += other.m_value; rubidium@7763: } rubidium@7763: return *this; rubidium@7763: } rubidium@7763: rubidium@7763: /* Operators for addition and substraction */ rubidium@7763: FORCEINLINE OverflowSafeInt operator + (const OverflowSafeInt& other) const { OverflowSafeInt result = *this; result += other; return result; } rubidium@7763: FORCEINLINE OverflowSafeInt operator + (const int other) const { OverflowSafeInt result = *this; result += (int64)other; return result; } rubidium@7763: FORCEINLINE OverflowSafeInt operator + (const uint other) const { OverflowSafeInt result = *this; result += (int64)other; return result; } rubidium@7763: FORCEINLINE OverflowSafeInt& operator -= (const OverflowSafeInt& other) { return *this += (-other); } rubidium@7763: FORCEINLINE OverflowSafeInt operator - (const OverflowSafeInt& other) const { OverflowSafeInt result = *this; result -= other; return result; } rubidium@7763: FORCEINLINE OverflowSafeInt operator - (const int other) const { OverflowSafeInt result = *this; result -= (int64)other; return result; } rubidium@7763: FORCEINLINE OverflowSafeInt operator - (const uint other) const { OverflowSafeInt result = *this; result -= (int64)other; return result; } rubidium@7763: truelight@7838: FORCEINLINE OverflowSafeInt& operator ++ () { return *this += 1; } truelight@7838: FORCEINLINE OverflowSafeInt& operator -- () { return *this += -1; } truelight@7838: FORCEINLINE OverflowSafeInt operator ++ (int) { OverflowSafeInt org = *this; *this += 1; return org; } truelight@7838: FORCEINLINE OverflowSafeInt operator -- (int) { OverflowSafeInt org = *this; *this += -1; return org; } rubidium@7763: rubidium@7763: /** rubidium@7763: * Safe implementation of multiplication. rubidium@7763: * @param factor the factor to multiply this with. rubidium@7763: * @note when the multiplication would yield more than T_MAX (or less than T_MIN), rubidium@7763: * it will be T_MAX (respectively T_MIN). rubidium@7763: */ rubidium@7763: FORCEINLINE OverflowSafeInt& operator *= (const int factor) rubidium@7763: { skidd13@7923: if (factor != 0 && (T_MAX / abs(factor)) < abs(this->m_value)) { rubidium@7763: this->m_value = ((this->m_value < 0) == (factor < 0)) ? T_MAX : T_MIN ; rubidium@7763: } else { rubidium@7763: this->m_value *= factor ; rubidium@7763: } rubidium@7763: return *this; rubidium@7763: } rubidium@7763: rubidium@7763: /* Operators for multiplication */ rubidium@7763: FORCEINLINE OverflowSafeInt operator * (const int64 factor) const { OverflowSafeInt result = *this; result *= factor; return result; } rubidium@7763: FORCEINLINE OverflowSafeInt operator * (const int factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; } rubidium@7763: FORCEINLINE OverflowSafeInt operator * (const uint factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; } rubidium@7763: FORCEINLINE OverflowSafeInt operator * (const uint16 factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; } rubidium@7763: FORCEINLINE OverflowSafeInt operator * (const byte factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; } rubidium@7763: rubidium@7763: /* Operators for division */ rubidium@7763: FORCEINLINE OverflowSafeInt& operator /= (const int divisor) { this->m_value /= divisor; return *this; } rubidium@7763: FORCEINLINE OverflowSafeInt operator / (const OverflowSafeInt& divisor) const { OverflowSafeInt result = *this; result /= divisor.m_value; return result; } rubidium@7763: FORCEINLINE OverflowSafeInt operator / (const int divisor) const { OverflowSafeInt result = *this; result /= divisor; return result; } rubidium@7763: FORCEINLINE OverflowSafeInt operator / (const uint divisor) const { OverflowSafeInt result = *this; result /= (int)divisor; return result; } rubidium@7763: rubidium@7763: /* Operators for modulo */ rubidium@7763: FORCEINLINE OverflowSafeInt& operator %= (const int divisor) { this->m_value %= divisor; return *this; } rubidium@7763: FORCEINLINE OverflowSafeInt operator % (const int divisor) const { OverflowSafeInt result = *this; result %= divisor; return result; } rubidium@7763: rubidium@7763: /* Operators for shifting */ rubidium@7763: FORCEINLINE OverflowSafeInt& operator <<= (const int shift) { this->m_value <<= shift; return *this; } rubidium@7763: FORCEINLINE OverflowSafeInt operator << (const int shift) const { OverflowSafeInt result = *this; result <<= shift; return result; } rubidium@7763: FORCEINLINE OverflowSafeInt& operator >>= (const int shift) { this->m_value >>= shift; return *this; } rubidium@7763: FORCEINLINE OverflowSafeInt operator >> (const int shift) const { OverflowSafeInt result = *this; result >>= shift; return result; } rubidium@7763: rubidium@7763: /* Operators for (in)equality when comparing overflow safe ints */ rubidium@7763: FORCEINLINE bool operator == (const OverflowSafeInt& other) const { return this->m_value == other.m_value; } rubidium@7763: FORCEINLINE bool operator != (const OverflowSafeInt& other) const { return !(*this == other); } rubidium@7763: FORCEINLINE bool operator > (const OverflowSafeInt& other) const { return this->m_value > other.m_value; } rubidium@7763: FORCEINLINE bool operator >= (const OverflowSafeInt& other) const { return this->m_value >= other.m_value; } rubidium@7763: FORCEINLINE bool operator < (const OverflowSafeInt& other) const { return !(*this >= other); } rubidium@7763: FORCEINLINE bool operator <= (const OverflowSafeInt& other) const { return !(*this > other); } rubidium@7763: rubidium@7763: /* Operators for (in)equality when comparing non-overflow safe ints */ rubidium@7763: FORCEINLINE bool operator == (const int other) const { return this->m_value == other; } rubidium@7763: FORCEINLINE bool operator != (const int other) const { return !(*this == other); } rubidium@7763: FORCEINLINE bool operator > (const int other) const { return this->m_value > other; } rubidium@7763: FORCEINLINE bool operator >= (const int other) const { return this->m_value >= other; } rubidium@7763: FORCEINLINE bool operator < (const int other) const { return !(*this >= other); } rubidium@7763: FORCEINLINE bool operator <= (const int other) const { return !(*this > other); } rubidium@7763: rubidium@7763: FORCEINLINE operator int64 () const { return this->m_value; } rubidium@7763: }; rubidium@7763: rubidium@7763: /* Sometimes we got int64 operator OverflowSafeInt instead of vice versa. Handle that properly */ rubidium@7763: template FORCEINLINE OverflowSafeInt operator + (int64 a, OverflowSafeInt b) { return b + a; } rubidium@7763: template FORCEINLINE OverflowSafeInt operator - (int64 a, OverflowSafeInt b) { return -b + a; } rubidium@7763: template FORCEINLINE OverflowSafeInt operator * (int64 a, OverflowSafeInt b) { return b * a; } rubidium@7763: template FORCEINLINE OverflowSafeInt operator / (int64 a, OverflowSafeInt b) { return (OverflowSafeInt)a / (int)b; } rubidium@7763: rubidium@7763: /* Sometimes we got int operator OverflowSafeInt instead of vice versa. Handle that properly */ rubidium@7763: template FORCEINLINE OverflowSafeInt operator + (int a, OverflowSafeInt b) { return b + a; } rubidium@7763: template FORCEINLINE OverflowSafeInt operator - (int a, OverflowSafeInt b) { return -b + a; } rubidium@7763: template FORCEINLINE OverflowSafeInt operator * (int a, OverflowSafeInt b) { return b * a; } rubidium@7763: template FORCEINLINE OverflowSafeInt operator / (int a, OverflowSafeInt b) { return (OverflowSafeInt)a / (int)b; } rubidium@7763: rubidium@7763: /* Sometimes we got uint operator OverflowSafeInt instead of vice versa. Handle that properly */ rubidium@7763: template FORCEINLINE OverflowSafeInt operator + (uint a, OverflowSafeInt b) { return b + a; } rubidium@7763: template FORCEINLINE OverflowSafeInt operator - (uint a, OverflowSafeInt b) { return -b + a; } rubidium@7763: template FORCEINLINE OverflowSafeInt operator * (uint a, OverflowSafeInt b) { return b * a; } rubidium@7763: template FORCEINLINE OverflowSafeInt operator / (uint a, OverflowSafeInt b) { return (OverflowSafeInt)a / (int)b; } rubidium@7763: rubidium@7763: /* Sometimes we got byte operator OverflowSafeInt instead of vice versa. Handle that properly */ rubidium@7763: template FORCEINLINE OverflowSafeInt operator + (byte a, OverflowSafeInt b) { return b + a; } rubidium@7763: template FORCEINLINE OverflowSafeInt operator - (byte a, OverflowSafeInt b) { return -b + a; } rubidium@7763: template FORCEINLINE OverflowSafeInt operator * (byte a, OverflowSafeInt b) { return b * a; } rubidium@7763: template FORCEINLINE OverflowSafeInt operator / (byte a, OverflowSafeInt b) { return (OverflowSafeInt)a / (int)b; } rubidium@7763: rubidium@5587: #endif /* HELPERS_HPP */