From d0621a7d548c8664a480a7c09ffc49dc687e8142 Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Thu, 25 Jul 2024 16:29:24 +0200 Subject: [PATCH] Meson: Dismantle classes in favor of static methods Change-Id: Ifefd2847feb57c613a4089ed9d12b0d07d91d30c Reviewed-by: hjk --- .../mesonprojectmanager/mesoninfoparser.cpp | 331 ++++++++---------- 1 file changed, 152 insertions(+), 179 deletions(-) diff --git a/src/plugins/mesonprojectmanager/mesoninfoparser.cpp b/src/plugins/mesonprojectmanager/mesoninfoparser.cpp index 79959af3c69..083fadcca23 100644 --- a/src/plugins/mesonprojectmanager/mesoninfoparser.cpp +++ b/src/plugins/mesonprojectmanager/mesoninfoparser.cpp @@ -20,206 +20,179 @@ using namespace Utils; namespace MesonProjectManager::Internal::MesonInfoParser { -class BuildOptionsParser +static std::unique_ptr loadOption(const QJsonObject &option) { - static inline std::unique_ptr load_option(const QJsonObject &option) - { - const auto type = option["type"].toString(); - if (type == "string") - return std::make_unique(option["name"].toString(), - option["section"].toString(), - option["description"].toString(), - option["value"]); - if (type == "boolean") - return std::make_unique(option["name"].toString(), - option["section"].toString(), - option["description"].toString(), - option["value"]); - if (type == "combo") - return std::make_unique(option["name"].toString(), - option["section"].toString(), - option["description"].toString(), - option["choices"].toVariant().toStringList(), - option["value"]); - if (type == "integer") - return std::make_unique(option["name"].toString(), - option["section"].toString(), - option["description"].toString(), - option["value"]); - if (type == "array") - return std::make_unique(option["name"].toString(), - option["section"].toString(), - option["description"].toString(), - option["value"].toVariant()); - if (type == "feature") - return std::make_unique(option["name"].toString(), - option["section"].toString(), - option["description"].toString(), - option["value"]); - return std::make_unique(option["name"].toString(), + const auto type = option["type"].toString(); + if (type == "string") + return std::make_unique(option["name"].toString(), + option["section"].toString(), + option["description"].toString(), + option["value"]); + if (type == "boolean") + return std::make_unique(option["name"].toString(), option["section"].toString(), - option["description"].toString()); - } + option["description"].toString(), + option["value"]); + if (type == "combo") + return std::make_unique(option["name"].toString(), + option["section"].toString(), + option["description"].toString(), + option["choices"].toVariant().toStringList(), + option["value"]); + if (type == "integer") + return std::make_unique(option["name"].toString(), + option["section"].toString(), + option["description"].toString(), + option["value"]); + if (type == "array") + return std::make_unique(option["name"].toString(), + option["section"].toString(), + option["description"].toString(), + option["value"].toVariant()); + if (type == "feature") + return std::make_unique(option["name"].toString(), + option["section"].toString(), + option["description"].toString(), + option["value"]); + return std::make_unique(option["name"].toString(), + option["section"].toString(), + option["description"].toString()); +} - static inline std::vector> load_options(const QJsonArray &arr) - { - std::vector> buildOptions; - std::transform(std::cbegin(arr), - std::cend(arr), - std::back_inserter(buildOptions), - [](const auto &option) { return load_option(option.toObject()); }); - return buildOptions; - } - - std::vector> m_buildOptions; - -public: - BuildOptionsParser(const FilePath &buildDir) - { - FilePath path = buildDir / Constants::MESON_INFO_DIR / Constants::MESON_INTRO_BUIDOPTIONS; - auto arr = load(path.toFSPathString()); - if (arr) - m_buildOptions = load_options(*arr); - } - - BuildOptionsParser(const QJsonDocument &js) - { - auto obj = get(js.object(), "buildoptions"); - if (obj) - m_buildOptions = load_options(*obj); - } - - inline std::vector> takeBuildOptions() - { - return std::move(m_buildOptions); - } -}; - -class TargetParser +static std::vector> loadOptions(const QJsonArray &arr) { - static inline Target::SourceGroup extract_source(const QJsonValue &source) - { - const auto srcObj = source.toObject(); - return {srcObj["language"].toString(), - srcObj["compiler"].toVariant().toStringList(), - srcObj["parameters"].toVariant().toStringList(), - srcObj["sources"].toVariant().toStringList(), - srcObj["generated_sources"].toVariant().toStringList()}; - } + std::vector> buildOptions; + std::transform(std::cbegin(arr), + std::cend(arr), + std::back_inserter(buildOptions), + [](const auto &option) { return loadOption(option.toObject()); }); + return buildOptions; +} - static inline Target::SourceGroupList extract_sources(const QJsonArray &sources) - { - Target::SourceGroupList res; - std::transform(std::cbegin(sources), - std::cend(sources), - std::back_inserter(res), - extract_source); - return res; - } - - static inline Target extract_target(const QJsonValue &target) - { - auto targetObj = target.toObject(); - Target t{targetObj["type"].toString(), - targetObj["name"].toString(), - targetObj["id"].toString(), - targetObj["defined_in"].toString(), - targetObj["filename"].toVariant().toStringList(), - targetObj["extra_files"].toVariant().toStringList(), - targetObj["subproject"].toString(), - extract_sources(targetObj["target_sources"].toArray())}; - return t; - } - - static inline TargetsList load_targets(const QJsonArray &arr) - { - TargetsList targets; - std::transform(std::cbegin(arr), - std::cend(arr), - std::back_inserter(targets), - extract_target); - return targets; - } - -public: - static TargetsList targetList(const QJsonDocument &js) - { - if (auto obj = get(js.object(), "targets")) - return load_targets(*obj); - return {}; - } - - static TargetsList targetList(const FilePath &buildDir) - { - const FilePath path = buildDir / Constants::MESON_INFO_DIR / Constants::MESON_INTRO_TARGETS; - if (auto arr = load(path.toFSPathString())) - return load_targets(*arr); - return {}; - } -}; - -class BuildSystemFilesParser +static std::vector> buildOptionsList(const FilePath &buildDir) { - static void appendFiles(const std::optional &arr, FilePaths &dest) - { - if (arr) - std::transform(std::cbegin(*arr), - std::cend(*arr), - std::back_inserter(dest), - [](const auto &file) { - return FilePath::fromString(file.toString()); - }); - } + FilePath path = buildDir / Constants::MESON_INFO_DIR / Constants::MESON_INTRO_BUIDOPTIONS; + auto arr = load(path.toFSPathString()); + if (arr) + return loadOptions(*arr); + return {}; +} -public: - static inline FilePaths files(const FilePath &buildDir) - { - FilePaths files; - FilePath path = buildDir / Constants::MESON_INFO_DIR / Constants::MESON_INTRO_BUILDSYSTEM_FILES; - auto arr = load(path.toFSPathString()); - appendFiles(arr, files); - return files; - } +static std::vector> buildOptionsList(const QJsonDocument &js) +{ + auto obj = get(js.object(), "buildoptions"); + if (obj) + return loadOptions(*obj); + return {}; +} - static inline FilePaths files(const QJsonDocument &js) - { - FilePaths files; - auto arr = get(js.object(), "projectinfo", "buildsystem_files"); +static Target::SourceGroup extract_source(const QJsonValue &source) +{ + const auto srcObj = source.toObject(); + return { + srcObj["language"].toString(), + srcObj["compiler"].toVariant().toStringList(), + srcObj["parameters"].toVariant().toStringList(), + srcObj["sources"].toVariant().toStringList(), + srcObj["generated_sources"].toVariant().toStringList() + }; +} + +static Target::SourceGroupList extract_sources(const QJsonArray &sources) +{ + Target::SourceGroupList res; + std::transform(std::cbegin(sources), std::cend(sources), std::back_inserter(res), extract_source); + return res; +} + +static Target extract_target(const QJsonValue &target) +{ + auto targetObj = target.toObject(); + return { + targetObj["type"].toString(), + targetObj["name"].toString(), + targetObj["id"].toString(), + targetObj["defined_in"].toString(), + targetObj["filename"].toVariant().toStringList(), + targetObj["extra_files"].toVariant().toStringList(), + targetObj["subproject"].toString(), + extract_sources(targetObj["target_sources"].toArray()) + }; +} + +static TargetsList load_targets(const QJsonArray &arr) +{ + TargetsList targets; + std::transform(std::cbegin(arr), std::cend(arr), std::back_inserter(targets), extract_target); + return targets; +} + +static TargetsList targetsList(const QJsonDocument &js) +{ + if (auto obj = get(js.object(), "targets")) + return load_targets(*obj); + return {}; +} + +static TargetsList targetsList(const FilePath &buildDir) +{ + const FilePath path = buildDir / Constants::MESON_INFO_DIR / Constants::MESON_INTRO_TARGETS; + if (auto arr = load(path.toFSPathString())) + return load_targets(*arr); + return {}; +} + +static void appendFiles(const std::optional &arr, FilePaths &dest) +{ + if (!arr) + return; + std::transform(std::cbegin(*arr), std::cend(*arr), std::back_inserter(dest), [](const auto &file) { + return FilePath::fromString(file.toString()); + }); +} + +static FilePaths files(const FilePath &buildDir) +{ + FilePaths files; + FilePath path = buildDir / Constants::MESON_INFO_DIR / Constants::MESON_INTRO_BUILDSYSTEM_FILES; + auto arr = load(path.toFSPathString()); + appendFiles(arr, files); + return files; +} + +static FilePaths files(const QJsonDocument &js) +{ + FilePaths files; + auto arr = get(js.object(), "projectinfo", "buildsystem_files"); + appendFiles(arr, files); + const auto subprojects = get(js.object(), "projectinfo", "subprojects"); + for (const auto &subproject : *subprojects) { + auto arr = get(subproject.toObject(), "buildsystem_files"); appendFiles(arr, files); - const auto subprojects = get(js.object(), "projectinfo", "subprojects"); - for (const auto &subproject : *subprojects) { - auto arr = get(subproject.toObject(), "buildsystem_files"); - appendFiles(arr, files); - } - return files; } -}; + return files; +} Result parse(const FilePath &buildDir) { - return {TargetParser::targetList(buildDir), - BuildOptionsParser{buildDir}.takeBuildOptions(), - BuildSystemFilesParser::files(buildDir)}; + return {targetsList(buildDir), buildOptionsList(buildDir), files(buildDir)}; } Result parse(const QByteArray &data) { - auto json = QJsonDocument::fromJson(data); - return {TargetParser::targetList(json), - BuildOptionsParser{json}.takeBuildOptions(), - BuildSystemFilesParser::files(json)}; + const auto json = QJsonDocument::fromJson(data); + return {targetsList(json), buildOptionsList(json), files(json)}; } Result parse(QIODevice *introFile) { - if (introFile) { - if (!introFile->isOpen()) - introFile->open(QIODevice::ReadOnly | QIODevice::Text); - introFile->seek(0); - auto data = introFile->readAll(); - return parse(data); - } - return {}; + if (!introFile) + return {}; + if (!introFile->isOpen()) + introFile->open(QIODevice::ReadOnly | QIODevice::Text); + introFile->seek(0); + auto data = introFile->readAll(); + return parse(data); } } // namespace MesonProjectManager::Internal::MesonInfoParser