Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions strings/base_reference_produce.h
Original file line number Diff line number Diff line change
Expand Up @@ -509,12 +509,13 @@ namespace winrt::impl

WINRT_EXPORT namespace winrt
{
inline Windows::Foundation::IInspectable box_value(param::hstring const& value)
template <typename T, std::enable_if_t<std::is_constructible_v<hstring, T>, int> = 0>
Windows::Foundation::IInspectable box_value(T&& value)
{
return Windows::Foundation::IReference<hstring>(*(hstring*)(&value));
return Windows::Foundation::IReference<hstring>(hstring(std::forward<T>(value)));
}

template <typename T, std::enable_if_t<!std::is_convertible_v<T, param::hstring>, int> = 0>
template <typename T, std::enable_if_t<!std::is_constructible_v<hstring, T>, int> = 0>
Windows::Foundation::IInspectable box_value(T const& value)
{
if constexpr (std::is_base_of_v<Windows::Foundation::IInspectable, T>)
Expand Down
53 changes: 53 additions & 0 deletions test/test/box_string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "pch.h"

TEST_CASE("box_string")
{
// hstring
{
winrt::hstring value = L"hstring";
auto boxed = winrt::box_value(value);
REQUIRE(winrt::unbox_value<winrt::hstring>(boxed) == L"hstring");
}

// wchar_t const* (string literal)
{
auto boxed = winrt::box_value(L"literal");
REQUIRE(winrt::unbox_value<winrt::hstring>(boxed) == L"literal");
}

// std::wstring
{
std::wstring value = L"wstring";
auto boxed = winrt::box_value(value);
REQUIRE(winrt::unbox_value<winrt::hstring>(boxed) == L"wstring");
}

// std::wstring_view (null-terminated)
{
std::wstring_view value = L"view";
auto boxed = winrt::box_value(value);
REQUIRE(winrt::unbox_value<winrt::hstring>(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<winrt::hstring>(boxed) == L"ABC");
}

// Empty string
{
auto boxed = winrt::box_value(winrt::hstring{});
REQUIRE(winrt::unbox_value<winrt::hstring>(boxed) == L"");
}

// Empty wstring_view
{
std::wstring_view value;
auto boxed = winrt::box_value(value);
REQUIRE(winrt::unbox_value<winrt::hstring>(boxed) == L"");
}
}
1 change: 1 addition & 0 deletions test/test/test.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@
<ClCompile Include="box_array.cpp" />
<ClCompile Include="box_delegate.cpp" />
<ClCompile Include="box_guid.cpp" />
<ClCompile Include="box_string.cpp" />
<ClCompile Include="capture.cpp" />
<ClCompile Include="coro_foundation.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">NotUsing</PrecompiledHeader>
Expand Down