-
Notifications
You must be signed in to change notification settings - Fork 4
Add std::format / std::formatter for win32 HKEY #157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <algorithm> | ||
| #include <array> | ||
| #include <chrono> | ||
| #include <format> | ||
| #include <string_view> | ||
|
|
||
| #include <Windows.h> | ||
|
|
||
| using namespace std::string_view_literals; | ||
|
|
||
| template <typename CharT> | ||
| struct std::formatter<HKEY, CharT> | ||
| { | ||
| template <typename ParseContext> | ||
| constexpr ParseContext::iterator | ||
| parse(ParseContext& ctx) | ||
| { | ||
| auto it = ctx.begin(); | ||
| auto const end = ctx.end(); | ||
|
|
||
| if (it != end && *it != '}') | ||
| throw std::format_error("Invalid format string"); | ||
|
|
||
| return it; | ||
| } | ||
|
|
||
| template <typename FormatContext> | ||
| FormatContext::iterator | ||
| format(HKEY hkey, FormatContext& ctx) const | ||
| { | ||
| auto out = ctx.out(); | ||
|
|
||
| // | ||
| // Macros are to be hated but they are to be tolerated here. | ||
| // | ||
| // There are a number of "named" HKEY values. | ||
| // | ||
|
|
||
| #pragma push_macro("X") | ||
|
|
||
| #undef X | ||
| #define X(p) \ | ||
| case reinterpret_cast<uint64_t>(p): \ | ||
| { \ | ||
| constexpr auto lit = #p##sv; \ | ||
| return std::ranges::copy(lit.begin(), lit.end(), out).out; \ | ||
| } | ||
|
|
||
| switch (reinterpret_cast<uint64_t>(hkey)) | ||
| { | ||
| X(HKEY_CURRENT_USER) | ||
| X(HKEY_CLASSES_ROOT) | ||
| X(HKEY_LOCAL_MACHINE) | ||
| X(HKEY_USERS) | ||
| X(HKEY_PERFORMANCE_DATA) | ||
| X(HKEY_PERFORMANCE_TEXT) | ||
| X(HKEY_PERFORMANCE_NLSTEXT) | ||
| X(HKEY_CURRENT_CONFIG) | ||
| X(HKEY_DYN_DATA) | ||
| X(HKEY_CURRENT_USER_LOCAL_SETTINGS) | ||
| } | ||
|
|
||
| #pragma pop_macro("X") | ||
|
|
||
| if constexpr (std::is_same_v<CharT, char>) | ||
| { | ||
| return std::format_to(out, "{{hkey 0x{:x}}}", reinterpret_cast<uintptr_t>(hkey)); | ||
| } | ||
| else if constexpr (std::is_same_v<CharT, wchar_t>) | ||
| { | ||
| return std::format_to(out, L"{{hkey 0x{:x}}}", reinterpret_cast<uintptr_t>(hkey)); | ||
| } | ||
| else | ||
| { | ||
| throw std::runtime_error("Bad CharT"); | ||
| } | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -9,11 +9,12 @@ add_executable(${TEST_EXE_NAME} | |||||
| test_FILE_ACTION.cpp | ||||||
| test_FILE_NOTIFY_EXTENDED_INFORMATION.cpp | ||||||
| test_FILE_NOTIFY_INFORMATION.cpp | ||||||
| test_HKEY.cpp | ||||||
| test_HRESULT.cpp | ||||||
| test_NTSTATUS.cpp | ||||||
| test_OVERLAPPED.cpp | ||||||
| test_Win32ErrorCode.cpp | ||||||
| ) | ||||||
| ) | ||||||
|
||||||
| ) | |
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <format> | ||
| #include <string> | ||
| #include <string_view> | ||
|
|
||
| #include <m/formatters/HKEY.h> | ||
|
|
||
| #include <Windows.h> | ||
|
|
||
| using namespace std::string_literals; | ||
| using namespace std::string_view_literals; | ||
|
|
||
| TEST(HKEY, format_HKEY_CURRENT_USER) | ||
| { | ||
| auto s = std::format(L"{}", HKEY_CURRENT_USER); | ||
| EXPECT_EQ(s, L"HKEY_CURRENT_USER"s); | ||
| } | ||
|
|
||
| TEST(HKEY, format_HKEY_LOCAL_MACHINE) | ||
| { | ||
| auto s = std::format(L"{}", HKEY_LOCAL_MACHINE); | ||
| EXPECT_EQ(s, L"HKEY_LOCAL_MACHINE"s); | ||
| } | ||
|
|
||
| TEST(HKEY, format_default_hkey) | ||
| { | ||
| auto s = std::format(L"{}", HKEY{}); | ||
| EXPECT_EQ(s, L"{hkey 0x0}"s); | ||
| } | ||
|
|
||
|
Comment on lines
+17
to
+34
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused includes detected. The headers
<array>and<chrono>are included but not used in this file. These should be removed to reduce compilation dependencies and improve build times.