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
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
14QT_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
23namespace QtPrivate {
24
25template <typename T>
35
36// Containers that have a detach function are considered shared, and are OK in a foreach loop
37template <typename T, typename = decltype(std::declval<T>().detach())>
38inline 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
42template <typename T>
43QT_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")
46inline void warnIfContainerIsNotShared(...) {}
47#endif
48
49template<typename T>
51{
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
83QT_END_NAMESPACE
84
85#endif /* QFOREACH_H */
T::const_iterator e
Definition qforeach.h:33
T::const_iterator i
Definition qforeach.h:33
void warnIfContainerIsNotShared(int)
Definition qforeach.h:38
#define Q_FOREVER
Definition qforeach.h:71
#define Q_FOREACH(variable, container)
Definition qforeach.h:67
#define Q_FOREACH_JOIN_IMPL(A, B)
Definition qforeach.h:65
#define Q_FOREACH_JOIN(A, B)
Definition qforeach.h:64
#define Q_FOREACH_IMPL(variable, name, container)
Definition qforeach.h:60