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)
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
/////////////////
......
......@@ -161,9 +161,7 @@ QList<Core::Id> BuildConfiguration::knownStepLists() const
BuildStepList *BuildConfiguration::stepList(Core::Id id) const
{
return Utils::findOrDefault(m_stepLists, [id](BuildStepList *list) {
return id == list->id();
});
return Utils::findOrDefault(m_stepLists, Utils::equal(&BuildStepList::id, id));
}
QVariantMap BuildConfiguration::toMap() const
......
......@@ -131,9 +131,8 @@ QVariant ToolChainKitInformation::defaultValue(Kit *k) const
Abi abi = Abi::hostAbi();
ToolChain *tc = Utils::findOr(tcList, tcList.first(), [&abi](ToolChain *tc) {
return tc->targetAbi() == abi;
});
ToolChain *tc = Utils::findOr(tcList, tcList.first(),
Utils::equal(&ToolChain::targetAbi, abi));
return tc->id();
}
......
......@@ -409,9 +409,7 @@ Kit *KitManager::find(Core::Id id)
if (!id.isValid())
return 0;
return Utils::findOrDefault(kits(), [id](Kit *k) {
return k->id() == id;
});
return Utils::findOrDefault(kits(), Utils::equal(&Kit::id, id));
}
Kit *KitManager::find(const KitMatcher &matcher)
......
......@@ -227,16 +227,12 @@ void Project::setActiveTarget(Target *target)
Target *Project::target(Core::Id id) const
{
return Utils::findOrDefault(d->m_targets, [&id](Target *target) {
return target->id() == id;
});
return Utils::findOrDefault(d->m_targets, Utils::equal(&Target::id, id));
}
Target *Project::target(Kit *k) const
{
return Utils::findOrDefault(d->m_targets, [&k](Target *target) {
return target->kit() == k;
});
return Utils::findOrDefault(d->m_targets, Utils::equal(&Target::kit, k));
}
bool Project::supportsKit(Kit *k, QString *errorMessage) const
......@@ -499,9 +495,7 @@ void Project::setup(QList<const BuildInfo *> infoList)
continue;
Target *t = target(k);
if (!t) {
t = Utils::findOrDefault(toRegister, [&k](Target *i){
return i->kit() == k;
});
t = Utils::findOrDefault(toRegister, Utils::equal(&Target::kit, k));
}
if (!t) {
t = new Target(this, k);
......
......@@ -513,9 +513,7 @@ Project *SessionManager::projectForNode(Node *node)
Q_ASSERT(rootProjectNode);
return Utils::findOrDefault(d->m_projects, [rootProjectNode](Project *p) {
return p->rootProjectNode() == rootProjectNode;
});
return Utils::findOrDefault(d->m_projects, Utils::equal(&Project::rootProjectNode, rootProjectNode));
}
QList<Node *> SessionManager::nodesForFile(const QString &fileName, Project *project)
......
......@@ -680,9 +680,7 @@ void Target::updateDefaultRunConfigurations()
// Try to find a runconfiguration that matches the project name. That is a good
// candidate for something to run initially.
selected = Utils::findOr(newConfigured, selected,
[this] (RunConfiguration *rc) {
return rc->displayName() == project()->displayName();
});
Utils::equal(&RunConfiguration::displayName, project()->displayName()));
setActiveRunConfiguration(selected);
} else if (!newUnconfigured.isEmpty()){
setActiveRunConfiguration(newUnconfigured.at(0));
......
......@@ -314,9 +314,7 @@ ToolChain *ToolChainManager::findToolChain(const QString &id)
if (id.isEmpty())
return 0;
return Utils::findOrDefault(d->m_toolChains, [&id](ToolChain *tc) {
return tc->id() == id;
});
return Utils::findOrDefault(d->m_toolChains, Utils::equal(&ToolChain::id, id));
}
FileName ToolChainManager::defaultDebugger(const Abi &abi)
......
......@@ -375,9 +375,7 @@ ToolChainNode *ToolChainModel::createNode(ToolChainNode *parent, ToolChain *tc,
ToolChainNode *ToolChainModel::findToolChain(const QList<ToolChainNode *> &container, ToolChain *tc)
{
return Utils::findOrDefault(container, [tc] (ToolChainNode *n) {
return n->toolChain == tc;
});
return Utils::findOrDefault(container, Utils::equal(&ToolChainNode::toolChain, tc));
}
void ToolChainModel::addToolChain(ToolChain *tc)
......
......@@ -544,11 +544,8 @@ 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 = Utils::findOr(m_qtVersions,
0,
[&currentQtId](BaseQtVersion *version) {
return version->uniqueId() == currentQtId;
});
BaseQtVersion *newQtVersion = Utils::findOrDefault(m_qtVersions,
Utils::equal(&BaseQtVersion::uniqueId, currentQtId));
if (!newQtVersion)
newQtVersion = findHighestQtVersion();
......
......@@ -609,11 +609,8 @@ void QtOptionsPageWidget::addQtDir()
if (BuildableHelperLibrary::isQtChooser(fi))
qtVersion = FileName::fromString(BuildableHelperLibrary::qtChooserToQmakePath(fi.symLinkTarget()));
BaseQtVersion *version = Utils::findOr(m_versions,
0,
[&qtVersion](BaseQtVersion *v) {
return v->qmakeCommand() == qtVersion;
});
BaseQtVersion *version = Utils::findOrDefault(m_versions,
Utils::equal(&BaseQtVersion::qmakeCommand, qtVersion));
if (version) {
// Already exist
QMessageBox::warning(this, tr("Qt Version Already Known"),
......
......@@ -641,9 +641,7 @@ FileName QtVersionManager::findQMakeBinaryFromMakefile(const QString &makefile)
BaseQtVersion *QtVersionManager::qtVersionForQMakeBinary(const FileName &qmakePath)
{
return Utils::findOrDefault(versions(), [&qmakePath](BaseQtVersion *v) {
return v->qmakeCommand() == qmakePath;
});
return Utils::findOrDefault(versions(), Utils::equal(&BaseQtVersion::qmakeCommand, qmakePath));
}
void dumpQMakeAssignments(const QList<QMakeAssignment> &list)
......
......@@ -334,9 +334,7 @@ UiCodeModelManager::~UiCodeModelManager()
static UiCodeModelSupport *findUiFile(const QList<UiCodeModelSupport *> &range, const QString &uiFile)
{
return Utils::findOrDefault(range, [uiFile](UiCodeModelSupport *support) {
return support->uiFileName() == uiFile;
});
return Utils::findOrDefault(range, Utils::equal(&UiCodeModelSupport::uiFileName, uiFile));
}
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