File indexing completed on 2024-05-12 05:28:16
0001 // SPDX-FileCopyrightText: 2017 Christian Mollekopf <mollekopf@kolabsys.com> 0002 // SPDX-License-Identifier: LGPL-2.0-or-later 0003 0004 #pragma once 0005 0006 #include <QFuture> 0007 #include <QFutureWatcher> 0008 #include <QPointer> 0009 #include <QtConcurrent/QtConcurrentRun> 0010 0011 template<typename T> 0012 void asyncRun(QObject *object, std::function<T()> run, std::function<void(T)> continuation) 0013 { 0014 auto guard = QPointer<QObject>{object}; 0015 auto future = QtConcurrent::run(run); 0016 auto watcher = new QFutureWatcher<T>; 0017 QObject::connect(watcher, &QFutureWatcher<T>::finished, watcher, [watcher, continuation, guard]() { 0018 if (guard) { 0019 continuation(watcher->future().result()); 0020 } 0021 delete watcher; 0022 }); 0023 watcher->setFuture(future); 0024 }