CopperSpice API  1.9.1
QFuture< T > Class Template Reference

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

Classes

class  const_iterator
 Implements an STL style const iterator for QFuture More...
 

Public Methods

 QFuture ()
 
 QFuture (const QFuture &other)
 
 ~QFuture ()
 
const_iterator begin () const
 
void cancel ()
 
const_iterator constBegin () const
 
const_iterator constEnd () const
 
const_iterator end () const
 
bool isCanceled () const
 
bool isFinished () const
 
bool isPaused () const
 
bool isResultReadyAt (int index) const
 
bool isRunning () const
 
bool isStarted () const
 
 operator T () const
 
bool operator!= (const QFuture &other) const
 
QFuture< T > & operator= (const QFuture &other)
 
bool operator== (const QFuture &other) const
 
void pause ()
 
int progressMaximum () const
 
int progressMinimum () const
 
QString progressText () const
 
int progressValue () const
 
result () const
 
resultAt (int index) const
 
int resultCount () const
 
QList< T > results () const
 
void resume ()
 
void setPaused (bool paused)
 
void togglePaused ()
 
void waitForFinished ()
 

Friends

class QFutureWatcher< T >
 

Detailed Description

template<typename T>
class QFuture< T >

The QFuture class represents the result of an asynchronous computation. To start a computation use one of the APIs in QtConcurrent.

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 (QFutureIterator) and an STL-style iterators (QFuture::const_iterator). Using these iterators is another way to access results in the future.

QFuture also offers ways to interact with a running computation. For instance, the computation can be canceled by calling the method cancel( . To pause the computation use setPaused() or one of the pause(), resume(), or togglePaused() methods. Not all asynchronous computations can be canceled or paused. For example, the future returned by QtConcurrent::run() can not be canceled, but the future returned by QtConcurrent::mappedReduced() can be canceled.

Progress information is provided by the progressValue(), progressMinimum(), progressMaximum(), and progressText() functions. The waitForFinished() method 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() methods.

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, not the actual result data.

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

See also
QFutureWatcher, Concurrent Programming

Constructor & Destructor Documentation

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

Constructs an empty QFuture.

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

Constructs a copy of other.

See also
operator=()
template<typename T >
QFuture< T >::~QFuture ( )
inline

Destroys the QFuture object.

This methods does not wait for or cancel the asynchronous computation. Use waitForFinished() or QFutureSynchronizer when you need to ensure the computation is completed before the future is destroyed.

Method Documentation

template<typename T >
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()
template<typename T >
void QFuture< T >::cancel ( )
inline

Cancels the asynchronous computation represented by this future. Note, the cancellation is asynchronous. Use waitForFinished() after calling cancel() when you need synchronous cancellation.

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() can not be canceled; but the future returned by QtConcurrent::mappedReduced() can.

template<typename T >
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()
template<typename T >
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()
template<typename T >
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()
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.

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

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

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()
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()
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.

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.

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()
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.

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

Copy assigns from other and returns a reference to this object.

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.

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()
template<typename T >
int QFuture< T >::progressMaximum ( ) const
inline

Returns the maximum progressValue().

See also
progressValue(), progressMinimum()
template<typename T >
int QFuture< T >::progressMinimum ( ) const
inline

Returns the minimum progressValue().

See also
progressValue(), progressMaximum()
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.

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()
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()
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()
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()
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()
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()
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() can not be paused; but the future returned by QtConcurrent::mappedReduced() can.

See also
isPaused(), pause(), resume(), 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()
template<typename T >
void QFuture< T >::waitForFinished ( )
inline

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