author | skidd13 |
Thu, 22 Nov 2007 18:01:51 +0000 | |
changeset 8433 | 9bbc38806eeb |
parent 8428 | f8300c908bd9 |
child 8533 | a9b708fe4a00 |
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 |
|
6505
abcb0580d976
(svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents:
6482
diff
changeset
|
3 |
/** @file helpers.hpp */ |
abcb0580d976
(svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents:
6482
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 |
/** 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
|
43 |
#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
|
44 |
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
|
45 |
{ \ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
46 |
type e_org = e; \ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
47 |
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
|
48 |
return e_org; \ |
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 |
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
|
51 |
{ \ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
52 |
type e_org = e; \ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
53 |
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
|
54 |
return e_org; \ |
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 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
57 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
58 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
59 |
/** 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
|
60 |
# 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
|
61 |
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
|
62 |
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
|
63 |
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
|
64 |
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
|
65 |
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
|
66 |
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
|
67 |
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
|
68 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
69 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
70 |
/** 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
|
71 |
* other templates below. Here we have only forward declaration. For each enum type |
6455
4b0a3d229026
(svn r8866) -Doc: added more comments to template struct MakeEnumPropsT
KUDr
parents:
6453
diff
changeset
|
72 |
* we will create specialization derived from MakeEnumPropsT<>. |
6456
2b9af36fe022
(svn r8867) -Fix(r8866): revert yapf_costrail.hpp commited by mistake, helpers.hpp had wrong line ending
KUDr
parents:
6455
diff
changeset
|
73 |
* i.e.: |
2b9af36fe022
(svn r8867) -Fix(r8866): revert yapf_costrail.hpp commited by mistake, helpers.hpp had wrong line ending
KUDr
parents:
6455
diff
changeset
|
74 |
* template <> struct EnumPropsT<Track> : MakeEnumPropsT<Track, byte, TRACK_BEGIN, TRACK_END, INVALID_TRACK> {}; |
2b9af36fe022
(svn r8867) -Fix(r8866): revert yapf_costrail.hpp commited by mistake, helpers.hpp had wrong line ending
KUDr
parents:
6455
diff
changeset
|
75 |
* followed by: |
2b9af36fe022
(svn r8867) -Fix(r8866): revert yapf_costrail.hpp commited by mistake, helpers.hpp had wrong line ending
KUDr
parents:
6455
diff
changeset
|
76 |
* typedef TinyEnumT<Track> TrackByte; |
6455
4b0a3d229026
(svn r8866) -Doc: added more comments to template struct MakeEnumPropsT
KUDr
parents:
6453
diff
changeset
|
77 |
*/ |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
78 |
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
|
79 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
80 |
/** 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
|
81 |
* from outsize. It is used as base class of several EnumPropsT specializations each |
6455
4b0a3d229026
(svn r8866) -Doc: added more comments to template struct MakeEnumPropsT
KUDr
parents:
6453
diff
changeset
|
82 |
* dedicated to one of commonly used enumeration types. |
4b0a3d229026
(svn r8866) -Doc: added more comments to template struct MakeEnumPropsT
KUDr
parents:
6453
diff
changeset
|
83 |
* @param Tenum_t enumeration type that you want to describe |
4b0a3d229026
(svn r8866) -Doc: added more comments to template struct MakeEnumPropsT
KUDr
parents:
6453
diff
changeset
|
84 |
* @param Tstorage_t what storage type would be sufficient (i.e. byte) |
4b0a3d229026
(svn r8866) -Doc: added more comments to template struct MakeEnumPropsT
KUDr
parents:
6453
diff
changeset
|
85 |
* @param Tbegin first valid value from the contiguous range (i.e. TRACK_BEGIN) |
4b0a3d229026
(svn r8866) -Doc: added more comments to template struct MakeEnumPropsT
KUDr
parents:
6453
diff
changeset
|
86 |
* @param Tend one past the last valid value from the contiguous range (i.e. TRACK_END) |
4b0a3d229026
(svn r8866) -Doc: added more comments to template struct MakeEnumPropsT
KUDr
parents:
6453
diff
changeset
|
87 |
* @param Tinvalid value used as invalid value marker (i.e. INVALID_TRACK) |
4b0a3d229026
(svn r8866) -Doc: added more comments to template struct MakeEnumPropsT
KUDr
parents:
6453
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, 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
|
90 |
struct MakeEnumPropsT { |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
91 |
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
|
92 |
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
|
93 |
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
|
94 |
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
|
95 |
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
|
96 |
}; |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
97 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
98 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
99 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
100 |
/** 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
|
101 |
* 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
|
102 |
* 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
|
103 |
* 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
|
104 |
* '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
|
105 |
* 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
|
106 |
* 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
|
107 |
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
|
108 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
109 |
/** 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
|
110 |
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
|
111 |
{ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
112 |
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
|
113 |
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
|
114 |
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
|
115 |
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
|
116 |
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
|
117 |
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
|
118 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
119 |
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
|
120 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
121 |
/** 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
|
122 |
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
|
123 |
{ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
124 |
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
|
125 |
} |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
126 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
127 |
/** 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
|
128 |
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
|
129 |
{ |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
130 |
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
|
131 |
} |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
132 |
|
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
133 |
/** postfix ++ operator on tiny type */ |
8334
bca925892b54
(svn r11388) -Fix: postfix ++ returned new value, should (ofcourse) be old value (SmatZ)
truelight
parents:
8259
diff
changeset
|
134 |
FORCEINLINE TinyEnumT operator ++ (int) |
bca925892b54
(svn r11388) -Fix: postfix ++ returned new value, should (ofcourse) be old value (SmatZ)
truelight
parents:
8259
diff
changeset
|
135 |
{ |
bca925892b54
(svn r11388) -Fix: postfix ++ returned new value, should (ofcourse) be old value (SmatZ)
truelight
parents:
8259
diff
changeset
|
136 |
TinyEnumT org = *this; |
bca925892b54
(svn r11388) -Fix: postfix ++ returned new value, should (ofcourse) be old value (SmatZ)
truelight
parents:
8259
diff
changeset
|
137 |
if (++m_val >= end) m_val -= (storage_type)(end - begin); |
bca925892b54
(svn r11388) -Fix: postfix ++ returned new value, should (ofcourse) be old value (SmatZ)
truelight
parents:
8259
diff
changeset
|
138 |
return org; |
bca925892b54
(svn r11388) -Fix: postfix ++ returned new value, should (ofcourse) be old value (SmatZ)
truelight
parents:
8259
diff
changeset
|
139 |
} |
bca925892b54
(svn r11388) -Fix: postfix ++ returned new value, should (ofcourse) be old value (SmatZ)
truelight
parents:
8259
diff
changeset
|
140 |
|
bca925892b54
(svn r11388) -Fix: postfix ++ returned new value, should (ofcourse) be old value (SmatZ)
truelight
parents:
8259
diff
changeset
|
141 |
/** prefix ++ operator on tiny type */ |
bca925892b54
(svn r11388) -Fix: postfix ++ returned new value, should (ofcourse) be old value (SmatZ)
truelight
parents:
8259
diff
changeset
|
142 |
FORCEINLINE TinyEnumT& operator ++ () |
5838
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 |
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
|
145 |
return *this; |
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 |
}; |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
148 |
|
8259
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
149 |
/** |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
150 |
* Overflow safe template for integers, i.e. integers that will never overflow |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
151 |
* you multiply the maximum value with 2, or add 2, or substract somethng from |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
152 |
* the minimum value, etc. |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
153 |
* @param T the type these integers are stored with. |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
154 |
* @param T_MAX the maximum value for the integers. |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
155 |
* @param T_MIN the minimum value for the integers. |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
156 |
*/ |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
157 |
template <class T, T T_MAX, T T_MIN> |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
158 |
class OverflowSafeInt |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
159 |
{ |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
160 |
private: |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
161 |
/** The non-overflow safe backend to store the value in. */ |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
162 |
T m_value; |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
163 |
public: |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
164 |
OverflowSafeInt() : m_value(0) { } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
165 |
|
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
166 |
OverflowSafeInt(const OverflowSafeInt& other) { this->m_value = other.m_value; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
167 |
OverflowSafeInt(const int64 int_) { this->m_value = int_; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
168 |
|
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
169 |
FORCEINLINE OverflowSafeInt& operator = (const OverflowSafeInt& other) { this->m_value = other.m_value; return *this; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
170 |
|
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
171 |
FORCEINLINE OverflowSafeInt operator - () const { return OverflowSafeInt(-this->m_value); } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
172 |
|
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
173 |
/** |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
174 |
* Safe implementation of addition. |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
175 |
* @param other the amount to add |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
176 |
* @note when the addition would yield more than T_MAX (or less than T_MIN), |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
177 |
* it will be T_MAX (respectively T_MIN). |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
178 |
*/ |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
179 |
FORCEINLINE OverflowSafeInt& operator += (const OverflowSafeInt& other) |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
180 |
{ |
8419
de9999f762d0
(svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents:
8334
diff
changeset
|
181 |
if ((T_MAX - abs(other.m_value)) < abs(this->m_value) && |
8259
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
182 |
(this->m_value < 0) == (other.m_value < 0)) { |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
183 |
this->m_value = (this->m_value < 0) ? T_MIN : T_MAX ; |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
184 |
} else { |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
185 |
this->m_value += other.m_value; |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
186 |
} |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
187 |
return *this; |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
188 |
} |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
189 |
|
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
190 |
/* Operators for addition and substraction */ |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
191 |
FORCEINLINE OverflowSafeInt operator + (const OverflowSafeInt& other) const { OverflowSafeInt result = *this; result += other; return result; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
192 |
FORCEINLINE OverflowSafeInt operator + (const int other) const { OverflowSafeInt result = *this; result += (int64)other; return result; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
193 |
FORCEINLINE OverflowSafeInt operator + (const uint other) const { OverflowSafeInt result = *this; result += (int64)other; return result; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
194 |
FORCEINLINE OverflowSafeInt& operator -= (const OverflowSafeInt& other) { return *this += (-other); } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
195 |
FORCEINLINE OverflowSafeInt operator - (const OverflowSafeInt& other) const { OverflowSafeInt result = *this; result -= other; return result; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
196 |
FORCEINLINE OverflowSafeInt operator - (const int other) const { OverflowSafeInt result = *this; result -= (int64)other; return result; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
197 |
FORCEINLINE OverflowSafeInt operator - (const uint other) const { OverflowSafeInt result = *this; result -= (int64)other; return result; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
198 |
|
8334
bca925892b54
(svn r11388) -Fix: postfix ++ returned new value, should (ofcourse) be old value (SmatZ)
truelight
parents:
8259
diff
changeset
|
199 |
FORCEINLINE OverflowSafeInt& operator ++ () { return *this += 1; } |
bca925892b54
(svn r11388) -Fix: postfix ++ returned new value, should (ofcourse) be old value (SmatZ)
truelight
parents:
8259
diff
changeset
|
200 |
FORCEINLINE OverflowSafeInt& operator -- () { return *this += -1; } |
bca925892b54
(svn r11388) -Fix: postfix ++ returned new value, should (ofcourse) be old value (SmatZ)
truelight
parents:
8259
diff
changeset
|
201 |
FORCEINLINE OverflowSafeInt operator ++ (int) { OverflowSafeInt org = *this; *this += 1; return org; } |
bca925892b54
(svn r11388) -Fix: postfix ++ returned new value, should (ofcourse) be old value (SmatZ)
truelight
parents:
8259
diff
changeset
|
202 |
FORCEINLINE OverflowSafeInt operator -- (int) { OverflowSafeInt org = *this; *this += -1; return org; } |
8259
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
203 |
|
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
204 |
/** |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
205 |
* Safe implementation of multiplication. |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
206 |
* @param factor the factor to multiply this with. |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
207 |
* @note when the multiplication would yield more than T_MAX (or less than T_MIN), |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
208 |
* it will be T_MAX (respectively T_MIN). |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
209 |
*/ |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
210 |
FORCEINLINE OverflowSafeInt& operator *= (const int factor) |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
211 |
{ |
8419
de9999f762d0
(svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents:
8334
diff
changeset
|
212 |
if (factor != 0 && (T_MAX / abs(factor)) < abs(this->m_value)) { |
8259
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
213 |
this->m_value = ((this->m_value < 0) == (factor < 0)) ? T_MAX : T_MIN ; |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
214 |
} else { |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
215 |
this->m_value *= factor ; |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
216 |
} |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
217 |
return *this; |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
218 |
} |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
219 |
|
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
220 |
/* Operators for multiplication */ |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
221 |
FORCEINLINE OverflowSafeInt operator * (const int64 factor) const { OverflowSafeInt result = *this; result *= factor; return result; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
222 |
FORCEINLINE OverflowSafeInt operator * (const int factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
223 |
FORCEINLINE OverflowSafeInt operator * (const uint factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
224 |
FORCEINLINE OverflowSafeInt operator * (const uint16 factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
225 |
FORCEINLINE OverflowSafeInt operator * (const byte factor) const { OverflowSafeInt result = *this; result *= (int64)factor; return result; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
226 |
|
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
227 |
/* Operators for division */ |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
228 |
FORCEINLINE OverflowSafeInt& operator /= (const int divisor) { this->m_value /= divisor; return *this; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
229 |
FORCEINLINE OverflowSafeInt operator / (const OverflowSafeInt& divisor) const { OverflowSafeInt result = *this; result /= divisor.m_value; return result; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
230 |
FORCEINLINE OverflowSafeInt operator / (const int divisor) const { OverflowSafeInt result = *this; result /= divisor; return result; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
231 |
FORCEINLINE OverflowSafeInt operator / (const uint divisor) const { OverflowSafeInt result = *this; result /= (int)divisor; return result; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
232 |
|
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
233 |
/* Operators for modulo */ |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
234 |
FORCEINLINE OverflowSafeInt& operator %= (const int divisor) { this->m_value %= divisor; return *this; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
235 |
FORCEINLINE OverflowSafeInt operator % (const int divisor) const { OverflowSafeInt result = *this; result %= divisor; return result; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
236 |
|
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
237 |
/* Operators for shifting */ |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
238 |
FORCEINLINE OverflowSafeInt& operator <<= (const int shift) { this->m_value <<= shift; return *this; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
239 |
FORCEINLINE OverflowSafeInt operator << (const int shift) const { OverflowSafeInt result = *this; result <<= shift; return result; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
240 |
FORCEINLINE OverflowSafeInt& operator >>= (const int shift) { this->m_value >>= shift; return *this; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
241 |
FORCEINLINE OverflowSafeInt operator >> (const int shift) const { OverflowSafeInt result = *this; result >>= shift; return result; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
242 |
|
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
243 |
/* Operators for (in)equality when comparing overflow safe ints */ |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
244 |
FORCEINLINE bool operator == (const OverflowSafeInt& other) const { return this->m_value == other.m_value; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
245 |
FORCEINLINE bool operator != (const OverflowSafeInt& other) const { return !(*this == other); } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
246 |
FORCEINLINE bool operator > (const OverflowSafeInt& other) const { return this->m_value > other.m_value; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
247 |
FORCEINLINE bool operator >= (const OverflowSafeInt& other) const { return this->m_value >= other.m_value; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
248 |
FORCEINLINE bool operator < (const OverflowSafeInt& other) const { return !(*this >= other); } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
249 |
FORCEINLINE bool operator <= (const OverflowSafeInt& other) const { return !(*this > other); } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
250 |
|
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
251 |
/* Operators for (in)equality when comparing non-overflow safe ints */ |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
252 |
FORCEINLINE bool operator == (const int other) const { return this->m_value == other; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
253 |
FORCEINLINE bool operator != (const int other) const { return !(*this == other); } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
254 |
FORCEINLINE bool operator > (const int other) const { return this->m_value > other; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
255 |
FORCEINLINE bool operator >= (const int other) const { return this->m_value >= other; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
256 |
FORCEINLINE bool operator < (const int other) const { return !(*this >= other); } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
257 |
FORCEINLINE bool operator <= (const int other) const { return !(*this > other); } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
258 |
|
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
259 |
FORCEINLINE operator int64 () const { return this->m_value; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
260 |
}; |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
261 |
|
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
262 |
/* Sometimes we got int64 operator OverflowSafeInt instead of vice versa. Handle that properly */ |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
263 |
template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator + (int64 a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b + a; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
264 |
template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator - (int64 a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return -b + a; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
265 |
template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator * (int64 a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b * a; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
266 |
template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator / (int64 a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return (OverflowSafeInt<T, T_MAX, T_MIN>)a / (int)b; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
267 |
|
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
268 |
/* Sometimes we got int operator OverflowSafeInt instead of vice versa. Handle that properly */ |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
269 |
template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator + (int a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b + a; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
270 |
template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator - (int a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return -b + a; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
271 |
template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator * (int a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b * a; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
272 |
template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator / (int a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return (OverflowSafeInt<T, T_MAX, T_MIN>)a / (int)b; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
273 |
|
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
274 |
/* Sometimes we got uint operator OverflowSafeInt instead of vice versa. Handle that properly */ |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
275 |
template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator + (uint a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b + a; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
276 |
template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator - (uint a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return -b + a; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
277 |
template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator * (uint a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b * a; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
278 |
template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator / (uint a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return (OverflowSafeInt<T, T_MAX, T_MIN>)a / (int)b; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
279 |
|
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
280 |
/* Sometimes we got byte operator OverflowSafeInt instead of vice versa. Handle that properly */ |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
281 |
template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator + (byte a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b + a; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
282 |
template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator - (byte a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return -b + a; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
283 |
template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator * (byte a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return b * a; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
284 |
template <class T, int64 T_MAX, int64 T_MIN> FORCEINLINE OverflowSafeInt<T, T_MAX, T_MIN> operator / (byte a, OverflowSafeInt<T, T_MAX, T_MIN> b) { return (OverflowSafeInt<T, T_MAX, T_MIN>)a / (int)b; } |
127e5d73637d
(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents:
6505
diff
changeset
|
285 |
|
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff
changeset
|
286 |
#endif /* HELPERS_HPP */ |