src/helpers.hpp
author KUDr
Fri, 02 Mar 2007 21:21:41 +0000
branchcpp_gui
changeset 6273 d8a2c6844650
parent 6268 4b5241e5dd10
child 6285 187e3ef04cc9
permissions -rw-r--r--
(svn r8981) [cpp_gui] -Add: simple CaptionBar Widget
- no CloseButton and StickyButton yet
- no moving capability yet
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
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
     3
#ifndef HELPERS_HPP
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
     4
#define HELPERS_HPP
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
     5
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
     6
/** @file helpers.hpp */
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
     7
#include "macros.h"
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
     8
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
     9
/** 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
    10
*  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
    11
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
    12
{
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
    13
	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
    14
	return t_ptr;
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
    15
}
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
    16
/** 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
    17
*  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
    18
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
    19
{
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
    20
	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
    21
	return t_ptr;
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
    22
}
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
    23
/** 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
    24
*  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
    25
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
    26
{
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
    27
	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
    28
	return t_ptr;
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
    29
}
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
5984
fbef81292ff9 (svn r8276) -Fix
tron
parents: 5860
diff changeset
    32
/** type safe swap operation */
fbef81292ff9 (svn r8276) -Fix
tron
parents: 5860
diff changeset
    33
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
    34
{
5984
fbef81292ff9 (svn r8276) -Fix
tron
parents: 5860
diff changeset
    35
	T t = a;
fbef81292ff9 (svn r8276) -Fix
tron
parents: 5860
diff changeset
    36
	a = b;
fbef81292ff9 (svn r8276) -Fix
tron
parents: 5860
diff changeset
    37
	b = t;
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
    38
}
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
/** 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
    42
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
    43
/** 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
    44
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
    45
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
    46
/** 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
    47
#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
    48
	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
    49
	{ \
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
    50
		type e_org = e; \
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
    51
		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
    52
		return e_org; \
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
    53
	} \
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
    54
	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
    55
	{ \
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
    56
		type e_org = e; \
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
    57
		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
    58
		return e_org; \
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
    59
	}
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
/** 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
    64
# 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
    65
	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
    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) {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
    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 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
    72
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
    73
/** probably redundant enum combining operators (as we have conversion functions)
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
    74
 *  but the old code is full of such arithmetics */
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
    75
# define DECLARE_ENUM_AS_BIT_INDEX(idx_t, mask_t) \
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
    76
	FORCEINLINE mask_t operator << (int m, idx_t i) {return (mask_t)(m << (int)i);} \
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
    77
	FORCEINLINE mask_t operator << (mask_t m, int i) {return (mask_t)(((int)m) << i);} \
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
    78
	FORCEINLINE mask_t operator >> (mask_t m, int i) {return (mask_t)(((int)m) >> i);}
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
    79
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
    80
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
    81
/** 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
    82
 *  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
    83
 *  we will create specialization derived from MakeEnumPropsT<>.
4b5241e5dd10 (svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents: 6261
diff changeset
    84
 *  i.e.:
4b5241e5dd10 (svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents: 6261
diff changeset
    85
 *    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
    86
 *  followed by:
4b5241e5dd10 (svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents: 6261
diff changeset
    87
 *    typedef TinyEnumT<Track> TrackByte;
4b5241e5dd10 (svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents: 6261
diff changeset
    88
 */
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
    89
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
    90
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
    91
/** 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
    92
 *  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
    93
 *  dedicated to one of commonly used enumeration types.
4b5241e5dd10 (svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents: 6261
diff changeset
    94
 *  @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
    95
 *  @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
    96
 *  @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
    97
 *  @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
    98
 *  @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
    99
 */
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
   100
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
   101
struct MakeEnumPropsT {
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
   102
	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
   103
	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
   104
	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
   105
	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
   106
	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
   107
};
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
   108
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
   109
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
   110
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
   111
/** 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
   112
	*  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
   113
	*  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
   114
	*  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
   115
	*     '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
   116
	*  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
   117
	*  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
   118
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
   119
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
   120
/** 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
   121
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
   122
{
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
   123
	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
   124
	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
   125
	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
   126
	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
   127
	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
   128
	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
   129
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
   130
	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
   131
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
   132
	/** 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
   133
	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
   134
	{
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
   135
		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
   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
	/** 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
   139
	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
   140
	{
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
   141
		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
   142
	}
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
	/** 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
   145
	FORCEINLINE TinyEnumT& operator ++ (int)
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
   146
	{
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
   147
		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
   148
		return *this;
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
};
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
   151
6268
4b5241e5dd10 (svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents: 6261
diff changeset
   152
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
   153
{
6268
4b5241e5dd10 (svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents: 6261
diff changeset
   154
	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
   155
}
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
   156
6268
4b5241e5dd10 (svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents: 6261
diff changeset
   157
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
   158
{
6268
4b5241e5dd10 (svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents: 6261
diff changeset
   159
	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
   160
}
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
   161
6268
4b5241e5dd10 (svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents: 6261
diff changeset
   162
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
   163
{
6268
4b5241e5dd10 (svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents: 6261
diff changeset
   164
	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
   165
}
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
   166
6261
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
* Zero initialization end marker.
5fd6b1cfa424 (svn r8815) [cpp_gui] -Codechange: Added ZeroInitBegin and ZeroInitEnd helper structures
KUDr
parents: 5984
diff changeset
   169
* @see ZeroInitBegin for usage.
5fd6b1cfa424 (svn r8815) [cpp_gui] -Codechange: Added ZeroInitBegin and ZeroInitEnd helper structures
KUDr
parents: 5984
diff changeset
   170
*/
5fd6b1cfa424 (svn r8815) [cpp_gui] -Codechange: Added ZeroInitBegin and ZeroInitEnd helper structures
KUDr
parents: 5984
diff changeset
   171
struct ZeroInitEnd {
5fd6b1cfa424 (svn r8815) [cpp_gui] -Codechange: Added ZeroInitBegin and ZeroInitEnd helper structures
KUDr
parents: 5984
diff changeset
   172
};
5fd6b1cfa424 (svn r8815) [cpp_gui] -Codechange: Added ZeroInitBegin and ZeroInitEnd helper structures
KUDr
parents: 5984
diff changeset
   173
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
 * 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
   176
 * 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
   177
 * 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
   178
 * 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
   179
 * @see ZeroInitEnd
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
struct ZeroInitBegin {
5fd6b1cfa424 (svn r8815) [cpp_gui] -Codechange: Added ZeroInitBegin and ZeroInitEnd helper structures
KUDr
parents: 5984
diff changeset
   182
	ZeroInitBegin(const ZeroInitEnd &end)
5fd6b1cfa424 (svn r8815) [cpp_gui] -Codechange: Added ZeroInitBegin and ZeroInitEnd helper structures
KUDr
parents: 5984
diff changeset
   183
	{
5fd6b1cfa424 (svn r8815) [cpp_gui] -Codechange: Added ZeroInitBegin and ZeroInitEnd helper structures
KUDr
parents: 5984
diff changeset
   184
		memset(this, 0, ((byte*)&end) - ((byte*)this));
5fd6b1cfa424 (svn r8815) [cpp_gui] -Codechange: Added ZeroInitBegin and ZeroInitEnd helper structures
KUDr
parents: 5984
diff changeset
   185
	}
5fd6b1cfa424 (svn r8815) [cpp_gui] -Codechange: Added ZeroInitBegin and ZeroInitEnd helper structures
KUDr
parents: 5984
diff changeset
   186
};
5fd6b1cfa424 (svn r8815) [cpp_gui] -Codechange: Added ZeroInitBegin and ZeroInitEnd helper structures
KUDr
parents: 5984
diff changeset
   187
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
   188
#endif /* HELPERS_HPP */