Eradicate Q_FOREACH loops [1/2]: trivial cases

In this patch, we port Q_FOREACH loops to C++11 ranged-for loops. All cases are
trivial in the sense that either the argument is already const or is trivially
marked as const, either by qAsConst(), or, in the case of rvalues, by storing
to a const auto temporary first.

In addition, all loop bodies are clear enough to confirm that the container we
iterate over is not changed under iteration. This does not exclude cases where
a loop is prematurely exited just after calling a modifier on the container, as
that is safe, if not especially elegant.

Change-Id: I87a63f07797437d421567d60e52305391a3c4f21
Reviewed-by: Johan Helsing <johan.helsing@qt.io>
This commit is contained in:
Marc Mutz 2019-05-17 11:07:02 +02:00
parent 0899b4287b
commit 9f64f52e23
13 changed files with 26 additions and 20 deletions

View File

@ -49,7 +49,8 @@ namespace QtWaylandClient {
bool QWaylandWlShellIntegration::initialize(QWaylandDisplay *display)
{
Q_FOREACH (QWaylandDisplay::RegistryGlobal global, display->globals()) {
const auto globals = display->globals();
for (QWaylandDisplay::RegistryGlobal global : globals) {
if (global.interface == QLatin1String("wl_shell")) {
m_wlShell = new QtWayland::wl_shell(display->wl_registry(), global.id, 1);
break;

View File

@ -51,7 +51,8 @@ namespace QtWaylandClient {
bool QWaylandXdgShellV5Integration::initialize(QWaylandDisplay *display)
{
Q_FOREACH (QWaylandDisplay::RegistryGlobal global, display->globals()) {
const auto globals = display->globals();
for (QWaylandDisplay::RegistryGlobal global : globals) {
if (global.interface == QLatin1String("xdg_shell")) {
m_xdgShell.reset(new QWaylandXdgShellV5(display->wl_registry(), global.id));
break;

View File

@ -60,7 +60,8 @@ QWaylandDataSource::QWaylandDataSource(QWaylandDataDeviceManager *dataDeviceMana
{
if (!mimeData)
return;
Q_FOREACH (const QString &format, mimeData->formats()) {
const auto formats = mimeData->formats();
for (const QString &format : formats) {
offer(format);
}
}

View File

@ -366,7 +366,7 @@ void QWaylandDisplay::registry_global_remove(uint32_t id)
}
}
foreach (QWaylandScreen *screen, mScreens) {
for (QWaylandScreen *screen : qAsConst(mScreens)) {
if (screen->outputId() == id) {
mScreens.removeOne(screen);
QWindowSystemInterface::handleScreenRemoved(screen);

View File

@ -221,11 +221,11 @@ void QWaylandTextInput::zwp_text_input_v2_leave(uint32_t serial, ::wl_surface *s
void QWaylandTextInput::zwp_text_input_v2_modifiers_map(wl_array *map)
{
QList<QByteArray> modifiersMap = QByteArray::fromRawData(static_cast<const char*>(map->data), map->size).split('\0');
const QList<QByteArray> modifiersMap = QByteArray::fromRawData(static_cast<const char*>(map->data), map->size).split('\0');
m_modifiersMap.clear();
Q_FOREACH (const QByteArray &modifier, modifiersMap) {
for (const QByteArray &modifier : modifiersMap) {
if (modifier == "Shift")
m_modifiersMap.append(Qt::ShiftModifier);
else if (modifier == "Control")

View File

@ -1349,7 +1349,7 @@ bool QWaylandInputDevice::Touch::allTouchPointsReleased()
void QWaylandInputDevice::Touch::releasePoints()
{
Q_FOREACH (const QWindowSystemInterface::TouchPoint &previousPoint, mPrevTouchPoints) {
for (const QWindowSystemInterface::TouchPoint &previousPoint : qAsConst(mPrevTouchPoints)) {
QWindowSystemInterface::TouchPoint tp = previousPoint;
tp.state = Qt::TouchPointReleased;
mTouchPoints.append(tp);

View File

@ -421,7 +421,7 @@ void QWaylandIntegration::initializeShellIntegration()
preferredShells << QLatin1String("wl-shell") << QLatin1String("ivi-shell");
}
Q_FOREACH (QString preferredShell, preferredShells) {
for (const QString &preferredShell : qAsConst(preferredShells)) {
mShellIntegration.reset(createShellIntegration(preferredShell));
if (mShellIntegration) {
qCDebug(lcQpaWayland, "Using the '%s' shell integration", qPrintable(preferredShell));

View File

@ -175,7 +175,8 @@ QList<QPlatformScreen *> QWaylandScreen::virtualSiblings() const
void QWaylandScreen::setOrientationUpdateMask(Qt::ScreenOrientations mask)
{
foreach (QWindow *window, QGuiApplication::allWindows()) {
const auto allWindows = QGuiApplication::allWindows();
for (QWindow *window : allWindows) {
QWaylandWindow *w = static_cast<QWaylandWindow *>(window->handle());
if (w && w->waylandScreen() == this)
w->setOrientationMask(mask);

View File

@ -243,7 +243,8 @@ void QWaylandShmBackingStore::resize(const QSize &size, const QRegion &)
QWaylandShmBuffer *QWaylandShmBackingStore::getBuffer(const QSize &size)
{
foreach (QWaylandShmBuffer *b, mBuffers) {
const auto copy = mBuffers; // remove when ported to vector<unique_ptr> + remove_if
for (QWaylandShmBuffer *b : copy) {
if (!b->busy()) {
if (b->size() == size) {
return b;

View File

@ -94,7 +94,8 @@ QWaylandWindow::~QWaylandWindow()
reset(false);
const QWindow *parent = window();
foreach (QWindow *w, QGuiApplication::topLevelWindows()) {
const auto tlw = QGuiApplication::topLevelWindows();
for (QWindow *w : tlw) {
if (w->transientParent() == parent)
QWindowSystemInterface::handleCloseEvent(w);
}
@ -786,7 +787,7 @@ bool QWaylandWindow::createDecoration()
}
if (hadDecoration != (bool)mWindowDecoration) {
foreach (QWaylandSubSurface *subsurf, mChildren) {
for (QWaylandSubSurface *subsurf : qAsConst(mChildren)) {
QPoint pos = subsurf->window()->geometry().topLeft();
QMargins m = frameMargins();
subsurf->set_position(pos.x() + m.left(), pos.y() + m.top());

View File

@ -157,7 +157,7 @@ QInputMethodEvent QWaylandInputMethodEventBuilder::buildPreedit(const QString &t
attributes.append(QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, indexFromWayland(text, m_preeditCursor), 1, QVariant()));
}
Q_FOREACH (const QInputMethodEvent::Attribute &attr, m_preeditStyles) {
for (const QInputMethodEvent::Attribute &attr : qAsConst(m_preeditStyles)) {
int start = indexFromWayland(text, attr.start);
int length = indexFromWayland(text, attr.start + attr.length) - start;
attributes.append(QInputMethodEvent::Attribute(attr.type, start, length, attr.value));

View File

@ -542,7 +542,7 @@ bool Scanner::process()
if (hasEvents) {
printf("\n");
foreach (const WaylandEvent &e, interface.events) {
for (const WaylandEvent &e : interface.events) {
printf(" void send_");
printEvent(e);
printf(";\n");
@ -563,7 +563,7 @@ bool Scanner::process()
if (hasRequests) {
printf("\n");
foreach (const WaylandEvent &e, interface.requests) {
for (const WaylandEvent &e : interface.requests) {
printf(" virtual void %s_", interfaceNameStripped);
printEvent(e);
printf(";\n");
@ -826,7 +826,7 @@ bool Scanner::process()
printf("\n");
printf(" };\n");
foreach (const WaylandEvent &e, interface.requests) {
for (const WaylandEvent &e : interface.requests) {
printf("\n");
printf(" void %s::%s_", interfaceName, interfaceNameStripped);
printEvent(e, true);
@ -996,7 +996,7 @@ bool Scanner::process()
if (!interface.requests.isEmpty()) {
printf("\n");
foreach (const WaylandEvent &e, interface.requests) {
for (const WaylandEvent &e : interface.requests) {
const WaylandArgument *new_id = newIdArgument(e.arguments);
QByteArray new_id_str = "void ";
if (new_id) {
@ -1016,7 +1016,7 @@ bool Scanner::process()
if (hasEvents) {
printf("\n");
printf(" protected:\n");
foreach (const WaylandEvent &e, interface.events) {
for (const WaylandEvent &e : interface.events) {
printf(" virtual void %s_", interfaceNameStripped);
printEvent(e);
printf(";\n");

View File

@ -221,8 +221,8 @@ QSharedPointer<MockSurface> MockCompositor::surface()
QSharedPointer<MockSurface> result;
lock();
{
QVector<Impl::Surface *> surfaces = m_compositor->surfaces();
foreach (Impl::Surface *surface, surfaces) {
const QVector<Impl::Surface *> surfaces = m_compositor->surfaces();
for (Impl::Surface *surface : surfaces) {
// we don't want to mistake the cursor surface for a window surface
if (surface->isMapped()) {
result = surface->mockSurface();