Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qresultstore.h
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QTCORE_RESULTSTORE_H
6#define QTCORE_RESULTSTORE_H
7
8#include <QtCore/qmap.h>
9
10#include <utility>
11
13
14QT_BEGIN_NAMESPACE
15
16/*
17 ResultStore stores indexed results. Results can be added and retrieved
18 either individually batched in a QList. Retriveing results and checking
19 which indexes are in the store can be done either by iterating or by random
20 access. In addition results can be removed from the front of the store,
21 either individually or in batches.
22*/
23
24namespace QtPrivate {
25
27{
28public:
29 ResultItem(const void *_result, int _count) : m_count(_count), result(_result) { } // construct with vector of results
30 ResultItem(const void *_result) : m_count(0), result(_result) { } // construct with result
31 ResultItem() : m_count(0), result(nullptr) { }
32 bool isValid() const { return result != nullptr; }
33 bool isVector() const { return m_count != 0; }
34 int count() const { return (m_count == 0) ? 1 : m_count; }
35 int m_count; // result is either a pointer to a result or to a vector of results,
36 const void *result; // if count is 0 it's a result, otherwise it's a vector.
37};
38
39class Q_CORE_EXPORT ResultIteratorBase
40{
41public:
44 int vectorIndex() const;
45 int resultIndex() const;
46
48 int batchSize() const;
49 void batchedAdvance();
50#if QT_CORE_REMOVED_SINCE(6, 8)
51 bool operator==(const ResultIteratorBase &other) const;
52 bool operator!=(const ResultIteratorBase &other) const;
53#endif
54 bool isVector() const;
55 bool canIncrementVectorIndex() const;
56 bool isValid() const;
57
58private:
65protected:
68public:
69 template <typename T>
70 const T &value() const
71 {
72 return *pointer<T>();
73 }
74
75 template<typename T>
77 {
78 return *pointer<T>();
79 }
80
81 template <typename T>
83 {
84 const T *p = std::as_const(*this).pointer<T>();
85 return const_cast<T *>(p);
86 }
87
88 template <typename T>
89 const T *pointer() const
90 {
92 return &(reinterpret_cast<const QList<T> *>(mapIterator.value().result)->at(m_vectorIndex));
93 else
94 return reinterpret_cast<const T *>(mapIterator.value().result);
95 }
96};
97
98class Q_CORE_EXPORT ResultStoreBase final
99{
100public:
102 void setFilterMode(bool enable);
103 bool filterMode() const;
104 int addResult(int index, const void *result);
105 int addResults(int index, const void *results, int vectorSize, int logicalCount);
107 ResultIteratorBase end() const;
108 bool hasNextResult() const;
110 bool contains(int index) const;
111 int count() const;
112 // ### Qt 7: 'virtual' isn't required, can be removed, along with renaming
113 // the class to ResultStore and changing the members below to be private.
115#if defined(Q_CC_CLANG)
116# if __has_warning("-Wunnecessary-virtual-specifier")
117 QT_WARNING_DISABLE_CLANG("-Wunnecessary-virtual-specifier")
118# endif
119#endif
120 virtual ~ResultStoreBase();
122
123protected:
126 bool containsValidResultItem(int index) const;
127 void syncPendingResults();
128 void syncResultCount();
129 int updateInsertIndex(int index, int _count);
130
132 int insertIndex; // The index where the next results(s) will be inserted.
133 int resultCount; // The number of consecutive results stored, starting at index 0.
134
138
139 template <typename T>
140 static void clear(QMap<int, ResultItem> &store)
141 {
143 while (mapIterator != store.constEnd()) {
144 if (mapIterator.value().isVector())
145 delete reinterpret_cast<const QList<T> *>(mapIterator.value().result);
146 else
147 delete reinterpret_cast<const T *>(mapIterator.value().result);
148 ++mapIterator;
149 }
150 store.clear();
151 }
152
153public:
154 template <typename T, typename...Args>
156 {
157 if (containsValidResultItem(index)) // reject if already present
158 return -1;
159 return addResult(index, static_cast<void *>(new T(std::forward<Args>(args)...)));
160 }
161
162 template <typename T>
163 int addResult(int index, const T *result)
164 {
165 if (containsValidResultItem(index)) // reject if already present
166 return -1;
167
168 if (result == nullptr)
169 return addResult(index, static_cast<void *>(nullptr));
170
171 return addResult(index, static_cast<void *>(new T(*result)));
172 }
173
174 template <typename T>
176 {
177 static_assert(!std::is_reference_v<T>, "trying to move from an lvalue!");
178
180 }
181
182 template<typename T>
183 int addResults(int index, const QList<T> *results)
184 {
185 if (results->empty()) // reject if results are empty
186 return -1;
187
188 if (containsValidResultItem(index)) // reject if already present
189 return -1;
190
191 return addResults(index, new QList<T>(*results), results->size(), results->size());
192 }
193
194 template<typename T>
195 int addResults(int index, const QList<T> *results, int totalCount)
196 {
197 // reject if results are empty, and nothing is filtered away
198 if ((m_filterMode == false || results->size() == totalCount) && results->empty())
199 return -1;
200
201 if (containsValidResultItem(index)) // reject if already present
202 return -1;
203
204 if (m_filterMode == true && results->size() != totalCount && 0 == results->size())
205 return addResults(index, nullptr, 0, totalCount);
206
207 return addResults(index, new QList<T>(*results), results->size(), totalCount);
208 }
209
211 {
212 if (containsValidResultItem(index)) // reject if already present
213 return -1;
214
215 return addResult(index, static_cast<void *>(nullptr));
216 }
217
218 template <typename T>
220 {
221 if (containsValidResultItem(index)) // reject if already present
222 return -1;
223
224 QList<T> empty;
225 return addResults(index, &empty, _count);
226 }
227
228 template <typename T>
229 void clear()
230 {
232 resultCount = 0;
233 insertIndex = 0;
235 filteredResults = 0;
236 }
237};
238
239} // namespace QtPrivate
240
242
243
244QT_END_NAMESPACE
245
246#endif
ResultItem(const void *_result)
ResultItem(const void *_result, int _count)
bool isVector() const
static ResultIteratorBase findResult(const QMap< int, ResultItem > &store, int index)
Q_DECLARE_TYPEINFO(QtPrivate::ResultItem, Q_PRIMITIVE_TYPE)
QFuture< void > future
[5]