QtCore/qfuture.h Source File

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 
50 #include <QtCore/qstring.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;
114  typedef qptrdiff difference_type;
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:
198  : d(QFutureInterface<void>::canceledResult())
199  { }
200  explicit QFuture(QFutureInterfaceBase *p) // internal
201  : d(*p)
202  { }
203  QFuture(const QFuture &other)
204  : d(other.d)
205  { }
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>
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
const T & operator*() const
Definition: qfuture.h:124
bool isStarted() const
Definition: qfuture.h:92
GLdouble GLdouble GLdouble r
Definition: GLee.h:1189
QFuture< void > qToVoidFuture(const QFuture< T > &future)
Definition: qfuture.h:268
bool isPaused() const
Definition: qfuture.h:231
#define QT_END_NAMESPACE
Definition: qglobal.h:128
bool isRunning() const
Definition: qfuture.h:94
QFuture()
Definition: qfuture.h:67
void pause()
Definition: qfuture.h:88
bool isStarted() const
Definition: qfuture.h:236
#define QT_BEGIN_HEADER
Definition: qglobal.h:141
const_iterator operator++(int)
Definition: qfuture.h:138
void setPaused(bool paused)
Definition: qfuture.h:86
QFuture(const QFuture< T > &other)
Definition: qfuture.h:215
T result() const
Definition: qfuture.h:172
void waitForFinished()
Definition: qfuture.h:245
int progressMinimum() const
Definition: qfuture.h:242
const_iterator begin() const
Definition: qfuture.h:152
QFuture< T > future() const
const_iterator operator-(int j) const
Definition: qfuture.h:142
QFuture(QFutureInterfaceBase *p)
Definition: qfuture.h:200
void setPaused(bool paused)
qptrdiff difference_type
Definition: qfuture.h:114
bool operator!=(const const_iterator &other) const
Definition: qfuture.h:127
QFuture(const QFuture &other)
Definition: qfuture.h:73
bool isCanceled() const
Definition: qfuture.h:84
int progressMinimum() const
Definition: qfuture.h:98
bool isFinished() const
Definition: qfuture.h:93
QFuture(QFutureInterface< T > *p)
Definition: qfuture.h:70
void pause()
Definition: qfuture.h:232
void togglePaused()
Definition: qfuture.h:90
const_iterator operator+(int j) const
Definition: qfuture.h:141
std::bidirectional_iterator_tag iterator_category
Definition: qfuture.h:113
bool isPaused() const
Definition: qfuture.h:87
QString progressText() const
Definition: qfuture.h:244
int resultCount() const
Definition: qfuture.h:96
const_iterator(const const_iterator &o)
Definition: qfuture.h:121
int progressMaximum() const
Definition: qfuture.h:99
const_iterator & operator=(const const_iterator &o)
Definition: qfuture.h:122
int progressValue() const
Definition: qfuture.h:241
int resultCount() const
Definition: qfuture.h:240
#define QT_BEGIN_NAMESPACE
Definition: qglobal.h:127
#define Q_DECLARE_SEQUENTIAL_ITERATOR(C)
Definition: qiterator.h:83
bool isRunning() const
Definition: qfuture.h:238
QFutureInterface< T > d
Definition: qfuture.h:161
const_iterator & operator++()
Definition: qfuture.h:137
void setPaused(bool paused)
Definition: qfuture.h:230
void cancel()
Definition: qfuture.h:83
const_iterator & operator-=(int j)
Definition: qfuture.h:144
int progressValue() const
Definition: qfuture.h:97
const_iterator operator--(int)
Definition: qfuture.h:140
QString progressText() const
Definition: qfuture.h:100
void resume()
Definition: qfuture.h:233
bool operator==(const const_iterator &o) const
Definition: qfuture.h:136
const_iterator & operator--()
Definition: qfuture.h:139
bool operator!=(const QByteArray &a1, const QByteArray &a2)
Definition: qbytearray.h:533
QFuture< void > & operator=(const QFuture< T > &other)
Definition: qfuture.h:220
void cancel()
Definition: qfuture.h:227
bool operator!=(const QFuture &other) const
Definition: qfuture.h:81
bool isResultReadyAt(int resultIndex) const
Definition: qfuture.h:105
void waitForFinished()
Definition: qfuture.h:101
const_iterator constBegin() const
Definition: qfuture.h:153
GLfloat GLfloat p
Definition: GLee.h:5416
const_iterator constEnd() const
Definition: qfuture.h:155
bool isCanceled() const
Definition: qfuture.h:228
QList< T > results() const
Definition: qfuture.h:108
QFuture & operator=(const QFuture &other)
Definition: qfuture.h:165
const_iterator & operator+=(int j)
Definition: qfuture.h:143
T resultAt(int index) const
Definition: qfuture.h:179
const_iterator(QFuture const *const _future, int _index)
Definition: qfuture.h:120
bool operator==(const QFuture &other) const
Definition: qfuture.h:80
QFuture(const QFuture &other)
Definition: qfuture.h:203
bool operator==(const QFuture &other) const
Definition: qfuture.h:210
const_iterator ConstIterator
Definition: qfuture.h:150
void togglePaused()
Definition: qfuture.h:234
const_iterator end() const
Definition: qfuture.h:154
bool isFinished() const
Definition: qfuture.h:237
bool operator!=(const QFuture &other) const
Definition: qfuture.h:211
#define QT_END_HEADER
Definition: qglobal.h:142
int progressMaximum() const
Definition: qfuture.h:243
QFuture< T > future()
Definition: qfuture.h:186
void resume()
Definition: qfuture.h:89
GLuint index
Definition: GLee.h:1704
~QFuture()
Definition: qfuture.h:76
const T * operator->() const
Definition: qfuture.h:125