[Kdenlive-devel] [PATCH 46/46] Use const & with foreach loops where possible

Mikko Rapeli mikko.rapeli at iki.fi
Sat Jul 14 07:56:33 UTC 2012


krazy says:

Check for foreach loop issues [foreach]...OOPS! 22 issues found!
src/projecttree/proxyclipjob.cpp: non-const ref iterator line# 55 (1)
src/projecttree/meltjob.cpp: non-const ref iterator line# 99,122,131,153 (4)
src/titlewidget.cpp: non-const ref iterator line# 515 (1)
src/initeffects.cpp: non-const ref iterator line# 546 (1)
src/clipitem.cpp: non-const ref iterator line# 1976,2058 (2)
src/renderwidget.cpp: non-const ref iterator line# 1429,1446 (2)
src/geometrywidget.cpp: non-const ref iterator line# 281 (1)
src/effectstack/effectstackview2.cpp: values or keys iteration line# 661 (1)
src/mainwindow.cpp: non-const ref iterator line# 592 (1)
src/keyframeedit.cpp: non-const ref iterator line# 466 (1)
src/parameterplotter.cpp: non-const ref iterator line# 251 (1)
src/stopmotion/stopmotion.cpp: non-const ref iterator line# 441 (1)
src/documentchecker.cpp: non-const ref iterator line# 179,196,859 (3)
src/cliptranscode.cpp: non-const ref iterator line# 143 (1)
testingArea/audioOffset.cpp: non-const ref iterator line# 55 (1)
When not using POD types (int, double, pointer, ...) you should use const & for your foreach variables. There are two reasons for this: 1) Prevents you from the mistake of writing foreach loops that modify the list, that is 'foreach(Foo f, list) f.a = f.b = f.c = 0;' compiles but does not modify the contents of list 2) Saves a copy constructor call for each of the list elements
---
 src/clipitem.cpp                 |    4 ++--
 src/documentchecker.cpp          |    6 +++---
 src/geometrywidget.cpp           |    2 +-
 src/keyframeedit.cpp             |    2 +-
 src/mainwindow.cpp               |    2 +-
 src/parameterplotter.cpp         |    2 +-
 src/projecttree/meltjob.cpp      |    8 ++++----
 src/projecttree/proxyclipjob.cpp |   12 ++++++------
 src/renderwidget.cpp             |    4 ++--
 src/stopmotion/stopmotion.cpp    |    2 +-
 src/titlewidget.cpp              |    2 +-
 testingArea/audioOffset.cpp      |    2 +-
 12 files changed, 24 insertions(+), 24 deletions(-)

diff --git a/src/clipitem.cpp b/src/clipitem.cpp
index 281953a..fb41804 100644
--- a/src/clipitem.cpp
+++ b/src/clipitem.cpp
@@ -1973,7 +1973,7 @@ bool ClipItem::updateNormalKeyframes(QDomElement parameter, ItemInfo oldInfo)
 
     const QStringList data = parameter.attribute("keyframes").split(';', QString::SkipEmptyParts);
     QMap <int, double> keyframes;
-    foreach (QString keyframe, data) {
+    foreach (const QString &keyframe, data) {
 	int keyframepos = keyframe.section(':', 0, 0).toInt();
 	// if keyframe was at clip start, update it
 	if (keyframepos == oldin) {
@@ -2055,7 +2055,7 @@ void ClipItem::updateGeometryKeyframes(QDomElement effect, int paramIndex, int w
     if (offset > 0) {
         QStringList kfrs = data.split(';');
         data.clear();
-        foreach (QString keyframe, kfrs) {
+        foreach (const QString &keyframe, kfrs) {
             if (keyframe.contains('=')) {
                 int pos = keyframe.section('=', 0, 0).toInt();
                 pos += offset;
diff --git a/src/documentchecker.cpp b/src/documentchecker.cpp
index 8614eea..ca612bc 100644
--- a/src/documentchecker.cpp
+++ b/src/documentchecker.cpp
@@ -176,7 +176,7 @@ bool DocumentChecker::hasErrorInClips()
             filesToCheck.append(luma);
     }
     // Check existence of luma files
-    foreach (const QString lumafile, filesToCheck) {
+    foreach (const QString &lumafile, filesToCheck) {
         filePath = lumafile;
         if (!filePath.startsWith('/')) filePath.prepend(root);
         if (!QFile::exists(filePath)) {
@@ -193,7 +193,7 @@ bool DocumentChecker::hasErrorInClips()
     m_dialog->setFont(KGlobalSettings::toolBarFont());
     m_ui.setupUi(m_dialog);
 
-    foreach(const QString l, missingLumas) {
+    foreach(const QString &l, missingLumas) {
         QTreeWidgetItem *item = new QTreeWidgetItem(m_ui.treeWidget, QStringList() << i18n("Luma file") << l);
         item->setIcon(0, KIcon("dialog-close"));
         item->setData(0, idRole, l);
@@ -856,7 +856,7 @@ void DocumentChecker::slotDeleteSelected()
     if (!deletedLumas.isEmpty()) {
         QDomElement e;
         QDomNodeList transitions = m_doc.elementsByTagName("transition");
-        foreach (QString lumaPath, deletedLumas) {
+        foreach (const QString &lumaPath, deletedLumas) {
             for (int i = 0; i < transitions.count(); i++) {
                 e = transitions.item(i).toElement();
                 QString resource = EffectsList::property(e, "luma");
diff --git a/src/geometrywidget.cpp b/src/geometrywidget.cpp
index e9be104..df76b25 100644
--- a/src/geometrywidget.cpp
+++ b/src/geometrywidget.cpp
@@ -278,7 +278,7 @@ QString GeometryWidget::getExtraValue(const QString &name) const
         QStringList list = val.split(';', QString::SkipEmptyParts);
         val.clear();
         val.append(list.takeFirst().section('/', 0, 0));
-        foreach (const QString value, list) {
+        foreach (const QString &value, list) {
             val.append(';' + value.section('/', 0, 0));
         }
     }
diff --git a/src/keyframeedit.cpp b/src/keyframeedit.cpp
index 8014cd0..e676972 100644
--- a/src/keyframeedit.cpp
+++ b/src/keyframeedit.cpp
@@ -463,7 +463,7 @@ void KeyframeEdit::checkVisibleParam()
     if (m_params.count() == 0)
         return;
     
-    foreach(QDomElement elem, m_params) {
+    foreach(const QDomElement &elem, m_params) {
         if (elem.attribute("intimeline") == "1")
             return;
     }
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp
index b046c30..ba8e828 100644
--- a/src/mainwindow.cpp
+++ b/src/mainwindow.cpp
@@ -589,7 +589,7 @@ MainWindow::MainWindow(const QString &MltPath, const KUrl & Url, const QString &
     if (!clipsToLoad.isEmpty() && m_activeDocument) {
         QStringList list = clipsToLoad.split(',');
         QList <QUrl> urls;
-        foreach(QString path, list) {
+        foreach(const QString &path, list) {
             kDebug() << QDir::current().absoluteFilePath(path);
             urls << QUrl::fromLocalFile(QDir::current().absoluteFilePath(path));
         }
diff --git a/src/parameterplotter.cpp b/src/parameterplotter.cpp
index a34a320..fbf21f1 100644
--- a/src/parameterplotter.cpp
+++ b/src/parameterplotter.cpp
@@ -248,7 +248,7 @@ void ParameterPlotter::mousePressEvent(QMouseEvent * event)
                     newpoints.append(QPointF(pt->x(), pt->y()));
                 }
                 p->clearPoints();
-                foreach(const QPointF qf, newpoints) {
+                foreach(const QPointF &qf, newpoints) {
                     p->addPoint(qf);
                 }
                 replacePlotObject(m_activeIndexPlot, p);
diff --git a/src/projecttree/meltjob.cpp b/src/projecttree/meltjob.cpp
index 27bc049..0ec44cd 100644
--- a/src/projecttree/meltjob.cpp
+++ b/src/projecttree/meltjob.cpp
@@ -96,7 +96,7 @@ void MeltJob::startJob()
     else 
         prod = m_producer->cut(in, out);
     QStringList list = producerParams.split(' ', QString::SkipEmptyParts);
-    foreach(QString data, list) {
+    foreach(const QString &data, list) {
         if (data.contains('=')) {
             prod->set(data.section('=', 0, 0).toUtf8().constData(), data.section('=', 1, 1).toUtf8().constData());
         }
@@ -119,7 +119,7 @@ void MeltJob::startJob()
     m_consumer->set("real_time", -KdenliveSettings::mltthreads() );
 
     list = consumerParams.split(' ', QString::SkipEmptyParts);
-    foreach(QString data, list) {
+    foreach(const QString &data, list) {
         if (data.contains('=')) {
             kDebug()<<"// filter con: "<<data;
             m_consumer->set(data.section('=', 0, 0).toUtf8().constData(), data.section('=', 1, 1).toUtf8().constData());
@@ -128,7 +128,7 @@ void MeltJob::startJob()
     
     Mlt::Filter mltFilter(*m_profile, filter.toUtf8().data());
     list = filterParams.split(' ', QString::SkipEmptyParts);
-    foreach(QString data, list) {
+    foreach(const QString &data, list) {
         if (data.contains('=')) {
             kDebug()<<"// filter p: "<<data;
             mltFilter.set(data.section('=', 0, 0).toUtf8().constData(), data.section('=', 1, 1).toUtf8().constData());
@@ -150,7 +150,7 @@ void MeltJob::startJob()
     m_consumer->stop();
     QStringList wanted = properties.split(',', QString::SkipEmptyParts);
     stringMap jobResults;
-    foreach(const QString key, wanted) {
+    foreach(const QString &key, wanted) {
         QString value = mltFilter.get(key.toUtf8().constData());
         jobResults.insert(key, value);
     }
diff --git a/src/projecttree/proxyclipjob.cpp b/src/projecttree/proxyclipjob.cpp
index 88a6ac9..2c5a241 100644
--- a/src/projecttree/proxyclipjob.cpp
+++ b/src/projecttree/proxyclipjob.cpp
@@ -52,13 +52,13 @@ void ProxyJob::startJob()
         mltParameters << "-consumer" << "avformat:" + m_dest;
         QStringList params = m_proxyParams.split('-', QString::SkipEmptyParts);
                 
-        foreach(QString s, params) {
-            s = s.simplified();
-            if (s.count(' ') == 0) {
-                s.append("=1");
+        foreach(const QString &s, params) {
+            QString t = s.simplified();
+            if (t.count(' ') == 0) {
+                t.append("=1");
             }
-            else s.replace(' ', '=');
-            mltParameters << s;
+            else t.replace(' ', '=');
+            mltParameters << t;
         }
         
         mltParameters.append(QString("real_time=-%1").arg(KdenliveSettings::mltthreads()));
diff --git a/src/renderwidget.cpp b/src/renderwidget.cpp
index 0c5b139..a3c2b03 100644
--- a/src/renderwidget.cpp
+++ b/src/renderwidget.cpp
@@ -1426,7 +1426,7 @@ void RenderWidget::refreshParams()
         m_view.bitrateLabel->setEnabled(true);
         if ( item->data(BitratesRole).canConvert(QVariant::StringList) && item->data(BitratesRole).toStringList().count()) {
             QStringList bitrates = item->data(BitratesRole).toStringList();
-            foreach (QString bitrate, bitrates)
+            foreach (const QString &bitrate, bitrates)
                 m_view.comboBitrates->addItem(bitrate);
             if (item->data(DefaultBitrateRole).canConvert(QVariant::String))
                 m_view.comboBitrates->setCurrentIndex(bitrates.indexOf(item->data(DefaultBitrateRole).toString()));
@@ -1443,7 +1443,7 @@ void RenderWidget::refreshParams()
         m_view.audiobitrateLabel->setEnabled(true);
         if ( item->data(AudioBitratesRole).canConvert(QVariant::StringList) && item->data(AudioBitratesRole).toStringList().count()) {
             QStringList audiobitrates = item->data(AudioBitratesRole).toStringList();
-            foreach (QString bitrate, audiobitrates)
+            foreach (const QString &bitrate, audiobitrates)
                 m_view.comboAudioBitrates->addItem(bitrate);
             if (item->data(DefaultAudioBitrateRole).canConvert(QVariant::String))
                 m_view.comboAudioBitrates->setCurrentIndex(audiobitrates.indexOf(item->data(DefaultAudioBitrateRole).toString()));
diff --git a/src/stopmotion/stopmotion.cpp b/src/stopmotion/stopmotion.cpp
index f03fbb9..dc6c7b6 100644
--- a/src/stopmotion/stopmotion.cpp
+++ b/src/stopmotion/stopmotion.cpp
@@ -438,7 +438,7 @@ void StopmotionWidget::parseExistingSequences()
     //dir.setNameFilters(filters);
     QStringList sequences = dir.entryList(filters, QDir::Files, QDir::Name);
     //kDebug()<<"PF: "<<<<", sm: "<<sequences;
-    foreach(QString sequencename, sequences) {
+    foreach(const QString &sequencename, sequences) {
         sequence_name->addItem(sequencename.section('_', 0, -2));
     }
 }
diff --git a/src/titlewidget.cpp b/src/titlewidget.cpp
index b5073a2..d25b860 100644
--- a/src/titlewidget.cpp
+++ b/src/titlewidget.cpp
@@ -512,7 +512,7 @@ TitleWidget::TitleWidget(KUrl url, Timecode tc, QString projectTitlePath, Render
     //templateBox->setIconSize(QSize(60,60));
     templateBox->clear();
     templateBox->addItem("");
-    foreach(TitleTemplate t, titletemplates) {
+    foreach(const TitleTemplate &t, titletemplates) {
         templateBox->addItem(t.icon, t.name, t.file);
     }
     lastDocumentHash = QCryptographicHash::hash(xml().toString().toAscii(), QCryptographicHash::Md5).toHex();
diff --git a/testingArea/audioOffset.cpp b/testingArea/audioOffset.cpp
index 3d8a84b..13945f6 100644
--- a/testingArea/audioOffset.cpp
+++ b/testingArea/audioOffset.cpp
@@ -52,7 +52,7 @@ int main(int argc, char *argv[])
     bool useFFT = false;
 
     // Load arguments
-    foreach (QString str, args) {
+    foreach (const QString &str, args) {
 
         if (str.startsWith("--profile=")) {
             QString s = str;
-- 
1.7.10.4





More information about the Kdenlive mailing list