Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-86724

QFuture should be able to be completed with a known value

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Duplicate
    • Not Evaluated
    • 6.1
    • 6.0
    • Core: QtConcurrent
    • None
    • All

    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

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              sonakur Sona Kurazyan
              pschuchardt Philip Schuchardt
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes