forked from simulationcraft/simc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sc_timespan.hpp
277 lines (241 loc) · 7.01 KB
/
sc_timespan.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// ==========================================================================
// Dedmonwakeen's Raid DPS/TPS Simulator.
// Send questions to [email protected]
// ==========================================================================
// ==========================================================================
// Custom Time class
// ==========================================================================
#ifndef SC_TIMESPAN_HPP
#define SC_TIMESPAN_HPP
#include "config.hpp"
#include <cmath>
#include <numeric>
#include <limits>
#include <iostream>
#include <type_traits>
#include "util/generic.hpp"
// if timespan_t is in the global namespace, there's a name lookup issue with
// one of the Qt headers. Problem is avoided by defining in a sub-namespace
// and then lifting into the global namespace with a using declaration.
namespace timespan_adl_barrier
{
/**
* @brief Class for representing InGame time.
*
* World of Warcraft handles InGame time/events in milliseconds. This class
* emulates that format for exactness, as well as representing C++ type-safety
* when defining time values, without implicit conversion from or to any
* integral or floating numbers.
*/
class timespan_t
{
private:
using time_t = int_least64_t;
static time_t native_to_milli(time_t t)
{
return t;
}
static double native_to_second(time_t t)
{
return static_cast<double>(t) * (1.0 / 1000);
}
static double native_to_minute(time_t t)
{
return static_cast<double>(t) * (1.0 / (60 * 1000));
}
template<typename Rep>
static time_t milli_to_native(Rep t)
{
return static_cast<time_t>(t);
}
template<typename Rep>
static time_t second_to_native(Rep t)
{
return static_cast<time_t>(t * 1000);
}
template<typename Rep>
static time_t minute_to_native(Rep t)
{
return static_cast<time_t>(t * (60 * 1000));
}
time_t time;
template<typename Rep>
explicit timespan_t(Rep t) :
time(static_cast<time_t>(t))
{
}
public:
timespan_t() : time( 0 )
{ }
double total_minutes() const
{
return native_to_minute(time);
}
double total_seconds() const
{
return native_to_second(time);
}
time_t total_millis() const
{
return native_to_milli(time);
}
template<typename Rep>
static typename std::enable_if<std::is_arithmetic<Rep>::value, timespan_t>::type from_millis(
Rep millis)
{
return timespan_t(milli_to_native(millis));
}
template<typename Rep>
static typename std::enable_if<std::is_arithmetic<Rep>::value, timespan_t>::type from_seconds(
Rep seconds)
{
return timespan_t(second_to_native(seconds));
}
template<typename Rep>
static typename std::enable_if<std::is_arithmetic<Rep>::value, timespan_t>::type from_minutes(
Rep minutes)
{
return timespan_t(minute_to_native(minutes));
}
bool operator==(timespan_t right) const
{
return time == right.time;
}
bool operator!=(timespan_t right) const
{
return time != right.time;
}
bool operator>(timespan_t right) const
{
return time > right.time;
}
bool operator>=(timespan_t right) const
{
return time >= right.time;
}
bool operator<(timespan_t right) const
{
return time < right.time;
}
bool operator<=(timespan_t right) const
{
return time <= right.time;
}
timespan_t& operator+=(timespan_t right)
{
assert(right < zero() || *this < zero() || *this <= max() - right);
assert(right > zero() || *this >= min() - right);
time += right.time;
return *this;
}
timespan_t& operator-=(timespan_t right)
{
assert(right < zero() || *this >= right + min());
assert(right > zero() || *this <= right + max());
*this = *this - right;
return *this;
}
timespan_t&
operator %=(timespan_t right)
{
time %= right.time;
return *this;
}
template<typename Rep>
typename std::enable_if<std::is_arithmetic<Rep>::value, timespan_t&>::type operator*=(
Rep right)
{
time = static_cast<time_t>(time * right);
return *this;
}
template<typename Rep>
typename std::enable_if<std::is_arithmetic<Rep>::value, timespan_t&>::type operator/=(
Rep right)
{
time = static_cast<time_t>(time / right);
return *this;
}
friend timespan_t operator+(timespan_t right)
{
return right;
}
friend timespan_t operator-(timespan_t right)
{
return timespan_t(-right.time);
}
friend timespan_t operator+(timespan_t left, timespan_t right)
{
assert(right < zero() || left < zero() || left <= max() - right);
assert(right > zero() || left >= min() - right);
return timespan_t(left.time + right.time);
}
friend timespan_t operator-(timespan_t left, timespan_t right)
{
assert(right < zero() || left >= right + min());
assert(right > zero() || left <= right + max());
return timespan_t(left.time - right.time);
}
template<typename Rep>
friend typename std::enable_if<std::is_arithmetic<Rep>::value, timespan_t>::type operator*(
timespan_t left, Rep right)
{
return timespan_t(left.time * right);
}
template<typename Rep>
friend typename std::enable_if<std::is_arithmetic<Rep>::value, timespan_t>::type operator*(
Rep left, timespan_t right)
{
return timespan_t(left * right.time);
}
template<typename Rep>
friend typename std::enable_if<std::is_arithmetic<Rep>::value, timespan_t>::type operator/(
timespan_t left, Rep right)
{
return timespan_t(left.time / right);
}
friend double operator/(timespan_t left, timespan_t right)
{
return static_cast<double>(left.time) / right.time;
}
friend timespan_t operator%(timespan_t left, timespan_t right)
{
left %= right;
return left;
}
typedef time_t native_t;
// Only to be used to convert without loss of precision for a computation
// that will later be converted back via from_native().
static native_t to_native(timespan_t t)
{
return t.time;
}
// Only to be used to convert the result of to_native().
template<typename Rep>
static timespan_t from_native(Rep t)
{
return timespan_t(t);
}
static timespan_t zero()
{
return timespan_t();
}
static timespan_t max()
{
return timespan_t(std::numeric_limits<time_t>::max());
}
static timespan_t min()
{
if (std::is_floating_point<time_t>::value)
return timespan_t(-std::numeric_limits<time_t>::max());
else
return timespan_t(std::numeric_limits<time_t>::min());
}
};
inline std::ostream& operator<<(std::ostream &os, const timespan_t& x)
{
os << x.total_seconds() << "seconds";
return os;
}
} // namespace timespan_adl_barrier
using timespan_adl_barrier::timespan_t;
#endif // SC_TIMESPAN_HPP