From 08941a9a40c7786aa2ee8ff8e7c684eaf016513e Mon Sep 17 00:00:00 2001 From: Marc Chevrier Date: Fri, 15 Apr 2022 17:47:22 +0200 Subject: cmWindowsRegistry: Add helper for conversion between string and enum View --- Source/cmCMakeHostSystemInformationCommand.cxx | 18 ++++---------- Source/cmWindowsRegistry.cxx | 34 ++++++++++++++++++++++++++ Source/cmWindowsRegistry.h | 5 ++++ 3 files changed, 44 insertions(+), 13 deletions(-) diff --git a/Source/cmCMakeHostSystemInformationCommand.cxx b/Source/cmCMakeHostSystemInformationCommand.cxx index 0c41c68..2a019b1 100644 --- a/Source/cmCMakeHostSystemInformationCommand.cxx +++ b/Source/cmCMakeHostSystemInformationCommand.cxx @@ -9,7 +9,6 @@ #include #include #include -#include #include #include @@ -469,14 +468,6 @@ bool QueryWindowsRegistry(Range args, cmExecutionStatus& status, std::string const& variable) { using View = cmWindowsRegistry::View; - static std::unordered_map - ViewDefinitions{ - { "BOTH"_s, View::Both }, { "HOST"_s, View::Host }, - { "TARGET"_s, View::Target }, { "32"_s, View::Reg32 }, - { "64"_s, View::Reg64 }, { "32_64"_s, View::Reg32_64 }, - { "64_32"_s, View::Reg64_32 } - }; - if (args.empty()) { status.SetError("missing specification."); return false; @@ -522,8 +513,8 @@ bool QueryWindowsRegistry(Range args, cmExecutionStatus& status, "\"VALUE_NAMES\" or \"SUBKEYS\"."); return false; } - if (!arguments.View.empty() && - ViewDefinitions.find(arguments.View) == ViewDefinitions.end()) { + + if (!arguments.View.empty() && !cmWindowsRegistry::ToView(arguments.View)) { status.SetError( cmStrCat("given invalid value for \"VIEW\": ", arguments.View, '.')); return false; @@ -533,8 +524,9 @@ bool QueryWindowsRegistry(Range args, cmExecutionStatus& status, makefile.AddDefinition(variable, ""_s); - auto view = - arguments.View.empty() ? View::Both : ViewDefinitions[arguments.View]; + auto view = arguments.View.empty() + ? View::Both + : *cmWindowsRegistry::ToView(arguments.View); cmWindowsRegistry registry(makefile); if (arguments.ValueNames) { auto result = registry.GetValueNames(key, view); diff --git a/Source/cmWindowsRegistry.cxx b/Source/cmWindowsRegistry.cxx index b452ec6..9048334 100644 --- a/Source/cmWindowsRegistry.cxx +++ b/Source/cmWindowsRegistry.cxx @@ -3,6 +3,8 @@ #include "cmWindowsRegistry.h" +#include + #if defined(_WIN32) && !defined(__CYGWIN__) # include # include @@ -335,6 +337,38 @@ cmWindowsRegistry::cmWindowsRegistry(cmMakefile& makefile) #endif } +cm::optional cmWindowsRegistry::ToView( + cm::string_view name) +{ + static std::unordered_map + ViewDefinitions{ + { "BOTH"_s, View::Both }, { "HOST"_s, View::Host }, + { "TARGET"_s, View::Target }, { "32"_s, View::Reg32 }, + { "64"_s, View::Reg64 }, { "32_64"_s, View::Reg32_64 }, + { "64_32"_s, View::Reg64_32 } + }; + + auto it = ViewDefinitions.find(name); + + return it == ViewDefinitions.end() ? cm::nullopt + : cm::optional{ it->second }; +} + +cm::string_view cmWindowsRegistry::FromView(View view) +{ + static std::unordered_map + ViewDefinitions{ + { View::Both, "BOTH"_s }, { View::Host, "HOST"_s }, + { View::Target, "TARGET"_s }, { View::Reg32, "32"_s }, + { View::Reg64, "64"_s }, { View::Reg32_64, "32_64"_s }, + { View::Reg64_32, "64_32"_s } + }; + + auto it = ViewDefinitions.find(view); + + return it == ViewDefinitions.end() ? ""_s : it->second; +} + cm::string_view cmWindowsRegistry::GetLastError() const { return this->LastError; diff --git a/Source/cmWindowsRegistry.h b/Source/cmWindowsRegistry.h index 6f10b3a..e04ce87 100644 --- a/Source/cmWindowsRegistry.h +++ b/Source/cmWindowsRegistry.h @@ -27,6 +27,11 @@ public: Reg64 }; + // Helper routine to convert string to enum value + static cm::optional ToView(cm::string_view name); + // Helper routine to convert enum to string + static cm::string_view FromView(View view); + cm::optional ReadValue(cm::string_view key, View view = View::Both, cm::string_view separator = "\0"_s) -- cgit v0.12