forked from OpenCyphal-Garage/libcyphal
/
time.hpp
376 lines (321 loc) · 9.05 KB
/
time.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
* Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Concepts and requirements for exchanging time values between libuavcan, media
* layer implementations, and applications.
*/
/** @file */
#ifndef LIBUAVCAN_TIME_HPP_INCLUDED
#define LIBUAVCAN_TIME_HPP_INCLUDED
#include "libuavcan/libuavcan.hpp"
#include "libuavcan/util/math.hpp"
/**
* @namespace libuavcan
* The top-level namespace which contains all types, definitions, and nested
* namespaces for libuavcan.
*/
namespace libuavcan
{
#if LIBUAVCAN_MICROSECOND_SIZE_BYTES == 8
using DefaultMicrosecondSignedType = std::int64_t;
using DefaultMicrosecondUnsignedType = std::uint64_t;
#elif LIBUAVCAN_MICROSECOND_SIZE_BYTES == 4
using DefaultMicrosecondSignedType = std::int32_t;
using DefaultMicrosecondUnsignedType = std::uint32_t;
#elif LIBUAVCAN_MICROSECOND_SIZE_BYTES == 2
using DefaultMicrosecondSignedType = std::int16_t;
using DefaultMicrosecondUnsignedType = std::uint16_t;
#else
# error LIBUAVCAN_MICROSECOND_SIZE_BYTES is not a valid integer size for this library.
#endif
namespace duration
{
/**
* Protected base class for duration values. This provides a common implementation for
* various duration datatypes and enforces two concepts:
*
* -# duration math is saturing – MAX_DURATION + 1 == MAX_DURATION
* -# durations are signed integers – By default 8 byte integers but USecT can be redefined by
* a specialization.
*
* @tparam Type The type of the derived class. This must be an empty type.
* All storage will be provided by this base class.
* @tparam USecT The datatype returned when retrieving durations from
* realizations of this base class. This type must be signed.
*/
template <typename Type, typename USecT = libuavcan::DefaultMicrosecondSignedType>
class Base
{
USecT usec_; /**< Internal storage of the duration value in microseconds. */
protected:
/**
* Non-virtual destructor.
*/
~Base() = default;
Base()
: usec_(0)
{
static_assert(sizeof(Type) == sizeof(USecT),
"The duration abstraction must be the same size as the underlying duration type.");
static_assert(std::is_signed<USecT>::value, "The microsecond type must be signed for durations.");
}
Base(const Base& rhs)
: usec_(rhs.usec_)
{}
/**
* Move constructor takes value from rhs and
* resets rhs to 0.
*/
Base(Base&& rhs)
: usec_(rhs.usec_)
{
rhs.usec_ = 0;
}
public:
/**
* The underlying datatype for microsecond values. This must be signed for duration types.
*/
using MicrosecondType = USecT;
/**
* The specialized type of this base duration type.
*/
using DurationType = Type;
/**
* Get the largest possible number of microseconds this type can store.
*/
static Type getMaximum()
{
return fromMicrosecond(std::numeric_limits<USecT>::max());
}
/**
* Construct an instance of Type from a microsecond value.
*/
static Type fromMicrosecond(USecT us)
{
Type d;
d.usec_ = us;
return d;
}
/**
* Obtain the underlying microsecond value without conversion.
*/
USecT toMicrosecond() const
{
return usec_;
}
/**
* Get the absolute value of the duration as a duration type.
*/
Type getAbs() const
{
return Type::fromMicrosecond(std::abs(usec_));
}
Base& operator=(Base&& rhs)
{
usec_ = rhs.usec_;
rhs.usec_ = 0;
return *this;
}
Base& operator=(const Base& rhs)
{
usec_ = rhs.usec_;
return *this;
}
bool operator==(const Type& r) const
{
return usec_ == r.usec_;
}
bool operator!=(const Type& r) const
{
return !operator==(r);
}
bool operator<(const Type& r) const
{
return usec_ < r.usec_;
}
bool operator>(const Type& r) const
{
return usec_ > r.usec_;
}
bool operator<=(const Type& r) const
{
return usec_ <= r.usec_;
}
bool operator>=(const Type& r) const
{
return usec_ >= r.usec_;
}
Type operator+(const Type& r) const
{
return fromMicrosecond(util::saturating_add(usec_, r.usec_));
}
Type operator-(const Type& r) const
{
return fromMicrosecond(util::saturating_sub(usec_, r.usec_));
}
Type operator-() const
{
if (usec_ == std::numeric_limits<USecT>::max())
{
return fromMicrosecond(std::numeric_limits<USecT>::min() + 1);
}
else
{
return fromMicrosecond(-usec_);
}
}
Type& operator+=(const Type& r)
{
*this = *this + r;
return *static_cast<Type*>(this);
}
Type& operator-=(const Type& r)
{
*this = *this - r;
return *static_cast<Type*>(this);
}
};
/**
* A monotonic duration used by libuavcan.
*/
class LIBUAVCAN_EXPORT Monotonic : public Base<Monotonic>
{};
} // namespace duration
namespace time
{
/**
* Protected base class for time values.
*
* @tparam Type The type of the derived class. This must be an empty type.
* All storage will be provided by this base class.
* @tparam DType The type of duration used for this time type. Time is concrete and duration
* is relative.
* @tparam USecT The datatype returned when retrieving time from
* realizations of this base class. This type must be unsigned.
*/
template <typename Type, typename DType, typename USecT = DefaultMicrosecondUnsignedType>
class Base
{
USecT usec_;
protected:
~Base() {}
Base()
: usec_(0)
{
static_assert(sizeof(Type) == sizeof(USecT),
"The time abstraction must be the same size as the underlying time type.");
static_assert(!std::is_signed<USecT>::value, "Microsecond type must be unsigned for time.");
// Note that this also, somewhat, enforces that the duration type supports the duration "concept".
// It won't be until C++20 that this type can truly enforce this requirement. If you must re-implement
// the concept then remember that Duration math is saturating. It's much safer to just derive
// your duration from libuavcan::time::Base.
static_assert(sizeof(USecT) == sizeof(typename DType::MicrosecondType),
"Microsecond Type must be the same size as the duration type.");
}
Base(const Base& rhs)
: usec_(rhs.usec_)
{}
/**
* Move constructor takes value from rhs and
* resets rhs to 0.
*/
Base(Base&& rhs)
: usec_(rhs.usec_)
{
rhs.usec_ = 0;
}
public:
/**
* The underlying datatype for microsecond values. This must be unsigned for time types.
*/
using MicrosecondType = USecT;
/**
* The specialized type of this base time type.
*/
using DurationType = DType;
/**
* Get the largest possible number of microseconds this type can store.
*/
static Type getMaximum()
{
return fromMicrosecond(std::numeric_limits<USecT>::max());
}
/**
* Construct an instance of Type from a microsecond value.
*/
static Type fromMicrosecond(USecT us)
{
Type t;
t.usec_ = us;
return t;
}
/**
* Obtain the underlying microsecond value without conversion.
*/
USecT toMicrosecond() const
{
return usec_;
}
Base& operator=(Base&& rhs)
{
usec_ = rhs.usec_;
rhs.usec_ = 0;
return *this;
}
Base& operator=(const Base& rhs)
{
usec_ = rhs.usec_;
return *this;
}
bool operator==(const Type& r) const
{
return usec_ == r.usec_;
}
bool operator!=(const Type& r) const
{
return !operator==(r);
}
bool operator<(const Type& r) const
{
return usec_ < r.usec_;
}
bool operator>(const Type& r) const
{
return usec_ > r.usec_;
}
bool operator<=(const Type& r) const
{
return usec_ <= r.usec_;
}
bool operator>=(const Type& r) const
{
return usec_ >= r.usec_;
}
Type operator+(const DType& r) const
{
return fromMicrosecond(libuavcan::util::saturating_add(usec_, r.toMicrosecond()));
}
Type operator-(const DType& r) const
{
return fromMicrosecond(libuavcan::util::saturating_sub(usec_, r.toMicrosecond()));
}
Type& operator+=(const DType& r)
{
*this = *this + r;
return *static_cast<Type*>(this);
}
Type& operator-=(const DType& r)
{
*this = *this - r;
return *static_cast<Type*>(this);
}
};
/**
* A monotonic time value used by libuavcan.
*/
class LIBUAVCAN_EXPORT Monotonic : public Base<Monotonic, duration::Monotonic>
{};
} // namespace time
} // namespace libuavcan
#endif // LIBUAVCAN_TIME_HPP_INCLUDED