1 | // Copyright (C) 2016 Intel Corporation. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qdeadlinetimer.h" |
5 | #include "private/qnumeric_p.h" |
6 | |
7 | QT_BEGIN_NAMESPACE |
8 | |
9 | QT_IMPL_METATYPE_EXTERN(QDeadlineTimer) |
10 | |
11 | using namespace std::chrono; |
12 | |
13 | namespace { |
14 | struct TimeReference : std::numeric_limits<qint64> |
15 | { |
16 | static constexpr qint64 Min = min(); |
17 | static constexpr qint64 Max = max(); |
18 | }; |
19 | } |
20 | |
21 | template <typename Duration1, typename... Durations> |
22 | static qint64 add_saturate(qint64 t1, Duration1 dur, Durations... ) |
23 | { |
24 | qint64 v = dur.count(); |
25 | qint64 saturated = std::numeric_limits<qint64>::max(); |
26 | if (v < 0) |
27 | saturated = std::numeric_limits<qint64>::min(); |
28 | |
29 | // convert to nanoseconds with saturation |
30 | using Ratio = std::ratio_divide<typename Duration1::period, nanoseconds::period>; |
31 | static_assert(Ratio::den == 1, "sub-multiples of nanosecond are not supported" ); |
32 | if (qMulOverflow<Ratio::num>(v, &v)) |
33 | return saturated; |
34 | |
35 | qint64 r; |
36 | if (qAddOverflow(v1: t1, v2: v, r: &r)) |
37 | return saturated; |
38 | if constexpr (sizeof...(Durations)) { |
39 | // chain more additions |
40 | return add_saturate(r, extra...); |
41 | } |
42 | return r; |
43 | } |
44 | |
45 | /*! |
46 | \class QDeadlineTimer |
47 | \inmodule QtCore |
48 | \brief The QDeadlineTimer class marks a deadline in the future. |
49 | \since 5.8 |
50 | |
51 | \reentrant |
52 | \ingroup tools |
53 | |
54 | \compares strong |
55 | |
56 | The QDeadlineTimer class is usually used to calculate future deadlines and |
57 | verify whether the deadline has expired. QDeadlineTimer can also be used |
58 | for deadlines without expiration ("forever"). It forms a counterpart to |
59 | QElapsedTimer, which calculates how much time has elapsed since |
60 | QElapsedTimer::start() was called. |
61 | |
62 | QDeadlineTimer provides a more convenient API compared to |
63 | QElapsedTimer::hasExpired(). |
64 | |
65 | The typical use-case for the class is to create a QDeadlineTimer before the |
66 | operation in question is started, and then use remainingTime() or |
67 | hasExpired() to determine whether to continue trying the operation. |
68 | QDeadlineTimer objects can be passed to functions being called to execute |
69 | this operation so they know how long to still operate. |
70 | |
71 | \snippet code/src_corelib_kernel_qdeadlinetimer.cpp 0 |
72 | |
73 | Many QDeadlineTimer functions deal with time out values, which all are |
74 | measured in milliseconds. There are two special values, the same as many |
75 | other Qt functions named \c{waitFor} or similar: |
76 | |
77 | \list |
78 | \li 0: no time left, expired |
79 | \li -1: infinite time left, timer never expires |
80 | \endlist |
81 | |
82 | \section1 Reference Clocks |
83 | |
84 | QDeadlineTimer will use the same clock as QElapsedTimer (see |
85 | QElapsedTimer::clockType() and QElapsedTimer::isMonotonic()). |
86 | |
87 | \section1 Timer types |
88 | |
89 | Like QTimer and QChronoTimer, QDeadlineTimer can select among |
90 | different levels of coarseness on the timers. You can select |
91 | precise timing by passing Qt::PreciseTimer to the functions that |
92 | set of change the timer, or you can select coarse timing by passing |
93 | Qt::CoarseTimer. Qt::VeryCoarseTimer is currently interpreted the same |
94 | way as Qt::CoarseTimer. |
95 | |
96 | This feature is dependent on support from the operating system: if the OS |
97 | does not support a coarse timer functionality, then QDeadlineTimer will |
98 | behave like Qt::PreciseTimer was passed. |
99 | |
100 | QDeadlineTimer defaults to Qt::CoarseTimer because on operating systems |
101 | that do support coarse timing, making timing calls to that clock source is |
102 | often much more efficient. The level of coarseness depends on the |
103 | operating system, but should be in the order of a couple of milliseconds. |
104 | |
105 | \section1 \c{std::chrono} Compatibility |
106 | |
107 | QDeadlineTimer is compatible with the \c{std::chrono} API from C++11 and |
108 | can be constructed from or compared to both \c{std::chrono::duration} and |
109 | \c{std::chrono::time_point} objects. In addition, it is fully compatible |
110 | with the time literals from C++14, which allow one to write code as: |
111 | |
112 | \snippet code/src_corelib_kernel_qdeadlinetimer.cpp 1 |
113 | |
114 | As can be seen in the example above, QDeadlineTimer offers a templated |
115 | version of remainingTime() and deadline() that can be used to return |
116 | \c{std::chrono} objects. |
117 | |
118 | Note that comparing to \c{time_point} is not as efficient as comparing to |
119 | \c{duration}, since QDeadlineTimer may need to convert from its own |
120 | internal clock source to the clock source used by the \c{time_point} object. |
121 | Also note that, due to this conversion, the deadlines will not be precise, |
122 | so the following code is not expected to compare equally: |
123 | |
124 | \snippet code/src_corelib_kernel_qdeadlinetimer.cpp 2 |
125 | |
126 | \sa QTime, QChronoTimer, QDeadlineTimer, Qt::TimerType |
127 | */ |
128 | |
129 | /*! |
130 | \enum QDeadlineTimer::ForeverConstant |
131 | |
132 | \value Forever Used when creating a QDeadlineTimer to indicate the |
133 | deadline should not expire |
134 | */ |
135 | |
136 | /*! |
137 | \fn QDeadlineTimer::QDeadlineTimer() |
138 | \fn QDeadlineTimer::QDeadlineTimer(Qt::TimerType timerType) |
139 | |
140 | Constructs an expired QDeadlineTimer object. For this object, |
141 | remainingTime() will return 0. If \a timerType is not set, then the object |
142 | will use the \l{Qt::CoarseTimer}{coarse} \l{QDeadlineTimer#Timer types}{timer type}. |
143 | |
144 | The timer type \a timerType may be ignored, since the timer is already |
145 | expired. Similarly, for optimization purposes, this function will not |
146 | attempt to obtain the current time and will use a value known to be in the |
147 | past. Therefore, deadline() may return an unexpected value and this object |
148 | cannot be used in calculation of how long it is overdue. If that |
149 | functionality is required, use QDeadlineTimer::current(). |
150 | |
151 | \sa hasExpired(), remainingTime(), Qt::TimerType, current() |
152 | */ |
153 | |
154 | /*! |
155 | \fn QDeadlineTimer::QDeadlineTimer(ForeverConstant, Qt::TimerType timerType) |
156 | |
157 | QDeadlineTimer objects created with ForeverConstant never expire. |
158 | For such objects, remainingTime() will return -1, deadline() will return the |
159 | maximum value, and isForever() will return true. |
160 | |
161 | The timer type \a timerType may be ignored, since the timer will never |
162 | expire. |
163 | |
164 | \sa ForeverConstant, hasExpired(), isForever(), remainingTime(), timerType() |
165 | */ |
166 | |
167 | /*! |
168 | Constructs a QDeadlineTimer object with an expiry time of \a msecs msecs |
169 | from the moment of the creation of this object, if msecs is positive. If \a |
170 | msecs is zero, this QDeadlineTimer will be marked as expired, causing |
171 | remainingTime() to return zero and deadline() to return an indeterminate |
172 | time point in the past. If \a msecs is negative, the timer will be set to never |
173 | expire, causing remainingTime() to return -1 and deadline() to return the |
174 | maximum value. |
175 | |
176 | The QDeadlineTimer object will be constructed with the specified timer \a type. |
177 | |
178 | For optimization purposes, if \a msecs is zero, this function may skip |
179 | obtaining the current time and may instead use a value known to be in the |
180 | past. If that happens, deadline() may return an unexpected value and this |
181 | object cannot be used in calculation of how long it is overdue. If that |
182 | functionality is required, use QDeadlineTimer::current() and add time to |
183 | it. |
184 | |
185 | \note Prior to Qt 6.6, the only value that caused the timer to never expire |
186 | was -1. |
187 | |
188 | \sa hasExpired(), isForever(), remainingTime(), setRemainingTime() |
189 | */ |
190 | QDeadlineTimer::QDeadlineTimer(qint64 msecs, Qt::TimerType type) noexcept |
191 | { |
192 | setRemainingTime(msecs, type); |
193 | } |
194 | |
195 | /*! |
196 | \fn template <class Clock, class Duration> QDeadlineTimer::QDeadlineTimer(std::chrono::time_point<Clock, Duration> deadline, Qt::TimerType type) |
197 | |
198 | Constructs a QDeadlineTimer object with a deadline at \a deadline time |
199 | point, converting from the clock source \c{Clock} to Qt's internal clock |
200 | source (see QElapsedTimer::clockType()). |
201 | |
202 | If \a deadline is in the past, this QDeadlineTimer object is set to |
203 | expired, whereas if \a deadline is equal to \c{Duration::max()}, then this |
204 | object is set to never expire. |
205 | |
206 | The QDeadlineTimer object will be constructed with the specified timer \a type. |
207 | |
208 | \sa hasExpired(), isForever(), remainingTime(), setDeadline() |
209 | */ |
210 | |
211 | /*! |
212 | \fn template <class Rep, class Period> QDeadlineTimer::QDeadlineTimer(std::chrono::duration<Rep, Period> remaining, Qt::TimerType type) |
213 | |
214 | Constructs a QDeadlineTimer object with a remaining time of \a remaining. |
215 | If \a remaining is zero or negative, this QDeadlineTimer object will be |
216 | mark as expired, whereas if \a remaining is equal to \c{duration::max()}, |
217 | the object will be set to never expire. |
218 | |
219 | The QDeadlineTimer object will be constructed with the specified timer \a type. |
220 | |
221 | This constructor can be used with C++14's user-defined literals for time, such as in: |
222 | |
223 | \snippet code/src_corelib_kernel_qdeadlinetimer.cpp 3 |
224 | |
225 | For optimization purposes, if \a remaining is zero or negative, this |
226 | function may skip obtaining the current time and may instead use a value |
227 | known to be in the past. If that happens, deadline() may return an |
228 | unexpected value and this object cannot be used in calculation of how long |
229 | it is overdue. If that functionality is required, use |
230 | QDeadlineTimer::current() and add time to it. |
231 | |
232 | \sa hasExpired(), isForever(), remainingTime(), setRemainingTime() |
233 | */ |
234 | |
235 | /*! |
236 | \fn template <class Clock, class Duration> void QDeadlineTimer::setDeadline(std::chrono::time_point<Clock, Duration> deadline, Qt::TimerType type) |
237 | |
238 | Sets this QDeadlineTimer to the deadline marked by \a deadline time |
239 | point, converting from the clock source \c{Clock} to Qt's internal clock |
240 | source (see QElapsedTimer::clockType()). |
241 | |
242 | If \a deadline is in the past, this QDeadlineTimer object is set to |
243 | expired, whereas if \a deadline is equal to \c{Duration::max()}, then this |
244 | object is set to never expire. |
245 | |
246 | The timer type for this QDeadlineTimer object will be set to the specified \a type. |
247 | |
248 | \sa hasExpired(), isForever(), remainingTime(), |
249 | */ |
250 | |
251 | /*! |
252 | Sets the remaining time for this QDeadlineTimer object to \a msecs |
253 | milliseconds from now, if \a msecs has a positive value. If \a msecs is |
254 | zero, this QDeadlineTimer object will be marked as expired, whereas a |
255 | negative value will set it to never expire. |
256 | |
257 | For optimization purposes, if \a msecs is zero, this function may skip |
258 | obtaining the current time and may instead use a value known to be in the |
259 | past. If that happens, deadline() may return an unexpected value and this |
260 | object cannot be used in calculation of how long it is overdue. If that |
261 | functionality is required, use QDeadlineTimer::current() and add time to |
262 | it. |
263 | |
264 | The timer type for this QDeadlineTimer object will be set to the specified \a timerType. |
265 | |
266 | \note Prior to Qt 6.6, the only value that caused the timer to never expire |
267 | was -1. |
268 | |
269 | \sa setPreciseRemainingTime(), hasExpired(), isForever(), remainingTime() |
270 | */ |
271 | void QDeadlineTimer::setRemainingTime(qint64 msecs, Qt::TimerType timerType) noexcept |
272 | { |
273 | if (msecs < 0) { |
274 | *this = QDeadlineTimer(Forever, timerType); |
275 | } else if (msecs == 0) { |
276 | *this = QDeadlineTimer(timerType); |
277 | t1 = std::numeric_limits<qint64>::min(); |
278 | } else { |
279 | *this = current(timerType); |
280 | milliseconds ms(msecs); |
281 | t1 = add_saturate(t1, dur: ms); |
282 | } |
283 | } |
284 | |
285 | /*! |
286 | Sets the remaining time for this QDeadlineTimer object to \a secs seconds |
287 | plus \a nsecs nanoseconds from now, if \a secs has a positive value. If \a |
288 | secs is negative, this QDeadlineTimer will be set it to never expire (this |
289 | behavior does not apply to \a nsecs). If both parameters are zero, this |
290 | QDeadlineTimer will be marked as expired. |
291 | |
292 | For optimization purposes, if both \a secs and \a nsecs are zero, this |
293 | function may skip obtaining the current time and may instead use a value |
294 | known to be in the past. If that happens, deadline() may return an |
295 | unexpected value and this object cannot be used in calculation of how long |
296 | it is overdue. If that functionality is required, use |
297 | QDeadlineTimer::current() and add time to it. |
298 | |
299 | The timer type for this QDeadlineTimer object will be set to the specified |
300 | \a timerType. |
301 | |
302 | \note Prior to Qt 6.6, the only condition that caused the timer to never |
303 | expire was when \a secs was -1. |
304 | |
305 | \sa setRemainingTime(), hasExpired(), isForever(), remainingTime() |
306 | */ |
307 | void QDeadlineTimer::setPreciseRemainingTime(qint64 secs, qint64 nsecs, Qt::TimerType timerType) noexcept |
308 | { |
309 | if (secs < 0) { |
310 | *this = QDeadlineTimer(Forever, timerType); |
311 | } else if (secs == 0 && nsecs == 0) { |
312 | *this = QDeadlineTimer(timerType); |
313 | t1 = std::numeric_limits<qint64>::min(); |
314 | } else { |
315 | *this = current(timerType); |
316 | t1 = add_saturate(t1, dur: seconds{secs}, extra: nanoseconds{nsecs}); |
317 | } |
318 | } |
319 | |
320 | /*! |
321 | \overload |
322 | \fn template <class Rep, class Period> void QDeadlineTimer::setRemainingTime(std::chrono::duration<Rep, Period> remaining, Qt::TimerType type) |
323 | |
324 | Sets the remaining time for this QDeadlineTimer object to \a remaining. If |
325 | \a remaining is zero or negative, this QDeadlineTimer object will be mark |
326 | as expired, whereas if \a remaining is equal to \c{duration::max()}, the |
327 | object will be set to never expire. |
328 | |
329 | The timer type for this QDeadlineTimer object will be set to the specified \a type. |
330 | |
331 | This function can be used with C++14's user-defined literals for time, such as in: |
332 | |
333 | \snippet code/src_corelib_kernel_qdeadlinetimer.cpp 4 |
334 | |
335 | \note Qt detects the necessary C++14 compiler support by way of the feature |
336 | test recommendations from |
337 | \l{https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations} |
338 | {C++ Committee's Standing Document 6}. |
339 | |
340 | \sa setDeadline(), remainingTime(), hasExpired(), isForever() |
341 | */ |
342 | |
343 | /*! |
344 | \fn bool QDeadlineTimer::isForever() const |
345 | |
346 | Returns true if this QDeadlineTimer object never expires, false otherwise. |
347 | For timers that never expire, remainingTime() always returns -1 and |
348 | deadline() returns the maximum value. |
349 | |
350 | \sa ForeverConstant, hasExpired(), remainingTime() |
351 | */ |
352 | |
353 | /*! |
354 | Returns true if this QDeadlineTimer object has expired, false if there |
355 | remains time left. For objects that have expired, remainingTime() will |
356 | return zero and deadline() will return a time point in the past. |
357 | |
358 | QDeadlineTimer objects created with the \l {ForeverConstant} never expire |
359 | and this function always returns false for them. |
360 | |
361 | \sa isForever(), remainingTime() |
362 | */ |
363 | bool QDeadlineTimer::hasExpired() const noexcept |
364 | { |
365 | if (isForever()) |
366 | return false; |
367 | if (t1 == std::numeric_limits<qint64>::min()) |
368 | return true; |
369 | return *this <= current(timerType: timerType()); |
370 | } |
371 | |
372 | /*! |
373 | \fn Qt::TimerType QDeadlineTimer::timerType() const |
374 | |
375 | Returns the timer type is active for this object. |
376 | |
377 | \sa setTimerType() |
378 | */ |
379 | |
380 | /*! |
381 | Changes the timer type for this object to \a timerType. |
382 | |
383 | The behavior for each possible value of \a timerType is operating-system |
384 | dependent. Qt::PreciseTimer will use the most precise timer that Qt can |
385 | find, with resolution of 1 millisecond or better, whereas QDeadlineTimer |
386 | will try to use a more coarse timer for Qt::CoarseTimer and |
387 | Qt::VeryCoarseTimer. |
388 | |
389 | \sa Qt::TimerType |
390 | */ |
391 | void QDeadlineTimer::setTimerType(Qt::TimerType timerType) |
392 | { |
393 | type = timerType; |
394 | } |
395 | |
396 | /*! |
397 | Returns the remaining time in this QDeadlineTimer object in milliseconds. |
398 | If the timer has already expired, this function will return zero and it is |
399 | not possible to obtain the amount of time overdue with this function (to do |
400 | that, see deadline()). If the timer was set to never expire, this function |
401 | returns -1. |
402 | |
403 | This function is suitable for use in Qt APIs that take a millisecond |
404 | timeout, such as the many \l QIODevice \c waitFor functions or the timed |
405 | lock functions in \l QMutex, \l QWaitCondition, \l QSemaphore, or |
406 | \l QReadWriteLock. For example: |
407 | |
408 | \snippet code/src_corelib_kernel_qdeadlinetimer.cpp 5 |
409 | |
410 | \sa remainingTimeNSecs(), isForever(), hasExpired() |
411 | */ |
412 | qint64 QDeadlineTimer::remainingTime() const noexcept |
413 | { |
414 | if (isForever()) |
415 | return -1; |
416 | |
417 | nanoseconds nsecs(remainingTimeNSecs()); |
418 | return ceil<milliseconds>(d: nsecs).count(); |
419 | } |
420 | |
421 | /*! |
422 | Returns the remaining time in this QDeadlineTimer object in nanoseconds. If |
423 | the timer has already expired, this function will return zero and it is not |
424 | possible to obtain the amount of time overdue with this function. If the |
425 | timer was set to never expire, this function returns -1. |
426 | |
427 | \sa remainingTime(), isForever(), hasExpired() |
428 | */ |
429 | qint64 QDeadlineTimer::remainingTimeNSecs() const noexcept |
430 | { |
431 | if (isForever()) |
432 | return -1; |
433 | qint64 raw = rawRemainingTimeNSecs(); |
434 | return raw < 0 ? 0 : raw; |
435 | } |
436 | |
437 | /*! |
438 | \internal |
439 | Same as remainingTimeNSecs, but may return negative remaining times. Does |
440 | not deal with Forever. In case of underflow, which is only possible if the |
441 | timer has expired, an arbitrary negative value is returned. |
442 | */ |
443 | qint64 QDeadlineTimer::rawRemainingTimeNSecs() const noexcept |
444 | { |
445 | if (t1 == std::numeric_limits<qint64>::min()) |
446 | return t1; // we'd saturate to this anyway |
447 | |
448 | QDeadlineTimer now = current(timerType: timerType()); |
449 | qint64 r; |
450 | if (qSubOverflow(v1: t1, v2: now.t1, r: &r)) |
451 | return -1; // any negative number is fine |
452 | return r; |
453 | } |
454 | |
455 | /*! |
456 | Returns the absolute time point for the deadline stored in QDeadlineTimer |
457 | object, calculated in milliseconds relative to the reference clock, the |
458 | same as QElapsedTimer::msecsSinceReference(). The value will be in the past |
459 | if this QDeadlineTimer has expired. |
460 | |
461 | If this QDeadlineTimer never expires, this function returns |
462 | \c{std::numeric_limits<qint64>::max()}. |
463 | |
464 | This function can be used to calculate the amount of time a timer is |
465 | overdue, by subtracting QDeadlineTimer::current() or |
466 | QElapsedTimer::msecsSinceReference(), as in the following example: |
467 | |
468 | \snippet code/src_corelib_kernel_qdeadlinetimer.cpp 6 |
469 | |
470 | \note Timers that were created as expired have an indetermine time point in |
471 | the past as their deadline, so the above calculation may not work. |
472 | |
473 | \sa remainingTime(), deadlineNSecs(), setDeadline() |
474 | */ |
475 | qint64 QDeadlineTimer::deadline() const noexcept |
476 | { |
477 | if (isForever()) |
478 | return TimeReference::Max; |
479 | if (t1 == TimeReference::Min) |
480 | return t1; |
481 | |
482 | nanoseconds ns(t1); |
483 | return duration_cast<milliseconds>(d: ns).count(); |
484 | } |
485 | |
486 | /*! |
487 | Returns the absolute time point for the deadline stored in QDeadlineTimer |
488 | object, calculated in nanoseconds relative to the reference clock, the |
489 | same as QElapsedTimer::msecsSinceReference(). The value will be in the past |
490 | if this QDeadlineTimer has expired. |
491 | |
492 | If this QDeadlineTimer never expires or the number of nanoseconds until the |
493 | deadline can't be accommodated in the return type, this function returns |
494 | \c{std::numeric_limits<qint64>::max()}. |
495 | |
496 | This function can be used to calculate the amount of time a timer is |
497 | overdue, by subtracting QDeadlineTimer::current(), as in the following |
498 | example: |
499 | |
500 | \snippet code/src_corelib_kernel_qdeadlinetimer.cpp 7 |
501 | |
502 | \note Timers that were created as expired have an indetermine time point in |
503 | the past as their deadline, so the above calculation may not work. |
504 | |
505 | \sa remainingTime(), deadlineNSecs() |
506 | */ |
507 | qint64 QDeadlineTimer::deadlineNSecs() const noexcept |
508 | { |
509 | if (isForever()) |
510 | return TimeReference::Max; |
511 | |
512 | return t1; |
513 | } |
514 | |
515 | /*! |
516 | Sets the deadline for this QDeadlineTimer object to be the \a msecs |
517 | absolute time point, counted in milliseconds since the reference clock (the |
518 | same as QElapsedTimer::msecsSinceReference()), and the timer type to \a |
519 | timerType. If the value is in the past, this QDeadlineTimer will be marked |
520 | as expired. |
521 | |
522 | If \a msecs is \c{std::numeric_limits<qint64>::max()} or the deadline is |
523 | beyond a representable point in the future, this QDeadlineTimer will be set |
524 | to never expire. |
525 | |
526 | \sa setPreciseDeadline(), deadline(), deadlineNSecs(), setRemainingTime() |
527 | */ |
528 | void QDeadlineTimer::setDeadline(qint64 msecs, Qt::TimerType timerType) noexcept |
529 | { |
530 | if (msecs == TimeReference::Max) { |
531 | *this = QDeadlineTimer(Forever, timerType); |
532 | return; |
533 | } |
534 | |
535 | type = timerType; |
536 | t1 = add_saturate(t1: 0, dur: milliseconds{msecs}); |
537 | } |
538 | |
539 | /*! |
540 | Sets the deadline for this QDeadlineTimer object to be \a secs seconds and |
541 | \a nsecs nanoseconds since the reference clock epoch (the same as |
542 | QElapsedTimer::msecsSinceReference()), and the timer type to \a timerType. |
543 | If the value is in the past, this QDeadlineTimer will be marked as expired. |
544 | |
545 | If \a secs or \a nsecs is \c{std::numeric_limits<qint64>::max()}, this |
546 | QDeadlineTimer will be set to never expire. If \a nsecs is more than 1 |
547 | billion nanoseconds (1 second), then \a secs will be adjusted accordingly. |
548 | |
549 | \sa setDeadline(), deadline(), deadlineNSecs(), setRemainingTime() |
550 | */ |
551 | void QDeadlineTimer::setPreciseDeadline(qint64 secs, qint64 nsecs, Qt::TimerType timerType) noexcept |
552 | { |
553 | type = timerType; |
554 | t1 = add_saturate(t1: 0, dur: seconds{secs}, extra: nanoseconds{nsecs}); |
555 | } |
556 | |
557 | /*! |
558 | Returns a QDeadlineTimer object whose deadline is extended from \a dt's |
559 | deadline by \a nsecs nanoseconds. If \a dt was set to never expire, this |
560 | function returns a QDeadlineTimer that will not expire either. |
561 | |
562 | \note if \a dt was created as expired, its deadline is indeterminate and |
563 | adding an amount of time may or may not cause it to become unexpired. |
564 | */ |
565 | QDeadlineTimer QDeadlineTimer::addNSecs(QDeadlineTimer dt, qint64 nsecs) noexcept |
566 | { |
567 | if (dt.isForever()) |
568 | return dt; |
569 | |
570 | dt.t1 = add_saturate(t1: dt.t1, dur: nanoseconds{nsecs}); |
571 | return dt; |
572 | } |
573 | |
574 | /*! |
575 | \fn QDeadlineTimer QDeadlineTimer::current(Qt::TimerType timerType) |
576 | |
577 | Returns a QDeadlineTimer that is expired but is guaranteed to contain the |
578 | current time. Objects created by this function can participate in the |
579 | calculation of how long a timer is overdue, using the deadline() function. |
580 | |
581 | The QDeadlineTimer object will be constructed with the specified \a timerType. |
582 | */ |
583 | QDeadlineTimer QDeadlineTimer::current(Qt::TimerType timerType) noexcept |
584 | { |
585 | // ensure we get nanoseconds; this will work so long as steady_clock's |
586 | // time_point isn't of finer resolution (picoseconds) |
587 | std::chrono::nanoseconds ns = std::chrono::steady_clock::now().time_since_epoch(); |
588 | |
589 | QDeadlineTimer result; |
590 | result.t1 = ns.count(); |
591 | result.type = timerType; |
592 | return result; |
593 | } |
594 | |
595 | /*! |
596 | \fn bool QDeadlineTimer::operator==(const QDeadlineTimer &lhs, const QDeadlineTimer &rhs) |
597 | |
598 | Returns true if the deadline on \a lhs and the deadline in \a rhs are the |
599 | same, false otherwise. The timer type used to create the two deadlines is |
600 | ignored. This function is equivalent to: |
601 | |
602 | \snippet code/src_corelib_kernel_qdeadlinetimer.cpp 8 |
603 | |
604 | \note comparing QDeadlineTimer objects with different timer types is |
605 | not supported and may result in unpredictable behavior. |
606 | */ |
607 | |
608 | /*! |
609 | \fn bool QDeadlineTimer::operator!=(const QDeadlineTimer &lhs, const QDeadlineTimer &rhs) |
610 | |
611 | Returns true if the deadline on \a lhs and the deadline in \a rhs are |
612 | different, false otherwise. The timer type used to create the two deadlines |
613 | is ignored. This function is equivalent to: |
614 | |
615 | \snippet code/src_corelib_kernel_qdeadlinetimer.cpp 9 |
616 | |
617 | \note comparing QDeadlineTimer objects with different timer types is |
618 | not supported and may result in unpredictable behavior. |
619 | */ |
620 | |
621 | /*! |
622 | \fn bool QDeadlineTimer::operator<(const QDeadlineTimer &lhs, const QDeadlineTimer &rhs) |
623 | |
624 | Returns true if the deadline on \a lhs is earlier than the deadline in \a |
625 | rhs, false otherwise. The timer type used to create the two deadlines is |
626 | ignored. This function is equivalent to: |
627 | |
628 | \snippet code/src_corelib_kernel_qdeadlinetimer.cpp 10 |
629 | |
630 | \note comparing QDeadlineTimer objects with different timer types is |
631 | not supported and may result in unpredictable behavior. |
632 | */ |
633 | |
634 | /*! |
635 | \fn bool QDeadlineTimer::operator<=(const QDeadlineTimer &lhs, const QDeadlineTimer &rhs) |
636 | |
637 | Returns true if the deadline on \a lhs is earlier than or the same as the |
638 | deadline in \a rhs, false otherwise. The timer type used to create the two |
639 | deadlines is ignored. This function is equivalent to: |
640 | |
641 | \snippet code/src_corelib_kernel_qdeadlinetimer.cpp 11 |
642 | |
643 | \note comparing QDeadlineTimer objects with different timer types is |
644 | not supported and may result in unpredictable behavior. |
645 | */ |
646 | |
647 | /*! |
648 | \fn bool QDeadlineTimer::operator>(const QDeadlineTimer &lhs, const QDeadlineTimer &rhs) |
649 | |
650 | Returns true if the deadline on \a lhs is later than the deadline in \a |
651 | rhs, false otherwise. The timer type used to create the two deadlines is |
652 | ignored. This function is equivalent to: |
653 | |
654 | \snippet code/src_corelib_kernel_qdeadlinetimer.cpp 12 |
655 | |
656 | \note comparing QDeadlineTimer objects with different timer types is |
657 | not supported and may result in unpredictable behavior. |
658 | */ |
659 | |
660 | /*! |
661 | \fn bool QDeadlineTimer::operator>=(const QDeadlineTimer &lhs, const QDeadlineTimer &rhs) |
662 | |
663 | Returns true if the deadline on \a lhs is later than or the same as the |
664 | deadline in \a rhs, false otherwise. The timer type used to create the two |
665 | deadlines is ignored. This function is equivalent to: |
666 | |
667 | \snippet code/src_corelib_kernel_qdeadlinetimer.cpp 13 |
668 | |
669 | \note comparing QDeadlineTimer objects with different timer types is |
670 | not supported and may result in unpredictable behavior. |
671 | */ |
672 | |
673 | /*! |
674 | \fn QDeadlineTimer QDeadlineTimer::operator+(QDeadlineTimer dt, qint64 msecs) |
675 | |
676 | Returns a QDeadlineTimer object whose deadline is \a msecs later than the |
677 | deadline stored in \a dt. If \a dt is set to never expire, this function |
678 | returns a QDeadlineTimer that does not expire either. |
679 | |
680 | To add times of precision greater than 1 millisecond, use addNSecs(). |
681 | */ |
682 | |
683 | QDeadlineTimer operator+(QDeadlineTimer dt, qint64 msecs) |
684 | { |
685 | if (dt.isForever()) |
686 | return dt; |
687 | |
688 | dt.t1 = add_saturate(t1: dt.t1, dur: milliseconds{msecs}); |
689 | return dt; |
690 | } |
691 | |
692 | /*! |
693 | \fn QDeadlineTimer QDeadlineTimer::operator+(qint64 msecs, QDeadlineTimer dt) |
694 | |
695 | Returns a QDeadlineTimer object whose deadline is \a msecs later than the |
696 | deadline stored in \a dt. If \a dt is set to never expire, this function |
697 | returns a QDeadlineTimer that does not expire either. |
698 | |
699 | To add times of precision greater than 1 millisecond, use addNSecs(). |
700 | */ |
701 | |
702 | /*! |
703 | \fn QDeadlineTimer QDeadlineTimer::operator-(QDeadlineTimer dt, qint64 msecs) |
704 | |
705 | Returns a QDeadlineTimer object whose deadline is \a msecs before the |
706 | deadline stored in \a dt. If \a dt is set to never expire, this function |
707 | returns a QDeadlineTimer that does not expire either. |
708 | |
709 | To subtract times of precision greater than 1 millisecond, use addNSecs(). |
710 | */ |
711 | |
712 | /*! |
713 | \fn QDeadlineTimer &QDeadlineTimer::operator+=(qint64 msecs) |
714 | |
715 | Extends this QDeadlineTimer object by \a msecs milliseconds and returns |
716 | itself. If this object is set to never expire, this function does nothing. |
717 | |
718 | To add times of precision greater than 1 millisecond, use addNSecs(). |
719 | */ |
720 | |
721 | /*! |
722 | \fn QDeadlineTimer &QDeadlineTimer::operator-=(qint64 msecs) |
723 | |
724 | Shortens this QDeadlineTimer object by \a msecs milliseconds and returns |
725 | itself. If this object is set to never expire, this function does nothing. |
726 | |
727 | To subtract times of precision greater than 1 millisecond, use addNSecs(). |
728 | */ |
729 | |
730 | /*! |
731 | \fn void QDeadlineTimer::swap(QDeadlineTimer &other) |
732 | |
733 | Swaps this deadline timer with the \a other deadline timer. |
734 | */ |
735 | |
736 | /*! |
737 | \fn template <class Clock, class Duration> QDeadlineTimer & QDeadlineTimer::operator=(std::chrono::time_point<Clock, Duration> deadline_) |
738 | |
739 | Assigns \a deadline_ to this deadline timer. |
740 | */ |
741 | |
742 | /*! |
743 | \fn template <class Rep, class Period> QDeadlineTimer & QDeadlineTimer::operator=(std::chrono::duration<Rep, Period> remaining) |
744 | |
745 | Sets this deadline timer to the \a remaining time. |
746 | */ |
747 | |
748 | /*! |
749 | \fn std::chrono::nanoseconds QDeadlineTimer::remainingTimeAsDuration() const |
750 | |
751 | Returns the time remaining before the deadline. |
752 | */ |
753 | |
754 | QT_END_NAMESPACE |
755 | |