Commit 16ac3928 authored by Daniel Teske's avatar Daniel Teske

Algorithms: Introduce helper Utils::equal

Takes a member (function) pointer and a value and returns a functor,
that takes a instance of the mfp's class and returns whether it's equal
to value. Sounds complicated, but is a common pattern that is easy to
understand.

Change-Id: Iaaeb90488d34ddfd6940dadd4c66705381198fee
Reviewed-by: default avatarNikita Baryshnikov <nib952051@gmail.com>
Reviewed-by: default avatarTobias Hunger <tobias.hunger@digia.com>
parent eccf1dc1
...@@ -121,6 +121,23 @@ typename T::value_type findOrDefault(const T &container, F function) ...@@ -121,6 +121,23 @@ typename T::value_type findOrDefault(const T &container, F function)
return findOr(container, typename T::value_type(), function); return findOr(container, typename T::value_type(), function);
} }
//////////////////
// find helpers
//////////////////
template<typename R, typename S, typename T>
auto equal(R (S::*function)() const, T value)
-> decltype(std::bind<bool>(std::equal_to<T>(), value, std::bind(function, std::placeholders::_1)))
{
return std::bind<bool>(std::equal_to<T>(), value, std::bind(function, std::placeholders::_1));
}
template<typename R, typename S, typename T>
auto equal(R (S::*member), T value)
-> decltype(std::bind<bool>(std::equal_to<T>(), value, std::bind(member, std::placeholders::_1)))
{
return std::bind<bool>(std::equal_to<T>(), value, std::bind(member, std::placeholders::_1));
}
////////////////// //////////////////
// transform // transform
///////////////// /////////////////
......
...@@ -161,9 +161,7 @@ QList<Core::Id> BuildConfiguration::knownStepLists() const ...@@ -161,9 +161,7 @@ QList<Core::Id> BuildConfiguration::knownStepLists() const
BuildStepList *BuildConfiguration::stepList(Core::Id id) const BuildStepList *BuildConfiguration::stepList(Core::Id id) const
{ {
return Utils::findOrDefault(m_stepLists, [id](BuildStepList *list) { return Utils::findOrDefault(m_stepLists, Utils::equal(&BuildStepList::id, id));
return id == list->id();
});
} }
QVariantMap BuildConfiguration::toMap() const QVariantMap BuildConfiguration::toMap() const
......
...@@ -131,9 +131,8 @@ QVariant ToolChainKitInformation::defaultValue(Kit *k) const ...@@ -131,9 +131,8 @@ QVariant ToolChainKitInformation::defaultValue(Kit *k) const
Abi abi = Abi::hostAbi(); Abi abi = Abi::hostAbi();
ToolChain *tc = Utils::findOr(tcList, tcList.first(), [&abi](ToolChain *tc) { ToolChain *tc = Utils::findOr(tcList, tcList.first(),
return tc->targetAbi() == abi; Utils::equal(&ToolChain::targetAbi, abi));
});
return tc->id(); return tc->id();
} }
......
...@@ -409,9 +409,7 @@ Kit *KitManager::find(Core::Id id) ...@@ -409,9 +409,7 @@ Kit *KitManager::find(Core::Id id)
if (!id.isValid()) if (!id.isValid())
return 0; return 0;
return Utils::findOrDefault(kits(), [id](Kit *k) { return Utils::findOrDefault(kits(), Utils::equal(&Kit::id, id));
return k->id() == id;
});
} }
Kit *KitManager::find(const KitMatcher &matcher) Kit *KitManager::find(const KitMatcher &matcher)
......
...@@ -227,16 +227,12 @@ void Project::setActiveTarget(Target *target) ...@@ -227,16 +227,12 @@ void Project::setActiveTarget(Target *target)
Target *Project::target(Core::Id id) const Target *Project::target(Core::Id id) const
{ {
return Utils::findOrDefault(d->m_targets, [&id](Target *target) { return Utils::findOrDefault(d->m_targets, Utils::equal(&Target::id, id));
return target->id() == id;
});
} }
Target *Project::target(Kit *k) const Target *Project::target(Kit *k) const
{ {
return Utils::findOrDefault(d->m_targets, [&k](Target *target) { return Utils::findOrDefault(d->m_targets, Utils::equal(&Target::kit, k));
return target->kit() == k;
});
} }
bool Project::supportsKit(Kit *k, QString *errorMessage) const bool Project::supportsKit(Kit *k, QString *errorMessage) const
...@@ -499,9 +495,7 @@ void Project::setup(QList<const BuildInfo *> infoList) ...@@ -499,9 +495,7 @@ void Project::setup(QList<const BuildInfo *> infoList)
continue; continue;
Target *t = target(k); Target *t = target(k);
if (!t) { if (!t) {
t = Utils::findOrDefault(toRegister, [&k](Target *i){ t = Utils::findOrDefault(toRegister, Utils::equal(&Target::kit, k));
return i->kit() == k;
});
} }
if (!t) { if (!t) {
t = new Target(this, k); t = new Target(this, k);
......
...@@ -513,9 +513,7 @@ Project *SessionManager::projectForNode(Node *node) ...@@ -513,9 +513,7 @@ Project *SessionManager::projectForNode(Node *node)
Q_ASSERT(rootProjectNode); Q_ASSERT(rootProjectNode);
return Utils::findOrDefault(d->m_projects, [rootProjectNode](Project *p) { return Utils::findOrDefault(d->m_projects, Utils::equal(&Project::rootProjectNode, rootProjectNode));
return p->rootProjectNode() == rootProjectNode;
});
} }
QList<Node *> SessionManager::nodesForFile(const QString &fileName, Project *project) QList<Node *> SessionManager::nodesForFile(const QString &fileName, Project *project)
......
...@@ -680,9 +680,7 @@ void Target::updateDefaultRunConfigurations() ...@@ -680,9 +680,7 @@ void Target::updateDefaultRunConfigurations()
// Try to find a runconfiguration that matches the project name. That is a good // Try to find a runconfiguration that matches the project name. That is a good
// candidate for something to run initially. // candidate for something to run initially.
selected = Utils::findOr(newConfigured, selected, selected = Utils::findOr(newConfigured, selected,
[this] (RunConfiguration *rc) { Utils::equal(&RunConfiguration::displayName, project()->displayName()));
return rc->displayName() == project()->displayName();
});
setActiveRunConfiguration(selected); setActiveRunConfiguration(selected);
} else if (!newUnconfigured.isEmpty()){ } else if (!newUnconfigured.isEmpty()){
setActiveRunConfiguration(newUnconfigured.at(0)); setActiveRunConfiguration(newUnconfigured.at(0));
......
...@@ -314,9 +314,7 @@ ToolChain *ToolChainManager::findToolChain(const QString &id) ...@@ -314,9 +314,7 @@ ToolChain *ToolChainManager::findToolChain(const QString &id)
if (id.isEmpty()) if (id.isEmpty())
return 0; return 0;
return Utils::findOrDefault(d->m_toolChains, [&id](ToolChain *tc) { return Utils::findOrDefault(d->m_toolChains, Utils::equal(&ToolChain::id, id));
return tc->id() == id;
});
} }
FileName ToolChainManager::defaultDebugger(const Abi &abi) FileName ToolChainManager::defaultDebugger(const Abi &abi)
......
...@@ -375,9 +375,7 @@ ToolChainNode *ToolChainModel::createNode(ToolChainNode *parent, ToolChain *tc, ...@@ -375,9 +375,7 @@ ToolChainNode *ToolChainModel::createNode(ToolChainNode *parent, ToolChain *tc,
ToolChainNode *ToolChainModel::findToolChain(const QList<ToolChainNode *> &container, ToolChain *tc) ToolChainNode *ToolChainModel::findToolChain(const QList<ToolChainNode *> &container, ToolChain *tc)
{ {
return Utils::findOrDefault(container, [tc] (ToolChainNode *n) { return Utils::findOrDefault(container, Utils::equal(&ToolChainNode::toolChain, tc));
return n->toolChain == tc;
});
} }
void ToolChainModel::addToolChain(ToolChain *tc) void ToolChainModel::addToolChain(ToolChain *tc)
......
...@@ -544,11 +544,8 @@ void ExamplesListModel::updateQtVersions() ...@@ -544,11 +544,8 @@ void ExamplesListModel::updateQtVersions()
// try to select the previously selected Qt version, or // try to select the previously selected Qt version, or
// select examples corresponding to 'highest' Qt version // select examples corresponding to 'highest' Qt version
int currentQtId = m_exampleSetModel->getQtId(currentIndex); int currentQtId = m_exampleSetModel->getQtId(currentIndex);
BaseQtVersion *newQtVersion = Utils::findOr(m_qtVersions, BaseQtVersion *newQtVersion = Utils::findOrDefault(m_qtVersions,
0, Utils::equal(&BaseQtVersion::uniqueId, currentQtId));
[&currentQtId](BaseQtVersion *version) {
return version->uniqueId() == currentQtId;
});
if (!newQtVersion) if (!newQtVersion)
newQtVersion = findHighestQtVersion(); newQtVersion = findHighestQtVersion();
......
...@@ -609,11 +609,8 @@ void QtOptionsPageWidget::addQtDir() ...@@ -609,11 +609,8 @@ void QtOptionsPageWidget::addQtDir()
if (BuildableHelperLibrary::isQtChooser(fi)) if (BuildableHelperLibrary::isQtChooser(fi))
qtVersion = FileName::fromString(BuildableHelperLibrary::qtChooserToQmakePath(fi.symLinkTarget())); qtVersion = FileName::fromString(BuildableHelperLibrary::qtChooserToQmakePath(fi.symLinkTarget()));
BaseQtVersion *version = Utils::findOr(m_versions, BaseQtVersion *version = Utils::findOrDefault(m_versions,
0, Utils::equal(&BaseQtVersion::qmakeCommand, qtVersion));
[&qtVersion](BaseQtVersion *v) {
return v->qmakeCommand() == qtVersion;
});
if (version) { if (version) {
// Already exist // Already exist
QMessageBox::warning(this, tr("Qt Version Already Known"), QMessageBox::warning(this, tr("Qt Version Already Known"),
......
...@@ -641,9 +641,7 @@ FileName QtVersionManager::findQMakeBinaryFromMakefile(const QString &makefile) ...@@ -641,9 +641,7 @@ FileName QtVersionManager::findQMakeBinaryFromMakefile(const QString &makefile)
BaseQtVersion *QtVersionManager::qtVersionForQMakeBinary(const FileName &qmakePath) BaseQtVersion *QtVersionManager::qtVersionForQMakeBinary(const FileName &qmakePath)
{ {
return Utils::findOrDefault(versions(), [&qmakePath](BaseQtVersion *v) { return Utils::findOrDefault(versions(), Utils::equal(&BaseQtVersion::qmakeCommand, qmakePath));
return v->qmakeCommand() == qmakePath;
});
} }
void dumpQMakeAssignments(const QList<QMakeAssignment> &list) void dumpQMakeAssignments(const QList<QMakeAssignment> &list)
......
...@@ -334,9 +334,7 @@ UiCodeModelManager::~UiCodeModelManager() ...@@ -334,9 +334,7 @@ UiCodeModelManager::~UiCodeModelManager()
static UiCodeModelSupport *findUiFile(const QList<UiCodeModelSupport *> &range, const QString &uiFile) static UiCodeModelSupport *findUiFile(const QList<UiCodeModelSupport *> &range, const QString &uiFile)
{ {
return Utils::findOrDefault(range, [uiFile](UiCodeModelSupport *support) { return Utils::findOrDefault(range, Utils::equal(&UiCodeModelSupport::uiFileName, uiFile));
return support->uiFileName() == uiFile;
});
} }
void UiCodeModelManager::update(ProjectExplorer::Project *project, QHash<QString, QString> uiHeaders) void UiCodeModelManager::update(ProjectExplorer::Project *project, QHash<QString, QString> uiHeaders)
......
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