Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qdeadlinetimer.cpp
Go to the documentation of this file.
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
8
10
11using namespace std::chrono;
12
13namespace {
14struct TimeReference : std::numeric_limits<qint64>
15{
16 static constexpr qint64 Min = min();
17 static constexpr qint64 Max = max();
18};
19}
20
21template <typename Duration1, typename... Durations>
22static qint64 add_saturate(qint64 t1, Duration1 dur, Durations... extra)
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(t1, v, &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
194
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, ms);
282 }
283}
284
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, seconds{secs}, nanoseconds{nsecs});
317 }
318}
319
363bool 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());
370}
371
395
413{
414 if (isForever())
415 return -1;
416
417 nanoseconds nsecs(remainingTimeNSecs());
418 return ceil<milliseconds>(nsecs).count();
419}
420
430{
431 if (isForever())
432 return -1;
433 qint64 raw = rawRemainingTimeNSecs();
434 return raw < 0 ? 0 : raw;
435}
436
443qint64 QDeadlineTimer::rawRemainingTimeNSecs() const noexcept
444{
445 if (t1 == std::numeric_limits<qint64>::min())
446 return t1; // we'd saturate to this anyway
447
449 qint64 r;
450 if (qSubOverflow(t1, now.t1, &r))
451 return -1; // any negative number is fine
452 return r;
453}
454
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>(ns).count();
484}
485
508{
509 if (isForever())
510 return TimeReference::Max;
511
512 return t1;
513}
514
528void 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(0, milliseconds{msecs});
537}
538
552{
553 type = timerType;
554 t1 = add_saturate(0, seconds{secs}, nanoseconds{nsecs});
555}
556
566{
567 if (dt.isForever())
568 return dt;
569
570 dt.t1 = add_saturate(dt.t1, nanoseconds{nsecs});
571 return dt;
572}
573
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
590 result.t1 = ns.count();
591 result.type = timerType;
592 return result;
593}
594
684{
685 if (dt.isForever())
686 return dt;
687
688 dt.t1 = add_saturate(dt.t1, milliseconds{msecs});
689 return dt;
690}
691
\inmodule QtCore
static QDeadlineTimer current(Qt::TimerType timerType=Qt::CoarseTimer) noexcept
Returns a QDeadlineTimer that is expired but is guaranteed to contain the current time.
void setDeadline(qint64 msecs, Qt::TimerType timerType=Qt::CoarseTimer) noexcept
Sets the deadline for this QDeadlineTimer object to be the msecs absolute time point,...
void setRemainingTime(qint64 msecs, Qt::TimerType type=Qt::CoarseTimer) noexcept
Sets the remaining time for this QDeadlineTimer object to msecs milliseconds from now,...
static QDeadlineTimer addNSecs(QDeadlineTimer dt, qint64 nsecs) noexcept Q_DECL_PURE_FUNCTION
Returns a QDeadlineTimer object whose deadline is extended from dt's deadline by nsecs nanoseconds.
void setPreciseRemainingTime(qint64 secs, qint64 nsecs=0, Qt::TimerType type=Qt::CoarseTimer) noexcept
Sets the remaining time for this QDeadlineTimer object to secs seconds plus nsecs nanoseconds from no...
void setTimerType(Qt::TimerType type)
Changes the timer type for this object to timerType.
qint64 deadline() const noexcept Q_DECL_PURE_FUNCTION
Returns the absolute time point for the deadline stored in QDeadlineTimer object, calculated in milli...
bool hasExpired() const noexcept
Returns true if this QDeadlineTimer object has expired, false if there remains time left.
constexpr bool isForever() const noexcept
Returns true if this QDeadlineTimer object never expires, false otherwise.
constexpr QDeadlineTimer() noexcept=default
qint64 remainingTimeNSecs() const noexcept
Returns the remaining time in this QDeadlineTimer object in nanoseconds.
qint64 remainingTime() const noexcept
Returns the remaining time in this QDeadlineTimer object in milliseconds.
Qt::TimerType timerType() const noexcept
Returns the timer type is active for this object.
qint64 deadlineNSecs() const noexcept Q_DECL_PURE_FUNCTION
Returns the absolute time point for the deadline stored in QDeadlineTimer object, calculated in nanos...
void setPreciseDeadline(qint64 secs, qint64 nsecs=0, Qt::TimerType type=Qt::CoarseTimer) noexcept
Sets the deadline for this QDeadlineTimer object to be secs seconds and nsecs nanoseconds since the r...
Combined button and popup list for selecting options.
constexpr const T & min(const T &a, const T &b)
Definition qnumeric.h:366
TimerType
QDeadlineTimer operator+(QDeadlineTimer dt, qint64 msecs)
static qint64 add_saturate(qint64 t1, Duration1 dur, Durations... extra)
#define QT_IMPL_METATYPE_EXTERN(TYPE)
Definition qmetatype.h:1390
std::enable_if_t< std::is_unsigned_v< T >, bool > qAddOverflow(T v1, T v2, T *r)
Definition qnumeric.h:113
std::enable_if_t< std::is_unsigned_v< T >, bool > qSubOverflow(T v1, T v2, T *r)
Definition qnumeric.h:153
GLsizei const GLfloat * v
[13]
GLboolean r
[2]
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t1
[4]
GLenum type
GLuint64EXT * result
[6]
long long qint64
Definition qtypes.h:60
deadline setRemainingTime(250ms)