Qt 4.8
Classes | Public Types | Public Functions | Public Variables | Friends | List of all members
QFuture< T > Class Template Reference

The QFuture class represents the result of an asynchronous computation. More...

#include <qfuture.h>

Classes

class  const_iterator
 The QFuture::const_iterator class provides an STL-style const iterator for QFuture. More...
 

Public Types

typedef const_iterator ConstIterator
 Qt-style synonym for QFuture::const_iterator. More...
 

Public Functions

const_iterator begin () const
 Returns a const STL-style iterator pointing to the first result in the future. More...
 
void cancel ()
 Cancels the asynchronous computation represented by this future. More...
 
const_iterator constBegin () const
 Returns a const STL-style iterator pointing to the first result in the future. More...
 
const_iterator constEnd () const
 Returns a const STL-style iterator pointing to the imaginary result after the last result in the future. More...
 
const_iterator end () const
 Returns a const STL-style iterator pointing to the imaginary result after the last result in the future. More...
 
bool isCanceled () const
 Returns true if the asynchronous computation has been canceled with the cancel() function; otherwise returns false. More...
 
bool isFinished () const
 Returns true if the asynchronous computation represented by this future has finished; otherwise returns false. More...
 
bool isPaused () const
 Returns true if the asynchronous computation has been paused with the pause() function; otherwise returns false. More...
 
bool isResultReadyAt (int resultIndex) const
 Returns true if the result at index is immediately available; otherwise returns false. More...
 
bool isRunning () const
 Returns true if the asynchronous computation represented by this future is currently running; otherwise returns false. More...
 
bool isStarted () const
 Returns true if the asynchronous computation represented by this future has been started; otherwise returns false. More...
 
 operator T () const
 Returns the first result in the future. More...
 
bool operator!= (const QFuture &other) const
 Returns true if other is not a copy of this future; otherwise returns false. More...
 
QFutureoperator= (const QFuture &other)
 Assigns other to this future and returns a reference to this future. More...
 
bool operator== (const QFuture &other) const
 Returns true if other is a copy of this future; otherwise returns false. More...
 
void pause ()
 Pauses the asynchronous computation represented by this future. More...
 
int progressMaximum () const
 Returns the maximum progressValue(). More...
 
int progressMinimum () const
 Returns the minimum progressValue(). More...
 
QString progressText () const
 Returns the (optional) textual representation of the progress as reported by the asynchronous computation. More...
 
int progressValue () const
 Returns the current progress value, which is between the progressMinimum() and progressMaximum(). More...
 
 QFuture ()
 Constructs an empty future. More...
 
 QFuture (QFutureInterface< T > *p)
 
 QFuture (const QFuture &other)
 Constructs a copy of other. More...
 
result () const
 Returns the first result in the future. More...
 
resultAt (int index) const
 Returns the result at index in the future. More...
 
int resultCount () const
 Returns the number of continuous results available in this future. More...
 
QList< T > results () const
 Returns all results from the future. More...
 
void resume ()
 Resumes the asynchronous computation represented by this future. More...
 
void setPaused (bool paused)
 If paused is true, this function pauses the asynchronous computation represented by the future. More...
 
void togglePaused ()
 Toggles the paused state of the asynchronous computation. More...
 
void waitForFinished ()
 Waits for the asynchronous computation to finish (including cancel()ed computations). More...
 
 ~QFuture ()
 Destroys the future. More...
 

Public Variables

QFutureInterface< T > d
 

Friends

class const_iterator
 
class QFutureWatcher< T >
 

Detailed Description

template<typename T>
class QFuture< T >

The QFuture class represents the result of an asynchronous computation.

Note
This class or function is threadsafe.
Since
4.4

To start a computation, use one of the APIs in the Concurrent Programming {Qt Concurrent} framework.

QFuture allows threads to be synchronized against one or more results which will be ready at a later point in time. The result can be of any type that has a default constructor and a copy constructor. If a result is not available at the time of calling the result(), resultAt(), or results() functions, QFuture will wait until the result becomes available. You can use the isResultReadyAt() function to determine if a result is ready or not. For QFuture objects that report more than one result, the resultCount() function returns the number of continuous results. This means that it is always safe to iterate through the results from 0 to resultCount().

QFuture provides a Java-style iterators{Java-style iterator} (QFutureIterator) and an STL-style iterators{STL-style iterator} (QFuture::const_iterator). Using these iterators is another way to access results in the future.

QFuture also offers ways to interact with a runnning computation. For instance, the computation can be canceled with the cancel() function. To pause the computation, use the setPaused() function or one of the pause(), resume(), or togglePaused() convenience functions. Be aware that not all asynchronous computations can be canceled or paused. For example, the future returned by QtConcurrent::run() cannot be canceled; but the future returned by QtConcurrent::mappedReduced() can.

Progress information is provided by the progressValue(), progressMinimum(), progressMaximum(), and progressText() functions. The waitForFinished() function causes the calling thread to block and wait for the computation to finish, ensuring that all results are available.

The state of the computation represented by a QFuture can be queried using the isCanceled(), isStarted(), isFinished(), isRunning(), or isPaused() functions.

QFuture is a lightweight reference counted class that can be passed by value.

QFuture<void> is specialized to not contain any of the result fetching functions. Any QFuture<T> can be assigned or copied into a QFuture<void> as well. This is useful if only status or progress information is needed

To interact with running tasks using signals and slots, use QFutureWatcher.

See also
QFutureWatcher, {Concurrent Programming}{Qt Concurrent}

Definition at line 64 of file qfuture.h.

Typedefs

◆ ConstIterator

template<typename T>
QFuture< T >::ConstIterator

Qt-style synonym for QFuture::const_iterator.

Definition at line 150 of file qfuture.h.

Constructors and Destructors

◆ QFuture() [1/3]

template<typename T>
QFuture< T >::QFuture ( )
inline

Constructs an empty future.

Definition at line 67 of file qfuture.h.

Referenced by QFutureInterface< T >::future().

69  { }
QFutureInterface< T > d
Definition: qfuture.h:161

◆ QFuture() [2/3]

template<typename T>
QFuture< T >::QFuture ( QFutureInterface< T > *  resultHolder)
inlineexplicit
Warning
This function is not part of the public interface.

Definition at line 70 of file qfuture.h.

71  : d(*p)
72  { }
QFutureInterface< T > d
Definition: qfuture.h:161

◆ QFuture() [3/3]

template<typename T>
QFuture< T >::QFuture ( const QFuture< T > &  other)
inline

Constructs a copy of other.

See also
operator=()

Definition at line 73 of file qfuture.h.

74  : d(other.d)
75  { }
QFutureInterface< T > d
Definition: qfuture.h:161

◆ ~QFuture()

template<typename T>
QFuture< T >::~QFuture ( )
inline

Destroys the future.

Note that this neither waits nor cancels the asynchronous computation. Use waitForFinished() or QFutureSynchronizer when you need to ensure that the computation is completed before the future is destroyed.

Definition at line 76 of file qfuture.h.

Referenced by QFutureInterface< T >::future().

77  { }

Functions

◆ begin()

template<typename T>
QFuture::const_iterator QFuture< T >::begin ( ) const
inline

Returns a const STL-style iterator pointing to the first result in the future.

See also
constBegin(), end()

Definition at line 152 of file qfuture.h.

152 { return const_iterator(this, 0); }
friend class const_iterator
Definition: qfuture.h:149

◆ cancel()

template<typename T>
void QFuture< T >::cancel ( )
inline

Cancels the asynchronous computation represented by this future.

Note that the cancelation is asynchronous. Use waitForFinished() after calling cancel() when you need synchronous cancelation.

Results currently available may still be accessed on a canceled future, but new results will not become available after calling this function. Any QFutureWatcher object that is watching this future will not deliver progress and result ready signals on a canceled future.

Be aware that not all asynchronous computations can be canceled. For example, the future returned by QtConcurrent::run() cannot be canceled; but the future returned by QtConcurrent::mappedReduced() can.

Definition at line 83 of file qfuture.h.

Referenced by QFutureInterface< T >::future().

83 { d.cancel(); }
QFutureInterface< T > d
Definition: qfuture.h:161

◆ constBegin()

template<typename T>
QFuture::const_iterator QFuture< T >::constBegin ( ) const
inline

Returns a const STL-style iterator pointing to the first result in the future.

See also
begin(), constEnd()

Definition at line 153 of file qfuture.h.

153 { return const_iterator(this, 0); }
friend class const_iterator
Definition: qfuture.h:149

◆ constEnd()

template<typename T>
QFuture::const_iterator QFuture< T >::constEnd ( ) const
inline

Returns a const STL-style iterator pointing to the imaginary result after the last result in the future.

See also
constBegin(), end()

Definition at line 155 of file qfuture.h.

155 { return const_iterator(this, -1); }
friend class const_iterator
Definition: qfuture.h:149

◆ end()

template<typename T>
QFuture::const_iterator QFuture< T >::end ( ) const
inline

Returns a const STL-style iterator pointing to the imaginary result after the last result in the future.

See also
begin(), constEnd()

Definition at line 154 of file qfuture.h.

154 { return const_iterator(this, -1); }
friend class const_iterator
Definition: qfuture.h:149

◆ isCanceled()

template<typename T>
bool QFuture< T >::isCanceled ( ) const
inline

Returns true if the asynchronous computation has been canceled with the cancel() function; otherwise returns false.

Be aware that the computation may still be running even though this function returns true. See cancel() for more details.

Definition at line 84 of file qfuture.h.

Referenced by QFutureInterface< T >::future().

84 { return d.isCanceled(); }
QFutureInterface< T > d
Definition: qfuture.h:161

◆ isFinished()

template<typename T>
bool QFuture< T >::isFinished ( ) const
inline

Returns true if the asynchronous computation represented by this future has finished; otherwise returns false.

Definition at line 93 of file qfuture.h.

Referenced by QFutureInterface< T >::future().

93 { return d.isFinished(); }
QFutureInterface< T > d
Definition: qfuture.h:161

◆ isPaused()

template<typename T>
bool QFuture< T >::isPaused ( ) const
inline

Returns true if the asynchronous computation has been paused with the pause() function; otherwise returns false.

Be aware that the computation may still be running even though this function returns true. See setPaused() for more details.

See also
setPaused(), togglePaused()

Definition at line 87 of file qfuture.h.

Referenced by QFutureInterface< T >::future().

87 { return d.isPaused(); }
QFutureInterface< T > d
Definition: qfuture.h:161

◆ isResultReadyAt()

template<typename T>
bool QFuture< T >::isResultReadyAt ( int  index) const
inline

Returns true if the result at index is immediately available; otherwise returns false.

See also
resultAt(), resultCount()

Definition at line 105 of file qfuture.h.

105 { return d.isResultReadyAt(resultIndex); }
QFutureInterface< T > d
Definition: qfuture.h:161

◆ isRunning()

template<typename T>
bool QFuture< T >::isRunning ( ) const
inline

Returns true if the asynchronous computation represented by this future is currently running; otherwise returns false.

Definition at line 94 of file qfuture.h.

Referenced by QFutureInterface< T >::future(), and QFuture< T >::const_iterator::operator!=().

94 { return d.isRunning(); }
QFutureInterface< T > d
Definition: qfuture.h:161

◆ isStarted()

template<typename T>
bool QFuture< T >::isStarted ( ) const
inline

Returns true if the asynchronous computation represented by this future has been started; otherwise returns false.

Definition at line 92 of file qfuture.h.

Referenced by QFutureInterface< T >::future().

92 { return d.isStarted(); }
QFutureInterface< T > d
Definition: qfuture.h:161

◆ operator T()

template<typename T>
QFuture< T >::operator T ( ) const
inline

Returns the first result in the future.

If the result is not immediately available, this function will block and wait for the result to become available. This is a convenience method for calling result() or resultAt(0).

See also
result(), resultAt(), results()

Definition at line 107 of file qfuture.h.

107 { return result(); }
T result() const
Returns the first result in the future.
Definition: qfuture.h:172

◆ operator!=()

template<typename T>
bool QFuture< T >::operator!= ( const QFuture< T > &  other) const
inline

Returns true if other is not a copy of this future; otherwise returns false.

Definition at line 81 of file qfuture.h.

81 { return (d != other.d); }
QFutureInterface< T > d
Definition: qfuture.h:161

◆ operator=()

template<typename T >
QFuture< T > & QFuture< T >::operator= ( const QFuture< T > &  other)
inline

Assigns other to this future and returns a reference to this future.

Definition at line 165 of file qfuture.h.

Referenced by QFutureInterface< T >::future(), and QFuture< T >::~QFuture().

166 {
167  d = other.d;
168  return *this;
169 }
QFutureInterface< T > d
Definition: qfuture.h:161

◆ operator==()

template<typename T>
bool QFuture< T >::operator== ( const QFuture< T > &  other) const
inline

Returns true if other is a copy of this future; otherwise returns false.

Definition at line 80 of file qfuture.h.

80 { return (d == other.d); }
QFutureInterface< T > d
Definition: qfuture.h:161

◆ pause()

template<typename T>
void QFuture< T >::pause ( )
inline

Pauses the asynchronous computation represented by this future.

This is a convenience method that simply calls setPaused(true).

See also
resume()

Definition at line 88 of file qfuture.h.

Referenced by QFutureInterface< T >::future().

88 { setPaused(true); }
void setPaused(bool paused)
If paused is true, this function pauses the asynchronous computation represented by the future...
Definition: qfuture.h:86

◆ progressMaximum()

template<typename T>
int QFuture< T >::progressMaximum ( ) const
inline

Returns the maximum progressValue().

See also
progressValue(), progressMinimum()

Definition at line 99 of file qfuture.h.

Referenced by QFutureInterface< T >::future().

99 { return d.progressMaximum(); }
QFutureInterface< T > d
Definition: qfuture.h:161

◆ progressMinimum()

template<typename T>
int QFuture< T >::progressMinimum ( ) const
inline

Returns the minimum progressValue().

See also
progressValue(), progressMaximum()

Definition at line 98 of file qfuture.h.

Referenced by QFutureInterface< T >::future().

98 { return d.progressMinimum(); }
QFutureInterface< T > d
Definition: qfuture.h:161

◆ progressText()

template<typename T>
QString QFuture< T >::progressText ( ) const
inline

Returns the (optional) textual representation of the progress as reported by the asynchronous computation.

Be aware that not all computations provide a textual representation of the progress, and as such, this function may return an empty string.

Definition at line 100 of file qfuture.h.

Referenced by QFutureInterface< T >::future().

100 { return d.progressText(); }
QFutureInterface< T > d
Definition: qfuture.h:161

◆ progressValue()

template<typename T>
int QFuture< T >::progressValue ( ) const
inline

Returns the current progress value, which is between the progressMinimum() and progressMaximum().

See also
progressMinimum(), progressMaximum()

Definition at line 97 of file qfuture.h.

Referenced by QFutureInterface< T >::future().

97 { return d.progressValue(); }
QFutureInterface< T > d
Definition: qfuture.h:161

◆ result()

template<typename T >
T QFuture< T >::result ( ) const
inline

Returns the first result in the future.

If the result is not immediately available, this function will block and wait for the result to become available. This is a convenience method for calling resultAt(0).

See also
resultAt(), results()

Definition at line 172 of file qfuture.h.

Referenced by QFuture< T >::operator T(), and QFuture< T >::waitForFinished().

173 {
174  d.waitForResult(0);
175  return d.resultReference(0);
176 }
QFutureInterface< T > d
Definition: qfuture.h:161

◆ resultAt()

template<typename T >
T QFuture< T >::resultAt ( int  index) const
inline

Returns the result at index in the future.

If the result is not immediately available, this function will block and wait for the result to become available.

See also
result(), results(), resultCount()

Definition at line 179 of file qfuture.h.

Referenced by QFuture< T >::waitForFinished().

180 {
181  d.waitForResult(index);
182  return d.resultReference(index);
183 }
quint16 index
QFutureInterface< T > d
Definition: qfuture.h:161

◆ resultCount()

template<typename T>
int QFuture< T >::resultCount ( ) const
inline

Returns the number of continuous results available in this future.

The real number of results stored might be different from this value, due to gaps in the result set. It is always safe to iterate through the results from 0 to resultCount().

See also
result(), resultAt(), results()

Definition at line 96 of file qfuture.h.

Referenced by QFutureInterface< T >::future(), and QFuture< T >::const_iterator::operator!=().

96 { return d.resultCount(); }
QFutureInterface< T > d
Definition: qfuture.h:161

◆ results()

template<typename T>
QList< T > QFuture< T >::results ( ) const
inline

Returns all results from the future.

If the results are not immediately available, this function will block and wait for them to become available.

See also
result(), resultAt(), resultCount()

Definition at line 108 of file qfuture.h.

108 { return d.results(); }
QFutureInterface< T > d
Definition: qfuture.h:161

◆ resume()

template<typename T>
void QFuture< T >::resume ( )
inline

Resumes the asynchronous computation represented by this future.

This is a convenience method that simply calls setPaused(false).

See also
pause()

Definition at line 89 of file qfuture.h.

Referenced by QFutureInterface< T >::future().

89 { setPaused(false); }
void setPaused(bool paused)
If paused is true, this function pauses the asynchronous computation represented by the future...
Definition: qfuture.h:86

◆ setPaused()

template<typename T>
void QFuture< T >::setPaused ( bool  paused)
inline

If paused is true, this function pauses the asynchronous computation represented by the future.

If the computation is already paused, this function does nothing. Any QFutureWatcher object that is watching this future will stop delivering progress and result ready signals while the future is paused. Signal delivery will continue once the future is resumed.

If paused is false, this function resumes the asynchronous computation. If the computation was not previously paused, this function does nothing.

Be aware that not all computations can be paused. For example, the future returned by QtConcurrent::run() cannot be paused; but the future returned by QtConcurrent::mappedReduced() can.

See also
pause(), resume(), togglePaused()

Definition at line 86 of file qfuture.h.

Referenced by QFutureInterface< T >::future(), QFuture< T >::pause(), and QFuture< T >::resume().

86 { d.setPaused(paused); }
QFutureInterface< T > d
Definition: qfuture.h:161

◆ togglePaused()

template<typename T>
void QFuture< T >::togglePaused ( )
inline

Toggles the paused state of the asynchronous computation.

In other words, if the computation is currently paused, calling this function resumes it; if the computation is running, it is paused. This is a convenience method for calling setPaused(!isPaused()).

See also
setPaused(), pause(), resume()

Definition at line 90 of file qfuture.h.

Referenced by QFutureInterface< T >::future().

90 { d.togglePaused(); }
QFutureInterface< T > d
Definition: qfuture.h:161

◆ waitForFinished()

template<typename T>
void QFuture< T >::waitForFinished ( )
inline

Waits for the asynchronous computation to finish (including cancel()ed computations).

Definition at line 101 of file qfuture.h.

Referenced by QFutureInterface< T >::future().

101 { d.waitForFinished(); }
QFutureInterface< T > d
Definition: qfuture.h:161

Friends and Related Functions

◆ const_iterator

template<typename T>
friend class const_iterator
friend

Definition at line 149 of file qfuture.h.

◆ QFutureWatcher< T >

template<typename T>
friend class QFutureWatcher< T >
friend

Definition at line 158 of file qfuture.h.

Properties

◆ d

template<typename T>
QFutureInterface<T> QFuture< T >::d
mutable

The documentation for this class was generated from the following files: