Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qforeach.h
Go to the documentation of this file.
1
// Copyright (C) 2022 The Qt Company Ltd.
2
// Copyright (C) 2019 Intel Corporation.
3
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
// Qt-Security score:significant reason:default
5
6
#
ifndef
QFOREACH_H
7
#
define
QFOREACH_H
8
9
#
include
<
QtCore
/
qtclasshelpermacros
.
h
>
10
#
include
<
QtCore
/
qtconfigmacros
.
h
>
11
#
include
<
QtCore
/
qtdeprecationmarkers
.
h
>
12
#
include
<
QtCore
/
qttypetraits
.
h
>
13
14
QT_BEGIN_NAMESPACE
15
16
#
if
0
17
#
pragma
qt_class
(
QForeach
)
18
#
pragma
qt_sync_stop_processing
19
#
endif
20
21
#
ifndef
QT_NO_FOREACH
22
23
namespace
QtPrivate
{
24
25
template
<
typename
T>
26
class
QForeachContainer
{
27
Q_DISABLE_COPY_MOVE
(
QForeachContainer
)
28
public
:
29
QForeachContainer
(
const
T
&
t
) :
c
(
t
),
i
(
std
::
as_const
(
c
).
begin
()),
e
(
std
::
as_const
(
c
).
end
()) {}
30
QForeachContainer
(
T
&&
t
) :
c
(
std
::
move
(
t
)),
i
(
std
::
as_const
(
c
).
begin
()),
e
(
std
::
as_const
(
c
).
end
()) {}
31
32
T
c
;
33
typename
T::
const_iterator
i
,
e
;
34
};
35
36
// Containers that have a detach function are considered shared, and are OK in a foreach loop
37
template
<
typename
T,
typename
=
decltype
(
std
::
declval
<
T
>().
detach
())>
38
inline
void
warnIfContainerIsNotShared
(
int
) {}
39
40
#
if
QT_DEPRECATED_SINCE
(
6
,
0
)
41
// Other containers will copy themselves if used in foreach, this use is deprecated
42
template
<
typename
T
>
43
QT_DEPRECATED_VERSION_X_6_0
(
"Do not use foreach/Q_FOREACH with containers which are not implicitly shared. "
44
"Prefer using a range-based for loop with these containers: `for (const auto &it : container)`, "
45
"keeping in mind that range-based for doesn't copy the container as Q_FOREACH does"
)
46
inline
void
warnIfContainerIsNotShared
(...) {}
47
#
endif
48
49
template
<
typename
T
>
50
QForeachContainer
<
typename
std
::
decay
<
T
>::
type
>
qMakeForeachContainer
(
T
&&
t
)
51
{
52
warnIfContainerIsNotShared
<
typename
std
::
decay
<
T
>::
type
>(0);
53
return
QForeachContainer
<
typename
std
::
decay
<
T
>::
type
>(
std
::
forward
<
T
>(
t
));
54
}
55
56
}
57
58
// Use C++17 if statement with initializer. User's code ends up in a else so
59
// scoping of different ifs is not broken
60
#
define
Q_FOREACH_IMPL
(
variable
,
name
,
container
)
61
for
(
auto
name
=
QtPrivate
::
qMakeForeachContainer
(
container
)
;
name
.
i
!=
name
.
e
;
++
name
.
i
)
62
if
(
variable
=
*
name
.
i
;
false
)
{
}
else
63
64
#
define
Q_FOREACH_JOIN
(
A
,
B
)
Q_FOREACH_JOIN_IMPL
(
A
,
B
)
65
#
define
Q_FOREACH_JOIN_IMPL
(
A
,
B
)
A
##
B
66
67
#
define
Q_FOREACH
(
variable
,
container
)
68
Q_FOREACH_IMPL
(
variable
,
Q_FOREACH_JOIN
(
_container_
,
__LINE__
)
,
container
)
69
#
endif
// QT_NO_FOREACH
70
71
#
define
Q_FOREVER
for
(
;
;
)
72
#
ifndef
QT_NO_KEYWORDS
73
#
ifndef
QT_NO_FOREACH
74
#
ifndef
foreach
75
#
define
foreach
Q_FOREACH
76
#
endif
77
#
endif
// QT_NO_FOREACH
78
#
ifndef
forever
79
#
define
forever
Q_FOREVER
80
#
endif
81
#
endif
82
83
QT_END_NAMESPACE
84
85
#
endif
/* QFOREACH_H */
QtPrivate::QForeachContainer
Definition
qforeach.h:26
QtPrivate::QForeachContainer::e
T::const_iterator e
Definition
qforeach.h:33
QtPrivate::QForeachContainer::i
T::const_iterator i
Definition
qforeach.h:33
QtPrivate
Definition
qalloc.h:29
QtPrivate::warnIfContainerIsNotShared
void warnIfContainerIsNotShared(int)
Definition
qforeach.h:38
Q_FOREVER
#define Q_FOREVER
Definition
qforeach.h:71
Q_FOREACH
#define Q_FOREACH(variable, container)
Definition
qforeach.h:67
Q_FOREACH_JOIN_IMPL
#define Q_FOREACH_JOIN_IMPL(A, B)
Definition
qforeach.h:65
Q_FOREACH_JOIN
#define Q_FOREACH_JOIN(A, B)
Definition
qforeach.h:64
Q_FOREACH_IMPL
#define Q_FOREACH_IMPL(variable, name, container)
Definition
qforeach.h:60
qtbase
src
corelib
global
qforeach.h
Generated on
for Qt by
1.18.0