source: trunk/src/corelib/concurrent/qfuture.cpp@ 440

Last change on this file since 440 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 22.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/*! \class QFuture
43 \threadsafe
44 \brief The QFuture class represents the result of an asynchronous computation.
45 \since 4.4
46
47 To start a computation, use one of the APIs in the
48 \l {threads.html#qtconcurrent-intro}{Qt Concurrent} framework.
49
50 QFuture allows threads to be synchronized against one or more results
51 which will be ready at a later point in time. The result can be of any type
52 that has a default constructor and a copy constructor. If a result is not
53 available at the time of calling the result(), resultAt(), or results()
54 functions, QFuture will wait until the result becomes available. You can
55 use the isResultReadyAt() function to determine if a result is ready or
56 not. For QFuture objects that report more than one result, the
57 resultCount() function returns the number of continuous results. This
58 means that it is always safe to iterate through the results from 0 to
59 resultCount().
60
61 QFuture provides a \l{Java-style iterators}{Java-style iterator}
62 (QFutureIterator) and an \l{STL-style iterators}{STL-style iterator}
63 (QFuture::const_iterator). Using these iterators is another way to access
64 results in the future.
65
66 QFuture also offers ways to interact with a runnning computation. For
67 instance, the computation can be canceled with the cancel() function. To
68 pause the computation, use the setPaused() function or one of the pause(),
69 resume(), or togglePaused() convenience functions. Be aware that not all
70 asynchronous computations can be canceled or paused. For example, the
71 future returned by QtConcurrent::run() cannot be canceled; but the
72 future returned by QtConcurrent::mappedReduced() can.
73
74 Progress information is provided by the progressValue(),
75 progressMinimum(), progressMaximum(), and progressText() functions. The
76 waitForFinished() function causes the calling thread to block and wait for
77 the computation to finish, ensuring that all results are available.
78
79 The state of the computation represented by a QFuture can be queried using
80 the isCanceled(), isStarted(), isFinished(), isRunning(), or isPaused()
81 functions.
82
83 QFuture is a lightweight reference counted class that can be passed by
84 value.
85
86 QFuture<void> is specialized to not contain any of the result fetching
87 functions. Any QFuture<T> can be assigned or copied into a QFuture<void>
88 as well. This is useful if only status or progress information is needed
89 - not the actual result data.
90
91 To interact with running tasks using signals and slots, use QFutureWatcher.
92
93 \sa QFutureWatcher, {threads.html#qtconcurrent-intro}{Qt Concurrent}
94*/
95
96/*! \fn QFuture::QFuture()
97
98 Constructs an empty future.
99*/
100
101/*! \fn QFuture::QFuture(const QFuture &other)
102
103 Constructs a copy of \a other.
104
105 \sa operator=()
106*/
107
108/*! \fn QFuture::QFuture(QFutureInterface<T> *resultHolder)
109 \internal
110*/
111
112/*! \fn QFuture::~QFuture()
113
114 Destroys the future.
115
116 Note that this neither waits nor cancels the asynchronous computation. Use
117 waitForFinished() or QFutureSynchronizer when you need to ensure that the
118 computation is completed before the future is destroyed.
119*/
120
121/*! \fn QFuture &QFuture::operator=(const QFuture &other)
122
123 Assigns \a other to this future and returns a reference to this future.
124*/
125
126/*! \fn bool QFuture::operator==(const QFuture &other) const
127
128 Returns true if \a other is a copy of this future; otherwise returns false.
129*/
130
131/*! \fn bool QFuture::operator!=(const QFuture &other) const
132
133 Returns true if \a other is \e not a copy of this future; otherwise returns
134 false.
135*/
136
137/*! \fn void QFuture::cancel()
138
139 Cancels the asynchronous computation represented by this future. Note that
140 the cancelation is asynchronous. Use waitForFinished() after calling
141 cancel() when you need synchronous cancelation.
142
143 Results currently available may still be accessed on a canceled future,
144 but new results will \e not become available after calling this function.
145 Any QFutureWatcher object that is watching this future will not deliver
146 progress and result ready signals on a canceled future.
147
148 Be aware that not all asynchronous computations can be canceled. For
149 example, the future returned by QtConcurrent::run() cannot be canceled;
150 but the future returned by QtConcurrent::mappedReduced() can.
151*/
152
153/*! \fn bool QFuture::isCanceled() const
154
155 Returns true if the asynchronous computation has been canceled with the
156 cancel() function; otherwise returns false.
157
158 Be aware that the computation may still be running even though this
159 function returns true. See cancel() for more details.
160*/
161
162/*! \fn void QFuture::setPaused(bool paused)
163
164 If \a paused is true, this function pauses the asynchronous computation
165 represented by the future. If the computation is already paused, this
166 function does nothing. Any QFutureWatcher object that is watching this
167 future will stop delivering progress and result ready signals while the
168 future is paused. Signal delivery will continue once the future is
169 resumed.
170
171 If \a paused is false, this function resumes the asynchronous computation.
172 If the computation was not previously paused, this function does nothing.
173
174 Be aware that not all computations can be paused. For example, the future
175 returned by QtConcurrent::run() cannot be paused; but the future returned
176 by QtConcurrent::mappedReduced() can.
177
178 \sa pause(), resume(), togglePaused()
179*/
180
181/*! \fn bool QFuture::isPaused() const
182
183 Returns true if the asynchronous computation has been paused with the
184 pause() function; otherwise returns false.
185
186 Be aware that the computation may still be running even though this
187 function returns true. See setPaused() for more details.
188
189 \sa setPaused(), togglePaused()
190*/
191
192/*! \fn void QFuture::pause()
193
194 Pauses the asynchronous computation represented by this future. This is a
195 convenience method that simply calls setPaused(true).
196
197 \sa resume()
198*/
199
200/*! \fn void QFuture::resume()
201
202 Resumes the asynchronous computation represented by this future. This is a
203 convenience method that simply calls setPaused(false).
204
205 \sa pause()
206*/
207
208/*! \fn void QFuture::togglePaused()
209
210 Toggles the paused state of the asynchronous computation. In other words,
211 if the computation is currently paused, calling this function resumes it;
212 if the computation is running, it is paused. This is a convenience method
213 for calling setPaused(!isPaused()).
214
215 \sa setPaused(), pause(), resume()
216*/
217
218/*! \fn bool QFuture::isStarted() const
219
220 Returns true if the asynchronous computation represented by this future
221 has been started; otherwise returns false.
222*/
223
224/*! \fn bool QFuture::isFinished() const
225
226 Returns true if the asynchronous computation represented by this future
227 has finished; otherwise returns false.
228*/
229
230/*! \fn bool QFuture::isRunning() const
231
232 Returns true if the asynchronous computation represented by this future is
233 currently running; otherwise returns false.
234*/
235
236/*! \fn int QFuture::resultCount() const
237
238 Returns the number of continuous results available in this future. The real
239 number of results stored might be different from this value, due to gaps
240 in the result set. It is always safe to iterate through the results from 0
241 to resultCount().
242 \sa result(), resultAt(), results()
243*/
244
245/*! \fn int QFuture::progressValue() const
246
247 Returns the current progress value, which is between the progressMinimum()
248 and progressMaximum().
249
250 \sa progressMinimum(), progressMaximum()
251*/
252
253/*! \fn int QFuture::progressMinimum() const
254
255 Returns the minimum progressValue().
256
257 \sa progressValue(), progressMaximum()
258*/
259
260/*! \fn int QFuture::progressMaximum() const
261
262 Returns the maximum progressValue().
263
264 \sa progressValue(), progressMinimum()
265*/
266
267/*! \fn QString QFuture::progressText() const
268
269 Returns the (optional) textual representation of the progress as reported
270 by the asynchronous computation.
271
272 Be aware that not all computations provide a textual representation of the
273 progress, and as such, this function may return an empty string.
274*/
275
276/*! \fn void QFuture::waitForFinished()
277
278 Waits for the asynchronous computation to finish (including cancel()ed
279 computations).
280*/
281
282/*! \fn T QFuture::result() const
283
284 Returns the first result in the future. If the result is not immediately
285 available, this function will block and wait for the result to become
286 available. This is a convenience method for calling resultAt(0).
287
288 \sa resultAt(), results()
289*/
290
291/*! \fn T QFuture::resultAt(int index) const
292
293 Returns the result at \a index in the future. If the result is not
294 immediately available, this function will block and wait for the result to
295 become available.
296
297 \sa result(), results(), resultCount()
298*/
299
300/*! \fn bool QFuture::isResultReadyAt(int index) const
301
302 Returns true if the result at \a index is immediately available; otherwise
303 returns false.
304
305 \sa resultAt(), resultCount()
306*/
307
308/*! \fn QFuture::operator T() const
309
310 Returns the first result in the future. If the result is not immediately
311 available, this function will block and wait for the result to become
312 available. This is a convenience method for calling result() or
313 resultAt(0).
314
315 \sa result(), resultAt(), results()
316*/
317
318/*! \fn QList<T> QFuture::results() const
319
320 Returns all results from the future. If the results are not immediately
321 available, this function will block and wait for them to become available.
322
323 \sa result(), resultAt(), resultCount()
324*/
325
326/*! \fn QFuture::const_iterator QFuture::begin() const
327
328 Returns a const \l{STL-style iterator} pointing to the first result in the
329 future.
330
331 \sa constBegin(), end()
332*/
333
334/*! \fn QFuture::const_iterator QFuture::end() const
335
336 Returns a const \l{STL-style iterator} pointing to the imaginary result
337 after the last result in the future.
338
339 \sa begin(), constEnd()
340*/
341
342/*! \fn QFuture::const_iterator QFuture::constBegin() const
343
344 Returns a const \l{STL-style iterator} pointing to the first result in the
345 future.
346
347 \sa begin(), constEnd()
348*/
349
350/*! \fn QFuture::const_iterator QFuture::constEnd() const
351
352 Returns a const \l{STL-style iterator} pointing to the imaginary result
353 after the last result in the future.
354
355 \sa constBegin(), end()
356*/
357
358/*! \class QFuture::const_iterator
359 \reentrant
360 \since 4.4
361
362 \brief The QFuture::const_iterator class provides an STL-style const
363 iterator for QFuture.
364
365 QFuture provides both \l{STL-style iterators} and \l{Java-style iterators}.
366 The STL-style iterators are more low-level and more cumbersome to use; on
367 the other hand, they are slightly faster and, for developers who already
368 know STL, have the advantage of familiarity.
369
370 The default QFuture::const_iterator constructor creates an uninitialized
371 iterator. You must initialize it using a QFuture function like
372 QFuture::constBegin() or QFuture::constEnd() before you start iterating.
373 Here's a typical loop that prints all the results available in a future:
374
375 \snippet doc/src/snippets/code/src_corelib_concurrent_qfuture.cpp 0
376
377 \sa QFutureIterator, QFuture
378*/
379
380/*! \typedef QFuture::const_iterator::iterator_category
381
382 Typedef for std::bidirectional_iterator_tag. Provided for STL compatibility.
383*/
384
385/*! \typedef QFuture::const_iterator::difference_type
386
387 Typedef for ptrdiff_t. Provided for STL compatibility.
388*/
389
390/*! \typedef QFuture::const_iterator::value_type
391
392 Typedef for T. Provided for STL compatibility.
393*/
394
395/*! \typedef QFuture::const_iterator::pointer
396
397 Typedef for const T *. Provided for STL compatibility.
398*/
399
400/*! \typedef QFuture::const_iterator::reference
401
402 Typedef for const T &. Provided for STL compatibility.
403*/
404
405/*! \fn QFuture::const_iterator::const_iterator()
406
407 Constructs an uninitialized iterator.
408
409 Functions like operator*() and operator++() should not be called on an
410 uninitialized iterartor. Use operator=() to assign a value to it before
411 using it.
412
413 \sa QFuture::constBegin() QFuture::constEnd()
414*/
415
416/*! \fn QFuture::const_iterator::const_iterator(QFuture const * const future, int index)
417 \internal
418*/
419
420/*! \fn QFuture::const_iterator::const_iterator(const const_iterator &other)
421
422 Constructs a copy of \a other.
423*/
424
425/*! \fn QFuture::const_iterator &QFuture::const_iterator::operator=(const const_iterator &other)
426
427 Assigns \a other to this iterator.
428*/
429
430/*! \fn const T &QFuture::const_iterator::operator*() const
431
432 Returns the current result.
433*/
434
435/*! \fn const T *QFuture::const_iterator::operator->() const
436
437 Returns a pointer to the current result.
438*/
439
440/*! \fn bool QFuture::const_iterator::operator!=(const const_iterator &other) const
441
442 Returns true if \a other points to a different result than this iterator;
443 otherwise returns false.
444
445 \sa operator==()
446*/
447
448/*! \fn bool QFuture::const_iterator::operator==(const const_iterator &other) const
449
450 Returns true if \a other points to the same result as this iterator;
451 otherwise returns false.
452
453 \sa operator!=()
454*/
455
456/*! \fn QFuture::const_iterator &QFuture::const_iterator::operator++()
457
458 The prefix ++ operator (\c{++it}) advances the iterator to the next result
459 in the future and returns an iterator to the new current result.
460
461 Calling this function on QFuture::constEnd() leads to undefined results.
462
463 \sa operator--()
464*/
465
466/*! \fn QFuture::const_iterator QFuture::const_iterator::operator++(int)
467
468 \overload
469
470 The postfix ++ operator (\c{it++}) advances the iterator to the next
471 result in the future and returns an iterator to the previously current
472 result.
473*/
474
475/*! \fn QFuture::const_iterator &QFuture::const_iterator::operator--()
476
477 The prefix -- operator (\c{--it}) makes the preceding result current and
478 returns an iterator to the new current result.
479
480 Calling this function on QFuture::constBegin() leads to undefined results.
481
482 \sa operator++()
483*/
484
485/*! \fn QFuture::const_iterator QFuture::const_iterator::operator--(int)
486
487 \overload
488
489 The postfix -- operator (\c{it--}) makes the preceding result current and
490 returns an iterator to the previously current result.
491*/
492
493/*! \fn QFuture::const_iterator &QFuture::const_iterator::operator+=(int j)
494
495 Advances the iterator by \a j results. (If \a j is negative, the iterator
496 goes backward.)
497
498 \sa operator-=(), operator+()
499*/
500
501/*! \fn QFuture::const_iterator &QFuture::const_iterator::operator-=(int j)
502
503 Makes the iterator go back by \a j results. (If \a j is negative, the
504 iterator goes forward.)
505
506 \sa operator+=(), operator-()
507*/
508
509/*! \fn QFuture::const_iterator QFuture::const_iterator::operator+(int j) const
510
511 Returns an iterator to the results at \a j positions forward from this
512 iterator. (If \a j is negative, the iterator goes backward.)
513
514 \sa operator-(), operator+=()
515*/
516
517/*! \fn QFuture::const_iterator QFuture::const_iterator::operator-(int j) const
518
519 Returns an iterator to the result at \a j positions backward from this
520 iterator. (If \a j is negative, the iterator goes forward.)
521
522 \sa operator+(), operator-=()
523*/
524
525/*! \typedef QFuture::ConstIterator
526
527 Qt-style synonym for QFuture::const_iterator.
528*/
529
530/*!
531 \class QFutureIterator
532 \reentrant
533 \since 4.4
534 \inmodule QtCore
535
536 \brief The QFutureIterator class provides a Java-style const iterator for
537 QFuture.
538
539 QFuture has both \l{Java-style iterators} and \l{STL-style iterators}. The
540 Java-style iterators are more high-level and easier to use than the
541 STL-style iterators; on the other hand, they are slightly less efficient.
542
543 An alternative to using iterators is to use index positions. Some QFuture
544 member functions take an index as their first parameter, making it
545 possible to access results without using iterators.
546
547 QFutureIterator\<T\> allows you to iterate over a QFuture\<T\>. Note that
548 there is no mutable iterator for QFuture (unlike the other Java-style
549 iterators).
550
551 The QFutureIterator constructor takes a QFuture as its argument. After
552 construction, the iterator is located at the very beginning of the result
553 list (i.e. before the first result). Here's how to iterate over all the
554 results sequentially:
555
556 \snippet doc/src/snippets/code/src_corelib_concurrent_qfuture.cpp 1
557
558 The next() function returns the next result (waiting for it to become
559 available, if necessary) from the future and advances the iterator. Unlike
560 STL-style iterators, Java-style iterators point \e between results rather
561 than directly \e at results. The first call to next() advances the iterator
562 to the position between the first and second result, and returns the first
563 result; the second call to next() advances the iterator to the position
564 between the second and third result, and returns the second result; and
565 so on.
566
567 \img javaiterators1.png
568
569 Here's how to iterate over the elements in reverse order:
570
571 \snippet doc/src/snippets/code/src_corelib_concurrent_qfuture.cpp 2
572
573 If you want to find all occurrences of a particular value, use findNext()
574 or findPrevious() in a loop.
575
576 Multiple iterators can be used on the same future. If the future is
577 modified while a QFutureIterator is active, the QFutureIterator will
578 continue iterating over the original future, ignoring the modified copy.
579
580 \sa QFuture::const_iterator, QFuture
581*/
582
583/*!
584 \fn QFutureIterator::QFutureIterator(const QFuture<T> &future)
585
586 Constructs an iterator for traversing \a future. The iterator is set to be
587 at the front of the result list (before the first result).
588
589 \sa operator=()
590*/
591
592/*! \fn QFutureIterator &QFutureIterator::operator=(const QFuture<T> &future)
593
594 Makes the iterator operate on \a future. The iterator is set to be at the
595 front of the result list (before the first result).
596
597 \sa toFront(), toBack()
598*/
599
600/*! \fn void QFutureIterator::toFront()
601
602 Moves the iterator to the front of the result list (before the first
603 result).
604
605 \sa toBack(), next()
606*/
607
608/*! \fn void QFutureIterator::toBack()
609
610 Moves the iterator to the back of the result list (after the last result).
611
612 \sa toFront(), previous()
613*/
614
615/*! \fn bool QFutureIterator::hasNext() const
616
617 Returns true if there is at least one result ahead of the iterator, e.g.,
618 the iterator is \e not at the back of the result list; otherwise returns
619 false.
620
621 \sa hasPrevious(), next()
622*/
623
624/*! \fn const T &QFutureIterator::next()
625
626 Returns the next result and advances the iterator by one position.
627
628 Calling this function on an iterator located at the back of the result
629 list leads to undefined results.
630
631 \sa hasNext(), peekNext(), previous()
632*/
633
634/*! \fn const T &QFutureIterator::peekNext() const
635
636 Returns the next result without moving the iterator.
637
638 Calling this function on an iterator located at the back of the result
639 list leads to undefined results.
640
641 \sa hasNext(), next(), peekPrevious()
642*/
643
644/*! \fn bool QFutureIterator::hasPrevious() const
645
646 Returns true if there is at least one result ahead of the iterator, e.g.,
647 the iterator is \e not at the front of the result list; otherwise returns
648 false.
649
650 \sa hasNext(), previous()
651*/
652
653/*! \fn const T &QFutureIterator::previous()
654
655 Returns the previous result and moves the iterator back by one position.
656
657 Calling this function on an iterator located at the front of the result
658 list leads to undefined results.
659
660 \sa hasPrevious(), peekPrevious(), next()
661*/
662
663/*! \fn const T &QFutureIterator::peekPrevious() const
664
665 Returns the previous result without moving the iterator.
666
667 Calling this function on an iterator located at the front of the result
668 list leads to undefined results.
669
670 \sa hasPrevious(), previous(), peekNext()
671*/
672
673/*! \fn bool QFutureIterator::findNext(const T &value)
674
675 Searches for \a value starting from the current iterator position forward.
676 Returns true if \a value is found; otherwise returns false.
677
678 After the call, if \a value was found, the iterator is positioned just
679 after the matching result; otherwise, the iterator is positioned at the
680 back of the result list.
681
682 \sa findPrevious()
683*/
684
685/*! \fn bool QFutureIterator::findPrevious(const T &value)
686
687 Searches for \a value starting from the current iterator position
688 backward. Returns true if \a value is found; otherwise returns false.
689
690 After the call, if \a value was found, the iterator is positioned just
691 before the matching result; otherwise, the iterator is positioned at the
692 front of the result list.
693
694 \sa findNext()
695*/
Note: See TracBrowser for help on using the repository browser.