4#ifndef QDEADLINETIMER_H
5#define QDEADLINETIMER_H
7#include <QtCore/qelapsedtimer.h>
8#include <QtCore/qmetatype.h>
9#include <QtCore/qnamespace.h>
32 :
t1((std::numeric_limits<qint64>::max)()),
type(type_) {}
39 {
return t1 == (std::numeric_limits<qint64>::max)(); }
40 bool hasExpired() const noexcept;
42 Qt::TimerType timerType() const noexcept
46 qint64 remainingTime() const noexcept;
47 qint64 remainingTimeNSecs() const noexcept;
49 void setPreciseRemainingTime(
qint64 secs,
qint64 nsecs = 0,
50 Qt::TimerType
type =
Qt::CoarseTimer) noexcept;
54 void setDeadline(
qint64 msecs,
Qt::TimerType timerType =
Qt::CoarseTimer) noexcept;
56 Qt::TimerType
type =
Qt::CoarseTimer) noexcept;
63 {
return dt + msecs; }
65 {
return dt + (-msecs); }
67 {
return (dt1.deadlineNSecs() - dt2.deadlineNSecs()) / (1000 * 1000); }
69 { *
this = *
this + msecs;
return *
this; }
71 { *
this = *
this + (-msecs);
return *
this; }
73 template <
class Clock,
class Duration =
typename Clock::duration>
76 { setDeadline(deadline_, type_); }
77 template <
class Clock,
class Duration =
typename Clock::duration>
79 { setDeadline(deadline_);
return *
this; }
81 template <
class Clock,
class Duration =
typename Clock::duration>
82 void setDeadline(std::chrono::time_point<Clock, Duration> tp,
85 template <
class Clock,
class Duration =
typename Clock::duration>
86 std::chrono::time_point<Clock, Duration>
deadline()
const;
88 template <
class Rep,
class Period>
93 template <
class Rep,
class Period>
97 template <
class Rep,
class Period>
100 using namespace std::chrono;
101 if (remaining == remaining.max())
104 setPreciseRemainingTime(0, ceil<nanoseconds>(remaining).
count(), type_);
110 return std::chrono::nanoseconds::max();
111 qint64 nsecs = rawRemainingTimeNSecs();
113 return std::chrono::nanoseconds::zero();
114 return std::chrono::nanoseconds(nsecs);
117 template <
class Rep,
class Period>
120 template <
class Rep,
class Period>
122 {
return dt +
value; }
123 template <
class Rep,
class Period>
125 {
return dt = dt +
value; }
131 return lhs.t1 == rhs.t1;
141#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
146 qint64 rawRemainingTimeNSecs() const noexcept;
149template<class Clock, class Duration>
152 using namespace std::chrono;
153 if constexpr (std::is_same_v<Clock, steady_clock>) {
155 return time_point<Clock, Duration>(
val);
157 auto val = nanoseconds(rawRemainingTimeNSecs()) + Clock::now();
158 return time_point_cast<Duration>(
val);
162template<
class Clock,
class Duration>
165 using namespace std::chrono;
166 if (tp == tp.max()) {
169 }
else if constexpr (std::is_same_v<Clock, steady_clock>) {
170 setPreciseDeadline(0,
171 duration_cast<nanoseconds>(tp.time_since_epoch()).count(),
174 setPreciseRemainingTime(0, duration_cast<nanoseconds>(tp - Clock::now()).
count(), type_);
void setDeadline(qint64 msecs, Qt::TimerType timerType=Qt::CoarseTimer) noexcept
Sets the deadline for this QDeadlineTimer object to be the msecs absolute time point,...
friend QDeadlineTimer operator+(std::chrono::duration< Rep, Period > value, QDeadlineTimer dt)
QDeadlineTimer(std::chrono::duration< Rep, Period > remaining, Qt::TimerType type_=Qt::CoarseTimer)
Constructs a QDeadlineTimer object with a remaining time of remaining.
friend QDeadlineTimer operator+=(QDeadlineTimer &dt, std::chrono::duration< Rep, Period > value)
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.
QDeadlineTimer & operator=(std::chrono::duration< Rep, Period > remaining)
Sets this deadline timer to the remaining time.
QDeadlineTimer(std::chrono::time_point< Clock, Duration > deadline_, Qt::TimerType type_=Qt::CoarseTimer)
Constructs a QDeadlineTimer object with a deadline at deadline time point, converting from the clock ...
ForeverConstant
\value Forever Used when creating a QDeadlineTimer to indicate the deadline should not expire
constexpr QDeadlineTimer(ForeverConstant, Qt::TimerType type_=Qt::CoarseTimer) noexcept
QDeadlineTimer objects created with ForeverConstant never expire.
QDeadlineTimer & operator=(std::chrono::time_point< Clock, Duration > deadline_)
Assigns deadline_ to this deadline timer.
friend Qt::strong_ordering compareThreeWay(const QDeadlineTimer &lhs, const QDeadlineTimer &rhs) noexcept
constexpr bool isForever() const noexcept
Returns true if this QDeadlineTimer object never expires, false otherwise.
friend qint64 operator-(QDeadlineTimer dt1, QDeadlineTimer dt2)
constexpr QDeadlineTimer() noexcept=default
void swap(QDeadlineTimer &other) noexcept
Swaps this deadline timer with the other deadline timer.
friend QDeadlineTimer operator-(QDeadlineTimer dt, qint64 msecs)
Returns a QDeadlineTimer object whose deadline is msecs before the deadline stored in dt.
friend QDeadlineTimer operator+(QDeadlineTimer dt, std::chrono::duration< Rep, Period > value)
friend bool comparesEqual(const QDeadlineTimer &lhs, const QDeadlineTimer &rhs) noexcept
std::chrono::nanoseconds remainingTimeAsDuration() const noexcept
Returns the time remaining before the deadline.
QDeadlineTimer & operator-=(qint64 msecs)
Shortens this QDeadlineTimer object by msecs milliseconds and returns itself.
QDeadlineTimer & operator+=(qint64 msecs)
Extends this QDeadlineTimer object by msecs milliseconds and returns itself.
void setRemainingTime(std::chrono::duration< Rep, Period > remaining, Qt::TimerType type_=Qt::CoarseTimer)
This is an overloaded member function, provided for convenience. It differs from the above function o...
\inmodule QtCore \title Classes and helpers for defining comparison operators \keyword qtcompare
Combined button and popup list for selecting options.
constexpr Qt::strong_ordering compareThreeWay(LeftInt lhs, RightInt rhs) noexcept
#define Q_DECLARE_STRONGLY_ORDERED(...)
#define Q_DECL_PURE_FUNCTION
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
GLenum GLenum GLsizei count
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t1
[4]
return lhs deadlineNSecs()
[7]
QDeadlineTimer deadline(30s)
deadline setRemainingTime(250ms)