summaryrefslogtreecommitdiffstats
path: root/common/synclistresult.h
diff options
context:
space:
mode:
Diffstat (limited to 'common/synclistresult.h')
-rw-r--r--common/synclistresult.h54
1 files changed, 54 insertions, 0 deletions
diff --git a/common/synclistresult.h b/common/synclistresult.h
new file mode 100644
index 0000000..5fa0efd
--- /dev/null
+++ b/common/synclistresult.h
@@ -0,0 +1,54 @@
1#pragma once
2
3#include <QList>
4#include <functional>
5#include <QSharedPointer>
6#include <clientapi.h>
7
8namespace async {
9
10/*
11* A result set specialization that provides a syncronous list.
12*
13* Only for testing purposes.
14*
15* WARNING: The nested eventloop can cause all sorts of trouble. Use only in testing code.
16*/
17template<class T>
18class SyncListResult : public QList<T> {
19public:
20 SyncListResult(const QSharedPointer<ResultEmitter<T> > &emitter)
21 :QList<T>(),
22 mComplete(false),
23 mEmitter(emitter)
24 {
25 emitter->onAdded([this](const T &value) {
26 this->append(value);
27 });
28 emitter->onComplete([this]() {
29 mComplete = true;
30 if (eventLoopAborter) {
31 eventLoopAborter();
32 //Be safe in case of a second invocation of the complete handler
33 eventLoopAborter = std::function<void()>();
34 }
35 });
36 emitter->onClear([this]() {
37 this->clear();
38 });
39 }
40
41 void exec()
42 {
43 QEventLoop eventLoop;
44 eventLoopAborter = [&eventLoop]() { eventLoop.quit(); };
45 eventLoop.exec();
46 }
47
48private:
49 bool mComplete;
50 QSharedPointer<ResultEmitter<T> > mEmitter;
51 std::function<void()> eventLoopAborter;
52};
53
54}