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
qelapsedtimer.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
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 "qelapsedtimer.h"
5
7
181{
182 // we use std::chrono::steady_clock
183 return MonotonicClock;
184}
185
199{
200 // We trust std::chrono::steady_clock to be steady (monotonic); if the
201 // Standard Library is lying to us, users must complain to their vendor.
202 return true;
203}
204
225void QElapsedTimer::start() noexcept
226{
227 static_assert(sizeof(t1) == sizeof(Duration::rep));
228
229 // This assignment will work so long as TimePoint uses the same time
230 // duration or one of finer granularity than steady_clock::time_point. That
231 // means it will work until the first steady_clock using picoseconds.
232 TimePoint now = std::chrono::steady_clock::now();
233 t1 = now.time_since_epoch().count();
234 QT6_ONLY(t2 = 0);
235}
236
256{
257 QElapsedTimer old = *this;
258 start();
259 return old.msecsTo(*this);
260}
261
277{
278 TimePoint then{Duration(t1)};
279 return std::chrono::steady_clock::now() - then;
280}
281
297{
298 return durationElapsed().count();
299}
300
311{
312 using namespace std::chrono;
313 return duration_cast<milliseconds>(durationElapsed()).count();
314}
315
332{
333 using namespace std::chrono;
334 return duration_cast<milliseconds>(Duration(t1)).count();
335}
336
350{
351 Duration d1(t1);
352 Duration d2(other.t1);
353 return d2 - d1;
354}
355
367{
368 using namespace std::chrono;
369 return duration_cast<milliseconds>(durationTo(other)).count();
370}
371
383{
384 using namespace std::chrono;
385 return duration_cast<seconds>(durationTo(other)).count();
386}
387
388static const qint64 invalidData = Q_INT64_C(0x8000000000000000);
389
401{
402 t1 = t2 = invalidData;
403}
404
411bool QElapsedTimer::isValid() const noexcept
412{
413 return t1 != invalidData && t2 != invalidData;
414}
415
427{
428 // if timeout is -1, quint64(timeout) is LLINT_MAX, so this will be
429 // considered as never expired
430 return quint64(elapsed()) > quint64(timeout);
431}
432
433bool operator<(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept
434{
435 return lhs.t1 < rhs.t1;
436}
437
\inmodule QtCore
qint64 msecsSinceReference() const noexcept
Returns the number of milliseconds between last time this QElapsedTimer object was started and its re...
void invalidate() noexcept
Marks this QElapsedTimer object as invalid.
Duration durationTo(const QElapsedTimer &other) const noexcept
Duration durationElapsed() const noexcept
qint64 elapsed() const noexcept
Returns the number of milliseconds since this QElapsedTimer was last started.
qint64 restart() noexcept
Restarts the timer and returns the number of milliseconds elapsed since the previous start.
static bool isMonotonic() noexcept
Returns true if this is a monotonic clock, false otherwise.
ClockType
This enum contains the different clock types that QElapsedTimer may use.
bool hasExpired(qint64 timeout) const noexcept
Returns true if elapsed() exceeds the given timeout, otherwise false.
void start() noexcept
\typealias QElapsedTimer::Duration Synonym for std::chrono::nanoseconds.
bool isValid() const noexcept
Returns false if the timer has never been started or invalidated by a call to invalidate().
qint64 secsTo(const QElapsedTimer &other) const noexcept
Returns the number of seconds between this QElapsedTimer and other.
std::chrono::nanoseconds Duration
qint64 nsecsElapsed() const noexcept
static ClockType clockType() noexcept
Returns the clock type that this QElapsedTimer implementation uses.
std::chrono::time_point< std::chrono::steady_clock, Duration > TimePoint
qint64 msecsTo(const QElapsedTimer &other) const noexcept
Returns the number of milliseconds between this QElapsedTimer and other.
Combined button and popup list for selecting options.
class QT6_ONLY(Q_CORE_EXPORT) QChar
Definition qchar.h:44
bool operator<(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept
static const qint64 invalidData
GLbitfield GLuint64 timeout
[4]
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t1
[4]
static double elapsed(qint64 after, qint64 before)
unsigned long long quint64
Definition qtypes.h:61
long long qint64
Definition qtypes.h:60
#define Q_INT64_C(c)
Definition qtypes.h:57
QDate d1(1995, 5, 17)
[0]
QDate d2(1995, 5, 20)
QSharedPointer< T > other(t)
[5]