3 #ifndef HELPERS_HPP |
3 #ifndef HELPERS_HPP |
4 #define HELPERS_HPP |
4 #define HELPERS_HPP |
5 |
5 |
6 /** @file helpers.hpp */ |
6 /** @file helpers.hpp */ |
7 #include "macros.h" |
7 #include "macros.h" |
8 |
|
9 #ifdef __cplusplus |
|
10 |
8 |
11 /** When allocating using malloc/calloc in C++ it is usually needed to cast the return value |
9 /** When allocating using malloc/calloc in C++ it is usually needed to cast the return value |
12 * from void* to the proper pointer type. Another alternative would be MallocT<> as follows */ |
10 * from void* to the proper pointer type. Another alternative would be MallocT<> as follows */ |
13 template <typename T> FORCEINLINE T* MallocT(size_t num_elements) |
11 template <typename T> FORCEINLINE T* MallocT(size_t num_elements) |
14 { |
12 { |
80 FORCEINLINE mask_t operator >> (mask_t m, int i) {return (mask_t)(((int)m) >> i);} |
78 FORCEINLINE mask_t operator >> (mask_t m, int i) {return (mask_t)(((int)m) >> i);} |
81 |
79 |
82 |
80 |
83 /** Informative template class exposing basic enumeration properties used by several |
81 /** Informative template class exposing basic enumeration properties used by several |
84 * other templates below. Here we have only forward declaration. For each enum type |
82 * other templates below. Here we have only forward declaration. For each enum type |
85 * we will create specialization derived from MakeEnumPropsT<>. */ |
83 * we will create specialization derived from MakeEnumPropsT<>. |
|
84 * i.e.: |
|
85 * template <> struct EnumPropsT<Track> : MakeEnumPropsT<Track, byte, TRACK_BEGIN, TRACK_END, INVALID_TRACK> {}; |
|
86 * followed by: |
|
87 * typedef TinyEnumT<Track> TrackByte; |
|
88 */ |
86 template <typename Tenum_t> struct EnumPropsT; |
89 template <typename Tenum_t> struct EnumPropsT; |
87 |
90 |
88 /** Helper template class that makes basic properties of given enumeration type visible |
91 /** Helper template class that makes basic properties of given enumeration type visible |
89 * from outsize. It is used as base class of several EnumPropsT specializations each |
92 * from outsize. It is used as base class of several EnumPropsT specializations each |
90 * dedicated to one of commonly used enumeration types. */ |
93 * dedicated to one of commonly used enumeration types. |
|
94 * @param Tenum_t enumeration type that you want to describe |
|
95 * @param Tstorage_t what storage type would be sufficient (i.e. byte) |
|
96 * @param Tbegin first valid value from the contiguous range (i.e. TRACK_BEGIN) |
|
97 * @param Tend one past the last valid value from the contiguous range (i.e. TRACK_END) |
|
98 * @param Tinvalid value used as invalid value marker (i.e. INVALID_TRACK) |
|
99 */ |
91 template <typename Tenum_t, typename Tstorage_t, Tenum_t Tbegin, Tenum_t Tend, Tenum_t Tinvalid> |
100 template <typename Tenum_t, typename Tstorage_t, Tenum_t Tbegin, Tenum_t Tend, Tenum_t Tinvalid> |
92 struct MakeEnumPropsT { |
101 struct MakeEnumPropsT { |
93 typedef Tenum_t type; ///< enum type (i.e. Trackdir) |
102 typedef Tenum_t type; ///< enum type (i.e. Trackdir) |
94 typedef Tstorage_t storage; ///< storage type (i.e. byte) |
103 typedef Tstorage_t storage; ///< storage type (i.e. byte) |
95 static const Tenum_t begin = Tbegin; ///< lowest valid value (i.e. TRACKDIR_BEGIN) |
104 static const Tenum_t begin = Tbegin; ///< lowest valid value (i.e. TRACKDIR_BEGIN) |
138 if (++m_val >= end) m_val -= (storage_type)(end - begin); |
147 if (++m_val >= end) m_val -= (storage_type)(end - begin); |
139 return *this; |
148 return *this; |
140 } |
149 } |
141 }; |
150 }; |
142 |
151 |
143 template <typename T> FORCEINLINE T ClrBitT(T t, int bit_index) |
152 template <typename T> void ClrBitT(T &t, int bit_index) |
144 { |
153 { |
145 int val = t; |
154 t = (T)(t & ~((T)1 << bit_index)); |
146 CLRBIT(val, bit_index); |
|
147 return (T)val; |
|
148 } |
155 } |
149 |
156 |
150 template <typename T> FORCEINLINE T SetBitT(T t, int bit_index) |
157 template <typename T> void SetBitT(T &t, int bit_index) |
151 { |
158 { |
152 int val = t; |
159 t = (T)(t | ((T)1 << bit_index)); |
153 SETBIT(val, bit_index); |
|
154 return (T)val; |
|
155 } |
160 } |
156 |
161 |
157 template <typename T> FORCEINLINE T ToggleBitT(T t, int bit_index) |
162 template <typename T> void ToggleBitT(T &t, int bit_index) |
158 { |
163 { |
159 int val = t; |
164 t = (T)(t ^ ((T)1 << bit_index)); |
160 TOGGLEBIT(val, bit_index); |
|
161 return (T)val; |
|
162 } |
165 } |
163 |
166 |
164 /** |
167 /** |
165 * Zero initialization end marker. |
168 * Zero initialization end marker. |
166 * @see ZeroInitBegin for usage. |
169 * @see ZeroInitBegin for usage. |
180 { |
183 { |
181 memset(this, 0, ((byte*)&end) - ((byte*)this)); |
184 memset(this, 0, ((byte*)&end) - ((byte*)this)); |
182 } |
185 } |
183 }; |
186 }; |
184 |
187 |
185 #else // __cplusplus |
|
186 |
|
187 #define DECLARE_POSTFIX_INCREMENT(E) |
|
188 #define DECLARE_ENUM_AS_BIT_SET(E) |
|
189 #define DECLARE_ENUM_AS_BIT_INDEX(E1,E2) |
|
190 |
|
191 #endif // __cplusplus |
|
192 |
|
193 #endif /* HELPERS_HPP */ |
188 #endif /* HELPERS_HPP */ |