summaryrefslogtreecommitdiffstats
path: root/framework/src/async.h
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2017-12-08 22:46:00 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2017-12-08 22:46:00 +0100
commita860b011d7b12ae17d278d36a30eaa7754b7a2ce (patch)
tree92becfc44c7dca901255282433abb594bff50e2f /framework/src/async.h
parente2520f1208a826f39e958908755efb2e39ee3950 (diff)
downloadkube-a860b011d7b12ae17d278d36a30eaa7754b7a2ce.tar.gz
kube-a860b011d7b12ae17d278d36a30eaa7754b7a2ce.zip
Always guard async callbacks
Diffstat (limited to 'framework/src/async.h')
-rw-r--r--framework/src/async.h40
1 files changed, 40 insertions, 0 deletions
diff --git a/framework/src/async.h b/framework/src/async.h
new file mode 100644
index 00000000..34233244
--- /dev/null
+++ b/framework/src/async.h
@@ -0,0 +1,40 @@
1/*
2 Copyright (c) 2017 Christian Mollekopf <mollekopf@kolabsys.com>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19#pragma once
20
21#include <QtConcurrent/QtConcurrentRun>
22#include <QFuture>
23#include <QFutureWatcher>
24#include <QPointer>
25
26template<typename T>
27void asyncRun(QObject *object, std::function<T()> run, std::function<void(T)> continuation)
28{
29 auto guard = QPointer<QObject>{object};
30 auto future = QtConcurrent::run(run);
31 auto watcher = new QFutureWatcher<T>;
32 QObject::connect(watcher, &QFutureWatcher<T>::finished, watcher, [watcher, continuation, guard]() {
33 if (guard) {
34 continuation(watcher->future().result());
35 }
36 delete watcher;
37 });
38 watcher->setFuture(future);
39}
40