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