Skip to content

Commit

Permalink
Add to_hstring overload for IStringable (#1271)
Browse files Browse the repository at this point in the history
  • Loading branch information
JaiganeshKumaran authored Feb 20, 2023
1 parent 4e674c7 commit abcdc75
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions cppwinrt/code_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -3253,6 +3253,7 @@ struct WINRT_IMPL_EMPTY_BASES produce_dispatch_to_overridable<T, D, %>
w.write(strings::base_reference_produce);
w.write(strings::base_deferral);
w.write(strings::base_coroutine_foundation);
w.write(strings::base_stringable_to_hstring);
w.write(strings::base_stringable_format);
w.write(strings::base_stringable_streams);
}
Expand Down
1 change: 1 addition & 0 deletions cppwinrt/cppwinrt.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<ClInclude Include="..\strings\base_string.h" />
<ClInclude Include="..\strings\base_stringable_format.h" />
<ClInclude Include="..\strings\base_stringable_streams.h" />
<ClInclude Include="..\strings\base_stringable_to_hstring.h" />
<ClInclude Include="..\strings\base_string_input.h" />
<ClInclude Include="..\strings\base_string_operators.h" />
<ClInclude Include="..\strings\base_stringable_format_1.h" />
Expand Down
5 changes: 4 additions & 1 deletion cppwinrt/cppwinrt.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
<ClInclude Include="..\strings\base_stringable_format.h">
<Filter>strings</Filter>
</ClInclude>
<ClInclude Include="..\strings\base_stringable_streams.h">
<ClInclude Include="..\strings\base_stringable_streams.h">
<Filter>strings</Filter>
</ClInclude>
<ClInclude Include="..\strings\base_xaml_component_connector.h">
Expand All @@ -184,6 +184,9 @@
<ClInclude Include="..\strings\base_xaml_component_connector_winui.h">
<Filter>strings</Filter>
</ClInclude>
<ClInclude Include="..\strings\base_stringable_to_hstring.h">
<Filter>strings</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="$(OutDir)version.rc" />
Expand Down
8 changes: 8 additions & 0 deletions strings/base_stringable_to_hstring.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

WINRT_EXPORT namespace winrt
{
inline hstring to_hstring(Windows::Foundation::IStringable const& stringable)
{
return stringable.ToString();
}
}
12 changes: 12 additions & 0 deletions test/old_tests/UnitTests/to_hstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

using namespace winrt;

struct stringable : winrt::implements<stringable, winrt::Windows::Foundation::IStringable>
{
winrt::hstring ToString()
{
return L"a stringable object";
}
};

namespace
{
void test_cases()
Expand Down Expand Up @@ -82,6 +90,10 @@ namespace
hstring const c = to_hstring(b);
REQUIRE(a == c);
}
{
auto const obj = make<stringable>();
REQUIRE(to_hstring(obj) == obj.ToString());
}
}
}

Expand Down

0 comments on commit abcdc75

Please sign in to comment.