From 474060a45600d61816e98aa9afba58a88de2f766 Mon Sep 17 00:00:00 2001 From: Aleix Pol Date: Fri, 11 Sep 2020 03:22:57 +0200 Subject: [PATCH] Improve loop implementations in WindowQuadList Prefer for() to foreach(), as the latter is deprecated. Prefer iterating QList using the iteration_expression for() loops, rather than doing it by index. --- libkwineffects/kwineffects.cpp | 48 ++++++++++++++-------------------- 1 file changed, 19 insertions(+), 29 deletions(-) diff --git a/libkwineffects/kwineffects.cpp b/libkwineffects/kwineffects.cpp index 003942d50..d1c3e2d13 100644 --- a/libkwineffects/kwineffects.cpp +++ b/libkwineffects/kwineffects.cpp @@ -928,7 +928,8 @@ bool WindowQuad::smoothNeeded() const WindowQuadList WindowQuadList::splitAtX(double x) const { WindowQuadList ret; - foreach (const WindowQuad & quad, *this) { + ret.reserve(count()); + for (const WindowQuad & quad : *this) { #if !defined(QT_NO_DEBUG) if (quad.isTransformed()) qFatal("Splitting quads is allowed only in pre-paint calls!"); @@ -960,7 +961,8 @@ WindowQuadList WindowQuadList::splitAtX(double x) const WindowQuadList WindowQuadList::splitAtY(double y) const { WindowQuadList ret; - foreach (const WindowQuad & quad, *this) { + ret.reserve(count()); + for (const WindowQuad & quad : *this) { #if !defined(QT_NO_DEBUG) if (quad.isTransformed()) qFatal("Splitting quads is allowed only in pre-paint calls!"); @@ -1013,7 +1015,7 @@ WindowQuadList WindowQuadList::makeGrid(int maxQuadSize) const WindowQuadList ret; - foreach (const WindowQuad &quad, *this) { + for (const WindowQuad &quad : *this) { const double quadLeft = quad.left(); const double quadRight = quad.right(); const double quadTop = quad.top(); @@ -1057,7 +1059,7 @@ WindowQuadList WindowQuadList::makeRegularGrid(int xSubdivisions, int ySubdivisi double top = first().top(); double bottom = first().bottom(); - foreach (const WindowQuad &quad, *this) { + for (const WindowQuad &quad : *this) { #if !defined(QT_NO_DEBUG) if (quad.isTransformed()) qFatal("Splitting quads is allowed only in pre-paint calls!"); @@ -1073,7 +1075,7 @@ WindowQuadList WindowQuadList::makeRegularGrid(int xSubdivisions, int ySubdivisi WindowQuadList ret; - foreach (const WindowQuad &quad, *this) { + for (const WindowQuad &quad : *this) { const double quadLeft = quad.left(); const double quadRight = quad.right(); const double quadTop = quad.top(); @@ -1130,8 +1132,7 @@ void WindowQuadList::makeInterleavedArrays(unsigned int type, GLVertex2D *vertic case GL_QUADS: #if defined(__SSE2__) if (!(intptr_t(vertex) & 0xf)) { - for (int i = 0; i < count(); i++) { - const WindowQuad &quad = at(i); + for (const WindowQuad &quad : *this) { alignas(16) GLVertex2D v[4]; for (int j = 0; j < 4; j++) { @@ -1154,9 +1155,7 @@ void WindowQuadList::makeInterleavedArrays(unsigned int type, GLVertex2D *vertic } else #endif // __SSE2__ { - for (int i = 0; i < count(); i++) { - const WindowQuad &quad = at(i); - + for (const WindowQuad &quad : *this) { for (int j = 0; j < 4; j++) { const WindowVertex &wv = quad[j]; @@ -1173,8 +1172,7 @@ void WindowQuadList::makeInterleavedArrays(unsigned int type, GLVertex2D *vertic case GL_TRIANGLES: #if defined(__SSE2__) if (!(intptr_t(vertex) & 0xf)) { - for (int i = 0; i < count(); i++) { - const WindowQuad &quad = at(i); + for (const WindowQuad &quad : *this) { alignas(16) GLVertex2D v[4]; for (int j = 0; j < 4; j++) { @@ -1208,8 +1206,7 @@ void WindowQuadList::makeInterleavedArrays(unsigned int type, GLVertex2D *vertic } else #endif // __SSE2__ { - for (int i = 0; i < count(); i++) { - const WindowQuad &quad = at(i); + for (const WindowQuad &quad : *this) { GLVertex2D v[4]; // Four unique vertices / quad for (int j = 0; j < 4; j++) { @@ -1248,9 +1245,7 @@ void WindowQuadList::makeArrays(float **vertices, float **texcoords, const QSize // Note: The positions in a WindowQuad are stored in clockwise order const int index[] = { 1, 0, 3, 3, 2, 1 }; - for (int i = 0; i < count(); i++) { - const WindowQuad &quad = at(i); - + for (const WindowQuad &quad : *this) { for (int j = 0; j < 6; j++) { const WindowVertex &wv = quad[index[j]]; @@ -1280,7 +1275,7 @@ WindowQuadList WindowQuadList::select(WindowQuadType type) const WindowQuadList WindowQuadList::filterOut(WindowQuadType type) const { - foreach (const WindowQuad & q, *this) { + for (const WindowQuad & q : *this) { if (q.type() == type) { // something to filter out, make a copy and filter WindowQuadList ret; foreach (const WindowQuad & q, *this) { @@ -1295,18 +1290,12 @@ WindowQuadList WindowQuadList::filterOut(WindowQuadType type) const bool WindowQuadList::smoothNeeded() const { - foreach (const WindowQuad & q, *this) - if (q.smoothNeeded()) - return true; - return false; + return std::any_of(constBegin(), constEnd(), [] (const WindowQuad & q) { return q.smoothNeeded(); }); } bool WindowQuadList::isTransformed() const { - foreach (const WindowQuad & q, *this) - if (q.isTransformed()) - return true; - return false; + return std::any_of(constBegin(), constEnd(), [] (const WindowQuad & q) { return q.isTransformed(); }); } /*************************************************************** @@ -1357,9 +1346,10 @@ QRegion PaintClipper::paintArea() { Q_ASSERT(areas != nullptr); // can be called only with clip() == true const QSize &s = effects->virtualScreenSize(); - QRegion ret = QRegion(0, 0, s.width(), s.height()); - foreach (const QRegion & r, *areas) - ret &= r; + QRegion ret(0, 0, s.width(), s.height()); + for (const QRegion & r : qAsConst(*areas)) { + ret &= r; + } return ret; }