-
Notifications
You must be signed in to change notification settings - Fork 0
/
Duration.hpp
274 lines (246 loc) · 7.62 KB
/
Duration.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
#pragma once
/*
* Copyright (c) xEmbeddedTools team and contributors.
* Licensed under the Apache License, Version 2.0. See LICENSE file in the project root for details.
*/
// std
#include <cstdint>
#include <type_traits>
// hkm
#include <xmcu/assertion.hpp>
namespace xmcu {
template<typename Value_t, Value_t factor_t> class Duration
{
public:
using Value_type = Value_t;
static constexpr Value_t factor = factor_t;
constexpr Duration() = default;
constexpr Duration(Duration&&) = default;
constexpr Duration(const Duration&) = default;
constexpr Duration(Value_t a_v)
: v(a_v)
{
}
template<Value_t rhs_factor, std::enable_if_t<(factor_t < rhs_factor), bool> = true>
constexpr Duration(const Duration<Value_t, rhs_factor>& a_v)
: v(a_v.get() * rhs_factor)
{
}
template<Value_t rhs_factor, std::enable_if_t<(factor_t < rhs_factor), bool> = true>
explicit constexpr Duration(Duration<Value_t, rhs_factor>&& a_v)
: v(a_v.get() * rhs_factor)
{
}
constexpr Duration& operator=(const Duration&) = default;
constexpr Duration& operator=(Duration&&) = default;
template<Value_t rhs_factor> constexpr bool operator==(Duration<Value_t, rhs_factor> a_rhs)
{
return this->v * factor_t == a_rhs.get() * rhs_factor;
}
template<Value_t rhs_factor> constexpr bool operator!=(Duration<Value_t, rhs_factor> a_rhs)
{
return this->v * factor_t != a_rhs.get() * rhs_factor;
}
template<Value_t rhs_factor> constexpr bool operator>=(Duration<Value_t, rhs_factor> a_rhs)
{
return this->v * factor_t >= a_rhs.get() * rhs_factor;
}
template<Value_t rhs_factor> constexpr bool operator<=(Duration<Value_t, rhs_factor> a_rhs)
{
return this->v * factor_t <= a_rhs.get() * rhs_factor;
}
template<Value_t rhs_factor> constexpr bool operator>(Duration<Value_t, rhs_factor> a_rhs)
{
return this->v * factor_t > a_rhs.get() * rhs_factor;
}
template<Value_t rhs_factor> constexpr bool operator<(Duration<Value_t, rhs_factor> a_rhs)
{
return this->v * factor_t < a_rhs.get() * rhs_factor;
}
template<Value_t rhs_factor, std::enable_if_t<(factor_t < rhs_factor), bool> = true>
constexpr Duration<Value_t, factor>& operator=(const Duration<Value_t, rhs_factor>& a_rhs)
{
this->v = a_rhs.v * rhs_factor;
return *this;
}
template<Value_t rhs_factor, std::enable_if_t<(factor_t < rhs_factor), bool> = true>
constexpr Duration<Value_t, factor_t>& operator=(Duration<Value_t, rhs_factor>&& a_rhs)
{
this->v = a_rhs.v * rhs_factor;
return *this;
}
constexpr Duration& operator++()
{
this->v++;
return *this;
}
constexpr Duration operator++(int)
{
return Duration(this->v++);
}
constexpr Duration& operator--()
{
hkm_assert(this->v != 0x0u);
this->v--;
return *this;
}
constexpr Duration operator--(int)
{
hkm_assert(this->v != 0x0u);
return Duration(this->v--);
}
constexpr Duration& operator+=(const Duration& a_rhs)
{
this->v += a_rhs.v;
return *this;
}
constexpr Duration& operator-=(const Duration& a_rhs)
{
hkm_assert(this->v >= a_rhs.v);
this->v -= a_rhs.v;
return *this;
}
constexpr Duration& operator*=(Value_t a_rhs)
{
this->v *= a_rhs;
return *this;
}
constexpr Duration& operator/=(Value_t a_rhs)
{
hkm_assert(a_rhs > 0x0u);
// we don't use negative Durations?
// compilation error. . . :
// request for member 'get' in 'a_rhs', which is of non-class type 'long long unsigned int'
// hkm_assert(a_rhs.get() != 0x0u);
this->v /= a_rhs;
return *this;
}
constexpr Duration& operator%=(Value_t a_rhs)
{
this->v %= a_rhs;
return *this;
}
constexpr Duration& operator%=(const Duration& a_rhs)
{
this->v %= a_rhs.v;
return *this;
}
constexpr Value_t get() const
{
return this->v;
}
template<typename Ret_type_t> Ret_type_t get_in() const = delete;
private:
Value_t v = static_cast<Value_t>(0);
};
using Microseconds = Duration<std::uint64_t, 1u>;
using Milliseconds = Duration<std::uint64_t, 1000u>;
using Seconds = Duration<std::uint64_t, 1000000u>;
template<> template<> inline Microseconds Milliseconds::get_in() const
{
return this->v * 1000u;
}
template<> template<> inline Milliseconds Seconds::get_in() const
{
return this->v * 1000u;
}
inline Microseconds operator-(Microseconds a_lhs, Microseconds a_rhs)
{
hkm_assert(a_lhs.get() >= a_rhs.get());
return { a_lhs.get() - a_rhs.get() };
}
inline Microseconds operator-(Microseconds a_lhs, Milliseconds a_rhs)
{
hkm_assert(a_lhs.get() >= a_rhs.get_in<Microseconds>().get());
return { a_lhs - a_rhs.get_in<Microseconds>() };
}
inline Microseconds operator-(Milliseconds a_lhs, Microseconds a_rhs)
{
hkm_assert(a_lhs.get_in<Microseconds>().get() >= a_rhs.get());
return { a_lhs.get_in<Microseconds>() - a_rhs };
}
inline Milliseconds operator-(Milliseconds a_lhs, Milliseconds a_rhs)
{
hkm_assert(a_lhs.get() >= a_rhs.get());
return { a_lhs.get() - a_rhs.get() };
}
inline Milliseconds operator-(Seconds a_lhs, Milliseconds a_rhs)
{
hkm_assert(a_lhs.get_in<xmcu::Milliseconds>().get() >= a_rhs.get());
return { a_lhs.get_in<xmcu::Milliseconds>() - a_rhs };
}
inline Milliseconds operator-(Milliseconds a_lhs, Seconds a_rhs)
{
hkm_assert(a_lhs.get() >= a_rhs.get_in<xmcu::Milliseconds>().get());
return { a_lhs - a_rhs.get_in<xmcu::Milliseconds>() };
}
inline Seconds operator-(Seconds a_lhs, Seconds a_rhs)
{
hkm_assert(a_lhs.get() >= a_rhs.get());
return { a_lhs.get() - a_rhs.get() };
}
inline Microseconds operator+(Microseconds a_lhs, Microseconds a_rhs)
{
return { a_lhs.get() + a_rhs.get() };
}
inline Microseconds operator+(Microseconds a_lhs, Milliseconds a_rhs)
{
return { a_lhs + a_rhs.get_in<Microseconds>() };
}
inline Microseconds operator+(Milliseconds a_lhs, Microseconds a_rhs)
{
return { a_lhs.get_in<Microseconds>() + a_rhs };
}
inline Milliseconds operator+(Milliseconds a_lhs, Milliseconds a_rhs)
{
return { a_lhs.get() + a_rhs.get() };
}
inline Milliseconds operator+(Seconds a_lhs, Milliseconds a_rhs)
{
return { a_lhs.get_in<xmcu::Milliseconds>() + a_rhs };
}
inline Milliseconds operator+(Milliseconds a_lhs, Seconds a_rhs)
{
return { a_lhs + a_rhs.get_in<xmcu::Milliseconds>() };
}
inline Seconds operator+(Seconds a_lhs, Seconds a_rhs)
{
return { a_lhs.get() + a_rhs.get() };
}
inline Microseconds operator*(Microseconds a_lhs, Microseconds::Value_type a_rhs)
{
return { a_lhs.get() * a_rhs };
}
inline Microseconds operator*(Microseconds::Value_type a_lhs, Microseconds a_rhs)
{
return { a_lhs * a_rhs.get() };
}
inline Milliseconds operator*(Milliseconds a_lhs, Milliseconds::Value_type a_rhs)
{
return { a_lhs.get() * a_rhs };
}
inline Milliseconds operator*(Milliseconds::Value_type a_lhs, Milliseconds a_rhs)
{
return { a_lhs * a_rhs.get() };
}
inline Seconds operator*(Seconds a_lhs, Seconds::Value_type a_rhs)
{
return { a_lhs.get() * a_rhs };
}
inline Seconds operator*(Seconds::Value_type a_lhs, Seconds a_rhs)
{
return { a_lhs * a_rhs.get() };
}
} // namespace xmcu
constexpr inline xmcu::Microseconds operator"" _us(std::uint64_t a_value)
{
return { a_value };
}
constexpr inline xmcu::Milliseconds operator"" _ms(std::uint64_t a_value)
{
return { a_value };
}
constexpr inline xmcu::Seconds operator"" _s(std::uint64_t a_value)
{
return { a_value };
}