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
12
QT_REQUIRE_CONFIG
(
future
);
13
14
QT_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
24
namespace
QtPrivate
{
25
26
class
ResultItem
27
{
28
public
:
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
39
class
Q_CORE_EXPORT
ResultIteratorBase
40
{
41
public
:
42
ResultIteratorBase
();
43
ResultIteratorBase
(
QMap
<
int
,
ResultItem
>::
const_iterator
_mapIterator
,
int
_vectorIndex
= 0);
44
int
vectorIndex
()
const
;
45
int
resultIndex
()
const
;
46
47
ResultIteratorBase
operator
++();
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
58
private
:
59
friend
bool
comparesEqual
(
const
ResultIteratorBase
&
lhs
,
60
const
ResultIteratorBase
&
rhs
)
61
{
62
return
(
lhs
.
mapIterator
==
rhs
.
mapIterator
&&
lhs
.
m_vectorIndex
==
rhs
.
m_vectorIndex
);
63
}
64
Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT
(
ResultIteratorBase
)
65
protected
:
66
QMap
<
int
,
ResultItem
>::
const_iterator
mapIterator
;
67
int
m_vectorIndex
;
68
public
:
69
template
<
typename
T
>
70
const
T
&
value
()
const
71
{
72
return
*
pointer
<
T
>();
73
}
74
75
template
<
typename
T
>
76
T
&
value
()
77
{
78
return
*
pointer
<
T
>();
79
}
80
81
template
<
typename
T
>
82
T
*
pointer
()
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
{
91
if
(
mapIterator
.
value
().
isVector
())
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
98
class
Q_CORE_EXPORT ResultStoreBase
final
99
{
100
public
:
101
ResultStoreBase
();
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
);
106
ResultIteratorBase
begin
()
const
;
107
ResultIteratorBase
end
()
const
;
108
bool
hasNextResult
()
const
;
109
ResultIteratorBase
resultAt
(
int
index
)
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.
114
QT_WARNING_PUSH
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
();
121
QT_WARNING_POP
122
123
protected
:
124
int
insertResultItem
(
int
index
,
ResultItem
&
resultItem
);
125
void
insertResultItemIfValid
(
int
index
,
ResultItem
&
resultItem
);
126
bool
containsValidResultItem
(
int
index
)
const
;
127
void
syncPendingResults
();
128
void
syncResultCount
();
129
int
updateInsertIndex
(
int
index
,
int
_count
);
130
131
QMap
<
int
,
ResultItem
>
m_results
;
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
135
bool
m_filterMode
;
136
QMap
<
int
,
ResultItem
>
pendingResults
;
137
int
filteredResults
;
138
139
template
<
typename
T
>
140
static
void
clear
(
QMap
<
int
,
ResultItem
> &
store
)
141
{
142
QMap
<
int
,
ResultItem
>::
const_iterator
mapIterator
=
store
.
constBegin
();
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
153
public
:
154
template
<
typename
T
,
typename
...
Args
>
155
int
emplaceResult
(
int
index
,
Args
&&...
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
>
175
int
moveResult
(
int
index
,
T
&&
result
)
176
{
177
static_assert
(!
std
::
is_reference_v
<
T
>,
"trying to move from an lvalue!"
);
178
179
return
emplaceResult
<
std
::
remove_cv_t
<
T
>>(
index
,
std
::
forward
<
T
>(
result
));
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
210
int
addCanceledResult
(
int
index
)
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
>
219
int
addCanceledResults
(
int
index
,
int
_count
)
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
{
231
ResultStoreBase
::
clear
<
T
>(
m_results
);
232
resultCount
= 0;
233
insertIndex
= 0;
234
ResultStoreBase
::
clear
<
T
>(
pendingResults
);
235
filteredResults
= 0;
236
}
237
};
238
239
}
// namespace QtPrivate
240
241
Q_DECLARE_TYPEINFO
(
QtPrivate
::
ResultItem
,
Q_PRIMITIVE_TYPE
);
242
243
244
QT_END_NAMESPACE
245
246
#
endif
QtPrivate::ResultItem
Definition
qresultstore.h:27
QtPrivate::ResultItem::ResultItem
ResultItem(const void *_result)
Definition
qresultstore.h:30
QtPrivate::ResultItem::m_count
int m_count
Definition
qresultstore.h:35
QtPrivate::ResultItem::isValid
bool isValid() const
Definition
qresultstore.h:32
QtPrivate::ResultItem::ResultItem
ResultItem()
Definition
qresultstore.h:31
QtPrivate::ResultItem::result
const void * result
Definition
qresultstore.h:36
QtPrivate::ResultItem::ResultItem
ResultItem(const void *_result, int _count)
Definition
qresultstore.h:29
QtPrivate::ResultItem::count
int count() const
Definition
qresultstore.h:34
QtPrivate::ResultItem::isVector
bool isVector() const
Definition
qresultstore.h:33
QtPrivate::ResultIteratorBase
Definition
qresultstore.h:40
QPlatformGraphicsBufferHelper
\inmodule QtGui
QtPrivate
Definition
qalloc.h:27
QtPrivate::findResult
static ResultIteratorBase findResult(const QMap< int, ResultItem > &store, int index)
Definition
qresultstore.cpp:16
Q_DECLARE_TYPEINFO
Q_DECLARE_TYPEINFO(QtPrivate::ResultItem, Q_PRIMITIVE_TYPE)
future
QFuture< void > future
[5]
Definition
src_concurrent_qtconcurrentfilter.cpp:22
qtbase
src
corelib
thread
qresultstore.h
Generated on
for Qt by
1.14.0