PlasmaActivitiesStats

resultset_iterator.cpp
1/*
2 SPDX-FileCopyrightText: 2015, 2016 Ivan Cukic <ivan.cukic(at)kde.org>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include <optional>
8
9namespace KActivities
10{
11namespace Stats
12{
13using namespace Terms;
14
15typedef ResultSet::const_iterator iterator;
16
17// Iterator
18
19class ResultSet_IteratorPrivate
20{
21public:
22 ResultSet_IteratorPrivate(const ResultSet *resultSet, int currentRow = -1)
23 : resultSet(resultSet)
24 , currentRow(currentRow)
25 {
26 updateValue();
27 }
28
29 const ResultSet *resultSet;
30 int currentRow;
31 std::optional<ResultSet::Result> currentValue;
32
33 inline void moveTo(int row)
34 {
35 if (row == currentRow)
36 return;
37 currentRow = row;
38 updateValue();
39 }
40
41 inline void moveBy(int row)
42 {
43 moveTo(currentRow + row);
44 }
45
46 void updateValue()
47 {
48 if (!resultSet || !resultSet->d->query.seek(currentRow)) {
49 currentValue.reset();
50
51 } else {
52 auto value = resultSet->d->currentResult();
53 currentValue = std::move(value);
54 }
55 }
56
57 friend void swap(ResultSet_IteratorPrivate &left, ResultSet_IteratorPrivate &right)
58 {
59 std::swap(left.resultSet, right.resultSet);
60 std::swap(left.currentRow, right.currentRow);
61 std::swap(left.currentValue, right.currentValue);
62 }
63
64 bool operator==(const ResultSet_IteratorPrivate &other) const
65 {
66 bool thisValid = currentValue.has_value();
67 bool otherValid = other.currentValue.has_value();
68
69 return
70 // If one is valid, and the other is not,
71 // they are not equal
72 thisValid != otherValid ? false :
73
74 // If both are invalid, they are equal
75 !thisValid ? true
76 :
77
78 // Otherwise, really compare
79 resultSet == other.resultSet && currentRow == other.currentRow;
80 }
81
82 bool isValid() const
83 {
84 return currentValue.has_value();
85 }
86
87 static bool sameSource(const ResultSet_IteratorPrivate &left, const ResultSet_IteratorPrivate &right)
88 {
89 return left.resultSet == right.resultSet && left.resultSet != nullptr;
90 }
91};
92
93iterator::const_iterator(const ResultSet *resultSet, int currentRow)
94 : d(new ResultSet_IteratorPrivate(resultSet, currentRow))
95{
96}
97
98iterator::const_iterator()
99 : d(new ResultSet_IteratorPrivate(nullptr, -1))
100{
101}
102
103iterator::const_iterator(const const_iterator &source)
104 : d(new ResultSet_IteratorPrivate(source.d->resultSet, source.d->currentRow))
105{
106}
107
108bool iterator::isSourceValid() const
109{
110 return d->resultSet != nullptr;
111}
112
113iterator &iterator::operator=(const const_iterator &source)
114{
115 const_iterator temp(source);
116 swap(*d, *temp.d);
117 return *this;
118}
119
120iterator::~const_iterator()
121{
122 delete d;
123}
124
125iterator::reference iterator::operator*() const
126{
127 return d->currentValue.value();
128}
129
130iterator::pointer iterator::operator->() const
131{
132 return &d->currentValue.value();
133}
134
135// prefix
136iterator &iterator::operator++()
137{
138 d->currentRow++;
139 d->updateValue();
140
141 return *this;
142}
143
144// postfix
145iterator iterator::operator++(int)
146{
147 return const_iterator(d->resultSet, d->currentRow + 1);
148}
149
150// prefix
151iterator &iterator::operator--()
152{
153 d->currentRow--;
154 d->updateValue();
155
156 return *this;
157}
158
159// postfix
160iterator iterator::operator--(int)
161{
162 return const_iterator(d->resultSet, d->currentRow - 1);
163}
164
165iterator ResultSet::begin() const
166{
167 return const_iterator(this, d->database ? 0 : -1);
168}
169
170iterator ResultSet::end() const
171{
172 return const_iterator(this, -1);
173}
174
175iterator iterator::operator+(iterator::difference_type n) const
176{
177 return const_iterator(d->resultSet, d->currentRow + n);
178}
179
180iterator &iterator::operator+=(iterator::difference_type n)
181{
182 d->moveBy(n);
183 return *this;
184}
185
186iterator iterator::operator-(iterator::difference_type n) const
187{
188 return const_iterator(d->resultSet, d->currentRow - n);
189}
190
191iterator &iterator::operator-=(iterator::difference_type n)
192{
193 d->moveBy(-n);
194 return *this;
195}
196
197iterator::reference iterator::operator[](iterator::difference_type n) const
198{
199 return *(*this + n);
200}
201
202// bool iterator::operator==(const const_iterator &right) const
203// {
204// return *d == *right.d;
205// }
206//
207// bool iterator::operator!=(const const_iterator &right) const
208// {
209// return !(*d == *right.d);
210// }
211
212bool operator==(const iterator &left, const iterator &right)
213{
214 return *left.d == *right.d;
215}
216
217bool operator!=(const iterator &left, const iterator &right)
218{
219 return !(*left.d == *right.d);
220}
221
222#define COMPARATOR_IMPL(OP) \
223 bool operator OP(const iterator &left, const iterator &right) \
224 { \
225 return ResultSet_IteratorPrivate::sameSource(*left.d, *right.d) ? left.d->currentRow OP right.d->currentRow : false; \
226 }
227
228COMPARATOR_IMPL(<)
229COMPARATOR_IMPL(>)
230COMPARATOR_IMPL(<=)
231COMPARATOR_IMPL(>=)
232
233#undef COMPARATOR_IMPL
234
235iterator::difference_type operator-(const iterator &left, const iterator &right)
236{
237 return ResultSet_IteratorPrivate::sameSource(*left.d, *right.d) ? left.d->currentRow - right.d->currentRow : 0;
238}
239
240} // namespace Stats
241} // namespace KActivities
Class that can query the KActivities usage tracking mechanism for resources.
Definition resultset.h:44
Provides enums and strucss to use.for building queries with Query.
KIOCORE_EXPORT bool operator!=(const UDSEntry &entry, const UDSEntry &other)
KIOCORE_EXPORT bool operator==(const UDSEntry &entry, const UDSEntry &other)
QTextStream & left(QTextStream &stream)
QTextStream & right(QTextStream &stream)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Feb 28 2025 12:01:02 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.