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 QTIMERINFO_UNIX_P_H |
5 | #define QTIMERINFO_UNIX_P_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <QtCore/private/qglobal_p.h> |
19 | |
20 | #include "qabstracteventdispatcher.h" |
21 | |
22 | #include <sys/time.h> // struct timespec |
23 | #include <chrono> |
24 | |
25 | QT_BEGIN_NAMESPACE |
26 | |
27 | // internal timer info |
28 | struct QTimerInfo |
29 | { |
30 | using Duration = QAbstractEventDispatcher::Duration; |
31 | using TimePoint = std::chrono::time_point<std::chrono::steady_clock, Duration>; |
32 | QTimerInfo(Qt::TimerId timerId, Duration interval, Qt::TimerType type, QObject *obj) |
33 | : interval(interval), id(timerId), timerType(type), obj(obj) |
34 | { |
35 | } |
36 | |
37 | TimePoint timeout = {}; // - when to actually fire |
38 | Duration interval = Duration{-1}; // - timer interval |
39 | Qt::TimerId id = Qt::TimerId::Invalid; // - timer identifier |
40 | Qt::TimerType timerType; // - timer type |
41 | QObject *obj = nullptr; // - object to receive event |
42 | QTimerInfo **activateRef = nullptr; // - ref from activateTimers |
43 | }; |
44 | |
45 | class Q_CORE_EXPORT QTimerInfoList |
46 | { |
47 | public: |
48 | using Duration = QAbstractEventDispatcher::Duration; |
49 | using TimerInfo = QAbstractEventDispatcher::TimerInfoV2; |
50 | QTimerInfoList(); |
51 | |
52 | mutable std::chrono::steady_clock::time_point currentTime; |
53 | |
54 | std::optional<Duration> timerWait(); |
55 | void timerInsert(QTimerInfo *); |
56 | |
57 | Duration remainingDuration(Qt::TimerId timerId) const; |
58 | |
59 | void registerTimer(Qt::TimerId timerId, Duration interval, |
60 | Qt::TimerType timerType, QObject *object); |
61 | bool unregisterTimer(Qt::TimerId timerId); |
62 | bool unregisterTimers(QObject *object); |
63 | QList<TimerInfo> registeredTimers(QObject *object) const; |
64 | |
65 | int activateTimers(); |
66 | bool hasPendingTimers(); |
67 | |
68 | void clearTimers() |
69 | { |
70 | qDeleteAll(c: timers); |
71 | timers.clear(); |
72 | } |
73 | |
74 | bool isEmpty() const { return timers.empty(); } |
75 | |
76 | qsizetype size() const { return timers.size(); } |
77 | |
78 | auto findTimerById(Qt::TimerId timerId) const |
79 | { |
80 | auto matchesId = [timerId](const auto &t) { return t->id == timerId; }; |
81 | return std::find_if(first: timers.cbegin(), last: timers.cend(), pred: matchesId); |
82 | } |
83 | |
84 | private: |
85 | std::chrono::steady_clock::time_point updateCurrentTime() const; |
86 | |
87 | // state variables used by activateTimers() |
88 | QTimerInfo *firstTimerInfo = nullptr; |
89 | QList<QTimerInfo *> timers; |
90 | }; |
91 | |
92 | QT_END_NAMESPACE |
93 | |
94 | #endif // QTIMERINFO_UNIX_P_H |
95 | |