Skip to content

Commit b401564

Browse files
authored
Add type support for Windows.Foundation.Numerics.Rational (#1106)
1 parent 3b78eb2 commit b401564

File tree

5 files changed

+45
-11
lines changed

5 files changed

+45
-11
lines changed

cppwinrt/type_writers.h

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,18 @@ namespace cppwinrt
9292
return result;
9393
}
9494

95+
static bool transform_special_numeric_type(std::string_view& name)
96+
{
97+
if (name == "Matrix3x2") { name = "float3x2"; return true; }
98+
else if (name == "Matrix4x4") { name = "float4x4"; return true; }
99+
else if (name == "Plane") { name = "plane"; return true; }
100+
else if (name == "Quaternion") { name = "quaternion"; return true; }
101+
else if (name == "Vector2") { name = "float2"; return true; }
102+
else if (name == "Vector3") { name = "float3"; return true; }
103+
else if (name == "Vector4") { name = "float4"; return true; }
104+
return false;
105+
}
106+
95107
struct writer : writer_base<writer>
96108
{
97109
using writer_base<writer>::write;
@@ -277,8 +289,6 @@ namespace cppwinrt
277289
return;
278290
}
279291

280-
// TODO: get rid of all these renames once parity with cppwinrt.exe has been reached...
281-
282292
if (name == "EventRegistrationToken" && ns == "Windows.Foundation")
283293
{
284294
write("winrt::event_token");
@@ -291,16 +301,8 @@ namespace cppwinrt
291301
{
292302
auto category = get_category(type);
293303

294-
if (ns == "Windows.Foundation.Numerics")
304+
if (ns == "Windows.Foundation.Numerics" && transform_special_numeric_type(name))
295305
{
296-
if (name == "Matrix3x2") { name = "float3x2"; }
297-
else if (name == "Matrix4x4") { name = "float4x4"; }
298-
else if (name == "Plane") { name = "plane"; }
299-
else if (name == "Quaternion") { name = "quaternion"; }
300-
else if (name == "Vector2") { name = "float2"; }
301-
else if (name == "Vector3") { name = "float3"; }
302-
else if (name == "Vector4") { name = "float4"; }
303-
304306
write("winrt::@::%", ns, name);
305307
}
306308
else if (category == category::struct_type)

test/test/rational.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "pch.h"
2+
#include "winrt/test_component.h"
3+
#include "winrt/Windows.Foundation.Numerics.h"
4+
5+
using namespace winrt;
6+
using namespace test_component;
7+
8+
TEST_CASE("rational")
9+
{
10+
Simple simple;
11+
Windows::Foundation::Numerics::Rational rational = simple.ReturnRational();
12+
REQUIRE(rational.Numerator == 123);
13+
REQUIRE(rational.Denominator == 456);
14+
15+
Windows::Foundation::Numerics::float2 vector2 = simple.ReturnVector2();
16+
REQUIRE(vector2.x == 123.0);
17+
REQUIRE(vector2.y == 456.0);
18+
}

test/test/test.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@
428428
<ClCompile Include="pch.cpp">
429429
<PrecompiledHeader>Create</PrecompiledHeader>
430430
</ClCompile>
431+
<ClCompile Include="rational.cpp" />
431432
<ClCompile Include="return_params.cpp" />
432433
<ClCompile Include="return_params_abi.cpp" />
433434
<ClCompile Include="single_threaded_observable_vector.cpp" />

test/test_component/Simple.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ namespace winrt::test_component::implementation
1616
// All we care about static events (for now) is that they build.
1717
static event_token StaticEvent(Windows::Foundation::EventHandler<IInspectable> const&) { return {}; }
1818
static void StaticEvent(event_token) { }
19+
20+
Windows::Foundation::Numerics::float2 ReturnVector2()
21+
{
22+
return { 123.0, 456.0 };
23+
}
24+
25+
Windows::Foundation::Numerics::Rational ReturnRational()
26+
{
27+
return { 123, 456 };
28+
}
1929
};
2030
}
2131
namespace winrt::test_component::factory_implementation

test/test_component/test_component.idl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ namespace test_component
5656
Windows.Foundation.IAsyncAction Action(Windows.Foundation.DateTime value);
5757
Object Object(Windows.Foundation.DateTime value);
5858
static event Windows.Foundation.EventHandler<Object> StaticEvent;
59+
60+
Windows.Foundation.Numerics.Vector2 ReturnVector2();
61+
Windows.Foundation.Numerics.Rational ReturnRational();
5962
}
6063

6164
runtimeclass DeferrableEventArgs

0 commit comments

Comments
 (0)