From 9ddcd554d3df9cceaba7f2fcbfe1444edcd6efad Mon Sep 17 00:00:00 2001 From: Joerg Bornemann Date: Wed, 17 Jan 2018 14:06:28 +0100 Subject: Replace some uses of find_if with any_of and none_of Calls to find_if that just compare the result with an end iterator are replaced by calls to any_of or none_of. This leads to shorter, more expressive code. For increased readability, versions of any_of/none_of are provided that operate on whole containers instead of iterators. Change-Id: Ia4ff449176f22f1820635e810724983866d3265e Reviewed-by: Christian Kandeler --- src/app/qbs-setup-qt/setupqt.cpp | 8 ++++---- src/lib/corelib/language/moduleloader.cpp | 12 ++++-------- src/lib/corelib/tools/qbspluginmanager.cpp | 3 +-- src/lib/corelib/tools/stlutils.h | 12 ++++++++++++ 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/app/qbs-setup-qt/setupqt.cpp b/src/app/qbs-setup-qt/setupqt.cpp index 9cd5be283..7f9d98d81 100644 --- a/src/app/qbs-setup-qt/setupqt.cpp +++ b/src/app/qbs-setup-qt/setupqt.cpp @@ -61,6 +61,7 @@ #include namespace qbs { +using Internal::none_of; using Internal::contains; using Internal::HostOsInfo; using Internal::Tr; @@ -110,10 +111,9 @@ QList SetupQt::fetchEnvironments() const auto qmakePaths = collectQmakePaths(); for (const QString &qmakePath : qmakePaths) { const QtEnvironment env = fetchEnvironment(qmakePath); - if (std::find_if(qtEnvironments.cbegin(), qtEnvironments.cend(), - [&env](const QtEnvironment &otherEnv) { - return env.includePath == otherEnv.includePath; - }) == qtEnvironments.cend()) { + if (none_of(qtEnvironments, [&env](const QtEnvironment &otherEnv) { + return env.includePath == otherEnv.includePath; + })) { qtEnvironments.push_back(env); } } diff --git a/src/lib/corelib/language/moduleloader.cpp b/src/lib/corelib/language/moduleloader.cpp index 4908193fb..81d1088b5 100644 --- a/src/lib/corelib/language/moduleloader.cpp +++ b/src/lib/corelib/language/moduleloader.cpp @@ -406,12 +406,10 @@ private: // Export item prototypes do not have instantiated modules. // The module instances are where the Export is used. QBS_ASSERT(m_currentModuleInstance, return false); - auto it = std::find_if(m_currentModuleInstance->modules().cbegin(), - m_currentModuleInstance->modules().cend(), - [this] (const Item::Module &m) { + auto hasCurrentModuleName = [this](const Item::Module &m) { return m.name == m_currentModuleName; - }); - if (it != m_currentModuleInstance->modules().cend()) + }; + if (any_of(m_currentModuleInstance->modules(), hasCurrentModuleName)) return true; } @@ -1193,11 +1191,9 @@ private: bool moduleExists(const QualifiedId &name) const { const auto &deps = m_productItem->modules(); - auto it = std::find_if(deps.begin(), deps.end(), - [&name] (const Item::Module &module) { + return any_of(deps, [&name](const Item::Module &module) { return module.name == name; }); - return deps.end() != it; } const QString &m_productName; diff --git a/src/lib/corelib/tools/qbspluginmanager.cpp b/src/lib/corelib/tools/qbspluginmanager.cpp index 682e13921..a8d22f458 100644 --- a/src/lib/corelib/tools/qbspluginmanager.cpp +++ b/src/lib/corelib/tools/qbspluginmanager.cpp @@ -98,8 +98,7 @@ QbsPluginManager *QbsPluginManager::instance() void QbsPluginManager::registerStaticPlugin(QbsPluginLoadFunction load, QbsPluginUnloadFunction unload) { - auto begin = d->staticPlugins.cbegin(), end = d->staticPlugins.cend(); - if (std::find_if(begin, end, [&load](const QbsPlugin &p) { return p.load == load; }) == end) + if (none_of(d->staticPlugins, [&load](const QbsPlugin &p) { return p.load == load; })) d->staticPlugins.push_back(QbsPlugin { load, unload, false }); } diff --git a/src/lib/corelib/tools/stlutils.h b/src/lib/corelib/tools/stlutils.h index 894c9b4a1..1d69816ad 100644 --- a/src/lib/corelib/tools/stlutils.h +++ b/src/lib/corelib/tools/stlutils.h @@ -67,6 +67,18 @@ bool containsKey(const C &container, const typename C::key_type &v) return container.find(v) != end; } +template +bool any_of(const Container &container, const UnaryPredicate &predicate) +{ + return std::any_of(std::begin(container), std::end(container), predicate); +} + +template +bool none_of(const Container &container, const UnaryPredicate &predicate) +{ + return std::none_of(std::begin(container), std::end(container), predicate); +} + template C &operator<<(C &container, const typename C::value_type &v) { -- cgit v1.2.3