summaryrefslogtreecommitdiffstats
path: root/common/resultset.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'common/resultset.cpp')
-rw-r--r--common/resultset.cpp134
1 files changed, 134 insertions, 0 deletions
diff --git a/common/resultset.cpp b/common/resultset.cpp
new file mode 100644
index 0000000..6e1479a
--- /dev/null
+++ b/common/resultset.cpp
@@ -0,0 +1,134 @@
1/*
2 * Copyright (C) 2016 Christian Mollekopf <chrigi_1@fastmail.fm>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19#include "resultset.h"
20
21#include "common/log.h"
22
23ResultSet::ResultSet()
24 : mIt(nullptr)
25{
26
27}
28
29ResultSet::ResultSet(const ValueGenerator &generator, const SkipValue &skip)
30 : mIt(nullptr),
31 mValueGenerator(generator),
32 mSkip(skip)
33{
34
35}
36
37ResultSet::ResultSet(const IdGenerator &generator)
38 : mIt(nullptr),
39 mGenerator(generator),
40 mSkip([this]() {
41 next();
42 })
43{
44
45}
46
47ResultSet::ResultSet(const QVector<QByteArray> &resultSet)
48 : mResultSet(resultSet),
49 mIt(mResultSet.constBegin()),
50 mSkip([this]() {
51 if (mIt != mResultSet.constEnd()) {
52 mIt++;
53 }
54 }),
55 mFirst(true)
56{
57
58}
59
60ResultSet::ResultSet(const ResultSet &other)
61 : mResultSet(other.mResultSet),
62 mIt(nullptr),
63 mFirst(true)
64{
65 if (other.mValueGenerator) {
66 mValueGenerator = other.mValueGenerator;
67 mSkip = other.mSkip;
68 } else if (other.mGenerator) {
69 mGenerator = other.mGenerator;
70 mSkip = [this]() {
71 next();
72 };
73 } else {
74 mResultSet = other.mResultSet;
75 mIt = mResultSet.constBegin();
76 mSkip = [this]() {
77 if (mIt != mResultSet.constEnd()) {
78 mIt++;
79 }
80 };
81 }
82}
83
84bool ResultSet::next()
85{
86 if (mIt) {
87 if (mIt != mResultSet.constEnd() && !mFirst) {
88 mIt++;
89 }
90 mFirst = false;
91 return mIt != mResultSet.constEnd();
92 } else if (mGenerator) {
93 Q_ASSERT(mGenerator);
94 mCurrentValue = mGenerator();
95 if (!mCurrentValue.isNull()) {
96 return true;
97 }
98 } else {
99 next([](const Sink::ApplicationDomain::ApplicationDomainType::Ptr &value, Sink::Operation){ return false; });
100 }
101 return false;
102}
103
104bool ResultSet::next(std::function<bool(const Sink::ApplicationDomain::ApplicationDomainType::Ptr &value, Sink::Operation)> callback)
105{
106 Q_ASSERT(mValueGenerator);
107 return mValueGenerator(callback);
108}
109
110void ResultSet::skip(int number)
111{
112 Q_ASSERT(mSkip);
113 for (int i = 0; i < number; i++) {
114 mSkip();
115 }
116}
117
118QByteArray ResultSet::id()
119{
120 if (mIt) {
121 if (mIt == mResultSet.constEnd()) {
122 return QByteArray();
123 }
124 Q_ASSERT(mIt != mResultSet.constEnd());
125 return *mIt;
126 } else {
127 return mCurrentValue;
128 }
129}
130
131bool ResultSet::isEmpty()
132{
133 return mResultSet.isEmpty();
134}