Browse Source

cmWindowsRegistry: Add helper for conversion between string and enum View

Marc Chevrier 3 years ago
parent
commit
08941a9a40

+ 5 - 13
Source/cmCMakeHostSystemInformationCommand.cxx

@@ -9,7 +9,6 @@
 #include <map>
 #include <map>
 #include <string>
 #include <string>
 #include <type_traits>
 #include <type_traits>
-#include <unordered_map>
 #include <utility>
 #include <utility>
 
 
 #include <cm/optional>
 #include <cm/optional>
@@ -469,14 +468,6 @@ bool QueryWindowsRegistry(Range args, cmExecutionStatus& status,
                           std::string const& variable)
                           std::string const& variable)
 {
 {
   using View = cmWindowsRegistry::View;
   using View = cmWindowsRegistry::View;
-  static std::unordered_map<cm::string_view, cmWindowsRegistry::View>
-    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()) {
   if (args.empty()) {
     status.SetError("missing <key> specification.");
     status.SetError("missing <key> specification.");
     return false;
     return false;
@@ -522,8 +513,8 @@ bool QueryWindowsRegistry(Range args, cmExecutionStatus& status,
                     "\"VALUE_NAMES\" or \"SUBKEYS\".");
                     "\"VALUE_NAMES\" or \"SUBKEYS\".");
     return false;
     return false;
   }
   }
-  if (!arguments.View.empty() &&
-      ViewDefinitions.find(arguments.View) == ViewDefinitions.end()) {
+
+  if (!arguments.View.empty() && !cmWindowsRegistry::ToView(arguments.View)) {
     status.SetError(
     status.SetError(
       cmStrCat("given invalid value for \"VIEW\": ", arguments.View, '.'));
       cmStrCat("given invalid value for \"VIEW\": ", arguments.View, '.'));
     return false;
     return false;
@@ -533,8 +524,9 @@ bool QueryWindowsRegistry(Range args, cmExecutionStatus& status,
 
 
   makefile.AddDefinition(variable, ""_s);
   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);
   cmWindowsRegistry registry(makefile);
   if (arguments.ValueNames) {
   if (arguments.ValueNames) {
     auto result = registry.GetValueNames(key, view);
     auto result = registry.GetValueNames(key, view);

+ 34 - 0
Source/cmWindowsRegistry.cxx

@@ -3,6 +3,8 @@
 
 
 #include "cmWindowsRegistry.h"
 #include "cmWindowsRegistry.h"
 
 
+#include <unordered_map>
+
 #if defined(_WIN32) && !defined(__CYGWIN__)
 #if defined(_WIN32) && !defined(__CYGWIN__)
 #  include <algorithm>
 #  include <algorithm>
 #  include <cstdint>
 #  include <cstdint>
@@ -335,6 +337,38 @@ cmWindowsRegistry::cmWindowsRegistry(cmMakefile& makefile)
 #endif
 #endif
 }
 }
 
 
+cm::optional<cmWindowsRegistry::View> cmWindowsRegistry::ToView(
+  cm::string_view name)
+{
+  static std::unordered_map<cm::string_view, cmWindowsRegistry::View>
+    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<cmWindowsRegistry::View, cm::string_view>
+    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
 cm::string_view cmWindowsRegistry::GetLastError() const
 {
 {
   return this->LastError;
   return this->LastError;

+ 5 - 0
Source/cmWindowsRegistry.h

@@ -27,6 +27,11 @@ public:
     Reg64
     Reg64
   };
   };
 
 
+  // Helper routine to convert string to enum value
+  static cm::optional<View> ToView(cm::string_view name);
+  // Helper routine to convert enum to string
+  static cm::string_view FromView(View view);
+
   cm::optional<std::string> ReadValue(cm::string_view key,
   cm::optional<std::string> ReadValue(cm::string_view key,
                                       View view = View::Both,
                                       View view = View::Both,
                                       cm::string_view separator = "\0"_s)
                                       cm::string_view separator = "\0"_s)