Skip to content

Commit 3893ce8

Browse files
box_value constructor: Replace param::hstring with hstring (#1530)
* box_value constructor: Replace param::hstring with hstring * Update base_reference_produce.h * Fix and add tests * Fix github.dev bug
1 parent 16a1c29 commit 3893ce8

3 files changed

Lines changed: 58 additions & 3 deletions

File tree

strings/base_reference_produce.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,12 +509,13 @@ namespace winrt::impl
509509

510510
WINRT_EXPORT namespace winrt
511511
{
512-
inline Windows::Foundation::IInspectable box_value(param::hstring const& value)
512+
template <typename T, std::enable_if_t<std::is_constructible_v<hstring, T>, int> = 0>
513+
Windows::Foundation::IInspectable box_value(T&& value)
513514
{
514-
return Windows::Foundation::IReference<hstring>(*(hstring*)(&value));
515+
return Windows::Foundation::IReference<hstring>(hstring(std::forward<T>(value)));
515516
}
516517

517-
template <typename T, std::enable_if_t<!std::is_convertible_v<T, param::hstring>, int> = 0>
518+
template <typename T, std::enable_if_t<!std::is_constructible_v<hstring, T>, int> = 0>
518519
Windows::Foundation::IInspectable box_value(T const& value)
519520
{
520521
if constexpr (std::is_base_of_v<Windows::Foundation::IInspectable, T>)

test/test/box_string.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include "pch.h"
2+
3+
TEST_CASE("box_string")
4+
{
5+
// hstring
6+
{
7+
winrt::hstring value = L"hstring";
8+
auto boxed = winrt::box_value(value);
9+
REQUIRE(winrt::unbox_value<winrt::hstring>(boxed) == L"hstring");
10+
}
11+
12+
// wchar_t const* (string literal)
13+
{
14+
auto boxed = winrt::box_value(L"literal");
15+
REQUIRE(winrt::unbox_value<winrt::hstring>(boxed) == L"literal");
16+
}
17+
18+
// std::wstring
19+
{
20+
std::wstring value = L"wstring";
21+
auto boxed = winrt::box_value(value);
22+
REQUIRE(winrt::unbox_value<winrt::hstring>(boxed) == L"wstring");
23+
}
24+
25+
// std::wstring_view (null-terminated)
26+
{
27+
std::wstring_view value = L"view";
28+
auto boxed = winrt::box_value(value);
29+
REQUIRE(winrt::unbox_value<winrt::hstring>(boxed) == L"view");
30+
}
31+
32+
// std::wstring_view (not null-terminated)
33+
// Regression test for https://github.com/microsoft/cppwinrt/issues/1527
34+
{
35+
std::wstring source = L"ABCDE";
36+
std::wstring_view value(source.data(), 3); // "ABC"
37+
auto boxed = winrt::box_value(value);
38+
REQUIRE(winrt::unbox_value<winrt::hstring>(boxed) == L"ABC");
39+
}
40+
41+
// Empty string
42+
{
43+
auto boxed = winrt::box_value(winrt::hstring{});
44+
REQUIRE(winrt::unbox_value<winrt::hstring>(boxed) == L"");
45+
}
46+
47+
// Empty wstring_view
48+
{
49+
std::wstring_view value;
50+
auto boxed = winrt::box_value(value);
51+
REQUIRE(winrt::unbox_value<winrt::hstring>(boxed) == L"");
52+
}
53+
}

test/test/test.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@
233233
<ClCompile Include="box_array.cpp" />
234234
<ClCompile Include="box_delegate.cpp" />
235235
<ClCompile Include="box_guid.cpp" />
236+
<ClCompile Include="box_string.cpp" />
236237
<ClCompile Include="capture.cpp" />
237238
<ClCompile Include="coro_foundation.cpp">
238239
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">NotUsing</PrecompiledHeader>

0 commit comments

Comments
 (0)