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

QPromise: Provide a safe cast to QPromise<void> & type

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Unresolved
    • P3: Somewhat important
    • None
    • 6.2.0
    • Core: QtConcurrent
    • None
    • All

    Description

      This issue was hit many times already when trying to migrate from Utils::runAsync() using QFutureInterface<T> into QtConcurrent::run() using QPromise<T> inside QtCreator.

      It should be always possibile to access the QPromise<void> API from QPromise<T> object.
      The responsibility of the QPromise<void> is mainly to check for cancel of for progress reporting, while QPromise<T> adds a responsibility of delivering the results.

      The example use case:

      // The responsibility of the promise passed here is to check for cancel and report progress.
      // Moreover, this helper may live in some other library and may not have access to MyType at all.
      void helper(QPromise<void> &promise)
      {
          ...
          while (...) {
              if (promise.isCanceled())
                  return;
              promise.setProgressValue(i);
              ....
          }
          ....
      }
      
      // The responsibility of the promise passed here is to check for cancel, to report progress and to deliver result.
      void asyncTask1(QPromise<MyType> &promise)
      {
          ...
          helper(promise); // Can't pass QPromise<void> here.
          ...
      }
      
      // Same here, we reuse helper(), but work on different type of QPromise
      void asyncTask2(QPromise<MyOtherType> &promise)
      {
          ...
          helper(promise); // Can't pass QPromise<void> here.
          ...
          promise.addResult(...);
      }
      
      // Main thread
      void runAsync()
      {
          ....
          auto future1 = QtConcurrent::run(asyncTask1);
          auto future2 = QtConcurrent::run(asyncTask2);
           ....
      }
      

      As a quick workaround on the user side I wondered about:

      template <typename T>
      QPromise<void> &toVoidPromise(QPromise<T> &promise)
      {
          return reinterpret_cast<QPromise<void> &>(promise);
      }
      

      But I'm really not sure if that's guaranteed to work in 100% correctly.

      Attachments

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

        Activity

          People

            manordheim Mårten Nordheim
            jkobus Jarek Kobus
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes