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