From 176ee159b74838e2f1dabdb8b6a5dadf759c055b Mon Sep 17 00:00:00 2001 From: justanotheranonymoususer Date: Fri, 3 Apr 2026 01:06:44 +0300 Subject: [PATCH 1/3] unbox_value_or: Replace param::hstring with hstring --- strings/base_reference_produce.h | 6 +++--- test/test/box_string.cpp | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/strings/base_reference_produce.h b/strings/base_reference_produce.h index 2820aff50..a2125845c 100644 --- a/strings/base_reference_produce.h +++ b/strings/base_reference_produce.h @@ -541,8 +541,8 @@ WINRT_EXPORT namespace winrt } } - template , int> = 0> - hstring unbox_value_or(Windows::Foundation::IInspectable const& value, param::hstring const& default_value) + template && std::is_constructible_v, int> = 0> + hstring unbox_value_or(Windows::Foundation::IInspectable const& value, D const& default_value) { if (value) { @@ -552,7 +552,7 @@ WINRT_EXPORT namespace winrt } } - return *(hstring*)(&default_value); + return hstring(default_value); } template , int> = 0> diff --git a/test/test/box_string.cpp b/test/test/box_string.cpp index efff92160..a859bd1b6 100644 --- a/test/test/box_string.cpp +++ b/test/test/box_string.cpp @@ -50,4 +50,33 @@ TEST_CASE("box_string") auto boxed = winrt::box_value(value); REQUIRE(winrt::unbox_value(boxed) == L""); } + + // unbox_value_or: default not used (successful unbox) + { + auto boxed = winrt::box_value(L"boxed"); + REQUIRE(winrt::unbox_value_or(boxed, L"fallback") == L"boxed"); + REQUIRE(winrt::unbox_value_or(boxed, winrt::hstring{ L"fallback" }) == L"boxed"); + REQUIRE(winrt::unbox_value_or(boxed, std::wstring{ L"fallback" }) == L"boxed"); + REQUIRE(winrt::unbox_value_or(boxed, std::wstring_view{ L"fallback" }) == L"boxed"); + } + + // unbox_value_or: default used (failed unbox) with each string type + { + // hstring + REQUIRE(winrt::unbox_value_or(nullptr, winrt::hstring{ L"hstring" }) == L"hstring"); + + // wchar_t const* (string literal) + REQUIRE(winrt::unbox_value_or(nullptr, L"literal") == L"literal"); + + // std::wstring + REQUIRE(winrt::unbox_value_or(nullptr, std::wstring{ L"wstring" }) == L"wstring"); + + // std::wstring_view (null-terminated) + REQUIRE(winrt::unbox_value_or(nullptr, std::wstring_view{ L"view" }) == L"view"); + + // std::wstring_view (not null-terminated) + std::wstring source = L"ABCDE"; + std::wstring_view fallback(source.data(), 3); // "ABC" + REQUIRE(winrt::unbox_value_or(nullptr, fallback) == L"ABC"); + } } From 20c17ebcfee0501f1a3f9ff2b1dd6a6b3035ea8f Mon Sep 17 00:00:00 2001 From: justanotheranonymoususer Date: Fri, 3 Apr 2026 01:07:27 +0300 Subject: [PATCH 2/3] CRLF->LF in box_string.cpp --- test/test/box_string.cpp | 164 +++++++++++++++++++-------------------- 1 file changed, 82 insertions(+), 82 deletions(-) diff --git a/test/test/box_string.cpp b/test/test/box_string.cpp index a859bd1b6..797d578d5 100644 --- a/test/test/box_string.cpp +++ b/test/test/box_string.cpp @@ -1,82 +1,82 @@ -#include "pch.h" - -TEST_CASE("box_string") -{ - // hstring - { - winrt::hstring value = L"hstring"; - auto boxed = winrt::box_value(value); - REQUIRE(winrt::unbox_value(boxed) == L"hstring"); - } - - // wchar_t const* (string literal) - { - auto boxed = winrt::box_value(L"literal"); - REQUIRE(winrt::unbox_value(boxed) == L"literal"); - } - - // std::wstring - { - std::wstring value = L"wstring"; - auto boxed = winrt::box_value(value); - REQUIRE(winrt::unbox_value(boxed) == L"wstring"); - } - - // std::wstring_view (null-terminated) - { - std::wstring_view value = L"view"; - auto boxed = winrt::box_value(value); - REQUIRE(winrt::unbox_value(boxed) == L"view"); - } - - // std::wstring_view (not null-terminated) - // Regression test for https://github.com/microsoft/cppwinrt/issues/1527 - { - std::wstring source = L"ABCDE"; - std::wstring_view value(source.data(), 3); // "ABC" - auto boxed = winrt::box_value(value); - REQUIRE(winrt::unbox_value(boxed) == L"ABC"); - } - - // Empty string - { - auto boxed = winrt::box_value(winrt::hstring{}); - REQUIRE(winrt::unbox_value(boxed) == L""); - } - - // Empty wstring_view - { - std::wstring_view value; - auto boxed = winrt::box_value(value); - REQUIRE(winrt::unbox_value(boxed) == L""); - } - - // unbox_value_or: default not used (successful unbox) - { - auto boxed = winrt::box_value(L"boxed"); - REQUIRE(winrt::unbox_value_or(boxed, L"fallback") == L"boxed"); - REQUIRE(winrt::unbox_value_or(boxed, winrt::hstring{ L"fallback" }) == L"boxed"); - REQUIRE(winrt::unbox_value_or(boxed, std::wstring{ L"fallback" }) == L"boxed"); - REQUIRE(winrt::unbox_value_or(boxed, std::wstring_view{ L"fallback" }) == L"boxed"); - } - - // unbox_value_or: default used (failed unbox) with each string type - { - // hstring - REQUIRE(winrt::unbox_value_or(nullptr, winrt::hstring{ L"hstring" }) == L"hstring"); - - // wchar_t const* (string literal) - REQUIRE(winrt::unbox_value_or(nullptr, L"literal") == L"literal"); - - // std::wstring - REQUIRE(winrt::unbox_value_or(nullptr, std::wstring{ L"wstring" }) == L"wstring"); - - // std::wstring_view (null-terminated) - REQUIRE(winrt::unbox_value_or(nullptr, std::wstring_view{ L"view" }) == L"view"); - - // std::wstring_view (not null-terminated) - std::wstring source = L"ABCDE"; - std::wstring_view fallback(source.data(), 3); // "ABC" - REQUIRE(winrt::unbox_value_or(nullptr, fallback) == L"ABC"); - } -} +#include "pch.h" + +TEST_CASE("box_string") +{ + // hstring + { + winrt::hstring value = L"hstring"; + auto boxed = winrt::box_value(value); + REQUIRE(winrt::unbox_value(boxed) == L"hstring"); + } + + // wchar_t const* (string literal) + { + auto boxed = winrt::box_value(L"literal"); + REQUIRE(winrt::unbox_value(boxed) == L"literal"); + } + + // std::wstring + { + std::wstring value = L"wstring"; + auto boxed = winrt::box_value(value); + REQUIRE(winrt::unbox_value(boxed) == L"wstring"); + } + + // std::wstring_view (null-terminated) + { + std::wstring_view value = L"view"; + auto boxed = winrt::box_value(value); + REQUIRE(winrt::unbox_value(boxed) == L"view"); + } + + // std::wstring_view (not null-terminated) + // Regression test for https://github.com/microsoft/cppwinrt/issues/1527 + { + std::wstring source = L"ABCDE"; + std::wstring_view value(source.data(), 3); // "ABC" + auto boxed = winrt::box_value(value); + REQUIRE(winrt::unbox_value(boxed) == L"ABC"); + } + + // Empty string + { + auto boxed = winrt::box_value(winrt::hstring{}); + REQUIRE(winrt::unbox_value(boxed) == L""); + } + + // Empty wstring_view + { + std::wstring_view value; + auto boxed = winrt::box_value(value); + REQUIRE(winrt::unbox_value(boxed) == L""); + } + + // unbox_value_or: default not used (successful unbox) + { + auto boxed = winrt::box_value(L"boxed"); + REQUIRE(winrt::unbox_value_or(boxed, L"fallback") == L"boxed"); + REQUIRE(winrt::unbox_value_or(boxed, winrt::hstring{ L"fallback" }) == L"boxed"); + REQUIRE(winrt::unbox_value_or(boxed, std::wstring{ L"fallback" }) == L"boxed"); + REQUIRE(winrt::unbox_value_or(boxed, std::wstring_view{ L"fallback" }) == L"boxed"); + } + + // unbox_value_or: default used (failed unbox) with each string type + { + // hstring + REQUIRE(winrt::unbox_value_or(nullptr, winrt::hstring{ L"hstring" }) == L"hstring"); + + // wchar_t const* (string literal) + REQUIRE(winrt::unbox_value_or(nullptr, L"literal") == L"literal"); + + // std::wstring + REQUIRE(winrt::unbox_value_or(nullptr, std::wstring{ L"wstring" }) == L"wstring"); + + // std::wstring_view (null-terminated) + REQUIRE(winrt::unbox_value_or(nullptr, std::wstring_view{ L"view" }) == L"view"); + + // std::wstring_view (not null-terminated) + std::wstring source = L"ABCDE"; + std::wstring_view fallback(source.data(), 3); // "ABC" + REQUIRE(winrt::unbox_value_or(nullptr, fallback) == L"ABC"); + } +} From 65d059bc1a14be980c218ffd8357464b9ebfdff7 Mon Sep 17 00:00:00 2001 From: justanotheranonymoususer Date: Fri, 3 Apr 2026 01:15:20 +0300 Subject: [PATCH 3/3] Perfect forwarding to optimize more cases --- strings/base_reference_produce.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/strings/base_reference_produce.h b/strings/base_reference_produce.h index a2125845c..a888aa8b4 100644 --- a/strings/base_reference_produce.h +++ b/strings/base_reference_produce.h @@ -541,8 +541,8 @@ WINRT_EXPORT namespace winrt } } - template && std::is_constructible_v, int> = 0> - hstring unbox_value_or(Windows::Foundation::IInspectable const& value, D const& default_value) + template && std::is_constructible_v, int> = 0> + hstring unbox_value_or(Windows::Foundation::IInspectable const& value, D&& default_value) { if (value) { @@ -552,7 +552,7 @@ WINRT_EXPORT namespace winrt } } - return hstring(default_value); + return hstring(std::forward(default_value)); } template , int> = 0>