1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QFUTUREWATCHER_H
43 #define QFUTUREWATCHER_H
44 
45 #include <QtCore/qfuture.h>
46 
47 #include <QtCore/qobject.h>
48 
49 QT_BEGIN_HEADER
50 QT_BEGIN_NAMESPACE
51 
52 QT_MODULE(Core)
53 
54 class QEvent;
55 
56 class QFutureWatcherBasePrivate;
57 
58 #ifdef QT_NO_QFUTURE
59 class QFutureInterfaceBase;
60 #endif
61 
62 class Q_CORE_EXPORT QFutureWatcherBase : public QObject
63 {
64     Q_OBJECT
65     Q_DECLARE_PRIVATE(QFutureWatcherBase)
66 
67 public:
68     QFutureWatcherBase(QObject *parent = 0);
69 
70     int progressValue() const;
71     int progressMinimum() const;
72     int progressMaximum() const;
73     QString progressText() const;
74 
75     bool isStarted() const;
76     bool isFinished() const;
77     bool isRunning() const;
78     bool isCanceled() const;
79     bool isPaused() const;
80 
81     void waitForFinished();
82 
83     void setPendingResultsLimit(int limit);
84 
85     bool event(QEvent *event);
86 
87 Q_SIGNALS:
88     void started();
89     void finished();
90     void canceled();
91     void paused();
92     void resumed();
93     void resultReadyAt(int resultIndex);
94     void resultsReadyAt(int beginIndex, int endIndex);
95     void progressRangeChanged(int minimum, int maximum);
96     void progressValueChanged(int progressValue);
97     void progressTextChanged(const QString &progressText);
98 
99 public Q_SLOTS:
100     void cancel();
101     void setPaused(bool paused);
102     void pause();
103     void resume();
104     void togglePaused();
105 
106 protected:
107     void connectNotify (const char * signal);
108     void disconnectNotify (const char * signal);
109 
110     // called from setFuture() implemented in template sub-classes
111     void connectOutputInterface();
112     void disconnectOutputInterface(bool pendingAssignment = false);
113 
114 private:
115     // implemented in the template sub-classes
116     virtual const QFutureInterfaceBase &futureInterface() const = 0;
117     virtual QFutureInterfaceBase &futureInterface() = 0;
118 };
119 
120 #ifndef QT_NO_QFUTURE
121 
122 template <typename T>
123 class QFutureWatcher : public QFutureWatcherBase
124 {
125 public:
126     QFutureWatcher(QObject *_parent = 0)
QFutureWatcherBase(_parent)127         : QFutureWatcherBase(_parent)
128     { }
~QFutureWatcher()129     ~QFutureWatcher()
130     { disconnectOutputInterface(); }
131 
132     void setFuture(const QFuture<T> &future);
future()133     QFuture<T> future() const
134     { return m_future; }
135 
result()136     T result() const { return m_future.result(); }
resultAt(int index)137     T resultAt(int index) const { return m_future.resultAt(index); }
138 
139 #ifdef qdoc
140     int progressValue() const;
141     int progressMinimum() const;
142     int progressMaximum() const;
143     QString progressText() const;
144 
145     bool isStarted() const;
146     bool isFinished() const;
147     bool isRunning() const;
148     bool isCanceled() const;
149     bool isPaused() const;
150 
151     void waitForFinished();
152 
153     void setPendingResultsLimit(int limit);
154 
155 Q_SIGNALS:
156     void started();
157     void finished();
158     void canceled();
159     void paused();
160     void resumed();
161     void resultReadyAt(int resultIndex);
162     void resultsReadyAt(int beginIndex, int endIndex);
163     void progressRangeChanged(int minimum, int maximum);
164     void progressValueChanged(int progressValue);
165     void progressTextChanged(const QString &progressText);
166 
167 public Q_SLOTS:
168     void cancel();
169     void setPaused(bool paused);
170     void pause();
171     void resume();
172     void togglePaused();
173 #endif
174 
175 private:
176     QFuture<T> m_future;
futureInterface()177     const QFutureInterfaceBase &futureInterface() const { return m_future.d; }
futureInterface()178     QFutureInterfaceBase &futureInterface() { return m_future.d; }
179 };
180 
181 template <typename T>
setFuture(const QFuture<T> & _future)182 Q_INLINE_TEMPLATE void QFutureWatcher<T>::setFuture(const QFuture<T> &_future)
183 {
184     if (_future == m_future)
185         return;
186 
187     disconnectOutputInterface(true);
188     m_future = _future;
189     connectOutputInterface();
190 }
191 
192 template <>
193 class QFutureWatcher<void> : public QFutureWatcherBase
194 {
195 public:
196     QFutureWatcher(QObject *_parent = 0)
QFutureWatcherBase(_parent)197         : QFutureWatcherBase(_parent)
198     { }
~QFutureWatcher()199     ~QFutureWatcher()
200     { disconnectOutputInterface(); }
201 
202     void setFuture(const QFuture<void> &future);
future()203     QFuture<void> future() const
204     { return m_future; }
205 
206 private:
207     QFuture<void> m_future;
futureInterface()208     const QFutureInterfaceBase &futureInterface() const { return m_future.d; }
futureInterface()209     QFutureInterfaceBase &futureInterface() { return m_future.d; }
210 };
211 
setFuture(const QFuture<void> & _future)212 Q_INLINE_TEMPLATE void QFutureWatcher<void>::setFuture(const QFuture<void> &_future)
213 {
214     if (_future == m_future)
215         return;
216 
217     disconnectOutputInterface(true);
218     m_future = _future;
219     connectOutputInterface();
220 }
221 
222 #endif // QT_NO_QFUTURE
223 
224 QT_END_NAMESPACE
225 QT_END_HEADER
226 
227 #endif // QFUTUREWATCHER_H
228