Debian Codegraph @

src/util/dtimedloop.cpp

raw
1 // SPDX-FileCopyrightText: 2021 - 2022 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include "dtimedloop.h"
6 #include <DObject>
7 #include <DObjectPrivate>
8 #include <dthreadutils.h>
9
10 #include <QTime>
11 #include <QTimer>
12 #include <QLoggingCategory>
13
14 DCORE_BEGIN_NAMESPACE
15
16 #ifdef QT_DEBUG
17 Q_LOGGING_CATEGORY(logTimedLoop, "dtk.dtimedloop")
18 #else
19 Q_LOGGING_CATEGORY(logTimedLoop, "dtk.dtimedloop", QtInfoMsg)
20 #endif
21
22 class DTimedLoopPrivate : public DObjectPrivate
23 {
24 D_DECLARE_PUBLIC(DTimedLoop)
25 public:
26 DTimedLoopPrivate(DTimedLoop *qq = nullptr);
27 ~DTimedLoopPrivate();
28
29 int m_returnCode = 0;
30 QTime m_startTime;
31 QTime m_stopTime;
32 bool m_timeDumpFlag = false;
33 char __padding[3];
34 QString m_exectionName;
35
36 void setExecutionName(const QString &executionName);
37
38 class LoopGuard {
39 DTimedLoopPrivate *m_p = nullptr;
40
41 public:
42 LoopGuard(DTimedLoopPrivate *p)
43 : m_p (p)
44 {
45 m_p->m_startTime = QTime::currentTime();
46 }
47 ~LoopGuard() {
48 m_p->m_stopTime = QTime::currentTime();
49 if (!m_p->m_timeDumpFlag) {
50 return;
51 }
52 if (Q_UNLIKELY(m_p->m_exectionName.isEmpty())) {
53 qCDebug(logTimedLoop(),
54 "The execution time is %-5d ms",
55 m_p->m_startTime.msecsTo(QTime::currentTime()));
56 } else {
57 qCDebug(logTimedLoop(),
58 "The execution time is %-5d ms for \"%s\"",
59 m_p->m_startTime.msecsTo(QTime::currentTime()),
60 m_p->m_exectionName.toLocal8Bit().data());
61
62 m_p->m_exectionName.clear();
63 }
64 }
65 };
66 };
67
68 DTimedLoopPrivate::DTimedLoopPrivate(DTimedLoop *qq)
69 : DObjectPrivate (qq)
70 {
71 }
72
73 DTimedLoopPrivate::~DTimedLoopPrivate()
74 {
75 }
76
77 void DTimedLoopPrivate::setExecutionName(const QString &executionName)
78 {
79 m_exectionName = executionName;
80 }
81
82 DTimedLoop::DTimedLoop(QObject *parent) noexcept
83 : QEventLoop (parent)
84 , DObject (*new DTimedLoopPrivate(this))
85 {
86 }
87
88 DTimedLoop::DTimedLoop() noexcept
89 : QEventLoop ()
90 , DObject (*new DTimedLoopPrivate(this))
91 {
92 }
93
94 DTimedLoop::~DTimedLoop()
95 {
96 }
97
98 int DTimedLoop::runningTime() {
99 Q_D(DTimedLoop);
100 if (QEventLoop::isRunning()) {
101 return d->m_startTime.msecsTo(QTime::currentTime());
102 }
103 return d->m_startTime.msecsTo(d->m_stopTime);
104 }
105
106 void DTimedLoop::setTimeDump(bool flag)
107 {
108 Q_D(DTimedLoop);
109 d->m_timeDumpFlag = flag;
110 }
111
112 void DTimedLoop::exit(int returnCode)
113 {
114 // 避免在子线程中提前被执行
115 DThreadUtil::runInMainThread([this, returnCode]{
116 QEventLoop::exit(returnCode);
117 });
118 }
119
120 int DTimedLoop::exec(QEventLoop::ProcessEventsFlags flags)
121 {
122 Q_D(DTimedLoop);
123 DTimedLoopPrivate::LoopGuard guard(d);
124 return QEventLoop::exec(flags);
125 }
126
127 int DTimedLoop::exec(int durationTimeMs, QEventLoop::ProcessEventsFlags flags)
128 {
129 Q_D(DTimedLoop);
130 int runningTime = durationTimeMs < 0 ? 0 : durationTimeMs;
131 QTimer::singleShot(runningTime, [this] {
132 QEventLoop::exit(0);
133 });
134 DTimedLoopPrivate::LoopGuard guard(d);
135 return QEventLoop::exec(flags);
136 }
137
138 int DTimedLoop::exec(const QString &executionName, QEventLoop::ProcessEventsFlags flags)
139 {
140 Q_D(DTimedLoop);
141 d->setExecutionName(executionName);
142 return exec(flags);
143 }
144
145 int DTimedLoop::exec(int durationMs, const QString &executionName, QEventLoop::ProcessEventsFlags flags)
146 {
147 Q_D(DTimedLoop);
148 d->setExecutionName(executionName);
149 return exec(durationMs, flags);
150 }
151
152 DCORE_END_NAMESPACE