Commit 5a30007e authored by Daniel Teske's avatar Daniel Teske

ProjectExplorer: Use more algorithms

Make heavy use of lambdas

Change-Id: I53197d0ffb4ae8eec076f15335cc22b74e1def1d
Reviewed-by: Orgad Shaneh's avatarOrgad Shaneh <orgads@gmail.com>
Reviewed-by: default avatarEike Ziller <eike.ziller@digia.com>
Reviewed-by: default avatarhjk <hjk121@nokiamail.com>
parent c78258cd
......@@ -56,6 +56,40 @@ void erase(QList<T> &container, F predicate)
container.end());
}
template<typename T, typename F>
bool contains(const T &container, F function)
{
typename T::const_iterator end = container.end();
typename T::const_iterator begin = container.begin();
typename T::const_iterator it = std::find_if(begin, end, function);
return it != end;
}
template<typename T, typename F>
typename T::value_type findOr(const T &container, typename T::value_type other, F function)
{
typename T::const_iterator end = container.end();
typename T::const_iterator begin = container.begin();
typename T::const_iterator it = std::find_if(begin, end, function);
if (it == end)
return other;
return *it;
}
template<typename T, typename F>
typename T::value_type maxElementOr(const T &container, typename T::value_type other, F function)
{
typename T::const_iterator end = container.end();
typename T::const_iterator begin = container.begin();
typename T::const_iterator it = std::max_element(begin, end, function);
if (it == end)
return other;
return *it;
}
// Note: add overloads for other container types as needed
template<typename T, typename F>
Q_REQUIRED_RESULT
......
......@@ -32,6 +32,7 @@
#include "icore.h"
#include <utils/qtcassert.h>
#include <utils/algorithm.h>
#include <QByteArray>
#include <QCoreApplication>
......@@ -50,7 +51,6 @@
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include <algorithm>
#include <functional>
enum { debugMimeDB = 0 };
......@@ -898,16 +898,14 @@ struct RemovePred : std::unary_function<MimeType::IMagicMatcherSharedPointer, bo
MimeType::IMagicMatcherList MimeType::magicRuleMatchers() const
{
IMagicMatcherList ruleMatchers = m_d->magicMatchers;
ruleMatchers.erase(std::remove_if(ruleMatchers.begin(), ruleMatchers.end(), RemovePred(true)),
ruleMatchers.end());
Utils::erase(ruleMatchers, RemovePred(true));
return ruleMatchers;
}
void MimeType::setMagicRuleMatchers(const IMagicMatcherList &matchers)
{
m_d->magicMatchers.erase(std::remove_if(m_d->magicMatchers.begin(), m_d->magicMatchers.end(),
RemovePred(false)),
m_d->magicMatchers.end());
Utils::erase(m_d->magicMatchers, RemovePred(false));
m_d->magicMatchers.append(matchers);
}
......
......@@ -36,6 +36,7 @@
#include <texteditor/itexteditor.h>
#include <utils/filesearch.h>
#include <utils/algorithm.h>
#include <QSettings>
#include <QRegExp>
......@@ -90,11 +91,11 @@ Utils::FileIterator *AllProjectsFind::filesForProjects(const QStringList &nameFi
QStringList filteredFiles;
if (!filterRegs.isEmpty()) {
foreach (const QString &file, projectFiles) {
foreach (QRegExp reg, filterRegs) {
if (reg.exactMatch(file) || reg.exactMatch(QFileInfo(file).fileName())) {
filteredFiles.append(file);
break;
}
if (Utils::anyOf(filterRegs,
[&file](QRegExp reg) {
return (reg.exactMatch(file) || reg.exactMatch(QFileInfo(file).fileName()));
})) {
filteredFiles.append(file);
}
}
} else {
......
......@@ -43,6 +43,7 @@
#include <extensionsystem/invoker.h>
#include <utils/qtcassert.h>
#include <utils/algorithm.h>
#include <QAction>
#include <QVBoxLayout>
......@@ -231,10 +232,9 @@ void AppOutputPane::updateCloseActions()
bool AppOutputPane::aboutToClose() const
{
foreach (const RunControlTab &rt, m_runControlTabs)
if (rt.runControl->isRunning() && !rt.runControl->promptToStop())
return false;
return true;
return Utils::allOf(m_runControlTabs, [](const RunControlTab &rt) {
return !rt.runControl->isRunning() || rt.runControl->promptToStop();
});
}
void AppOutputPane::aboutToUnloadSession()
......@@ -599,10 +599,9 @@ void AppOutputPane::slotRunControlFinished2(RunControl *sender)
bool AppOutputPane::isRunning() const
{
foreach (const RunControlTab &rt, m_runControlTabs)
if (rt.runControl->isRunning())
return true;
return false;
return Utils::anyOf(m_runControlTabs, [](const RunControlTab &rt) {
return rt.runControl->isRunning();
});
}
bool AppOutputPane::canNext() const
......
......@@ -42,6 +42,7 @@
#include <utils/qtcassert.h>
#include <utils/stringutils.h>
#include <utils/algorithm.h>
#include <QDebug>
......@@ -154,10 +155,9 @@ Utils::AbstractMacroExpander *BuildConfiguration::macroExpander()
QList<Core::Id> BuildConfiguration::knownStepLists() const
{
QList<Core::Id> result;
foreach (BuildStepList *list, m_stepLists)
result.append(list->id());
return result;
return Utils::transform(m_stepLists, [](BuildStepList *list) {
return list->id();
});
}
BuildStepList *BuildConfiguration::stepList(Core::Id id) const
......@@ -344,16 +344,11 @@ IBuildConfigurationFactory *IBuildConfigurationFactory::find(Kit *k, const QStri
{
QList<IBuildConfigurationFactory *> factories
= ExtensionSystem::PluginManager::instance()->getObjects<IBuildConfigurationFactory>();
IBuildConfigurationFactory *factory = 0;
int priority = -1;
foreach (IBuildConfigurationFactory *i, factories) {
int iPriority = i->priority(k, projectPath);
if (iPriority > priority) {
factory = i;
priority = iPriority;
}
}
return factory;
return Utils::maxElementOr(factories, 0,
[&k, &projectPath](IBuildConfigurationFactory *a, IBuildConfigurationFactory *b) {
return a->priority(k, projectPath) > b->priority(k, projectPath);
});
}
// create
......@@ -361,16 +356,11 @@ IBuildConfigurationFactory * IBuildConfigurationFactory::find(Target *parent)
{
QList<IBuildConfigurationFactory *> factories
= ExtensionSystem::PluginManager::getObjects<IBuildConfigurationFactory>();
IBuildConfigurationFactory *factory = 0;
int priority = -1;
foreach (IBuildConfigurationFactory *i, factories) {
int iPriority = i->priority(parent);
if (iPriority > priority) {
factory = i;
priority = iPriority;
}
}
return factory;
return Utils::maxElementOr(factories, 0,
[&parent](IBuildConfigurationFactory *a, IBuildConfigurationFactory *b) {
return a->priority(parent) > b->priority(parent);
});
}
// clone
......
......@@ -42,6 +42,7 @@
#include <coreplugin/icore.h>
#include <extensionsystem/pluginmanager.h>
#include <utils/qtcassert.h>
#include <utils/algorithm.h>
#include <QPixmap>
#include <QPushButton>
......@@ -118,8 +119,9 @@ void DeviceSettingsWidget::initGui()
const QList<IDeviceFactory *> &factories
= ExtensionSystem::PluginManager::getObjects<IDeviceFactory>();
bool hasDeviceFactories = std::any_of(factories.constBegin(), factories.constEnd(),
[](IDeviceFactory *factory) { return factory->canCreate(); });
bool hasDeviceFactories = Utils::anyOf(factories, [](IDeviceFactory *factory) {
return factory->canCreate();
});
m_ui->addConfigButton->setEnabled(hasDeviceFactories);
......
......@@ -43,6 +43,7 @@
#include <projectexplorer/kitmanager.h>
#include <limits>
#include <utils/qtcassert.h>
#include <utils/algorithm.h>
/*!
\class ProjectExplorer::Project
......@@ -226,20 +227,16 @@ void Project::setActiveTarget(Target *target)
Target *Project::target(const Core::Id id) const
{
foreach (Target *target, d->m_targets) {
if (target->id() == id)
return target;
}
return 0;
return Utils::findOr(d->m_targets, 0, [&id](Target *target) {
return target->id() == id;
});
}
Target *Project::target(Kit *k) const
{
foreach (Target *target, d->m_targets) {
if (target->kit() == k)
return target;
}
return 0;
return Utils::findOr(d->m_targets, 0, [&k](Target *target) {
return target->kit() == k;
});
}
bool Project::supportsKit(Kit *k, QString *errorMessage) const
......@@ -501,12 +498,9 @@ void Project::setup(QList<const BuildInfo *> infoList)
continue;
Target *t = target(k);
if (!t) {
foreach (Target *i, toRegister) {
if (i->kit() == k) {
t = i;
break;
}
}
t = Utils::findOr(toRegister, 0, [&k](Target *i){
return i->kit() == k;
});
}
if (!t) {
t = new Target(this, k);
......
......@@ -49,6 +49,7 @@
#include <utils/runextensions.h>
#include <utils/synchronousprocess.h>
#include <utils/winutils.h>
#include <utils/algorithm.h>
#include <QDir>
#include <QUrl>
......@@ -479,16 +480,14 @@ QStringList BaseQtVersion::warningReason() const
ToolChain *BaseQtVersion::preferredToolChain(const FileName &ms) const
{
const FileName spec = ms.isEmpty() ? mkspec() : ms;
ToolChain *possibleTc = 0;
foreach (ToolChain *tc, ToolChainManager::toolChains()) {
if (!qtAbis().contains(tc->targetAbi()))
continue;
if (tc->suggestedMkspecList().contains(spec))
return tc; // perfect match
if (!possibleTc)
possibleTc = tc; // first possible match
}
return possibleTc;
QList<ToolChain *> toolchains = ToolChainManager::toolChains();
return Utils::findOr(toolchains,
toolchains.isEmpty() ? 0 : toolchains.first(),
[&spec, this](ToolChain *tc) {
return qtAbis().contains(tc->targetAbi())
&& tc->suggestedMkspecList().contains(spec);
});
}
FileName BaseQtVersion::qmakeCommand() const
......
......@@ -41,6 +41,7 @@
#include <qtsupport/qtversionmanager.h>
#include <utils/environment.h>
#include <utils/qtcassert.h>
#include <utils/algorithm.h>
#include <algorithm>
......@@ -262,11 +263,9 @@ static QString fixStringForTags(const QString &string)
static QStringList trimStringList(const QStringList &stringlist)
{
QStringList returnList;
foreach (const QString &string, stringlist)
returnList << string.trimmed();
return returnList;
return Utils::transform(stringlist, [](const QString &string) {
return string.trimmed();
});
}
static QString relativeOrInstallPath(const QString &path, const QString &manifestPath,
......@@ -540,13 +539,12 @@ void ExamplesListModel::updateQtVersions()
// try to select the previously selected Qt version, or
// select examples corresponding to 'highest' Qt version
int currentQtId = m_exampleSetModel->getQtId(currentIndex);
BaseQtVersion *newQtVersion = 0;
foreach (BaseQtVersion *version, m_qtVersions) {
if (version->uniqueId() == currentQtId) {
newQtVersion = version;
break;
}
}
BaseQtVersion *newQtVersion = Utils::findOr(m_qtVersions,
0,
[&currentQtId](BaseQtVersion *version) {
return version->uniqueId() == currentQtId;
});
if (!newQtVersion)
newQtVersion = findHighestQtVersion();
currentIndex = m_exampleSetModel->indexForQtVersion(newQtVersion);
......@@ -753,11 +751,9 @@ void ExamplesListModelFilter::updateFilter()
bool containsSubString(const QStringList &list, const QString &substr, Qt::CaseSensitivity cs)
{
foreach (const QString &elem, list)
if (elem.contains(substr, cs))
return true;
return false;
return Utils::contains(list, [&substr, &cs](const QString &elem) {
return elem.contains(substr, cs);
});
}
bool ExamplesListModelFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
......
......@@ -45,6 +45,7 @@
#include <utils/pathchooser.h>
#include <utils/qtcassert.h>
#include <utils/runextensions.h>
#include <utils/algorithm.h>
#include <QDir>
#include <QMessageBox>
......@@ -609,13 +610,11 @@ void QtOptionsPageWidget::addQtDir()
if (BuildableHelperLibrary::isQtChooser(fi))
qtVersion = FileName::fromString(BuildableHelperLibrary::qtChooserToQmakePath(fi.symLinkTarget()));
BaseQtVersion *version = 0;
foreach (BaseQtVersion *v, m_versions) {
if (v->qmakeCommand() == qtVersion) {
version = v;
break;
}
}
BaseQtVersion *version = Utils::findOr(m_versions,
0,
[&qtVersion](BaseQtVersion *v) {
return v->qmakeCommand() == qtVersion;
});
if (version) {
// Already exist
QMessageBox::warning(this, tr("Qt Version Already Known"),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment