1// Copyright (C) 2020 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 QFUTUREWATCHER_H
5#define QFUTUREWATCHER_H
6
7#include <QtCore/qfuture.h>
8#include <QtCore/qobject.h>
9
10QT_REQUIRE_CONFIG(future);
11
12QT_BEGIN_NAMESPACE
13
14
15class QEvent;
16
17class QFutureWatcherBasePrivate;
18class Q_CORE_EXPORT QFutureWatcherBase : public QObject
19{
20 Q_OBJECT
21 Q_DECLARE_PRIVATE(QFutureWatcherBase)
22
23public:
24 explicit QFutureWatcherBase(QObject *parent = nullptr);
25 // de-inline dtor
26
27 int progressValue() const;
28 int progressMinimum() const;
29 int progressMaximum() const;
30 QString progressText() const;
31
32 bool isStarted() const;
33 bool isFinished() const;
34 bool isRunning() const;
35 bool isCanceled() const;
36#if QT_DEPRECATED_SINCE(6, 0)
37 QT_DEPRECATED_VERSION_X_6_0("Use isSuspending() or isSuspended() instead.")
38 bool isPaused() const;
39#endif
40 bool isSuspending() const;
41 bool isSuspended() const;
42
43 void waitForFinished();
44
45 void setPendingResultsLimit(int limit);
46
47 bool event(QEvent *event) override;
48
49Q_SIGNALS:
50 void started();
51 void finished();
52 void canceled();
53#if QT_DEPRECATED_SINCE(6, 0)
54 QT_DEPRECATED_VERSION_X_6_0("Use suspending() instead.")
55 void paused();
56#endif
57 void suspending();
58 void suspended();
59 void resumed();
60 void resultReadyAt(int resultIndex);
61 void resultsReadyAt(int beginIndex, int endIndex);
62 void progressRangeChanged(int minimum, int maximum);
63 void progressValueChanged(int progressValue);
64 void progressTextChanged(const QString &progressText);
65
66public Q_SLOTS:
67 void cancel();
68 void setSuspended(bool suspend);
69 void suspend();
70 void resume();
71 void toggleSuspended();
72
73#if QT_DEPRECATED_SINCE(6, 0)
74 QT_DEPRECATED_VERSION_X_6_0("Use setSuspended() instead.")
75 void setPaused(bool paused);
76
77 QT_DEPRECATED_VERSION_X_6_0("Use suspended() instead.")
78 void pause();
79
80 QT_DEPRECATED_VERSION_X_6_0("Use toggleSuspended() instead.")
81 void togglePaused();
82#endif
83
84protected:
85 void connectNotify (const QMetaMethod &signal) override;
86 void disconnectNotify (const QMetaMethod &signal) override;
87
88 // called from setFuture() implemented in template sub-classes
89 void connectOutputInterface();
90 void disconnectOutputInterface(bool pendingAssignment = false);
91
92private:
93 // implemented in the template sub-classes
94 virtual const QFutureInterfaceBase &futureInterface() const = 0;
95 virtual QFutureInterfaceBase &futureInterface() = 0;
96};
97
98template <typename T>
99class QFutureWatcher : public QFutureWatcherBase
100{
101public:
102 explicit QFutureWatcher(QObject *_parent = nullptr)
103 : QFutureWatcherBase(_parent)
104 { }
105 ~QFutureWatcher()
106 { disconnectOutputInterface(); }
107
108 void setFuture(const QFuture<T> &future);
109 QFuture<T> future() const
110 { return m_future; }
111
112 template<typename U = T, typename = QtPrivate::EnableForNonVoid<U>>
113 T result() const { return m_future.result(); }
114
115 template<typename U = T, typename = QtPrivate::EnableForNonVoid<U>>
116 T resultAt(int index) const { return m_future.resultAt(index); }
117
118#ifdef Q_QDOC
119 int progressValue() const;
120 int progressMinimum() const;
121 int progressMaximum() const;
122 QString progressText() const;
123
124 bool isStarted() const;
125 bool isFinished() const;
126 bool isRunning() const;
127 bool isCanceled() const;
128#if QT_DEPRECATED_SINCE(6, 0)
129 bool isPaused() const;
130#endif
131 bool isSuspending() const;
132 bool isSuspended() const;
133
134 void waitForFinished();
135
136 void setPendingResultsLimit(int limit);
137
138Q_SIGNALS:
139 void started();
140 void finished();
141 void canceled();
142#if QT_DEPRECATED_SINCE(6, 0)
143 void paused();
144#endif
145 void suspending();
146 void suspended();
147 void resumed();
148 void resultReadyAt(int resultIndex);
149 void resultsReadyAt(int beginIndex, int endIndex);
150 void progressRangeChanged(int minimum, int maximum);
151 void progressValueChanged(int progressValue);
152 void progressTextChanged(const QString &progressText);
153
154public Q_SLOTS:
155 void cancel();
156 void setSuspended(bool suspend);
157 void suspend();
158 void resume();
159 void toggleSuspended();
160#if QT_DEPRECATED_SINCE(6, 0)
161 void setPaused(bool paused);
162 void pause();
163 void togglePaused();
164#endif // QT_DEPRECATED_SINCE(6, 0)
165
166#endif // Q_QDOC
167
168private:
169 QFuture<T> m_future;
170 const QFutureInterfaceBase &futureInterface() const override { return m_future.d; }
171 QFutureInterfaceBase &futureInterface() override { return m_future.d; }
172};
173
174template <typename T>
175Q_INLINE_TEMPLATE void QFutureWatcher<T>::setFuture(const QFuture<T> &_future)
176{
177 if (_future.d == m_future.d)
178 return;
179
180 disconnectOutputInterface(pendingAssignment: true);
181 m_future = _future;
182 connectOutputInterface();
183}
184
185QT_END_NAMESPACE
186
187#endif // QFUTUREWATCHER_H
188

source code of qtbase/src/corelib/thread/qfuturewatcher.h