author | celestar |
Sun, 11 Mar 2007 09:28:58 +0000 | |
branch | custombridgeheads |
changeset 5651 | 335d9bd345b0 |
parent 5650 | aefc131bf5ce |
child 5860 | 7fdc9b423ba1 |
permissions | -rw-r--r-- |
5650
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
1 |
/* $Id$ */ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
2 |
|
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
3 |
#ifndef HELPERS_HPP |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
4 |
#define HELPERS_HPP |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
5 |
|
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
6 |
/** @file helpers.hpp */ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
7 |
#include "macros.h" |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
8 |
|
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
9 |
#ifdef __cplusplus |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
10 |
|
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
11 |
/** When allocating using malloc/calloc in C++ it is usually needed to cast the return value |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
12 |
* from void* to the proper pointer type. Another alternative would be MallocT<> as follows */ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
13 |
template <typename T> FORCEINLINE bool MallocT(T** t_ptr, size_t num_elements) |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
14 |
{ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
15 |
*t_ptr = (T*)malloc(num_elements * sizeof(T)); |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
16 |
return (*t_ptr != NULL); |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
17 |
} |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
18 |
/** When allocating using malloc/calloc in C++ it is usually needed to cast the return value |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
19 |
* from void* to the proper pointer type. Another alternative would be MallocT<> as follows */ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
20 |
template <typename T> FORCEINLINE bool CallocT(T** t_ptr, size_t num_elements) |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
21 |
{ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
22 |
*t_ptr = (T*)calloc(num_elements, sizeof(T)); |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
23 |
return (*t_ptr != NULL); |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
24 |
} |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
25 |
/** When allocating using malloc/calloc in C++ it is usually needed to cast the return value |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
26 |
* from void* to the proper pointer type. Another alternative would be MallocT<> as follows */ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
27 |
template <typename T> FORCEINLINE bool ReallocT(T** t_ptr, size_t num_elements) |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
28 |
{ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
29 |
*t_ptr = (T*)realloc(*t_ptr, num_elements * sizeof(T)); |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
30 |
return (*t_ptr != NULL); |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
31 |
} |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
32 |
|
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
33 |
/** type safe swap operation */ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
34 |
template <typename T> void SwapT(T *a, T *b); |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
35 |
|
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
36 |
template <typename T> FORCEINLINE void SwapT(T *a, T *b) |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
37 |
{ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
38 |
T t = *a; |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
39 |
*a = *b; |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
40 |
*b = t; |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
41 |
} |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
42 |
|
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
43 |
|
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
44 |
/** returns the absolute value of (scalar) variable. @note assumes variable to be signed */ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
45 |
template <typename T> static inline T myabs(T a) { return a < (T)0 ? -a : a; } |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
46 |
/** returns the (absolute) difference between two (scalar) variables */ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
47 |
template <typename T> static inline T delta(T a, T b) { return a < b ? b - a : a - b; } |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
48 |
|
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
49 |
/** Some enums need to have allowed incrementing (i.e. StationClassID) */ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
50 |
#define DECLARE_POSTFIX_INCREMENT(type) \ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
51 |
FORCEINLINE type operator ++(type& e, int) \ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
52 |
{ \ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
53 |
type e_org = e; \ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
54 |
e = (type)((int)e + 1); \ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
55 |
return e_org; \ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
56 |
} \ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
57 |
FORCEINLINE type operator --(type& e, int) \ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
58 |
{ \ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
59 |
type e_org = e; \ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
60 |
e = (type)((int)e - 1); \ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
61 |
return e_org; \ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
62 |
} |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
63 |
|
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
64 |
|
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
65 |
|
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
66 |
/** Operators to allow to work with enum as with type safe bit set in C++ */ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
67 |
# define DECLARE_ENUM_AS_BIT_SET(mask_t) \ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
68 |
FORCEINLINE mask_t operator | (mask_t m1, mask_t m2) {return (mask_t)((int)m1 | m2);} \ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
69 |
FORCEINLINE mask_t operator & (mask_t m1, mask_t m2) {return (mask_t)((int)m1 & m2);} \ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
70 |
FORCEINLINE mask_t operator ^ (mask_t m1, mask_t m2) {return (mask_t)((int)m1 ^ m2);} \ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
71 |
FORCEINLINE mask_t& operator |= (mask_t& m1, mask_t m2) {m1 = m1 | m2; return m1;} \ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
72 |
FORCEINLINE mask_t& operator &= (mask_t& m1, mask_t m2) {m1 = m1 & m2; return m1;} \ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
73 |
FORCEINLINE mask_t& operator ^= (mask_t& m1, mask_t m2) {m1 = m1 ^ m2; return m1;} \ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
74 |
FORCEINLINE mask_t operator ~(mask_t m) {return (mask_t)(~(int)m);} |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
75 |
|
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
76 |
/** probably redundant enum combining operators (as we have conversion functions) |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
77 |
* but the old code is full of such arithmetics */ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
78 |
# define DECLARE_ENUM_AS_BIT_INDEX(idx_t, mask_t) \ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
79 |
FORCEINLINE mask_t operator << (int m, idx_t i) {return (mask_t)(m << (int)i);} \ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
80 |
FORCEINLINE mask_t operator << (mask_t m, int i) {return (mask_t)(((int)m) << i);} \ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
81 |
FORCEINLINE mask_t operator >> (mask_t m, int i) {return (mask_t)(((int)m) >> i);} |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
82 |
|
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
83 |
|
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
84 |
/** Informative template class exposing basic enumeration properties used by several |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
85 |
* other templates below. Here we have only forward declaration. For each enum type |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
86 |
* we will create specialization derived from MakeEnumPropsT<>. */ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
87 |
template <typename Tenum_t> struct EnumPropsT; |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
88 |
|
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
89 |
/** Helper template class that makes basic properties of given enumeration type visible |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
90 |
* from outsize. It is used as base class of several EnumPropsT specializations each |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
91 |
* dedicated to one of commonly used enumeration types. */ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
92 |
template <typename Tenum_t, typename Tstorage_t, Tenum_t Tbegin, Tenum_t Tend, Tenum_t Tinvalid> |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
93 |
struct MakeEnumPropsT { |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
94 |
typedef Tenum_t type; ///< enum type (i.e. Trackdir) |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
95 |
typedef Tstorage_t storage; ///< storage type (i.e. byte) |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
96 |
static const Tenum_t begin = Tbegin; ///< lowest valid value (i.e. TRACKDIR_BEGIN) |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
97 |
static const Tenum_t end = Tend; ///< one after the last valid value (i.e. TRACKDIR_END) |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
98 |
static const Tenum_t invalid = Tinvalid; ///< what value is used as invalid value (i.e. INVALID_TRACKDIR) |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
99 |
}; |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
100 |
|
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
101 |
|
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
102 |
|
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
103 |
/** In some cases we use byte or uint16 to store values that are defined as enum. It is |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
104 |
* necessary in order to control the sizeof() such values. Some compilers make enum |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
105 |
* the same size as int (4 or 8 bytes instead of 1 or 2). As a consequence the strict |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
106 |
* compiler type-checking causes errors like: |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
107 |
* 'HasPowerOnRail' : cannot convert parameter 1 from 'byte' to 'RailType' when |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
108 |
* u->u.rail.railtype is passed as argument or type RailType. In such cases it is better |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
109 |
* to teach the compiler that u->u.rail.railtype is to be treated as RailType. */ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
110 |
template <typename Tenum_t> struct TinyEnumT; |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
111 |
|
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
112 |
/** The general declaration of TinyEnumT<> (above) */ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
113 |
template <typename Tenum_t> struct TinyEnumT |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
114 |
{ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
115 |
typedef Tenum_t enum_type; ///< expose our enumeration type (i.e. Trackdir) to outside |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
116 |
typedef EnumPropsT<Tenum_t> Props; ///< make easier access to our enumeration propeties |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
117 |
typedef typename Props::storage storage_type; ///< small storage type |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
118 |
static const enum_type begin = Props::begin; ///< enum beginning (i.e. TRACKDIR_BEGIN) |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
119 |
static const enum_type end = Props::end; ///< enum end (i.e. TRACKDIR_END) |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
120 |
static const enum_type invalid = Props::invalid;///< invalid value (i.e. INVALID_TRACKDIR) |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
121 |
|
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
122 |
storage_type m_val; ///< here we hold the actual value in small (i.e. byte) form |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
123 |
|
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
124 |
/** Cast operator - invoked then the value is assigned to the Tenum_t type */ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
125 |
FORCEINLINE operator enum_type () const |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
126 |
{ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
127 |
return (enum_type)m_val; |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
128 |
} |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
129 |
|
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
130 |
/** Assignment operator (from Tenum_t type) */ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
131 |
FORCEINLINE TinyEnumT& operator = (enum_type e) |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
132 |
{ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
133 |
m_val = (storage_type)e; return *this; |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
134 |
} |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
135 |
|
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
136 |
/** postfix ++ operator on tiny type */ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
137 |
FORCEINLINE TinyEnumT& operator ++ (int) |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
138 |
{ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
139 |
if (++m_val >= end) m_val -= (storage_type)(end - begin); |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
140 |
return *this; |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
141 |
} |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
142 |
}; |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
143 |
|
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
144 |
template <typename Tenum_t> FORCEINLINE void SwapT(TinyEnumT<Tenum_t> *a, TinyEnumT<Tenum_t> *b) |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
145 |
{ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
146 |
SwapT(&a->m_val, &b->m_val); |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
147 |
} |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
148 |
|
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
149 |
template <typename T> FORCEINLINE T ClrBitT(T t, int bit_index) |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
150 |
{ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
151 |
int val = t; |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
152 |
CLRBIT(val, bit_index); |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
153 |
return (T)val; |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
154 |
} |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
155 |
|
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
156 |
template <typename T> FORCEINLINE T SetBitT(T t, int bit_index) |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
157 |
{ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
158 |
int val = t; |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
159 |
SETBIT(val, bit_index); |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
160 |
return (T)val; |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
161 |
} |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
162 |
|
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
163 |
template <typename T> FORCEINLINE T ToggleBitT(T t, int bit_index) |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
164 |
{ |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
165 |
int val = t; |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
166 |
TOGGLEBIT(val, bit_index); |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
167 |
return (T)val; |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
168 |
} |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
169 |
|
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
170 |
#else // __cplusplus |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
171 |
|
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
172 |
#define DECLARE_POSTFIX_INCREMENT(E) |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
173 |
#define DECLARE_ENUM_AS_BIT_SET(E) |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
174 |
#define DECLARE_ENUM_AS_BIT_INDEX(E1,E2) |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
175 |
|
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
176 |
#endif // __cplusplus |
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
177 |
|
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
diff
changeset
|
178 |
#endif /* HELPERS_HPP */ |