Qt 4.8
qfuture.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QFUTURE_H
43 #define QFUTURE_H
44 
45 #include <QtCore/qglobal.h>
46 
47 #ifndef QT_NO_QFUTURE
48 
49 #include <QtCore/qfutureinterface.h>
50 #include <QtCore/qstring.h>
51 #include <QtCore/qtconcurrentcompilertest.h>
52 
55 
56 QT_MODULE(Core)
57 
58 template <typename T>
60 template <>
61 class QFutureWatcher<void>;
62 
63 template <typename T>
64 class QFuture
65 {
66 public:
68  : d(QFutureInterface<T>::canceledResult())
69  { }
70  explicit QFuture(QFutureInterface<T> *p) // internal
71  : d(*p)
72  { }
73  QFuture(const QFuture &other)
74  : d(other.d)
75  { }
77  { }
78 
79  inline QFuture &operator=(const QFuture &other);
80  bool operator==(const QFuture &other) const { return (d == other.d); }
81  bool operator!=(const QFuture &other) const { return (d != other.d); }
82 
83  void cancel() { d.cancel(); }
84  bool isCanceled() const { return d.isCanceled(); }
85 
86  void setPaused(bool paused) { d.setPaused(paused); }
87  bool isPaused() const { return d.isPaused(); }
88  void pause() { setPaused(true); }
89  void resume() { setPaused(false); }
90  void togglePaused() { d.togglePaused(); }
91 
92  bool isStarted() const { return d.isStarted(); }
93  bool isFinished() const { return d.isFinished(); }
94  bool isRunning() const { return d.isRunning(); }
95 
96  int resultCount() const { return d.resultCount(); }
97  int progressValue() const { return d.progressValue(); }
98  int progressMinimum() const { return d.progressMinimum(); }
99  int progressMaximum() const { return d.progressMaximum(); }
100  QString progressText() const { return d.progressText(); }
101  void waitForFinished() { d.waitForFinished(); }
102 
103  inline T result() const;
104  inline T resultAt(int index) const;
105  bool isResultReadyAt(int resultIndex) const { return d.isResultReadyAt(resultIndex); }
106 
107  operator T() const { return result(); }
108  QList<T> results() const { return d.results(); }
109 
111  {
112  public:
113  typedef std::bidirectional_iterator_tag iterator_category;
115  typedef T value_type;
116  typedef const T *pointer;
117  typedef const T &reference;
118 
119  inline const_iterator() {}
120  inline const_iterator(QFuture const * const _future, int _index) : future(_future), index(_index) {}
121  inline const_iterator(const const_iterator &o) : future(o.future), index(o.index) {}
123  { future = o.future; index = o.index; return *this; }
124  inline const T &operator*() const { return future->d.resultReference(index); }
125  inline const T *operator->() const { return future->d.resultPointer(index); }
126 
127  inline bool operator!=(const const_iterator &other) const
128  {
129  if (index == -1 && other.index == -1) // comparing end != end?
130  return false;
131  if (other.index == -1)
132  return (future->isRunning() || (index < future->resultCount()));
133  return (index != other.index);
134  }
135 
136  inline bool operator==(const const_iterator &o) const { return !operator!=(o); }
137  inline const_iterator &operator++() { ++index; return *this; }
138  inline const_iterator operator++(int) { const_iterator r = *this; ++index; return r; }
139  inline const_iterator &operator--() { --index; return *this; }
140  inline const_iterator operator--(int) { const_iterator r = *this; --index; return r; }
141  inline const_iterator operator+(int j) const { return const_iterator(future, index + j); }
142  inline const_iterator operator-(int j) const { return const_iterator(future, index - j); }
143  inline const_iterator &operator+=(int j) { index += j; return *this; }
144  inline const_iterator &operator-=(int j) { index -= j; return *this; }
145  private:
146  QFuture const * future;
147  int index;
148  };
149  friend class const_iterator;
151 
152  const_iterator begin() const { return const_iterator(this, 0); }
153  const_iterator constBegin() const { return const_iterator(this, 0); }
154  const_iterator end() const { return const_iterator(this, -1); }
155  const_iterator constEnd() const { return const_iterator(this, -1); }
156 
157 private:
158  friend class QFutureWatcher<T>;
159 
160 public: // Warning: the d pointer is not documented and is considered private.
162 };
163 
164 template <typename T>
166 {
167  d = other.d;
168  return *this;
169 }
170 
171 template <typename T>
172 inline T QFuture<T>::result() const
173 {
174  d.waitForResult(0);
175  return d.resultReference(0);
176 }
177 
178 template <typename T>
179 inline T QFuture<T>::resultAt(int index) const
180 {
181  d.waitForResult(index);
182  return d.resultReference(index);
183 }
184 
185 template <typename T>
187 {
188  return QFuture<T>(this);
189 }
190 
192 
193 template <>
194 class QFuture<void>
195 {
196 public:
197  QFuture()
199  { }
200  explicit QFuture(QFutureInterfaceBase *p) // internal
201  : d(*p)
202  { }
203  QFuture(const QFuture &other)
204  : d(other.d)
205  { }
206  ~QFuture()
207  { }
208 
209  QFuture &operator=(const QFuture &other);
210  bool operator==(const QFuture &other) const { return (d == other.d); }
211  bool operator!=(const QFuture &other) const { return (d != other.d); }
212 
213 #if !defined(Q_CC_XLC)
214  template <typename T>
215  QFuture(const QFuture<T> &other)
216  : d(other.d)
217  { }
218 
219  template <typename T>
220  QFuture<void> &operator=(const QFuture<T> &other)
221  {
222  d = other.d;
223  return *this;
224  }
225 #endif
226 
227  void cancel() { d.cancel(); }
228  bool isCanceled() const { return d.isCanceled(); }
229 
230  void setPaused(bool paused) { d.setPaused(paused); }
231  bool isPaused() const { return d.isPaused(); }
232  void pause() { setPaused(true); }
233  void resume() { setPaused(false); }
234  void togglePaused() { d.togglePaused(); }
235 
236  bool isStarted() const { return d.isStarted(); }
237  bool isFinished() const { return d.isFinished(); }
238  bool isRunning() const { return d.isRunning(); }
239 
240  int resultCount() const { return d.resultCount(); }
241  int progressValue() const { return d.progressValue(); }
242  int progressMinimum() const { return d.progressMinimum(); }
243  int progressMaximum() const { return d.progressMaximum(); }
244  QString progressText() const { return d.progressText(); }
245  void waitForFinished() { d.waitForFinished(); }
246 
247 private:
248  friend class QFutureWatcher<void>;
249 
250 #ifdef QFUTURE_TEST
251 public:
252 #endif
253  mutable QFutureInterfaceBase d;
254 };
255 
257 {
258  d = other.d;
259  return *this;
260 }
261 
263 {
264  return QFuture<void>(this);
265 }
266 
267 template <typename T>
269 {
270  return QFuture<void>(future.d);
271 }
272 
275 
276 #endif // QT_NO_CONCURRENT
277 
278 #endif // QFUTURE_H
T value_type
Typedef for T.
Definition: qfuture.h:115
const_iterator operator++(int)
The postfix ++ operator (it++) advances the iterator to the next result in the future and returns an ...
Definition: qfuture.h:138
int resultCount() const
Returns the number of continuous results available in this future.
Definition: qfuture.h:96
bool isCanceled() const
Returns true if the asynchronous computation has been canceled with the cancel() function; otherwise ...
Definition: qfuture.h:84
QFuture< void > qToVoidFuture(const QFuture< T > &future)
Definition: qfuture.h:268
QFuture(QFutureInterface< T > *p)
Definition: qfuture.h:70
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
#define QT_MODULE(x)
Definition: qglobal.h:2783
void resume()
Resumes the asynchronous computation represented by this future.
Definition: qfuture.h:89
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
bool operator!=(const const_iterator &other) const
Returns true if other points to a different result than this iterator; otherwise returns false...
Definition: qfuture.h:127
bool operator==(const const_iterator &o) const
Returns true if other points to the same result as this iterator; otherwise returns false...
Definition: qfuture.h:136
const_iterator end() const
Returns a const STL-style iterator pointing to the imaginary result after the last result in the futu...
Definition: qfuture.h:154
~QFuture()
Destroys the future.
Definition: qfuture.h:76
QFuture< T > future()
Definition: qfuture.h:186
const T & reference
Typedef for const T &.
Definition: qfuture.h:117
void togglePaused()
Toggles the paused state of the asynchronous computation.
Definition: qfuture.h:90
The QString class provides a Unicode character string.
Definition: qstring.h:83
const_iterator(const const_iterator &o)
Constructs a copy of other.
Definition: qfuture.h:121
void cancel()
Cancels the asynchronous computation represented by this future.
Definition: qfuture.h:83
void pause()
Pauses the asynchronous computation represented by this future.
Definition: qfuture.h:88
void waitForFinished()
Waits for the asynchronous computation to finish (including cancel()ed computations).
Definition: qfuture.h:101
void setPaused(bool paused)
If paused is true, this function pauses the asynchronous computation represented by the future...
Definition: qfuture.h:86
const_iterator()
Constructs an uninitialized iterator.
Definition: qfuture.h:119
const_iterator & operator++()
The prefix ++ operator (++it) advances the iterator to the next result in the future and returns an i...
Definition: qfuture.h:137
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
#define Q_DECLARE_SEQUENTIAL_ITERATOR(C)
Definition: qiterator.h:83
int progressMaximum() const
Returns the maximum progressValue().
Definition: qfuture.h:99
QIntegerForSizeof< void * >::Signed qptrdiff
Definition: qglobal.h:987
T resultAt(int index) const
Returns the result at index in the future.
Definition: qfuture.h:179
bool isPaused() const
Returns true if the asynchronous computation has been paused with the pause() function; otherwise ret...
Definition: qfuture.h:87
qptrdiff difference_type
Typedef for ptrdiff_t.
Definition: qfuture.h:114
const T * operator->() const
Returns a pointer to the current result.
Definition: qfuture.h:125
bool operator==(const QFuture &other) const
Returns true if other is a copy of this future; otherwise returns false.
Definition: qfuture.h:80
const_iterator operator+(int j) const
Returns an iterator to the results at j positions forward from this iterator.
Definition: qfuture.h:141
T result() const
Returns the first result in the future.
Definition: qfuture.h:172
const_iterator & operator--()
The prefix – operator (–it) makes the preceding result current and returns an iterator to the new c...
Definition: qfuture.h:139
QFuture()
Constructs an empty future.
Definition: qfuture.h:67
const_iterator operator--(int)
The postfix – operator (it–) makes the preceding result current and returns an iterator to the prev...
Definition: qfuture.h:140
int progressValue() const
Returns the current progress value, which is between the progressMinimum() and progressMaximum().
Definition: qfuture.h:97
QFuture(const QFuture &other)
Constructs a copy of other.
Definition: qfuture.h:73
int progressMinimum() const
Returns the minimum progressValue().
Definition: qfuture.h:98
The QFuture class represents the result of an asynchronous computation.
Definition: qfuture.h:64
The QFuture::const_iterator class provides an STL-style const iterator for QFuture.
Definition: qfuture.h:110
bool isStarted() const
Returns true if the asynchronous computation represented by this future has been started; otherwise r...
Definition: qfuture.h:92
const T * pointer
Typedef for const T *.
Definition: qfuture.h:116
QFuture const * future
Definition: qfuture.h:146
const_iterator & operator+=(int j)
Advances the iterator by j results.
Definition: qfuture.h:143
bool isRunning() const
Returns true if the asynchronous computation represented by this future is currently running; otherwi...
Definition: qfuture.h:94
const_iterator begin() const
Returns a const STL-style iterator pointing to the first result in the future.
Definition: qfuture.h:152
const_iterator & operator-=(int j)
Makes the iterator go back by j results.
Definition: qfuture.h:144
QString progressText() const
Returns the (optional) textual representation of the progress as reported by the asynchronous computa...
Definition: qfuture.h:100
bool operator!=(const QFuture &other) const
Returns true if other is not a copy of this future; otherwise returns false.
Definition: qfuture.h:81
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first result in the future.
Definition: qfuture.h:153
quint16 index
std::bidirectional_iterator_tag iterator_category
Typedef for std::bidirectional_iterator_tag.
Definition: qfuture.h:113
QFuture & operator=(const QFuture &other)
Assigns other to this future and returns a reference to this future.
Definition: qfuture.h:165
const_iterator(QFuture const *const _future, int _index)
Definition: qfuture.h:120
const_iterator operator-(int j) const
Returns an iterator to the result at j positions backward from this iterator.
Definition: qfuture.h:142
QFutureInterface< T > d
Definition: qfuture.h:161
const_iterator ConstIterator
Qt-style synonym for QFuture::const_iterator.
Definition: qfuture.h:150
bool isResultReadyAt(int resultIndex) const
Returns true if the result at index is immediately available; otherwise returns false.
Definition: qfuture.h:105
#define QT_END_HEADER
Definition: qglobal.h:137
QList< T > results() const
Returns all results from the future.
Definition: qfuture.h:108
bool isFinished() const
Returns true if the asynchronous computation represented by this future has finished; otherwise retur...
Definition: qfuture.h:93
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary result after the last result in the futu...
Definition: qfuture.h:155
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
const T & operator*() const
Returns the current result.
Definition: qfuture.h:124
const_iterator & operator=(const const_iterator &o)
Assigns other to this iterator.
Definition: qfuture.h:122
The QFutureWatcher class allows monitoring a QFuture using signals and slots.
Definition: qfuture.h:59