rubidium@5838: /* $Id$ */ rubidium@5838: KUDr@6285: /** @file helpers.hpp */ KUDr@6285: rubidium@5838: #ifndef HELPERS_HPP rubidium@5838: #define HELPERS_HPP rubidium@5838: rubidium@5838: #include "macros.h" rubidium@5838: rubidium@5838: /** When allocating using malloc/calloc in C++ it is usually needed to cast the return value rubidium@5838: * from void* to the proper pointer type. Another alternative would be MallocT<> as follows */ KUDr@5860: template FORCEINLINE T* MallocT(size_t num_elements) rubidium@5838: { KUDr@5860: T *t_ptr = (T*)malloc(num_elements * sizeof(T)); KUDr@5860: return t_ptr; rubidium@5838: } rubidium@5838: /** When allocating using malloc/calloc in C++ it is usually needed to cast the return value rubidium@5838: * from void* to the proper pointer type. Another alternative would be MallocT<> as follows */ KUDr@5860: template FORCEINLINE T* CallocT(size_t num_elements) rubidium@5838: { KUDr@5860: T *t_ptr = (T*)calloc(num_elements, sizeof(T)); KUDr@5860: return t_ptr; rubidium@5838: } rubidium@5838: /** When allocating using malloc/calloc in C++ it is usually needed to cast the return value rubidium@5838: * from void* to the proper pointer type. Another alternative would be MallocT<> as follows */ KUDr@5860: template FORCEINLINE T* ReallocT(T* t_ptr, size_t num_elements) rubidium@5838: { KUDr@5860: t_ptr = (T*)realloc(t_ptr, num_elements * sizeof(T)); KUDr@5860: return t_ptr; rubidium@5838: } rubidium@5838: rubidium@5838: tron@5984: /** type safe swap operation */ tron@5984: template void Swap(T& a, T& b) rubidium@5838: { tron@5984: T t = a; tron@5984: a = b; tron@5984: b = t; rubidium@5838: } rubidium@5838: rubidium@5838: rubidium@5838: /** returns the absolute value of (scalar) variable. @note assumes variable to be signed */ rubidium@5838: template static inline T myabs(T a) { return a < (T)0 ? -a : a; } rubidium@5838: /** returns the (absolute) difference between two (scalar) variables */ rubidium@5838: template static inline T delta(T a, T b) { return a < b ? b - a : a - b; } rubidium@5838: rubidium@5838: /** Some enums need to have allowed incrementing (i.e. StationClassID) */ rubidium@5838: #define DECLARE_POSTFIX_INCREMENT(type) \ rubidium@5838: FORCEINLINE type operator ++(type& e, int) \ rubidium@5838: { \ rubidium@5838: type e_org = e; \ rubidium@5838: e = (type)((int)e + 1); \ rubidium@5838: return e_org; \ rubidium@5838: } \ rubidium@5838: FORCEINLINE type operator --(type& e, int) \ rubidium@5838: { \ rubidium@5838: type e_org = e; \ rubidium@5838: e = (type)((int)e - 1); \ rubidium@5838: return e_org; \ rubidium@5838: } rubidium@5838: rubidium@5838: rubidium@5838: rubidium@5838: /** Operators to allow to work with enum as with type safe bit set in C++ */ rubidium@5838: # define DECLARE_ENUM_AS_BIT_SET(mask_t) \ rubidium@5838: FORCEINLINE mask_t operator | (mask_t m1, mask_t m2) {return (mask_t)((int)m1 | m2);} \ rubidium@5838: FORCEINLINE mask_t operator & (mask_t m1, mask_t m2) {return (mask_t)((int)m1 & m2);} \ rubidium@5838: FORCEINLINE mask_t operator ^ (mask_t m1, mask_t m2) {return (mask_t)((int)m1 ^ m2);} \ rubidium@5838: FORCEINLINE mask_t& operator |= (mask_t& m1, mask_t m2) {m1 = m1 | m2; return m1;} \ rubidium@5838: FORCEINLINE mask_t& operator &= (mask_t& m1, mask_t m2) {m1 = m1 & m2; return m1;} \ rubidium@5838: FORCEINLINE mask_t& operator ^= (mask_t& m1, mask_t m2) {m1 = m1 ^ m2; return m1;} \ rubidium@5838: FORCEINLINE mask_t operator ~(mask_t m) {return (mask_t)(~(int)m);} rubidium@5838: rubidium@5838: rubidium@5838: /** Informative template class exposing basic enumeration properties used by several rubidium@5838: * other templates below. Here we have only forward declaration. For each enum type bjarni@6268: * we will create specialization derived from MakeEnumPropsT<>. bjarni@6268: * i.e.: bjarni@6268: * template <> struct EnumPropsT : MakeEnumPropsT {}; bjarni@6268: * followed by: bjarni@6268: * typedef TinyEnumT TrackByte; bjarni@6268: */ rubidium@5838: template struct EnumPropsT; rubidium@5838: rubidium@5838: /** Helper template class that makes basic properties of given enumeration type visible rubidium@5838: * from outsize. It is used as base class of several EnumPropsT specializations each bjarni@6268: * dedicated to one of commonly used enumeration types. bjarni@6268: * @param Tenum_t enumeration type that you want to describe bjarni@6268: * @param Tstorage_t what storage type would be sufficient (i.e. byte) bjarni@6268: * @param Tbegin first valid value from the contiguous range (i.e. TRACK_BEGIN) bjarni@6268: * @param Tend one past the last valid value from the contiguous range (i.e. TRACK_END) bjarni@6268: * @param Tinvalid value used as invalid value marker (i.e. INVALID_TRACK) bjarni@6268: */ rubidium@5838: template rubidium@5838: struct MakeEnumPropsT { rubidium@5838: typedef Tenum_t type; ///< enum type (i.e. Trackdir) rubidium@5838: typedef Tstorage_t storage; ///< storage type (i.e. byte) rubidium@5838: static const Tenum_t begin = Tbegin; ///< lowest valid value (i.e. TRACKDIR_BEGIN) rubidium@5838: static const Tenum_t end = Tend; ///< one after the last valid value (i.e. TRACKDIR_END) rubidium@5838: static const Tenum_t invalid = Tinvalid; ///< what value is used as invalid value (i.e. INVALID_TRACKDIR) rubidium@5838: }; rubidium@5838: rubidium@5838: rubidium@5838: rubidium@5838: /** In some cases we use byte or uint16 to store values that are defined as enum. It is rubidium@5838: * necessary in order to control the sizeof() such values. Some compilers make enum rubidium@5838: * the same size as int (4 or 8 bytes instead of 1 or 2). As a consequence the strict rubidium@5838: * compiler type-checking causes errors like: rubidium@5838: * 'HasPowerOnRail' : cannot convert parameter 1 from 'byte' to 'RailType' when rubidium@5838: * u->u.rail.railtype is passed as argument or type RailType. In such cases it is better rubidium@5838: * to teach the compiler that u->u.rail.railtype is to be treated as RailType. */ rubidium@5838: template struct TinyEnumT; rubidium@5838: rubidium@5838: /** The general declaration of TinyEnumT<> (above) */ rubidium@5838: template struct TinyEnumT rubidium@5838: { rubidium@5838: typedef Tenum_t enum_type; ///< expose our enumeration type (i.e. Trackdir) to outside rubidium@5838: typedef EnumPropsT Props; ///< make easier access to our enumeration propeties rubidium@5838: typedef typename Props::storage storage_type; ///< small storage type rubidium@5838: static const enum_type begin = Props::begin; ///< enum beginning (i.e. TRACKDIR_BEGIN) rubidium@5838: static const enum_type end = Props::end; ///< enum end (i.e. TRACKDIR_END) rubidium@5838: static const enum_type invalid = Props::invalid;///< invalid value (i.e. INVALID_TRACKDIR) rubidium@5838: rubidium@5838: storage_type m_val; ///< here we hold the actual value in small (i.e. byte) form rubidium@5838: rubidium@5838: /** Cast operator - invoked then the value is assigned to the Tenum_t type */ rubidium@5838: FORCEINLINE operator enum_type () const rubidium@5838: { rubidium@5838: return (enum_type)m_val; rubidium@5838: } rubidium@5838: rubidium@5838: /** Assignment operator (from Tenum_t type) */ rubidium@5838: FORCEINLINE TinyEnumT& operator = (enum_type e) rubidium@5838: { rubidium@5838: m_val = (storage_type)e; return *this; rubidium@5838: } rubidium@5838: rubidium@5838: /** postfix ++ operator on tiny type */ rubidium@5838: FORCEINLINE TinyEnumT& operator ++ (int) rubidium@5838: { rubidium@5838: if (++m_val >= end) m_val -= (storage_type)(end - begin); rubidium@5838: return *this; rubidium@5838: } rubidium@5838: }; rubidium@5838: bjarni@6268: template void ClrBitT(T &t, int bit_index) rubidium@5838: { bjarni@6268: t = (T)(t & ~((T)1 << bit_index)); rubidium@5838: } rubidium@5838: bjarni@6268: template void SetBitT(T &t, int bit_index) rubidium@5838: { bjarni@6268: t = (T)(t | ((T)1 << bit_index)); rubidium@5838: } rubidium@5838: bjarni@6268: template void ToggleBitT(T &t, int bit_index) rubidium@5838: { bjarni@6268: t = (T)(t ^ ((T)1 << bit_index)); rubidium@5838: } rubidium@5838: KUDr@6261: /** KUDr@6261: * Zero initialization end marker. KUDr@6261: * @see ZeroInitBegin for usage. KUDr@6261: */ KUDr@6261: struct ZeroInitEnd { KUDr@6261: }; KUDr@6261: KUDr@6261: /** KUDr@6261: * Zero initialization begin marker. When you need to initialize struct/class members by KUDr@6261: * memset(&m_first_member, 0, ((byte*)&m_last_member) - ((byte*)&m_first_member + sizeof(m_last_member)); KUDr@6261: * place ZeroInitBegin before m_first_member, place ZeroInitEnd after m_last_member, call ZeroInitBegin KUDr@6261: * constructor give him ZeroInitEnd& as parameter. This will clear whole area between them using memset. KUDr@6261: * @see ZeroInitEnd KUDr@6261: */ KUDr@6261: struct ZeroInitBegin { KUDr@6261: ZeroInitBegin(const ZeroInitEnd &end) KUDr@6261: { KUDr@6261: memset(this, 0, ((byte*)&end) - ((byte*)this)); KUDr@6261: } KUDr@6261: }; KUDr@6261: rubidium@5838: #endif /* HELPERS_HPP */