Merge branch 'master' of scm.dev.nokia.troll.no:qt/oslo-staging-1 into master-integration
[qt-netbsd.git] / src / corelib / concurrent / qfuture.h
blobe4023350ca5b0ea0ba88d242e36f73be510681cb
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtCore module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
38 ** $QT_END_LICENSE$
40 ****************************************************************************/
42 #ifndef QFUTURE_H
43 #define QFUTURE_H
45 #include <QtCore/qglobal.h>
47 #ifndef QT_NO_QFUTURE
49 #include <QtCore/qfutureinterface.h>
50 #include <QtCore/qstring.h>
51 #include <QtCore/qtconcurrentcompilertest.h>
53 QT_BEGIN_HEADER
54 QT_BEGIN_NAMESPACE
56 QT_MODULE(Core)
58 template <typename T>
59 class QFutureWatcher;
60 template <>
61 class QFutureWatcher<void>;
63 template <typename T>
64 class QFuture
66 public:
67 QFuture()
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 { }
76 ~QFuture()
77 { }
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); }
83 void cancel() { d.cancel(); }
84 bool isCanceled() const { return d.isCanceled(); }
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(); }
92 bool isStarted() const { return d.isStarted(); }
93 bool isFinished() const { return d.isFinished(); }
94 bool isRunning() const { return d.isRunning(); }
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(); }
103 inline T result() const;
104 inline T resultAt(int index) const;
105 bool isResultReadyAt(int resultIndex) const { return d.isResultReadyAt(resultIndex); }
107 operator T() const { return result(); }
108 QList<T> results() const { return d.results(); }
110 class const_iterator
112 public:
113 typedef std::bidirectional_iterator_tag iterator_category;
114 typedef ptrdiff_t difference_type;
115 typedef T value_type;
116 typedef const T *pointer;
117 typedef const T &reference;
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) {}
122 inline const_iterator &operator=(const const_iterator &o)
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); }
127 inline bool operator!=(const const_iterator &other) const
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);
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;
149 friend class const_iterator;
150 typedef const_iterator ConstIterator;
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); }
157 private:
158 friend class QFutureWatcher<T>;
160 public: // Warning: the d pointer is not documented and is considered private.
161 mutable QFutureInterface<T> d;
164 template <typename T>
165 inline QFuture<T> &QFuture<T>::operator=(const QFuture<T> &other)
167 d = other.d;
168 return *this;
171 template <typename T>
172 inline T QFuture<T>::result() const
174 d.waitForResult(0);
175 return d.resultReference(0);
178 template <typename T>
179 inline T QFuture<T>::resultAt(int index) const
181 d.waitForResult(index);
182 return d.resultReference(index);
185 template <typename T>
186 inline QFuture<T> QFutureInterface<T>::future()
188 return QFuture<T>(this);
191 Q_DECLARE_SEQUENTIAL_ITERATOR(Future)
193 template <>
194 class QFuture<void>
196 public:
197 QFuture()
198 : d(QFutureInterface<void>::canceledResult())
200 explicit QFuture(QFutureInterfaceBase *p) // internal
201 : d(*p)
203 QFuture(const QFuture &other)
204 : d(other.d)
206 ~QFuture()
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); }
213 #if !defined(QT_NO_MEMBER_TEMPLATES) && !defined(Q_CC_XLC)
214 template <typename T>
215 QFuture(const QFuture<T> &other)
216 : d(other.d)
219 template <typename T>
220 QFuture<void> &operator=(const QFuture<T> &other)
222 d = other.d;
223 return *this;
225 #endif
227 void cancel() { d.cancel(); }
228 bool isCanceled() const { return d.isCanceled(); }
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(); }
236 bool isStarted() const { return d.isStarted(); }
237 bool isFinished() const { return d.isFinished(); }
238 bool isRunning() const { return d.isRunning(); }
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(); }
247 private:
248 friend class QFutureWatcher<void>;
250 #ifdef QFUTURE_TEST
251 public:
252 #endif
253 mutable QFutureInterfaceBase d;
256 inline QFuture<void> &QFuture<void>::operator=(const QFuture<void> &other)
258 d = other.d;
259 return *this;
262 inline QFuture<void> QFutureInterface<void>::future()
264 return QFuture<void>(this);
267 template <typename T>
268 QFuture<void> qToVoidFuture(const QFuture<T> &future)
270 return QFuture<void>(future.d);
273 QT_END_NAMESPACE
274 QT_END_HEADER
276 #endif // QT_NO_CONCURRENT
278 #endif // QFUTURE_H