Skip to content

Commit 6ce4fa9

Browse files
authored
C++ Streams support for hstring and IStringable (#1221)
1 parent d6ef811 commit 6ce4fa9

File tree

9 files changed

+63
-0
lines changed

9 files changed

+63
-0
lines changed

cppwinrt/code_writers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3256,6 +3256,7 @@ struct WINRT_IMPL_EMPTY_BASES produce_dispatch_to_overridable<T, D, %>
32563256
w.write(strings::base_deferral);
32573257
w.write(strings::base_coroutine_foundation);
32583258
w.write(strings::base_stringable_format);
3259+
w.write(strings::base_stringable_streams);
32593260
}
32603261
else if (namespace_name == "Windows.Foundation.Collections")
32613262
{

cppwinrt/cppwinrt.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
<ClInclude Include="..\strings\base_std_hash.h" />
8181
<ClInclude Include="..\strings\base_string.h" />
8282
<ClInclude Include="..\strings\base_stringable_format.h" />
83+
<ClInclude Include="..\strings\base_stringable_streams.h" />
8384
<ClInclude Include="..\strings\base_string_input.h" />
8485
<ClInclude Include="..\strings\base_string_operators.h" />
8586
<ClInclude Include="..\strings\base_stringable_format_1.h" />

cppwinrt/cppwinrt.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@
168168
</ClInclude>
169169
<ClInclude Include="..\strings\base_stringable_format.h">
170170
<Filter>strings</Filter>
171+
</ClInclude>
172+
<ClInclude Include="..\strings\base_stringable_streams.h">
173+
<Filter>strings</Filter>
171174
</ClInclude>
172175
<ClInclude Include="..\strings\base_xaml_component_connector.h">
173176
<Filter>strings</Filter>

strings/base_includes.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
#include <directxmath.h>
2626
#endif
2727

28+
#ifndef WINRT_LEAN_AND_MEAN
29+
#include <ostream>
30+
#endif
31+
2832
#ifdef __cpp_lib_format
2933
#include <format>
3034
#endif

strings/base_string_operators.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,12 @@ WINRT_EXPORT namespace winrt
160160
{
161161
return impl::concat_hstring(left, right);
162162
}
163+
164+
#ifndef WINRT_LEAN_AND_MEAN
165+
inline std::wostream& operator<<(std::wostream& stream, hstring const& string)
166+
{
167+
stream << static_cast<std::wstring_view>(string);
168+
return stream;
169+
}
170+
#endif
163171
}

strings/base_stringable_streams.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
#ifndef WINRT_LEAN_AND_MEAN
3+
inline std::wostream& operator<<(std::wostream& stream, winrt::Windows::Foundation::IStringable const& stringable)
4+
{
5+
stream << stringable.ToString();
6+
return stream;
7+
}
8+
#endif

test/old_tests/UnitTests/Tests.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
<ClCompile Include="Reference.cpp" />
119119
<ClCompile Include="Release.cpp" />
120120
<ClCompile Include="smart_pointers.cpp" />
121+
<ClCompile Include="streams.cpp" />
121122
<ClCompile Include="struct.cpp" />
122123
<ClCompile Include="StructCodeGen.cpp" />
123124
<ClCompile Include="Structures.cpp" />

test/old_tests/UnitTests/Tests.vcxproj.filters

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
<ClCompile Include="conditional_implements_pure.cpp" />
8989
<ClCompile Include="capture.cpp" />
9090
<ClCompile Include="com_ref.cpp" />
91+
<ClCompile Include="streams.cpp" />
9192
</ItemGroup>
9293
<ItemGroup>
9394
<ClInclude Include="catch.hpp" />
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "pch.h"
2+
#include "catch.hpp"
3+
#include <sstream>
4+
5+
struct stringable : winrt::implements<stringable, winrt::Windows::Foundation::IStringable>
6+
{
7+
winrt::hstring ToString()
8+
{
9+
return L"a stringable object";
10+
}
11+
};
12+
13+
TEST_CASE("streams")
14+
{
15+
{
16+
std::wstringstream ss;
17+
winrt::hstring str = L"Hello World";
18+
ss << str;
19+
REQUIRE(ss.str() == str);
20+
}
21+
22+
{
23+
// Support embedded nulls.
24+
std::wstringstream ss;
25+
winrt::hstring str = L"Hello\0World";
26+
ss << str;
27+
REQUIRE(ss.str() == str);
28+
}
29+
30+
{
31+
std::wstringstream ss;
32+
winrt::Windows::Foundation::IStringable obj = winrt::make<stringable>();
33+
ss << obj;
34+
REQUIRE(ss.str() == obj.ToString());
35+
}
36+
}

0 commit comments

Comments
 (0)