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#ifndef QELAPSEDTIMER_H
5#define QELAPSEDTIMER_H
6
7#include <QtCore/qglobal.h>
8
9#include <chrono>
10
11QT_BEGIN_NAMESPACE
12
13class Q_CORE_EXPORT QElapsedTimer
14{
15public:
16 enum ClockType {
17 SystemTime,
18 MonotonicClock,
19 TickCounter Q_DECL_ENUMERATOR_DEPRECATED_X(
20 "Not supported anymore. Use PerformanceCounter instead."),
21 MachAbsoluteTime,
22 PerformanceCounter
23 };
24
25 // similar to std::chrono::*_clock
26 using Duration = std::chrono::nanoseconds;
27 using TimePoint = std::chrono::time_point<std::chrono::steady_clock, Duration>;
28
29 constexpr QElapsedTimer() = default;
30
31 static ClockType clockType() noexcept;
32 static bool isMonotonic() noexcept;
33
34 void start() noexcept;
35 qint64 restart() noexcept;
36 void invalidate() noexcept;
37 bool isValid() const noexcept;
38
39 Duration durationElapsed() const noexcept;
40 qint64 nsecsElapsed() const noexcept;
41 qint64 elapsed() const noexcept;
42 bool hasExpired(qint64 timeout) const noexcept;
43
44 qint64 msecsSinceReference() const noexcept;
45 Duration durationTo(const QElapsedTimer &other) const noexcept;
46 qint64 msecsTo(const QElapsedTimer &other) const noexcept;
47 qint64 secsTo(const QElapsedTimer &other) const noexcept;
48
49 friend bool operator==(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept
50 { return lhs.t1 == rhs.t1 && lhs.t2 == rhs.t2; }
51 friend bool operator!=(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept
52 { return !(lhs == rhs); }
53
54 friend bool Q_CORE_EXPORT operator<(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept;
55
56private:
57 qint64 t1 = Q_INT64_C(0x8000000000000000);
58 qint64 t2 = Q_INT64_C(0x8000000000000000);
59};
60
61QT_END_NAMESPACE
62
63#endif // QELAPSEDTIMER_H
64

source code of qtbase/src/corelib/kernel/qelapsedtimer.h