Port away from most cases of Q_FOREACH to C++11 ranged for loop.

In order to avoid deep copies, Qt containers over which we iterate must be const
The remaining unported cases require qAsConst (Qt 5.7) or std::as_const (C++17)
This commit is contained in:
Andrius Štikonas 2016-09-01 13:21:05 +01:00
parent b0122f25e5
commit 9f0ca042fe
35 changed files with 105 additions and 92 deletions

View file

@ -155,10 +155,10 @@ lookForFstabEntries( const QString& partitionPath )
QFile fstabFile( mountsDir.path() + "/etc/fstab" );
if ( fstabFile.open( QIODevice::ReadOnly | QIODevice::Text ) )
{
QStringList fstabLines = QString::fromLocal8Bit( fstabFile.readAll() )
const QStringList fstabLines = QString::fromLocal8Bit( fstabFile.readAll() )
.split( '\n' );
foreach ( const QString& rawLine, fstabLines )
for ( const QString& rawLine : fstabLines )
{
QString line = rawLine.simplified();
if ( line.startsWith( '#' ) )
@ -194,7 +194,7 @@ findPartitionPathForMountPoint( const FstabEntryList& fstab,
if ( fstab.isEmpty() )
return QString();
foreach ( const FstabEntry& entry, fstab )
for ( const FstabEntry& entry : fstab )
{
if ( entry.mountPoint == mountPoint )
{
@ -283,7 +283,8 @@ runOsprober( PartitionCoreModule* core )
QString osProberReport( "Osprober lines, clean:\n" );
QStringList osproberCleanLines;
OsproberEntryList osproberEntries;
foreach ( const QString& line, osproberOutput.split( '\n' ) )
const auto lines = osproberOutput.split( '\n' );
for ( const QString& line : lines )
{
if ( !line.simplified().isEmpty() )
{

View file

@ -449,7 +449,7 @@ PartitionCoreModule::dumpQueue() const
}
OsproberEntryList
const OsproberEntryList
PartitionCoreModule::osproberEntries() const
{
return m_osproberLines;

View file

@ -127,7 +127,7 @@ public:
void dumpQueue() const;
OsproberEntryList osproberEntries() const;
const OsproberEntryList osproberEntries() const;
Q_SIGNALS:
void hasRootMountPointChanged( bool value );

View file

@ -129,7 +129,7 @@ AlongsidePage::init( PartitionCoreModule* core )
string( Calamares::Branding::ProductName ) ) );
} );
foreach ( const OsproberEntry& e, m_core->osproberEntries() )
for ( const OsproberEntry& e : m_core->osproberEntries() )
{
if ( e.canBeResized )
m_partitionsComboBox->addItem( e.prettyName + " (" + e.path + ")", e.path );

View file

@ -733,7 +733,7 @@ ChoicePage::doReplaceSelectedPartition( const QModelIndex& current )
// Find out is the selected partition has a rootfs. If yes, then make the
// m_reuseHomeCheckBox visible and set its text to something meaningful.
homePartitionPath->clear();
foreach ( const OsproberEntry& osproberEntry, m_core->osproberEntries() )
for ( const OsproberEntry& osproberEntry : m_core->osproberEntries() )
if ( osproberEntry.path == partPath )
*homePartitionPath = osproberEntry.homePath;
if ( homePartitionPath->isEmpty() )
@ -1329,7 +1329,7 @@ OsproberEntryList
ChoicePage::getOsproberEntriesForDevice( Device* device ) const
{
OsproberEntryList eList;
foreach ( const OsproberEntry& entry, m_core->osproberEntries() )
for ( const OsproberEntry& entry : m_core->osproberEntries() )
{
if ( entry.path.startsWith( device->deviceNode() ) )
eList.append( entry );

View file

@ -240,11 +240,11 @@ PartitionLabelsView::drawLabels( QPainter* painter,
if ( !modl )
return;
QModelIndexList indexesToDraw = getIndexesToDraw( parent );
const QModelIndexList indexesToDraw = getIndexesToDraw( parent );
int label_x = rect.x();
int label_y = rect.y();
foreach ( const QModelIndex& index, indexesToDraw )
for ( const QModelIndex& index : indexesToDraw )
{
QStringList texts = buildTexts( index );
@ -306,12 +306,12 @@ PartitionLabelsView::sizeForAllLabels( int maxLineWidth ) const
if ( !modl )
return QSize();
QModelIndexList indexesToDraw = getIndexesToDraw( QModelIndex() );
const QModelIndexList indexesToDraw = getIndexesToDraw( QModelIndex() );
int lineLength = 0;
int numLines = 1;
int singleLabelHeight = 0;
foreach ( const QModelIndex& index, indexesToDraw )
for ( const QModelIndex& index : indexesToDraw )
{
QStringList texts = buildTexts( index );
@ -349,7 +349,7 @@ PartitionLabelsView::sizeForLabel( const QStringList& text ) const
{
int vertOffset = 0;
int width = 0;
foreach ( const QString& textLine, text )
for ( const QString& textLine : text )
{
QSize textSize = fontMetrics().size( Qt::TextSingleLine, textLine );
@ -371,7 +371,7 @@ PartitionLabelsView::drawLabel( QPainter* painter,
painter->setPen( Qt::black );
int vertOffset = 0;
int width = 0;
foreach ( const QString& textLine, text )
for ( const QString& textLine : text )
{
QSize textSize = painter->fontMetrics().size( Qt::TextSingleLine, textLine );
painter->drawText( pos.x()+LABEL_PARTITION_SQUARE_MARGIN,
@ -402,12 +402,12 @@ PartitionLabelsView::indexAt( const QPoint& point ) const
if ( !modl )
return QModelIndex();
QModelIndexList indexesToDraw = getIndexesToDraw( QModelIndex() );
const QModelIndexList indexesToDraw = getIndexesToDraw( QModelIndex() );
QRect rect = this->rect();
int label_x = rect.x();
int label_y = rect.y();
foreach ( const QModelIndex& index, indexesToDraw )
for ( const QModelIndex& index : indexesToDraw )
{
QStringList texts = buildTexts( index );
@ -437,12 +437,12 @@ PartitionLabelsView::visualRect( const QModelIndex& idx ) const
if ( !modl )
return QRect();
QModelIndexList indexesToDraw = getIndexesToDraw( QModelIndex() );
const QModelIndexList indexesToDraw = getIndexesToDraw( QModelIndex() );
QRect rect = this->rect();
int label_x = rect.x();
int label_y = rect.y();
foreach ( const QModelIndex& index, indexesToDraw )
for ( const QModelIndex& index : indexesToDraw )
{
QStringList texts = buildTexts( index );

View file

@ -105,7 +105,7 @@ PartitionSplitterWidget::setupItems( const QVector<PartitionSplitterItem>& items
m_items.clear();
m_items = items;
repaint();
foreach ( const PartitionSplitterItem& item, items )
for ( const PartitionSplitterItem& item : items )
cDebug() << "PSI added item" << item.itemPath << "size" << item.size;
}

View file

@ -149,7 +149,7 @@ ReplaceWidget::onPartitionSelected()
PartitionModel* model = qobject_cast< PartitionModel* >( m_ui->partitionTreeView->model() );
if ( model && ok )
{
QStringList osproberLines = Calamares::JobQueue::instance()
const QStringList osproberLines = Calamares::JobQueue::instance()
->globalStorage()
->value( "osproberLines" ).toStringList();
@ -197,7 +197,7 @@ ReplaceWidget::onPartitionSelected()
QString prettyName = tr( "Data partition (%1)" )
.arg( partition->fileSystem().name() );
foreach ( const QString& line, osproberLines )
for ( const QString& line : osproberLines )
{
QStringList lineColumns = line.split( ':' );

View file

@ -72,8 +72,8 @@ ClearMountsJob::exec()
process.start();
process.waitForFinished();
QString partitions = process.readAllStandardOutput();
QStringList partitionsList = partitions.simplified().split( ' ' );
const QString partitions = process.readAllStandardOutput();
const QStringList partitionsList = partitions.simplified().split( ' ' );
// Build a list of partitions of type 82 (Linux swap / Solaris).
// We then need to clear them just in case they contain something resumable from a
@ -100,7 +100,8 @@ ClearMountsJob::exec()
*it = (*it).simplified().split( ' ' ).first();
}
foreach ( QString mapperPath, getCryptoDevices() )
const QStringList cryptoDevices = getCryptoDevices();
for ( const QString &mapperPath : cryptoDevices )
{
tryUmount( mapperPath );
QString news = tryCryptoClose( mapperPath );
@ -113,8 +114,8 @@ ClearMountsJob::exec()
process.waitForFinished();
if ( process.exitCode() == 0 ) //means LVM2 tools are installed
{
QStringList lvscanLines = QString::fromLocal8Bit( process.readAllStandardOutput() ).split( '\n' );
foreach ( const QString& lvscanLine, lvscanLines )
const QStringList lvscanLines = QString::fromLocal8Bit( process.readAllStandardOutput() ).split( '\n' );
for ( const QString& lvscanLine : lvscanLines )
{
QString lvPath = lvscanLine.simplified().split( ' ' ).value( 1 ); //second column
lvPath = lvPath.replace( '\'', "" );
@ -137,8 +138,8 @@ ClearMountsJob::exec()
{
QSet< QString > vgSet;
QStringList pvdisplayLines = pvdisplayOutput.split( '\n' );
foreach ( const QString& pvdisplayLine, pvdisplayLines )
const QStringList pvdisplayLines = pvdisplayOutput.split( '\n' );
for ( const QString& pvdisplayLine : pvdisplayLines )
{
QString pvPath = pvdisplayLine.simplified().split( ' ' ).value( 0 );
QString vgName = pvdisplayLine.simplified().split( ' ' ).value( 1 );
@ -160,7 +161,8 @@ ClearMountsJob::exec()
else
cDebug() << "WARNING: this system does not seem to have LVM2 tools.";
foreach ( QString mapperPath, getCryptoDevices() )
const QStringList cryptoDevices2 = getCryptoDevices();
for ( const QString &mapperPath : cryptoDevices2 )
{
tryUmount( mapperPath );
QString news = tryCryptoClose( mapperPath );
@ -168,7 +170,7 @@ ClearMountsJob::exec()
goodNews.append( news );
}
foreach ( QString p, partitionsList )
for ( const QString &p : partitionsList )
{
QString partPath = QString( "/dev/%1" ).arg( p );
@ -247,13 +249,13 @@ ClearMountsJob::tryCryptoClose( const QString& mapperPath )
QStringList
ClearMountsJob::getCryptoDevices()
ClearMountsJob::getCryptoDevices() const
{
QDir mapperDir( "/dev/mapper" );
QFileInfoList fiList = mapperDir.entryInfoList( QDir::Files );
const QFileInfoList fiList = mapperDir.entryInfoList( QDir::Files );
QStringList list;
QProcess process;
foreach ( QFileInfo fi, fiList )
for ( const QFileInfo &fi : fiList )
{
if ( fi.baseName() == "control" )
continue;

View file

@ -39,7 +39,7 @@ private:
QString tryUmount( const QString& partPath );
QString tryClearSwap( const QString& partPath );
QString tryCryptoClose( const QString& mapperPath );
QStringList getCryptoDevices();
QStringList getCryptoDevices() const;
Device* m_device;
};

View file

@ -126,7 +126,8 @@ FillGlobalStorageJob::prettyDescription() const
{
QStringList lines;
foreach ( QVariant partitionItem, createPartitionList().toList() )
const auto partitionList = createPartitionList().toList();
for ( const QVariant &partitionItem : partitionList )
{
if ( partitionItem.type() == QVariant::Map )
{