summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--async/CMakeLists.txt10
-rw-r--r--async/autotests/CMakeLists.txt8
-rw-r--r--async/autotests/kjobtest.cpp69
-rw-r--r--async/autotests/testkjob.cpp28
-rw-r--r--async/autotests/testkjob.h48
-rw-r--r--async/src/CMakeLists.txt6
-rw-r--r--async/src/async.h47
7 files changed, 213 insertions, 3 deletions
diff --git a/async/CMakeLists.txt b/async/CMakeLists.txt
index 19ac407..a6b53f8 100644
--- a/async/CMakeLists.txt
+++ b/async/CMakeLists.txt
@@ -1,2 +1,12 @@
1project(libasync)
2
3option(WITH_KJOB "Enable native support for KJob in libasync API (enabled by default)" ON)
4
5if (WITH_KJOB)
6 set(MINUMUM_FRAMEWORKS_VERSION "5.8.0")
7 find_package(KF5CoreAddons REQUIRED ${MINUMUM_FRAMEWORKS_VERSION})
8 add_definitions(-DWITH_KJOB)
9endif()
10
1add_subdirectory(src) 11add_subdirectory(src)
2add_subdirectory(autotests) \ No newline at end of file 12add_subdirectory(autotests) \ No newline at end of file
diff --git a/async/autotests/CMakeLists.txt b/async/autotests/CMakeLists.txt
index a2bedc8..8116f13 100644
--- a/async/autotests/CMakeLists.txt
+++ b/async/autotests/CMakeLists.txt
@@ -2,4 +2,10 @@ include_directories(../src ${CMAKE_CURRENT_BINARY_DIR})
2 2
3add_executable(asynctest asynctest.cpp) 3add_executable(asynctest asynctest.cpp)
4qt5_use_modules(asynctest Test) 4qt5_use_modules(asynctest Test)
5target_link_libraries(asynctest akonadi2async Qt5::Core Qt5::Test) \ No newline at end of file 5target_link_libraries(asynctest akonadi2async Qt5::Core Qt5::Test)
6
7if (WITH_KJOB)
8 add_executable(kjobtest kjobtest.cpp testkjob.cpp)
9 qt5_use_modules(kjobtest Test)
10 target_link_libraries(kjobtest akonadi2async Qt5::Core Qt5::Test KF5::CoreAddons)
11endif () \ No newline at end of file
diff --git a/async/autotests/kjobtest.cpp b/async/autotests/kjobtest.cpp
new file mode 100644
index 0000000..be92d68
--- /dev/null
+++ b/async/autotests/kjobtest.cpp
@@ -0,0 +1,69 @@
1/*
2 * Copyright 2015 Daniel Vrátil <dvratil@redhat.com>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License or (at your option) version 3 or any later version
8 * accepted by the membership of KDE e.V. (or its successor approved
9 * by the membership of KDE e.V.), which shall act as a proxy
10 * defined in Section 14 of version 3 of the license.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#include "../src/async.h"
23#include "testkjob.h"
24
25#include <QtTest/QTest>
26
27class KJobTest : public QObject
28{
29 Q_OBJECT
30
31public:
32 KJobTest()
33 {}
34
35 ~KJobTest()
36 {}
37
38private Q_SLOTS:
39 void testSingleKJob();
40 void testKJobChain();
41
42};
43
44void KJobTest::testSingleKJob()
45{
46 auto job = Async::start<int, TestKJob, &TestKJob::result, int>();
47
48 auto future = job.exec(42);
49 future.waitForFinished();
50
51 QVERIFY(future.isFinished());
52 QCOMPARE(future.value(), 42);
53}
54
55void KJobTest::testKJobChain()
56{
57 auto job = Async::start<int, TestKJob, &TestKJob::result, int>()
58 .then<int, TestKJob, &TestKJob::result, int>();
59
60 auto future = job.exec(42);
61 future.waitForFinished();
62
63 QVERIFY(future.isFinished());
64 QCOMPARE(future.value(), 42);
65}
66
67QTEST_MAIN(KJobTest)
68
69#include "kjobtest.moc" \ No newline at end of file
diff --git a/async/autotests/testkjob.cpp b/async/autotests/testkjob.cpp
new file mode 100644
index 0000000..b86f913
--- /dev/null
+++ b/async/autotests/testkjob.cpp
@@ -0,0 +1,28 @@
1#include "testkjob.h"
2
3TestKJob::TestKJob(int result)
4 : mResult(result)
5{
6 connect(&mTimer, &QTimer::timeout,
7 this, &TestKJob::onTimeout);
8 mTimer.setSingleShot(true);
9 mTimer.setInterval(200);
10}
11
12TestKJob::~TestKJob()
13{}
14
15void TestKJob::start()
16{
17 mTimer.start();
18}
19
20int TestKJob::result()
21{
22 return mResult;
23}
24
25void TestKJob::onTimeout()
26{
27 emitResult();
28} \ No newline at end of file
diff --git a/async/autotests/testkjob.h b/async/autotests/testkjob.h
new file mode 100644
index 0000000..eead98e
--- /dev/null
+++ b/async/autotests/testkjob.h
@@ -0,0 +1,48 @@
1/*
2 * Copyright 2015 Daniel Vrátil <dvratil@redhat.com>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License or (at your option) version 3 or any later version
8 * accepted by the membership of KDE e.V. (or its successor approved
9 * by the membership of KDE e.V.), which shall act as a proxy
10 * defined in Section 14 of version 3 of the license.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#ifndef TESTKJOB_H
23#define TESTKJOB_H
24
25#include <KJob>
26#include <QTimer>
27
28class TestKJob : public KJob
29{
30 Q_OBJECT
31
32public:
33 TestKJob(int result);
34 ~TestKJob();
35
36 void start();
37
38 int result();
39
40private Q_SLOTS:
41 void onTimeout();
42
43private:
44 int mResult;
45 QTimer mTimer;
46};
47
48#endif // TESTKJOB_H \ No newline at end of file
diff --git a/async/src/CMakeLists.txt b/async/src/CMakeLists.txt
index 7d17f2b..6f8ab63 100644
--- a/async/src/CMakeLists.txt
+++ b/async/src/CMakeLists.txt
@@ -8,5 +8,9 @@ set(async_SRCS
8) 8)
9 9
10add_library(${PROJECT_NAME} SHARED ${async_SRCS}) 10add_library(${PROJECT_NAME} SHARED ${async_SRCS})
11target_link_libraries(${PROJECT_NAME} Qt5::Core) 11target_link_libraries(${PROJECT_NAME} PUBLIC Qt5::Core)
12if (WITH_KJOB)
13 target_link_libraries(${PROJECT_NAME} PUBLIC KF5::CoreAddons)
14endif ()
15
12install(TARGETS ${PROJECT_NAME} ${KDE_INSTALL_TARGETS_DEFAULT_ARGS}) 16install(TARGETS ${PROJECT_NAME} ${KDE_INSTALL_TARGETS_DEFAULT_ARGS})
diff --git a/async/src/async.h b/async/src/async.h
index 2be1260..8da3daa 100644
--- a/async/src/async.h
+++ b/async/src/async.h
@@ -33,6 +33,11 @@
33 33
34#include <QDebug> 34#include <QDebug>
35 35
36#ifdef WITH_KJOB
37#include <KJob>
38#endif
39
40
36/* 41/*
37 * TODO: instead of passing the future objects callbacks could be provided for result reporting (we can still use the future object internally 42 * TODO: instead of passing the future objects callbacks could be provided for result reporting (we can still use the future object internally
38 */ 43 */
@@ -196,10 +201,15 @@ Job<Out, In ...> start(ThenTask<Out, In ...> func);
196template<typename Out, typename ... In> 201template<typename Out, typename ... In>
197Job<Out, In ...> start(SyncThenTask<Out, In ...> func); 202Job<Out, In ...> start(SyncThenTask<Out, In ...> func);
198 203
204#ifdef WITH_KJOB
205template<typename ReturnType, typename KJobType, ReturnType (KJobType::*KJobResultMethod)(), typename ... Args>
206Job<ReturnType, Args ...> start();
207#endif
208
199 209
200/** 210/**
201 * A null job. 211 * A null job.
202 * 212 *
203 * An async noop. 213 * An async noop.
204 * 214 *
205 */ 215 */
@@ -283,6 +293,11 @@ class Job : public JobBase
283 template<typename OutOther, typename ... InOther> 293 template<typename OutOther, typename ... InOther>
284 friend Job<OutOther, InOther ...> start(Async::SyncThenTask<OutOther, InOther ...> func); 294 friend Job<OutOther, InOther ...> start(Async::SyncThenTask<OutOther, InOther ...> func);
285 295
296#ifdef WITH_KJOB
297 template<typename ReturnType, typename KJobType, ReturnType (KJobType::*KJobResultMethod)(), typename ... Args>
298 friend Job<ReturnType, Args ...> start();
299#endif
300
286public: 301public:
287 template<typename OutOther, typename ... InOther> 302 template<typename OutOther, typename ... InOther>
288 Job<OutOther, InOther ...> then(ThenTask<OutOther, InOther ...> func, ErrorHandler errorFunc = ErrorHandler()) 303 Job<OutOther, InOther ...> then(ThenTask<OutOther, InOther ...> func, ErrorHandler errorFunc = ErrorHandler())
@@ -304,6 +319,14 @@ public:
304 return then<OutOther, InOther ...>(nestedJobWrapper<OutOther, InOther ...>(otherJob), errorFunc); 319 return then<OutOther, InOther ...>(nestedJobWrapper<OutOther, InOther ...>(otherJob), errorFunc);
305 } 320 }
306 321
322#ifdef WITH_KJOB
323 template<typename ReturnType, typename KJobType, ReturnType (KJobType::*KJobResultMethod)(), typename ... Args>
324 Job<ReturnType, Args ...> then()
325 {
326 return start<ReturnType, KJobType, KJobResultMethod, Args ...>();
327 }
328#endif
329
307 template<typename OutOther, typename InOther> 330 template<typename OutOther, typename InOther>
308 Job<OutOther, InOther> each(EachTask<OutOther, InOther> func, ErrorHandler errorFunc = ErrorHandler()) 331 Job<OutOther, InOther> each(EachTask<OutOther, InOther> func, ErrorHandler errorFunc = ErrorHandler())
309 { 332 {
@@ -442,6 +465,28 @@ Job<Out, In ...> start(SyncThenTask<Out, In ...> func)
442 new Private::SyncThenExecutor<Out, In ...>(func, ErrorHandler(), Private::ExecutorBasePtr()))); 465 new Private::SyncThenExecutor<Out, In ...>(func, ErrorHandler(), Private::ExecutorBasePtr())));
443} 466}
444 467
468#ifdef WITH_KJOB
469template<typename ReturnType, typename KJobType, ReturnType (KJobType::*KJobResultMethod)(), typename ... Args>
470Job<ReturnType, Args ...> start()
471{
472 return Job<ReturnType, Args ...>(Private::ExecutorBasePtr(
473 new Private::ThenExecutor<ReturnType, Args ...>([](const Args & ... args, Async::Future<ReturnType> &future)
474 {
475 KJobType *job = new KJobType(args ...);
476 job->connect(job, &KJob::finished,
477 [&future](KJob *job) {
478 if (job->error()) {
479 future.setError(job->error(), job->errorString());
480 } else {
481 future.setValue((static_cast<KJobType*>(job)->*KJobResultMethod)());
482 future.setFinished();
483 }
484 });
485 job->start();
486 }, ErrorHandler(), Private::ExecutorBasePtr())));
487}
488#endif
489
445template<typename Out> 490template<typename Out>
446Job<Out> null() 491Job<Out> null()
447{ 492{