Skip to content

Commit

Permalink
Convert from Qt foreach() macro to C++ standard range-for. (plugins)
Browse files Browse the repository at this point in the history
The use of foreach has caused some unexpected problems in the
scheduler, which were chased down to containers detaching in the call
to get the last item of the range.  This detach guarantees that the
current iterator is now invalid and the operation will eventually run
off the end of the range.

The Qt company is also planning to remove the foreach/Q_FOREACH macro
in an (as yet unspecified) upcoming release.

Replace all instances of the foreach macro with a standard C++
range-for iteration, and (in almost all cases) use the qAsConst macro
mark to the ranges as constant to prevent detaching of the data.
There are a couple of places where adding the qAsConst macro throws a
compiler error, so omit qAsConst in those instances.  These cases all
appear to occur with temporary lists, and this behavior is intentional
on Qt's part (see the Qt documentation).

Refs #13621
  • Loading branch information
linuxdude42 committed May 22, 2020
1 parent 1a1b698 commit 5d9b74e
Show file tree
Hide file tree
Showing 26 changed files with 99 additions and 99 deletions.
8 changes: 4 additions & 4 deletions mythplugins/mytharchive/mytharchive/exportnative.cpp
Expand Up @@ -131,7 +131,7 @@ void ExportNative::updateSizeBar()
{
int64_t size = 0;

foreach (auto a, m_archiveList)
for (const auto *a : qAsConst(m_archiveList))
size += a->size;

m_usedSpace = size / 1024 / 1024;
Expand Down Expand Up @@ -229,7 +229,7 @@ void ExportNative::updateArchiveList(void)
}
else
{
foreach (auto a, m_archiveList)
for (auto *a : qAsConst(m_archiveList))
{
auto* item = new MythUIButtonListItem(m_archiveButtonList, a->title);
item->SetData(QVariant::fromValue(a));
Expand Down Expand Up @@ -311,7 +311,7 @@ void ExportNative::saveConfiguration(void)
":STARTTIME, :SIZE, :FILENAME, :HASCUTLIST, :DURATION, "
":CUTDURATION, :VIDEOWIDTH, :VIDEOHEIGHT, :FILECODEC, "
":VIDEOCODEC, :ENCODERPROFILE);");
foreach (auto a, m_archiveList)
for (const auto * a : qAsConst(m_archiveList))
{
query.bindValue(":TYPE", a->type);
query.bindValue(":TITLE", a->title);
Expand Down Expand Up @@ -380,7 +380,7 @@ void ExportNative::createConfigFile(const QString &filename)
job.appendChild(media);

// now loop though selected archive items and add them to the xml file
foreach (auto a, m_archiveList)
for (const auto * a : qAsConst(m_archiveList))
{
QDomElement file = doc.createElement("file");
file.setAttribute("type", a->type.toLower() );
Expand Down
14 changes: 7 additions & 7 deletions mythplugins/mytharchive/mytharchive/fileselector.cpp
Expand Up @@ -208,7 +208,7 @@ void FileSelector::OKPressed()

// remove any items that have been removed from the list
QList<ArchiveItem *> tempAList;
foreach (auto a, *m_archiveList)
for (auto *a : qAsConst(*m_archiveList))
{
bool found = false;

Expand All @@ -226,7 +226,7 @@ void FileSelector::OKPressed()
tempAList.append(a);
}

foreach (auto x, tempAList)
for (auto *x : qAsConst(tempAList))
m_archiveList->removeAll(x);

// remove any items that are already in the list
Expand All @@ -235,7 +235,7 @@ void FileSelector::OKPressed()
{
f = m_selectedList.at(x);

foreach (auto a, *m_archiveList)
for (const auto *a : qAsConst(*m_archiveList))
{
if (a->filename == f)
{
Expand Down Expand Up @@ -345,9 +345,9 @@ void FileSelector::updateSelectedList()
m_selectedList.takeFirst();
m_selectedList.clear();

foreach (auto a, *m_archiveList)
for (const auto *a : qAsConst(*m_archiveList))
{
foreach (auto f, m_fileData)
for (const auto *f : qAsConst(m_fileData))
{
if (f->filename == a->filename)
{
Expand Down Expand Up @@ -379,7 +379,7 @@ void FileSelector::updateFileList()
filters << "*";
QFileInfoList list = d.entryInfoList(filters, QDir::Dirs, QDir::Name);

foreach (const auto & fi, list)
for (const auto & fi : qAsConst(list))
{
if (fi.fileName() != ".")
{
Expand All @@ -405,7 +405,7 @@ void FileSelector::updateFileList()
filters.clear();
filters = m_filemask.split(" ", QString::SkipEmptyParts);
list = d.entryInfoList(filters, QDir::Files, QDir::Name);
foreach (const auto & fi, list)
for (const auto & fi : qAsConst(list))
{
auto *data = new FileData;
data->selected = false;
Expand Down
8 changes: 4 additions & 4 deletions mythplugins/mytharchive/mytharchive/mythburn.cpp
Expand Up @@ -192,7 +192,7 @@ bool MythBurn::keyPressEvent(QKeyEvent *event)
void MythBurn::updateSizeBar(void)
{
int64_t size = 0;
foreach (auto a, m_archiveList)
for (const auto *a : qAsConst(m_archiveList))
size += a->newsize;

uint usedSpace = size / 1024 / 1024;
Expand Down Expand Up @@ -411,7 +411,7 @@ void MythBurn::updateArchiveList(void)
}
else
{
foreach (auto a, m_archiveList)
for (auto *a : qAsConst(m_archiveList))
{
QCoreApplication::processEvents();
// get duration of this file
Expand Down Expand Up @@ -548,7 +548,7 @@ EncoderProfile *MythBurn::getDefaultProfile(ArchiveItem *item)
QString defaultProfile =
gCoreContext->GetSetting("MythArchiveDefaultEncProfile", "SP");

foreach (auto x, m_profileList)
for (auto *x : qAsConst(m_profileList))
if (x->name == defaultProfile)
profile = x;
}
Expand Down Expand Up @@ -694,7 +694,7 @@ void MythBurn::loadConfiguration(void)

EncoderProfile *MythBurn::getProfileFromName(const QString &profileName)
{
foreach (auto x, m_profileList)
for (auto *x : qAsConst(m_profileList))
if (x->name == profileName)
return x;

Expand Down
16 changes: 8 additions & 8 deletions mythplugins/mytharchive/mytharchive/recordingselector.cpp
Expand Up @@ -284,11 +284,11 @@ void RecordingSelector::OKPressed()
// loop though selected recordings and add them to the list
// remove any items that have been removed from the list
QList<ArchiveItem *> tempAList;
foreach (auto a, *m_archiveList)
for (auto *a : qAsConst(*m_archiveList))
{
bool found = false;

foreach (auto p, m_selectedList)
for (auto *p : qAsConst(m_selectedList))
{
if (a->type != "Recording" || a->filename == p->GetPlaybackURL(false, true))
{
Expand All @@ -301,14 +301,14 @@ void RecordingSelector::OKPressed()
tempAList.append(a);
}

foreach (auto x, tempAList)
for (auto *x : qAsConst(tempAList))
m_archiveList->removeAll(x);

// remove any items that are already in the list
QList<ProgramInfo *> tempSList;
foreach (auto p, m_selectedList)
for (auto *p : qAsConst(m_selectedList))
{
foreach (auto a, *m_archiveList)
for (const auto *a : qAsConst(*m_archiveList))
{
if (a->filename == p->GetPlaybackURL(false, true))
{
Expand All @@ -318,11 +318,11 @@ void RecordingSelector::OKPressed()
}
}

foreach (auto x, tempSList)
for (auto *x : qAsConst(tempSList))
m_selectedList.removeAll(x);

// add all that are left
foreach (auto p, m_selectedList)
for (auto *p : qAsConst(m_selectedList))
{
auto *a = new ArchiveItem;
a->type = "Recording";
Expand Down Expand Up @@ -498,7 +498,7 @@ void RecordingSelector::updateSelectedList()

m_selectedList.clear();

foreach (auto a, *m_archiveList)
for (const auto *a : qAsConst(*m_archiveList))
{
for (auto *p : *m_recordingList)
{
Expand Down
2 changes: 1 addition & 1 deletion mythplugins/mytharchive/mytharchive/themeselector.cpp
Expand Up @@ -134,7 +134,7 @@ void DVDThemeSelector::getThemeList(void)
filters << "*";
QFileInfoList list = d.entryInfoList(filters, QDir::Dirs, QDir::Name);

foreach (const auto & fi, list)
for (const auto & fi : qAsConst(list))
{
if (QFile::exists(m_themeDir + fi.fileName() + "/preview.png"))
{
Expand Down
4 changes: 2 additions & 2 deletions mythplugins/mytharchive/mytharchive/thumbfinder.cpp
Expand Up @@ -92,7 +92,7 @@ ThumbFinder::ThumbFinder(MythScreenStack *parent, ArchiveItem *archiveItem,
{
// copy thumbList so we can abandon changes if required
m_thumbList.clear();
foreach (auto item, m_archiveItem->thumbList)
for (const auto *item : qAsConst(m_archiveItem->thumbList))
{
auto *thumb = new ThumbImage;
*thumb = *item;
Expand Down Expand Up @@ -288,7 +288,7 @@ void ThumbFinder::savePressed()
delete m_archiveItem->thumbList.takeFirst();
m_archiveItem->thumbList.clear();

foreach (auto item, m_thumbList)
for (const auto *item : qAsConst(m_thumbList))
{
auto *thumb = new ThumbImage;
*thumb = *item;
Expand Down
18 changes: 9 additions & 9 deletions mythplugins/mytharchive/mytharchive/videoselector.cpp
Expand Up @@ -243,11 +243,11 @@ void VideoSelector::OKPressed()
// loop though selected videos and add them to the list
// remove any items that have been removed from the list
QList<ArchiveItem *> tempAList;
foreach (auto a, *m_archiveList)
for (auto *a : qAsConst(*m_archiveList))
{
bool found = false;

foreach (auto v, m_selectedList)
for (const auto *v : qAsConst(m_selectedList))
{
if (a->type != "Video" || a->filename == v->filename)
{
Expand All @@ -260,14 +260,14 @@ void VideoSelector::OKPressed()
tempAList.append(a);
}

foreach (auto x, tempAList)
for (auto *x : qAsConst(tempAList))
m_archiveList->removeAll(x);

// remove any items that are already in the list
QList<VideoInfo *> tempSList;
foreach (auto v, m_selectedList)
for (auto *v : qAsConst(m_selectedList))
{
foreach (auto a, *m_archiveList)
for (const auto *a : qAsConst(*m_archiveList))
{
if (a->filename == v->filename)
{
Expand All @@ -277,11 +277,11 @@ void VideoSelector::OKPressed()
}
}

foreach (auto x, tempSList)
for (auto *x : qAsConst(tempSList))
m_selectedList.removeAll(x);

// add all that are left
foreach (auto v, m_selectedList)
for (const auto *v : qAsConst(m_selectedList))
{
auto *a = new ArchiveItem;
a->type = "Video";
Expand Down Expand Up @@ -521,9 +521,9 @@ void VideoSelector::updateSelectedList()

m_selectedList.clear();

foreach (auto a, *m_archiveList)
for (const auto *a : qAsConst(*m_archiveList))
{
foreach (auto v, *m_videoList)
for (auto *v : qAsConst(*m_videoList))
{
if (v->filename == a->filename)
{
Expand Down
6 changes: 3 additions & 3 deletions mythplugins/mythbrowser/mythbrowser/bookmarkmanager.cpp
Expand Up @@ -235,7 +235,7 @@ uint BookmarkManager::GetMarkedCount(void)
{
uint count = 0;

foreach (auto site, m_siteList)
for (auto *site : qAsConst(m_siteList))
{
if (site && site->m_selected)
count++;
Expand Down Expand Up @@ -559,7 +559,7 @@ void BookmarkManager::slotDoDeleteMarked(bool doDelete)

QString category = m_groupList->GetValue();

foreach (auto site, m_siteList)
for (auto *site : qAsConst(m_siteList))
{
if (site && site->m_selected)
RemoveFromDB(site);
Expand Down Expand Up @@ -597,7 +597,7 @@ void BookmarkManager::slotShowMarked(void)
QString zoom = gCoreContext->GetSetting("WebBrowserZoomLevel", "1.0");
QStringList urls;

foreach (auto site, m_siteList)
for (const auto *site : qAsConst(m_siteList))
{
if (site && site->m_selected)
urls.append(site->m_url);
Expand Down
10 changes: 5 additions & 5 deletions mythplugins/mythgame/mythgame/gamehandler.cpp
Expand Up @@ -300,7 +300,7 @@ static void UpdateGameCounts(const QStringList& updatelist)
QString firstname;
QString basename;

foreach (auto GameType, updatelist)
for (const auto & GameType : qAsConst(updatelist))
{
LOG(VB_GENERAL, LOG_NOTICE,
LOC + QString("Update gametype %1").arg(GameType));
Expand Down Expand Up @@ -776,7 +776,7 @@ void GameHandler::processAllGames(void)
checkHandlers();
QStringList updatelist;

foreach (auto handler, *handlers)
for (auto *handler : qAsConst(*handlers))
{
if (handler)
{
Expand All @@ -797,7 +797,7 @@ GameHandler* GameHandler::GetHandler(RomInfo *rominfo)
if (!rominfo)
return nullptr;

foreach (auto handler, *handlers)
for (auto *handler : qAsConst(*handlers))
{
if (handler)
{
Expand All @@ -814,7 +814,7 @@ GameHandler* GameHandler::GetHandlerByName(const QString& systemname)
if (systemname.isEmpty() || systemname.isNull())
return nullptr;

foreach (auto handler, *handlers)
for (auto *handler : qAsConst(*handlers))
{
if (handler)
{
Expand Down Expand Up @@ -914,7 +914,7 @@ void GameHandler::Launchgame(RomInfo *romdata, const QString& systemname)
QStringList cmdlist = exec.split(";");
if (cmdlist.count() > 0)
{
foreach (auto & cmd, cmdlist)
for (const auto & cmd : qAsConst(cmdlist))
{
LOG(VB_GENERAL, LOG_INFO, LOC +
QString("Executing : %1").arg(cmd));
Expand Down
10 changes: 5 additions & 5 deletions mythplugins/mythgame/mythgame/gamescan.cpp
Expand Up @@ -64,7 +64,7 @@ void GameScannerThread::verifyFiles()
GameScanner::tr("Verifying game files..."));

// For every file we know about, check to see if it still exists.
foreach (auto info, m_dbgames)
for (const auto *info : qAsConst(m_dbgames))
{
QString romfile = info->Romname();
QString system = info->System();
Expand Down Expand Up @@ -104,7 +104,7 @@ void GameScannerThread::updateDB()
SendProgressEvent(counter, (uint)(m_files.size() + m_remove.size()),
GameScanner::tr("Updating game database..."));

foreach (auto & file, m_files)
for (const auto & file : qAsConst(m_files))
{
if (!file.indb)
{
Expand All @@ -119,7 +119,7 @@ void GameScannerThread::updateDB()
SendProgressEvent(++counter);
}

foreach (const uint & p, m_remove)
for (const uint & p : qAsConst(m_remove))
{
removeOrphan(p);
m_dbDataChanged = true;
Expand All @@ -143,7 +143,7 @@ bool GameScannerThread::buildFileList()
QDir dir((*iter)->SystemRomPath());
QStringList extensions = (*iter)->ValidExtensions();
QStringList filters;
foreach (auto & ext, extensions)
for (const auto & ext : qAsConst(extensions))
{
filters.append(QString("*.%1").arg(ext));
}
Expand All @@ -152,7 +152,7 @@ bool GameScannerThread::buildFileList()
dir.setFilter(QDir::Files | QDir::Readable | QDir::NoDotAndDotDot);

QStringList files = dir.entryList();
foreach (auto & file, files)
for (const auto & file : qAsConst(files))
{
RomFileInfo info;
info.system = (*iter)->SystemName();
Expand Down
2 changes: 1 addition & 1 deletion mythplugins/mythgame/mythgame/gameui.cpp
Expand Up @@ -273,7 +273,7 @@ void GameUI::itemClicked(MythUIButtonListItem* /*item*/)
chooseSystemPopup->SetReturnEvent(this, "chooseSystemPopup");
QString all_systems = romInfo->AllSystems();
QStringList players = all_systems.split(',');
foreach (auto & player, players)
for (const auto & player : qAsConst(players))
chooseSystemPopup->AddButton(player);
popupStack->AddScreen(chooseSystemPopup);
}
Expand Down

0 comments on commit 5d9b74e

Please sign in to comment.