1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-05-09 06:42:18 +08:00

cmWindowsRegistry: Add helper for conversion between string and enum View

This commit is contained in:
Marc Chevrier 2022-04-15 17:47:22 +02:00
parent 769f25aa3c
commit 08941a9a40
3 changed files with 44 additions and 13 deletions

View File

@ -9,7 +9,6 @@
#include <map>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <cm/optional>
@ -469,14 +468,6 @@ bool QueryWindowsRegistry(Range args, cmExecutionStatus& status,
std::string const& variable)
{
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()) {
status.SetError("missing <key> 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);

View File

@ -3,6 +3,8 @@
#include "cmWindowsRegistry.h"
#include <unordered_map>
#if defined(_WIN32) && !defined(__CYGWIN__)
# include <algorithm>
# include <cstdint>
@ -335,6 +337,38 @@ cmWindowsRegistry::cmWindowsRegistry(cmMakefile& makefile)
#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
{
return this->LastError;

View File

@ -27,6 +27,11 @@ public:
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,
View view = View::Both,
cm::string_view separator = "\0"_s)