Skip to content

Commit

Permalink
C++ Streams support for hstring and IStringable (#1221)
Browse files Browse the repository at this point in the history
  • Loading branch information
JaiganeshKumaran authored Nov 1, 2022
1 parent d6ef811 commit 6ce4fa9
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions cppwinrt/code_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -3256,6 +3256,7 @@ struct WINRT_IMPL_EMPTY_BASES produce_dispatch_to_overridable<T, D, %>
w.write(strings::base_deferral);
w.write(strings::base_coroutine_foundation);
w.write(strings::base_stringable_format);
w.write(strings::base_stringable_streams);
}
else if (namespace_name == "Windows.Foundation.Collections")
{
Expand Down
1 change: 1 addition & 0 deletions cppwinrt/cppwinrt.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<ClInclude Include="..\strings\base_std_hash.h" />
<ClInclude Include="..\strings\base_string.h" />
<ClInclude Include="..\strings\base_stringable_format.h" />
<ClInclude Include="..\strings\base_stringable_streams.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
3 changes: 3 additions & 0 deletions cppwinrt/cppwinrt.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@
</ClInclude>
<ClInclude Include="..\strings\base_stringable_format.h">
<Filter>strings</Filter>
</ClInclude>
<ClInclude Include="..\strings\base_stringable_streams.h">
<Filter>strings</Filter>
</ClInclude>
<ClInclude Include="..\strings\base_xaml_component_connector.h">
<Filter>strings</Filter>
Expand Down
4 changes: 4 additions & 0 deletions strings/base_includes.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
#include <directxmath.h>
#endif

#ifndef WINRT_LEAN_AND_MEAN
#include <ostream>
#endif

#ifdef __cpp_lib_format
#include <format>
#endif
Expand Down
8 changes: 8 additions & 0 deletions strings/base_string_operators.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,12 @@ WINRT_EXPORT namespace winrt
{
return impl::concat_hstring(left, right);
}

#ifndef WINRT_LEAN_AND_MEAN
inline std::wostream& operator<<(std::wostream& stream, hstring const& string)
{
stream << static_cast<std::wstring_view>(string);
return stream;
}
#endif
}
8 changes: 8 additions & 0 deletions strings/base_stringable_streams.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

#ifndef WINRT_LEAN_AND_MEAN
inline std::wostream& operator<<(std::wostream& stream, winrt::Windows::Foundation::IStringable const& stringable)
{
stream << stringable.ToString();
return stream;
}
#endif
1 change: 1 addition & 0 deletions test/old_tests/UnitTests/Tests.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
<ClCompile Include="Reference.cpp" />
<ClCompile Include="Release.cpp" />
<ClCompile Include="smart_pointers.cpp" />
<ClCompile Include="streams.cpp" />
<ClCompile Include="struct.cpp" />
<ClCompile Include="StructCodeGen.cpp" />
<ClCompile Include="Structures.cpp" />
Expand Down
1 change: 1 addition & 0 deletions test/old_tests/UnitTests/Tests.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
<ClCompile Include="conditional_implements_pure.cpp" />
<ClCompile Include="capture.cpp" />
<ClCompile Include="com_ref.cpp" />
<ClCompile Include="streams.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="catch.hpp" />
Expand Down
36 changes: 36 additions & 0 deletions test/old_tests/UnitTests/streams.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "pch.h"
#include "catch.hpp"
#include <sstream>

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

TEST_CASE("streams")
{
{
std::wstringstream ss;
winrt::hstring str = L"Hello World";
ss << str;
REQUIRE(ss.str() == str);
}

{
// Support embedded nulls.
std::wstringstream ss;
winrt::hstring str = L"Hello\0World";
ss << str;
REQUIRE(ss.str() == str);
}

{
std::wstringstream ss;
winrt::Windows::Foundation::IStringable obj = winrt::make<stringable>();
ss << obj;
REQUIRE(ss.str() == obj.ToString());
}
}

0 comments on commit 6ce4fa9

Please sign in to comment.