diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-12-27 11:51:19 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-12-27 11:51:19 +0100 |
commit | 3df975bd9cd244cb941a3ce5068924ef3a05df4f (patch) | |
tree | 436535ac2732b6b729d8d07f9dd4adcf422d8f87 /common/asyncutils.h | |
parent | e490c4798253a418d9fda17f40eff822f8d6ae36 (diff) | |
download | sink-3df975bd9cd244cb941a3ce5068924ef3a05df4f.tar.gz sink-3df975bd9cd244cb941a3ce5068924ef3a05df4f.zip |
Use KAsync::Job as abstraction for the threading implementation
Diffstat (limited to 'common/asyncutils.h')
-rw-r--r-- | common/asyncutils.h | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/common/asyncutils.h b/common/asyncutils.h new file mode 100644 index 0000000..ddcc37c --- /dev/null +++ b/common/asyncutils.h | |||
@@ -0,0 +1,42 @@ | |||
1 | /* | ||
2 | Copyright (c) 2015 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 <Async/Async> | ||
22 | #include <QtConcurrent/QtConcurrentRun> | ||
23 | #include <QFuture> | ||
24 | #include <QFutureWatcher> | ||
25 | |||
26 | namespace async { | ||
27 | template<typename T> | ||
28 | KAsync::Job<T> run(const std::function<T()> &f) | ||
29 | { | ||
30 | return KAsync::start<T>([f](KAsync::Future<T> &future) { | ||
31 | auto result = QtConcurrent::run(f); | ||
32 | auto watcher = new QFutureWatcher<T>; | ||
33 | watcher->setFuture(result); | ||
34 | QObject::connect(watcher, &QFutureWatcher<T>::finished, watcher, [&future, watcher]() { | ||
35 | future.setValue(watcher->future().result()); | ||
36 | delete watcher; | ||
37 | future.setFinished(); | ||
38 | }); | ||
39 | }); | ||
40 | } | ||
41 | |||
42 | } | ||