author | KUDr |
Sat, 21 Apr 2007 08:23:57 +0000 | |
branch | cpp_gui |
changeset 6308 | 646711c5feaa |
parent 6285 | 187e3ef04cc9 |
permissions | -rw-r--r-- |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
1 |
/* $Id$ */ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
2 |
|
6285
187e3ef04cc9
(svn r9004) [cpp_gui] -Sync with trunk (r8900..r9003)
KUDr
parents:
6268
diff
changeset
|
3 |
/** @file helpers.hpp */ |
187e3ef04cc9
(svn r9004) [cpp_gui] -Sync with trunk (r8900..r9003)
KUDr
parents:
6268
diff
changeset
|
4 |
|
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
5 |
#ifndef HELPERS_HPP |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
6 |
#define HELPERS_HPP |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
7 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
8 |
#include "macros.h" |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
9 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
10 |
/** When allocating using malloc/calloc in C++ it is usually needed to cast the return value |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
11 |
* from void* to the proper pointer type. Another alternative would be MallocT<> as follows */ |
5860
7fdc9b423ba1
(svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents:
5838
diff
changeset
|
12 |
template <typename T> FORCEINLINE T* MallocT(size_t num_elements) |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
13 |
{ |
5860
7fdc9b423ba1
(svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents:
5838
diff
changeset
|
14 |
T *t_ptr = (T*)malloc(num_elements * sizeof(T)); |
7fdc9b423ba1
(svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents:
5838
diff
changeset
|
15 |
return t_ptr; |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
16 |
} |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
17 |
/** When allocating using malloc/calloc in C++ it is usually needed to cast the return value |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
18 |
* from void* to the proper pointer type. Another alternative would be MallocT<> as follows */ |
5860
7fdc9b423ba1
(svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents:
5838
diff
changeset
|
19 |
template <typename T> FORCEINLINE T* CallocT(size_t num_elements) |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
20 |
{ |
5860
7fdc9b423ba1
(svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents:
5838
diff
changeset
|
21 |
T *t_ptr = (T*)calloc(num_elements, sizeof(T)); |
7fdc9b423ba1
(svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents:
5838
diff
changeset
|
22 |
return t_ptr; |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
23 |
} |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
24 |
/** When allocating using malloc/calloc in C++ it is usually needed to cast the return value |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
25 |
* from void* to the proper pointer type. Another alternative would be MallocT<> as follows */ |
5860
7fdc9b423ba1
(svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents:
5838
diff
changeset
|
26 |
template <typename T> FORCEINLINE T* ReallocT(T* t_ptr, size_t num_elements) |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
27 |
{ |
5860
7fdc9b423ba1
(svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents:
5838
diff
changeset
|
28 |
t_ptr = (T*)realloc(t_ptr, num_elements * sizeof(T)); |
7fdc9b423ba1
(svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents:
5838
diff
changeset
|
29 |
return t_ptr; |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
30 |
} |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
31 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
32 |
|
5984 | 33 |
/** type safe swap operation */ |
34 |
template<typename T> void Swap(T& a, T& b) |
|
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
35 |
{ |
5984 | 36 |
T t = a; |
37 |
a = b; |
|
38 |
b = t; |
|
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
39 |
} |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
40 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
41 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
42 |
/** returns the absolute value of (scalar) variable. @note assumes variable to be signed */ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
43 |
template <typename T> static inline T myabs(T a) { return a < (T)0 ? -a : a; } |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
44 |
/** returns the (absolute) difference between two (scalar) variables */ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
45 |
template <typename T> static inline T delta(T a, T b) { return a < b ? b - a : a - b; } |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
46 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
47 |
/** Some enums need to have allowed incrementing (i.e. StationClassID) */ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
48 |
#define DECLARE_POSTFIX_INCREMENT(type) \ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
49 |
FORCEINLINE type operator ++(type& e, int) \ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
50 |
{ \ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
51 |
type e_org = e; \ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
52 |
e = (type)((int)e + 1); \ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
53 |
return e_org; \ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
54 |
} \ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
55 |
FORCEINLINE type operator --(type& e, int) \ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
56 |
{ \ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
57 |
type e_org = e; \ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
58 |
e = (type)((int)e - 1); \ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
59 |
return e_org; \ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
60 |
} |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
61 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
62 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
63 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
64 |
/** Operators to allow to work with enum as with type safe bit set in C++ */ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
65 |
# define DECLARE_ENUM_AS_BIT_SET(mask_t) \ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
66 |
FORCEINLINE mask_t operator | (mask_t m1, mask_t m2) {return (mask_t)((int)m1 | m2);} \ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
67 |
FORCEINLINE mask_t operator & (mask_t m1, mask_t m2) {return (mask_t)((int)m1 & m2);} \ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
68 |
FORCEINLINE mask_t operator ^ (mask_t m1, mask_t m2) {return (mask_t)((int)m1 ^ m2);} \ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
69 |
FORCEINLINE mask_t& operator |= (mask_t& m1, mask_t m2) {m1 = m1 | m2; return m1;} \ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
70 |
FORCEINLINE mask_t& operator &= (mask_t& m1, mask_t m2) {m1 = m1 & m2; return m1;} \ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
71 |
FORCEINLINE mask_t& operator ^= (mask_t& m1, mask_t m2) {m1 = m1 ^ m2; return m1;} \ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
72 |
FORCEINLINE mask_t operator ~(mask_t m) {return (mask_t)(~(int)m);} |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
73 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
74 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
75 |
/** Informative template class exposing basic enumeration properties used by several |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
76 |
* other templates below. Here we have only forward declaration. For each enum type |
6268
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6261
diff
changeset
|
77 |
* we will create specialization derived from MakeEnumPropsT<>. |
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6261
diff
changeset
|
78 |
* i.e.: |
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6261
diff
changeset
|
79 |
* template <> struct EnumPropsT<Track> : MakeEnumPropsT<Track, byte, TRACK_BEGIN, TRACK_END, INVALID_TRACK> {}; |
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6261
diff
changeset
|
80 |
* followed by: |
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6261
diff
changeset
|
81 |
* typedef TinyEnumT<Track> TrackByte; |
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6261
diff
changeset
|
82 |
*/ |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
83 |
template <typename Tenum_t> struct EnumPropsT; |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
84 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
85 |
/** Helper template class that makes basic properties of given enumeration type visible |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
86 |
* from outsize. It is used as base class of several EnumPropsT specializations each |
6268
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6261
diff
changeset
|
87 |
* dedicated to one of commonly used enumeration types. |
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6261
diff
changeset
|
88 |
* @param Tenum_t enumeration type that you want to describe |
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6261
diff
changeset
|
89 |
* @param Tstorage_t what storage type would be sufficient (i.e. byte) |
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6261
diff
changeset
|
90 |
* @param Tbegin first valid value from the contiguous range (i.e. TRACK_BEGIN) |
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6261
diff
changeset
|
91 |
* @param Tend one past the last valid value from the contiguous range (i.e. TRACK_END) |
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6261
diff
changeset
|
92 |
* @param Tinvalid value used as invalid value marker (i.e. INVALID_TRACK) |
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6261
diff
changeset
|
93 |
*/ |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
94 |
template <typename Tenum_t, typename Tstorage_t, Tenum_t Tbegin, Tenum_t Tend, Tenum_t Tinvalid> |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
95 |
struct MakeEnumPropsT { |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
96 |
typedef Tenum_t type; ///< enum type (i.e. Trackdir) |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
97 |
typedef Tstorage_t storage; ///< storage type (i.e. byte) |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
98 |
static const Tenum_t begin = Tbegin; ///< lowest valid value (i.e. TRACKDIR_BEGIN) |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
99 |
static const Tenum_t end = Tend; ///< one after the last valid value (i.e. TRACKDIR_END) |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
100 |
static const Tenum_t invalid = Tinvalid; ///< what value is used as invalid value (i.e. INVALID_TRACKDIR) |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
101 |
}; |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
102 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
103 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
104 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
105 |
/** In some cases we use byte or uint16 to store values that are defined as enum. It is |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
106 |
* necessary in order to control the sizeof() such values. Some compilers make enum |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
107 |
* the same size as int (4 or 8 bytes instead of 1 or 2). As a consequence the strict |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
108 |
* compiler type-checking causes errors like: |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
109 |
* 'HasPowerOnRail' : cannot convert parameter 1 from 'byte' to 'RailType' when |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
110 |
* u->u.rail.railtype is passed as argument or type RailType. In such cases it is better |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
111 |
* to teach the compiler that u->u.rail.railtype is to be treated as RailType. */ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
112 |
template <typename Tenum_t> struct TinyEnumT; |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
113 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
114 |
/** The general declaration of TinyEnumT<> (above) */ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
115 |
template <typename Tenum_t> struct TinyEnumT |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
116 |
{ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
117 |
typedef Tenum_t enum_type; ///< expose our enumeration type (i.e. Trackdir) to outside |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
118 |
typedef EnumPropsT<Tenum_t> Props; ///< make easier access to our enumeration propeties |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
119 |
typedef typename Props::storage storage_type; ///< small storage type |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
120 |
static const enum_type begin = Props::begin; ///< enum beginning (i.e. TRACKDIR_BEGIN) |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
121 |
static const enum_type end = Props::end; ///< enum end (i.e. TRACKDIR_END) |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
122 |
static const enum_type invalid = Props::invalid;///< invalid value (i.e. INVALID_TRACKDIR) |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
123 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
124 |
storage_type m_val; ///< here we hold the actual value in small (i.e. byte) form |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
125 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
126 |
/** Cast operator - invoked then the value is assigned to the Tenum_t type */ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
127 |
FORCEINLINE operator enum_type () const |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
128 |
{ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
129 |
return (enum_type)m_val; |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
130 |
} |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
131 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
132 |
/** Assignment operator (from Tenum_t type) */ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
133 |
FORCEINLINE TinyEnumT& operator = (enum_type e) |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
134 |
{ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
135 |
m_val = (storage_type)e; return *this; |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
136 |
} |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
137 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
138 |
/** postfix ++ operator on tiny type */ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
139 |
FORCEINLINE TinyEnumT& operator ++ (int) |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
140 |
{ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
141 |
if (++m_val >= end) m_val -= (storage_type)(end - begin); |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
142 |
return *this; |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
143 |
} |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
144 |
}; |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
145 |
|
6268
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6261
diff
changeset
|
146 |
template <typename T> void ClrBitT(T &t, int bit_index) |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
147 |
{ |
6268
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6261
diff
changeset
|
148 |
t = (T)(t & ~((T)1 << bit_index)); |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
149 |
} |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
150 |
|
6268
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6261
diff
changeset
|
151 |
template <typename T> void SetBitT(T &t, int bit_index) |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
152 |
{ |
6268
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6261
diff
changeset
|
153 |
t = (T)(t | ((T)1 << bit_index)); |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
154 |
} |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
155 |
|
6268
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6261
diff
changeset
|
156 |
template <typename T> void ToggleBitT(T &t, int bit_index) |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
157 |
{ |
6268
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6261
diff
changeset
|
158 |
t = (T)(t ^ ((T)1 << bit_index)); |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
159 |
} |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
160 |
|
6261
5fd6b1cfa424
(svn r8815) [cpp_gui] -Codechange: Added ZeroInitBegin and ZeroInitEnd helper structures
KUDr
parents:
5984
diff
changeset
|
161 |
/** |
5fd6b1cfa424
(svn r8815) [cpp_gui] -Codechange: Added ZeroInitBegin and ZeroInitEnd helper structures
KUDr
parents:
5984
diff
changeset
|
162 |
* Zero initialization end marker. |
5fd6b1cfa424
(svn r8815) [cpp_gui] -Codechange: Added ZeroInitBegin and ZeroInitEnd helper structures
KUDr
parents:
5984
diff
changeset
|
163 |
* @see ZeroInitBegin for usage. |
5fd6b1cfa424
(svn r8815) [cpp_gui] -Codechange: Added ZeroInitBegin and ZeroInitEnd helper structures
KUDr
parents:
5984
diff
changeset
|
164 |
*/ |
5fd6b1cfa424
(svn r8815) [cpp_gui] -Codechange: Added ZeroInitBegin and ZeroInitEnd helper structures
KUDr
parents:
5984
diff
changeset
|
165 |
struct ZeroInitEnd { |
5fd6b1cfa424
(svn r8815) [cpp_gui] -Codechange: Added ZeroInitBegin and ZeroInitEnd helper structures
KUDr
parents:
5984
diff
changeset
|
166 |
}; |
5fd6b1cfa424
(svn r8815) [cpp_gui] -Codechange: Added ZeroInitBegin and ZeroInitEnd helper structures
KUDr
parents:
5984
diff
changeset
|
167 |
|
5fd6b1cfa424
(svn r8815) [cpp_gui] -Codechange: Added ZeroInitBegin and ZeroInitEnd helper structures
KUDr
parents:
5984
diff
changeset
|
168 |
/** |
5fd6b1cfa424
(svn r8815) [cpp_gui] -Codechange: Added ZeroInitBegin and ZeroInitEnd helper structures
KUDr
parents:
5984
diff
changeset
|
169 |
* Zero initialization begin marker. When you need to initialize struct/class members by |
5fd6b1cfa424
(svn r8815) [cpp_gui] -Codechange: Added ZeroInitBegin and ZeroInitEnd helper structures
KUDr
parents:
5984
diff
changeset
|
170 |
* memset(&m_first_member, 0, ((byte*)&m_last_member) - ((byte*)&m_first_member + sizeof(m_last_member)); |
5fd6b1cfa424
(svn r8815) [cpp_gui] -Codechange: Added ZeroInitBegin and ZeroInitEnd helper structures
KUDr
parents:
5984
diff
changeset
|
171 |
* place ZeroInitBegin before m_first_member, place ZeroInitEnd after m_last_member, call ZeroInitBegin |
5fd6b1cfa424
(svn r8815) [cpp_gui] -Codechange: Added ZeroInitBegin and ZeroInitEnd helper structures
KUDr
parents:
5984
diff
changeset
|
172 |
* constructor give him ZeroInitEnd& as parameter. This will clear whole area between them using memset. |
5fd6b1cfa424
(svn r8815) [cpp_gui] -Codechange: Added ZeroInitBegin and ZeroInitEnd helper structures
KUDr
parents:
5984
diff
changeset
|
173 |
* @see ZeroInitEnd |
5fd6b1cfa424
(svn r8815) [cpp_gui] -Codechange: Added ZeroInitBegin and ZeroInitEnd helper structures
KUDr
parents:
5984
diff
changeset
|
174 |
*/ |
5fd6b1cfa424
(svn r8815) [cpp_gui] -Codechange: Added ZeroInitBegin and ZeroInitEnd helper structures
KUDr
parents:
5984
diff
changeset
|
175 |
struct ZeroInitBegin { |
5fd6b1cfa424
(svn r8815) [cpp_gui] -Codechange: Added ZeroInitBegin and ZeroInitEnd helper structures
KUDr
parents:
5984
diff
changeset
|
176 |
ZeroInitBegin(const ZeroInitEnd &end) |
5fd6b1cfa424
(svn r8815) [cpp_gui] -Codechange: Added ZeroInitBegin and ZeroInitEnd helper structures
KUDr
parents:
5984
diff
changeset
|
177 |
{ |
5fd6b1cfa424
(svn r8815) [cpp_gui] -Codechange: Added ZeroInitBegin and ZeroInitEnd helper structures
KUDr
parents:
5984
diff
changeset
|
178 |
memset(this, 0, ((byte*)&end) - ((byte*)this)); |
5fd6b1cfa424
(svn r8815) [cpp_gui] -Codechange: Added ZeroInitBegin and ZeroInitEnd helper structures
KUDr
parents:
5984
diff
changeset
|
179 |
} |
5fd6b1cfa424
(svn r8815) [cpp_gui] -Codechange: Added ZeroInitBegin and ZeroInitEnd helper structures
KUDr
parents:
5984
diff
changeset
|
180 |
}; |
5fd6b1cfa424
(svn r8815) [cpp_gui] -Codechange: Added ZeroInitBegin and ZeroInitEnd helper structures
KUDr
parents:
5984
diff
changeset
|
181 |
|
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
182 |
#endif /* HELPERS_HPP */ |