diff options
author | Denis Shienkov <denis.shienkov@gmail.com> | 2022-01-17 22:17:58 +0300 |
---|---|---|
committer | Denis Shienkov <denis.shienkov@gmail.com> | 2022-01-27 14:06:28 +0000 |
commit | d3e44609fd28268b525d12853e90b53d9e3f2183 (patch) | |
tree | b569ed52fe947984a985950aff117306137e63bc /src | |
parent | 249c5ab3b4c3d1c68b294ff93ec8089f85728051 (diff) | |
download | qbs-d3e44609fd28268b525d12853e90b53d9e3f2183.tar.gz |
Use std::transform where it is possible
... to unify and simplify a code.
Change-Id: Ia493c5f428c1da4df3c8623e448be8ac72fd4f44
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
Diffstat (limited to 'src')
29 files changed, 124 insertions, 134 deletions
diff --git a/src/app/qbs-create-project/createproject.cpp b/src/app/qbs-create-project/createproject.cpp index cde4a1a18..4167a396e 100644 --- a/src/app/qbs-create-project/createproject.cpp +++ b/src/app/qbs-create-project/createproject.cpp @@ -62,10 +62,10 @@ void ProjectCreator::run(const QString &topLevelDir, ProjectStructure projectStr const QStringList &whiteList, const QStringList &blackList) { m_projectStructure = projectStructure; - for (const QString &s : whiteList) - m_whiteList.push_back(QRegularExpression(QRegularExpression::wildcardToRegularExpression(s))); - for (const QString &s : blackList) - m_blackList.push_back(QRegularExpression(QRegularExpression::wildcardToRegularExpression(s))); + qbs::Internal::transform(whiteList, m_whiteList, [](const QString &s) { + return QRegularExpression(QRegularExpression::wildcardToRegularExpression(s)); }); + qbs::Internal::transform(blackList, m_blackList, [](const QString &s) { + return QRegularExpression(QRegularExpression::wildcardToRegularExpression(s)); }); m_topLevelProject.dirPath = topLevelDir; setupProject(&m_topLevelProject); serializeProject(m_topLevelProject); diff --git a/src/app/qbs-setup-toolchains/clangclprobe.cpp b/src/app/qbs-setup-toolchains/clangclprobe.cpp index d1a3a9ac5..3643a828d 100644 --- a/src/app/qbs-setup-toolchains/clangclprobe.cpp +++ b/src/app/qbs-setup-toolchains/clangclprobe.cpp @@ -50,6 +50,7 @@ #include <tools/profile.h> #include <tools/qttools.h> #include <tools/settings.h> +#include <tools/stlutils.h> #include <QtCore/qdir.h> #include <QtCore/qfileinfo.h> @@ -125,10 +126,8 @@ void clangClProbe(Settings *settings, std::vector<Profile> &profiles) QStringLiteral("x86_64"), QStringLiteral("x86") }; - for (const auto &arch: architectures) { + qbs::Internal::transform(architectures, profiles, [settings, clangCl](const auto &arch) { const auto profileName = QStringLiteral("clang-cl-%1").arg(arch); - auto profile = createProfileHelper( - settings, profileName, clangCl.toolchainInstallPath, clangCl.vcvarsallPath, arch); - profiles.push_back(std::move(profile)); - } + return createProfileHelper(settings, profileName, clangCl.toolchainInstallPath, + clangCl.vcvarsallPath, arch); }); } diff --git a/src/app/qbs-setup-toolchains/cosmicprobe.cpp b/src/app/qbs-setup-toolchains/cosmicprobe.cpp index a41b21ee8..82b7193bc 100644 --- a/src/app/qbs-setup-toolchains/cosmicprobe.cpp +++ b/src/app/qbs-setup-toolchains/cosmicprobe.cpp @@ -171,10 +171,8 @@ void cosmicProbe(Settings *settings, std::vector<Profile> &profiles) qbsInfo() << Tr::tr("Trying to detect COSMIC toolchains..."); const std::vector<ToolchainInstallInfo> allInfos = installedCosmicsFromPath(); - for (const ToolchainInstallInfo &info : allInfos) { - const auto profile = createCosmicProfileHelper(info, settings); - profiles.push_back(profile); - } + qbs::Internal::transform(allInfos, profiles, [settings](const auto &info) { + return createCosmicProfileHelper(info, settings); }); if (allInfos.empty()) qbsInfo() << Tr::tr("No COSMIC toolchains found."); diff --git a/src/app/qbs-setup-toolchains/dmcprobe.cpp b/src/app/qbs-setup-toolchains/dmcprobe.cpp index 3ac12eba5..6b74c5b9d 100644 --- a/src/app/qbs-setup-toolchains/dmcprobe.cpp +++ b/src/app/qbs-setup-toolchains/dmcprobe.cpp @@ -211,10 +211,8 @@ void dmcProbe(Settings *settings, std::vector<Profile> &profiles) qbsInfo() << Tr::tr("Trying to detect DMC toolchains..."); const std::vector<ToolchainInstallInfo> allInfos = installedDmcsFromPath(); - for (const ToolchainInstallInfo &info : allInfos) { - const auto profile = createDmcProfileHelper(info, settings); - profiles.push_back(profile); - } + qbs::Internal::transform(allInfos, profiles, [settings](const auto &info) { + return createDmcProfileHelper(info, settings); }); if (allInfos.empty()) qbsInfo() << Tr::tr("No DMC toolchains found."); diff --git a/src/app/qbs-setup-toolchains/gccprobe.cpp b/src/app/qbs-setup-toolchains/gccprobe.cpp index 85aba8a68..bbe77bb02 100644 --- a/src/app/qbs-setup-toolchains/gccprobe.cpp +++ b/src/app/qbs-setup-toolchains/gccprobe.cpp @@ -47,6 +47,7 @@ #include <tools/hostosinfo.h> #include <tools/profile.h> #include <tools/settings.h> +#include <tools/stlutils.h> #include <tools/toolchains.h> #include <QtCore/qdir.h> @@ -269,11 +270,8 @@ static QStringList buildCompilerNameFilters(const QString &compilerName) QLatin1String("*-*-*-*-") + compilerName + QLatin1String("-[1-9]*") }; - std::transform(filters.begin(), filters.end(), filters.begin(), - [](const auto &filter) { - return HostOsInfo::appendExecutableSuffix(filter); - }); - + Internal::transform(filters, [](const auto &filter) { + return HostOsInfo::appendExecutableSuffix(filter); }); return filters; } diff --git a/src/app/qbs-setup-toolchains/iarewprobe.cpp b/src/app/qbs-setup-toolchains/iarewprobe.cpp index 10845e4bc..adf5febb3 100644 --- a/src/app/qbs-setup-toolchains/iarewprobe.cpp +++ b/src/app/qbs-setup-toolchains/iarewprobe.cpp @@ -323,10 +323,8 @@ void iarProbe(Settings *settings, std::vector<Profile> &profiles) pathInfos.cbegin(), pathInfos.cend(), std::back_inserter(allInfos)); - for (const ToolchainInstallInfo &info : allInfos) { - const auto profile = createIarProfileHelper(info, settings); - profiles.push_back(profile); - } + qbs::Internal::transform(allInfos, profiles, [settings](const auto &info) { + return createIarProfileHelper(info, settings); }); if (allInfos.empty()) qbsInfo() << Tr::tr("No IAR toolchains found."); diff --git a/src/app/qbs-setup-toolchains/keilprobe.cpp b/src/app/qbs-setup-toolchains/keilprobe.cpp index 9a54a7861..4bab54c92 100644 --- a/src/app/qbs-setup-toolchains/keilprobe.cpp +++ b/src/app/qbs-setup-toolchains/keilprobe.cpp @@ -416,10 +416,8 @@ void keilProbe(Settings *settings, std::vector<Profile> &profiles) pathInfos.cbegin(), pathInfos.cend(), std::back_inserter(allInfos)); - for (const ToolchainInstallInfo &info : allInfos) { - const auto profile = createKeilProfileHelper(info, settings); - profiles.push_back(profile); - } + qbs::Internal::transform(allInfos, profiles, [settings](const auto &info) { + return createKeilProfileHelper(info, settings); }); if (allInfos.empty()) qbsInfo() << Tr::tr("No KEIL toolchains found."); diff --git a/src/app/qbs-setup-toolchains/msvcprobe.cpp b/src/app/qbs-setup-toolchains/msvcprobe.cpp index 00aedaaab..a0fa7cee6 100644 --- a/src/app/qbs-setup-toolchains/msvcprobe.cpp +++ b/src/app/qbs-setup-toolchains/msvcprobe.cpp @@ -130,12 +130,13 @@ void msvcProbe(Settings *settings, std::vector<Profile> &profiles) if (sdk.isDefault) defaultWinSDK = sdk; const auto ais = MSVC::findSupportedArchitectures(sdk); - for (const MSVCArchInfo &ai : ais) { + + qbs::Internal::transform(ais, winSDKs, [&sdk](const auto &ai) { WinSDK specificSDK = sdk; specificSDK.architecture = ai.arch; specificSDK.binPath = ai.binPath; - winSDKs.push_back(specificSDK); - } + return specificSDK; + }); } } diff --git a/src/lib/corelib/api/projectdata.cpp b/src/lib/corelib/api/projectdata.cpp index 289c14781..750186539 100644 --- a/src/lib/corelib/api/projectdata.cpp +++ b/src/lib/corelib/api/projectdata.cpp @@ -45,8 +45,9 @@ #include <tools/fileinfo.h> #include <tools/jsliterals.h> #include <tools/qbsassert.h> -#include <tools/stringconstants.h> #include <tools/qttools.h> +#include <tools/stlutils.h> +#include <tools/stringconstants.h> #include <tools/stringconstants.h> #include <QtCore/qdir.h> @@ -62,9 +63,10 @@ using namespace Internal; template<typename T> static QJsonArray toJsonArray(const QList<T> &list, const QStringList &moduleProperties) { + // We can't use transformed from stlutils.h here as QJsonArray has not the reserve() method. QJsonArray jsonArray; - std::transform(list.begin(), list.end(), std::back_inserter(jsonArray), - [&moduleProperties](const T &v) { return v.toJson(moduleProperties);}); + transform(list, jsonArray, [&moduleProperties](const T &v) { + return v.toJson(moduleProperties);}); return jsonArray; } @@ -218,11 +220,7 @@ bool GroupData::isEnabled() const QStringList GroupData::allFilePaths() const { const QList<ArtifactData> &artifacts = allSourceArtifacts(); - QStringList paths; - paths.reserve(artifacts.size()); - std::transform(artifacts.cbegin(), artifacts.cend(), std::back_inserter(paths), - [](const ArtifactData &sa) { return sa.filePath(); }); - return paths; + return transformed<QStringList>(artifacts, [](const auto &sa) { return sa.filePath(); }); } bool operator!=(const GroupData &lhs, const GroupData &rhs) diff --git a/src/lib/corelib/buildgraph/buildgraphloader.cpp b/src/lib/corelib/buildgraph/buildgraphloader.cpp index 3e5dab2ee..9d65f42e4 100644 --- a/src/lib/corelib/buildgraph/buildgraphloader.cpp +++ b/src/lib/corelib/buildgraph/buildgraphloader.cpp @@ -971,10 +971,8 @@ void BuildGraphLoader::rescueOldBuildData(const ResolvedProductConstPtr &restore rad.children.emplace_back(child->product->name, child->product->multiplexConfigurationId, child->filePath(), childrenInfo.childrenAddedByScanner.contains(child)); - std::transform(oldArtifact->fileDependencies.cbegin(), - oldArtifact->fileDependencies.cend(), - std::back_inserter(rad.fileDependencies), - std::mem_fn(&FileDependency::filePath)); + transform(oldArtifact->fileDependencies, rad.fileDependencies, + std::mem_fn(&FileDependency::filePath)); } newlyResolvedProduct->buildData->addRescuableArtifactData(oldArtifact->filePath(), rad); } diff --git a/src/lib/corelib/buildgraph/executor.cpp b/src/lib/corelib/buildgraph/executor.cpp index 3df2b3625..f0fe7d726 100644 --- a/src/lib/corelib/buildgraph/executor.cpp +++ b/src/lib/corelib/buildgraph/executor.cpp @@ -1107,10 +1107,8 @@ void Executor::checkForUnbuiltProducts() m_logger.qbsInfo() << Tr::tr("Build done%1.").arg(configString()); } else { m_error.append(Tr::tr("The following products could not be built%1:").arg(configString())); - QStringList productNames; - std::transform(unbuiltProducts.cbegin(), unbuiltProducts.cend(), - std::back_inserter(productNames), - [](const ResolvedProductConstPtr &p) { return p->fullDisplayName(); }); + auto productNames = transformed<QStringList>(unbuiltProducts, [](const auto &p) { + return p->fullDisplayName(); }); std::sort(productNames.begin(), productNames.end()); m_error.append(productNames.join(QLatin1String(", "))); } diff --git a/src/lib/corelib/buildgraph/inputartifactscanner.cpp b/src/lib/corelib/buildgraph/inputartifactscanner.cpp index ad674596a..05bbc0d4b 100644 --- a/src/lib/corelib/buildgraph/inputartifactscanner.cpp +++ b/src/lib/corelib/buildgraph/inputartifactscanner.cpp @@ -49,11 +49,12 @@ #include <language/language.h> #include <logging/categories.h> +#include <tools/error.h> #include <tools/fileinfo.h> -#include <tools/scannerpluginmanager.h> #include <tools/qbsassert.h> -#include <tools/error.h> #include <tools/qttools.h> +#include <tools/scannerpluginmanager.h> +#include <tools/stlutils.h> #include <QtCore/qdir.h> #include <QtCore/qstringlist.h> @@ -212,9 +213,9 @@ Set<DependencyScanner *> InputArtifactScanner::scannersForArtifact(const Artifac InputArtifactScannerContext::DependencyScannerCacheItem &cache = scannerCache[fileTag]; if (!cache.valid) { cache.valid = true; - for (ScannerPlugin *scanner : ScannerPluginManager::scannersForFileTag(fileTag)) { - cache.scanners.push_back(std::make_shared<PluginDependencyScanner>(scanner)); - } + const auto scanners = ScannerPluginManager::scannersForFileTag(fileTag); + transform(scanners, cache.scanners, [](const auto &scanner) { + return std::make_shared<PluginDependencyScanner>(scanner); }); for (const ResolvedScannerConstPtr &scanner : product->scanners) { if (scanner->inputs.contains(fileTag)) { cache.scanners.push_back( diff --git a/src/lib/corelib/generators/generatorutils.cpp b/src/lib/corelib/generators/generatorutils.cpp index 8089592c9..c49e1fa75 100644 --- a/src/lib/corelib/generators/generatorutils.cpp +++ b/src/lib/corelib/generators/generatorutils.cpp @@ -30,6 +30,8 @@ #include "generatorutils.h" +#include <tools/stlutils.h> + namespace qbs { namespace gen { namespace utils { @@ -189,11 +191,10 @@ QStringList cppStringModuleProperties(const PropertyMap &qbsProps, { QStringList properties; for (const auto &propertyName : propertyNames) { - const auto entries = qbsProps.getModuleProperty( - Internal::StringConstants::cppModule(), - propertyName).toStringList(); - for (const auto &entry : entries) - properties.push_back(entry.trimmed()); + const auto entries = qbsProps.getModuleProperty(Internal::StringConstants::cppModule(), + propertyName).toStringList(); + Internal::transform(entries, properties, [](const auto &entry) { + return entry.trimmed(); }); } return properties; } diff --git a/src/lib/corelib/jsextensions/binaryfile.cpp b/src/lib/corelib/jsextensions/binaryfile.cpp index 88b3608de..3731c4da7 100644 --- a/src/lib/corelib/jsextensions/binaryfile.cpp +++ b/src/lib/corelib/jsextensions/binaryfile.cpp @@ -42,6 +42,7 @@ #include <language/scriptengine.h> #include <logging/translator.h> #include <tools/hostosinfo.h> +#include <tools/stlutils.h> #include <QtCore/qfile.h> #include <QtCore/qfileinfo.h> @@ -214,10 +215,7 @@ QVariantList BinaryFile::read(qint64 size) .arg(m_file->fileName(), m_file->errorString())); } - QVariantList data; - std::for_each(bytes.constBegin(), bytes.constEnd(), [&data](const char &c) { - data.append(c); }); - return data; + return transformed<QVariantList>(bytes, [](const char &c) { return c; }); } void BinaryFile::write(const QVariantList &data) @@ -225,9 +223,8 @@ void BinaryFile::write(const QVariantList &data) if (checkForClosed()) return; - QByteArray bytes; - std::for_each(data.constBegin(), data.constEnd(), [&bytes](const QVariant &v) { - bytes.append(char(v.toUInt() & 0xFF)); }); + const auto bytes = transformed<QByteArray>(data, [](const QVariant &v) { + return char(v.toUInt() & 0xFF); }); const qint64 size = m_file->write(bytes); if (Q_UNLIKELY(size == -1)) { diff --git a/src/lib/corelib/jsextensions/pkgconfigjs.cpp b/src/lib/corelib/jsextensions/pkgconfigjs.cpp index 59aa3a970..76cc35084 100644 --- a/src/lib/corelib/jsextensions/pkgconfigjs.cpp +++ b/src/lib/corelib/jsextensions/pkgconfigjs.cpp @@ -180,11 +180,8 @@ PcPackage::VariablesMap variablesFromQVariantMap(const QVariantMap &map) std::vector<std::string> stringListToStdVector(const QStringList &list) { - std::vector<std::string> result; - result.reserve(list.size()); - for (const auto &string : list) - result.push_back(string.toStdString()); - return result; + return transformed<std::vector<std::string>>(list, [](const auto &s) { + return s.toStdString(); }); } } // namespace diff --git a/src/lib/corelib/jsextensions/utilitiesextension.cpp b/src/lib/corelib/jsextensions/utilitiesextension.cpp index 53596bd1b..2d00b5991 100644 --- a/src/lib/corelib/jsextensions/utilitiesextension.cpp +++ b/src/lib/corelib/jsextensions/utilitiesextension.cpp @@ -144,10 +144,9 @@ QScriptValue UtilitiesExtension::js_canonicalPlatform(QScriptContext *context, if (context->argumentCount() == 1 && value.isString()) { return engine->toScriptValue([&value] { - QStringList list; - for (const auto &s : HostOsInfo::canonicalOSIdentifiers(value.toString().toStdString())) - list.push_back(QString::fromStdString(s)); - return list; + const auto ids = HostOsInfo::canonicalOSIdentifiers(value.toString().toStdString()); + return transformed<QStringList>(ids, [](const auto &s) { + return QString::fromStdString(s); }); }()); } diff --git a/src/lib/corelib/language/moduleproviderloader.cpp b/src/lib/corelib/language/moduleproviderloader.cpp index 86246b0d6..309e01e2c 100644 --- a/src/lib/corelib/language/moduleproviderloader.cpp +++ b/src/lib/corelib/language/moduleproviderloader.cpp @@ -53,6 +53,7 @@ #include <tools/fileinfo.h> #include <tools/jsliterals.h> +#include <tools/stlutils.h> #include <tools/stringconstants.h> #include <QtCore/qtemporaryfile.h> @@ -79,8 +80,8 @@ ModuleProviderLoader::ModuleProviderResult ModuleProviderLoader::executeModulePr << "not found, checking for module providers"; const auto providerNames = getModuleProviders(productContext.item); if (providerNames) { - for (const auto &providerName : *providerNames) - providersToRun.push_back({providerName, ModuleProviderLookup::Named}); + providersToRun = transformed<std::vector<Provider>>(*providerNames, [](const auto &name) { + return Provider{name, ModuleProviderLookup::Named}; }); } else { for (QualifiedId providerName = moduleName; !providerName.empty(); providerName.pop_back()) { @@ -215,11 +216,8 @@ std::optional<std::vector<QualifiedId>> ModuleProviderLoader::getModuleProviders const auto providers = m_evaluator->optionalStringListValue(item, StringConstants::qbsModuleProviders()); if (providers) { - std::vector<QualifiedId> result; - result.reserve(providers->size()); - for (const auto &provider : *providers) - result.push_back(QualifiedId::fromString(provider)); - return result; + return transformed<std::vector<QualifiedId>>(*providers, [](const auto &provider) { + return QualifiedId::fromString(provider); }); } item = item->parent(); } diff --git a/src/lib/corelib/language/projectresolver.cpp b/src/lib/corelib/language/projectresolver.cpp index 83f42e4e2..68430d175 100644 --- a/src/lib/corelib/language/projectresolver.cpp +++ b/src/lib/corelib/language/projectresolver.cpp @@ -646,8 +646,7 @@ static QualifiedIdSet propertiesToEvaluate(std::deque<QualifiedId> initialProps, const auto insertResult = allProperties.insert(prop); if (!insertResult.second) continue; - for (const QualifiedId &directDep : deps.value(prop)) - remainingProps.push_back(directDep); + transform(deps.value(prop), remainingProps, [](const QualifiedId &id) { return id; }); } return allProperties; } @@ -1103,8 +1102,11 @@ void ProjectResolver::resolveExport(Item *exportItem, ProjectContext *) return p1.fullName < p2.fullName; }; std::sort(exportedModule.m_properties.begin(), exportedModule.m_properties.end(), cmpFunc); - for (const Item * const child : exportItem->children()) - exportedModule.children.push_back(resolveExportChild(child, exportedModule)); + + transform(exportItem->children(), exportedModule.children, + [&exportedModule, this](const auto &child) { + return resolveExportChild(child, exportedModule); }); + for (const JsImport &jsImport : exportItem->file()->jsImports()) { if (usesImport(exportedModule, jsImport.scopeName)) { exportedModule.importStatements << getLineAtLocation(jsImport.location, @@ -1130,13 +1132,13 @@ std::unique_ptr<ExportedItem> ProjectResolver::resolveExportChild(const Item *it // the original type name. exportedItem->name = item->typeName(); - for (const Item * const child : item->children()) - exportedItem->children.push_back(resolveExportChild(child, module)); + transform(item->children(), exportedItem->children, [&module, this](const auto &child) { + return resolveExportChild(child, module); }); + setupExportedProperties(item, QString(), exportedItem->properties); return exportedItem; } - QString ProjectResolver::sourceCodeAsFunction(const JSSourceValueConstPtr &value, const PropertyDeclaration &decl) const { diff --git a/src/lib/corelib/language/scriptengine.cpp b/src/lib/corelib/language/scriptengine.cpp index 8d2c6a2c5..0614f31f5 100644 --- a/src/lib/corelib/language/scriptengine.cpp +++ b/src/lib/corelib/language/scriptengine.cpp @@ -183,8 +183,8 @@ void ScriptEngine::import(const JsImport &jsImport, QScriptValue &targetObject) m_jsImportCache.insert(jsImport, jsImportValue); std::vector<QString> &filePathsForScriptValue = m_filePathsPerImport[jsImportValue.objectId()]; - for (const QString &fp : jsImport.filePaths) - filePathsForScriptValue.push_back(fp); + transform(jsImport.filePaths, filePathsForScriptValue, [](const auto &fp) { + return fp; }); } QScriptValue sv = newObject(); diff --git a/src/lib/corelib/language/value.cpp b/src/lib/corelib/language/value.cpp index 16326f521..5a4da2c8f 100644 --- a/src/lib/corelib/language/value.cpp +++ b/src/lib/corelib/language/value.cpp @@ -103,9 +103,9 @@ JSSourceValue::JSSourceValue(const JSSourceValue &other) : Value(other) m_baseValue = other.m_baseValue ? std::static_pointer_cast<JSSourceValue>(other.m_baseValue->clone()) : JSSourceValuePtr(); - m_alternatives.reserve(other.m_alternatives.size()); - for (const Alternative &otherAlt : other.m_alternatives) - m_alternatives.push_back(otherAlt.clone()); + m_alternatives = transformed<std::vector<Alternative>>( + other.m_alternatives, [](const auto &alternative) { + return alternative.clone(); }); } JSSourceValuePtr JSSourceValue::create(bool createdByPropertiesBlock) diff --git a/src/lib/corelib/tools/clangclinfo.cpp b/src/lib/corelib/tools/clangclinfo.cpp index 3aaee0786..a9a1cb449 100644 --- a/src/lib/corelib/tools/clangclinfo.cpp +++ b/src/lib/corelib/tools/clangclinfo.cpp @@ -147,9 +147,8 @@ std::vector<ClangClInfo> ClangClInfo::installedCompilers( std::vector<ClangClInfo> result; result.reserve(compilerPaths.size() + msvcs.size()); - - for (const auto &path: compilerPaths) - result.push_back({getToolchainInstallPath(QFileInfo(path)), vcvarsallPath}); + transform(compilerPaths, result, [&vcvarsallPath](const auto &path) { + return ClangClInfo{getToolchainInstallPath(QFileInfo(path)), vcvarsallPath}; }); // If we didn't find custom LLVM installation, try to find if it's installed with Visual Studio for (const auto &msvc : msvcs) { diff --git a/src/lib/corelib/tools/jsonhelper.h b/src/lib/corelib/tools/jsonhelper.h index e695bd465..cb911c45a 100644 --- a/src/lib/corelib/tools/jsonhelper.h +++ b/src/lib/corelib/tools/jsonhelper.h @@ -40,6 +40,8 @@ #ifndef QBS_JSON_HELPER_H #define QBS_JSON_HELPER_H +#include <tools/stlutils.h> + #include <QtCore/qjsonarray.h> #include <QtCore/qjsonobject.h> #include <QtCore/qjsonvalue.h> @@ -60,10 +62,7 @@ template<> inline QString fromJson(const QJsonValue &v) { return v.toString(); } template<> inline QStringList fromJson(const QJsonValue &v) { const QJsonArray &jsonList = v.toArray(); - QStringList stringList; - std::transform(jsonList.begin(), jsonList.end(), std::back_inserter(stringList), - [](const auto &v) { return v.toString(); }); - return stringList; + return transformed<QStringList>(jsonList, [](const auto &v) { return v.toString(); }); } template<> inline QVariantMap fromJson(const QJsonValue &v) { return v.toObject().toVariantMap(); } template<> inline QProcessEnvironment fromJson(const QJsonValue &v) diff --git a/src/lib/corelib/tools/msvcinfo.cpp b/src/lib/corelib/tools/msvcinfo.cpp index 58cd458c3..418a76ba1 100644 --- a/src/lib/corelib/tools/msvcinfo.cpp +++ b/src/lib/corelib/tools/msvcinfo.cpp @@ -669,12 +669,12 @@ std::vector<MSVC> MSVC::installedCompilers(Logger &logger) } const auto ais = findSupportedArchitectures(msvc); - for (const MSVCArchInfo &ai : ais) { + transform(ais, msvcs, [&msvc](const auto &ai) { MSVC specificMSVC = msvc; specificMSVC.architecture = ai.arch; specificMSVC.binPath = ai.binPath; - msvcs.push_back(specificMSVC); - } + return specificMSVC; + }); } return msvcs; } diff --git a/src/lib/corelib/tools/set.h b/src/lib/corelib/tools/set.h index ddd38b6f3..461175615 100644 --- a/src/lib/corelib/tools/set.h +++ b/src/lib/corelib/tools/set.h @@ -292,11 +292,7 @@ template<typename T> void Set<T>::store(PersistentPool &pool) const #ifdef QT_CORE_LIB template<typename T> QStringList Set<T>::toStringList() const { - QStringList sl; - sl.reserve(int(size())); - std::transform(cbegin(), cend(), std::back_inserter(sl), - [this](const T &e) { return toString(e); }); - return sl; + return transformed<QStringList>(*this, [this](const T &e) { return toString(e); }); } template<typename T> QString Set<T>::toString() const diff --git a/src/lib/corelib/tools/stlutils.h b/src/lib/corelib/tools/stlutils.h index 0a19957b5..104f88aaf 100644 --- a/src/lib/corelib/tools/stlutils.h +++ b/src/lib/corelib/tools/stlutils.h @@ -54,6 +54,28 @@ C sorted(const C &container) return result; } +template <typename To, typename From, typename Op> +To transformed(const From &from, Op op) +{ + To to; + to.reserve(from.size()); + std::transform(std::cbegin(from), std::cend(from), std::back_inserter(to), std::move(op)); + return to; +} + +template <typename C, typename Op> +void transform(C &&container, Op op) +{ + std::transform(std::begin(container), std::end(container), std::begin(container), + std::move(op)); +} + +template <typename To, typename From, typename Op> +void transform(const From &from, To &&to, Op op) +{ + std::transform(std::cbegin(from), std::cend(from), std::back_inserter(to), std::move(op)); +} + template <class C, class T> bool contains(const C &container, const T &v) { diff --git a/src/plugins/generator/keiluv/archs/arm/armtargetlinkergroup_v5.cpp b/src/plugins/generator/keiluv/archs/arm/armtargetlinkergroup_v5.cpp index 5532b58b0..52a19cf81 100644 --- a/src/plugins/generator/keiluv/archs/arm/armtargetlinkergroup_v5.cpp +++ b/src/plugins/generator/keiluv/archs/arm/armtargetlinkergroup_v5.cpp @@ -34,6 +34,8 @@ #include <generators/generatorutils.h> +#include <tools/stlutils.h> + namespace qbs { namespace keiluv { namespace arm { @@ -91,11 +93,8 @@ struct LinkerPageOptions final // Transform all paths to relative. const QString baseDirectory = qbs::gen::utils::buildRootPath(qbsProject); - std::transform(scatterFiles.begin(), scatterFiles.end(), - std::back_inserter(scatterFiles), - [baseDirectory](const QString &scatterFile) { - return gen::utils::relativeFilePath(baseDirectory, scatterFile); - }); + Internal::transform(scatterFiles, [&baseDirectory](const auto &file) { + return gen::utils::relativeFilePath(baseDirectory, file); }); // Make a first scatter file as a main scatter file. // Other scatter files will be interpretes as a misc controls. diff --git a/src/plugins/generator/keiluv/archs/mcs51/mcs51utils.cpp b/src/plugins/generator/keiluv/archs/mcs51/mcs51utils.cpp index 698f0df78..70ec47a62 100644 --- a/src/plugins/generator/keiluv/archs/mcs51/mcs51utils.cpp +++ b/src/plugins/generator/keiluv/archs/mcs51/mcs51utils.cpp @@ -30,6 +30,8 @@ #include "mcs51utils.h" +#include <tools/stlutils.h> + namespace qbs { namespace keiluv { namespace mcs51 { @@ -71,8 +73,7 @@ QString flagValue(const QStringList &flags, const QString &flagKey) QStringList flagValueParts(const QString &flagValue, const QLatin1Char &sep) { auto parts = flagValue.split(sep); - std::transform(parts.begin(), parts.end(), parts.begin(), - [](const auto &part) { return part.trimmed(); }); + Internal::transform(parts, [](const auto &part) { return part.trimmed(); }); return parts; } diff --git a/src/plugins/generator/keiluv/keiluvutils.cpp b/src/plugins/generator/keiluv/keiluvutils.cpp index ef93b9739..628a8e98a 100644 --- a/src/plugins/generator/keiluv/keiluvutils.cpp +++ b/src/plugins/generator/keiluv/keiluvutils.cpp @@ -32,6 +32,8 @@ #include <generators/generatorutils.h> +#include <tools/stlutils.h> + namespace qbs { namespace KeiluvUtils { @@ -82,10 +84,8 @@ QStringList includes(const PropertyMap &qbsProps) qbsProps, {QStringLiteral("includePaths"), QStringLiteral("systemIncludePaths")}); // Transform include path separators to native. - std::transform(paths.begin(), paths.end(), paths.begin(), - [](const auto &path) { - return QDir::toNativeSeparators(path); - }); + Internal::transform(paths, [](const auto &path) { + return QDir::toNativeSeparators(path); }); return paths; } @@ -100,23 +100,18 @@ QStringList staticLibraries(const PropertyMap &qbsProps) auto libs = gen::utils::cppStringModuleProperties( qbsProps, {QStringLiteral("staticLibraries")}); // Transform library path separators to native. - std::transform(libs.begin(), libs.end(), libs.begin(), - [](const auto &path) { - return QDir::toNativeSeparators(path); - }); + Internal::transform(libs, [](const auto &path) { + return QDir::toNativeSeparators(path); }); return libs; } QStringList dependencies(const std::vector<ProductData> &qbsProductDeps) { - QStringList deps; - for (const ProductData &qbsProductDep : qbsProductDeps) { - const auto path = qbsProductDep.buildDirectory() - + QLatin1String("/obj/") - + gen::utils::targetBinary(qbsProductDep); - deps.push_back(QDir::toNativeSeparators(path)); - } - return deps; + return Internal::transformed<QStringList>(qbsProductDeps, [](const auto &dep) { + const auto path = dep.buildDirectory() + QLatin1String("/obj/") + + gen::utils::targetBinary(dep); + return QDir::toNativeSeparators(path); + }); } } // namespace KeiluvUtils diff --git a/src/plugins/generator/makefilegenerator/makefilegenerator.cpp b/src/plugins/generator/makefilegenerator/makefilegenerator.cpp index abea0c0b0..c53dccd31 100644 --- a/src/plugins/generator/makefilegenerator/makefilegenerator.cpp +++ b/src/plugins/generator/makefilegenerator/makefilegenerator.cpp @@ -286,8 +286,8 @@ void qbs::MakefileGenerator::generate() } if (!processCommandEncountered && builtByDefault) { const auto outputs = transformerData.outputs(); - for (const ArtifactData &output : outputs) - filesCreatedByJsCommands.push_back(output.filePath()); + transform(outputs, filesCreatedByJsCommands, [](const auto &output) { + return output.filePath(); }); } } stream << "install-" << productTarget << ": " << productTarget << '\n'; |