Details
-
Suggestion
-
Resolution: Duplicate
-
Not Evaluated
-
6.0
-
None
Description
When setting up async pipeline, sometimes the results of the QFuture are already known. Having a completed future allows users to reuse the existing async pipeline. completed() can be implemented as a static function inside of QFuture to create a completed QFuture(). This prevents users from modifying the value of a QFuture return by QtConcurrent::run() or other similar functions. For example:
QFuture<int> myFuture = QFuture<int>::completed(5); myFuture.then([](int printVar) { //This should run right away because myFuture is completed() qDebug() << printVar; }
QFuture::completed() should be easy to implement with a QPromise, but I'm not sure if there's lighter weight solution inside of QFuture.
An example implementation of this is located from lines 1742 to 1765 of h[https://github.com/vpicaver/asyncfuture/blob/master/asyncfuture.h|asyncfuture.h]. Here's the implementation:
template <typename T> QFuture<T> completed(const T &val) { QFutureInterface<T> fi; fi.setProgressRange(0, 1); fi.reportFinished(&val); return QFuture<T>(&fi); }
QFuture<T>::completed() should also support QList<T> for setting multiple results.
Attachments
Issue Links
- duplicates
-
QTBUG-86713 Add support for creating a ready QFuture
-
- Closed
-