Skip to content

Commit

Permalink
Numerics type mappings (#145)
Browse files Browse the repository at this point in the history
* Numerics type mappings

* cruft

* register xaml type mappings
  • Loading branch information
Scottj1s authored Apr 16, 2020
1 parent 492abf5 commit 83ad781
Show file tree
Hide file tree
Showing 9 changed files with 302 additions and 19 deletions.
57 changes: 56 additions & 1 deletion TestComponentCSharp/Class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,62 @@ namespace winrt::TestComponentCSharp::implementation
{
_matrix3D = value;
}

Windows::Foundation::Numerics::float3x2 Class::Matrix3x2Property()
{
return _matrix3x2;
}
void Class::Matrix3x2Property(Windows::Foundation::Numerics::float3x2 const& value)
{
_matrix3x2 = value;
}
Windows::Foundation::Numerics::float4x4 Class::Matrix4x4Property()
{
return _matrix4x4;
}
void Class::Matrix4x4Property(Windows::Foundation::Numerics::float4x4 const& value)
{
_matrix4x4 = value;
}
Windows::Foundation::Numerics::plane Class::PlaneProperty()
{
return _plane;
}
void Class::PlaneProperty(Windows::Foundation::Numerics::plane const& value)
{
_plane = value;
}
Windows::Foundation::Numerics::quaternion Class::QuaternionProperty()
{
return _quaternion;
}
void Class::QuaternionProperty(Windows::Foundation::Numerics::quaternion const& value)
{
_quaternion = value;
}
Windows::Foundation::Numerics::float2 Class::Vector2Property()
{
return _vector2;
}
void Class::Vector2Property(Windows::Foundation::Numerics::float2 const& value)
{
_vector2 = value;
}
Windows::Foundation::Numerics::float3 Class::Vector3Property()
{
return _vector3;
}
void Class::Vector3Property(Windows::Foundation::Numerics::float3 const& value)
{
_vector3 = value;
}
Windows::Foundation::Numerics::float4 Class::Vector4Property()
{
return _vector4;
}
void Class::Vector4Property(Windows::Foundation::Numerics::float4 const& value)
{
_vector4 = value;
}
IReference<Point> Class::GetPointReference()
{
return _point;
Expand Down
21 changes: 21 additions & 0 deletions TestComponentCSharp/Class.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ namespace winrt::TestComponentCSharp::implementation
Windows::UI::Xaml::Media::Animation::KeyTime _keyTime{};
Windows::UI::Xaml::Media::Animation::RepeatBehavior _repeatBehavior{};
Windows::UI::Xaml::Media::Media3D::Matrix3D _matrix3D{};
Windows::Foundation::Numerics::float3x2 _matrix3x2;
Windows::Foundation::Numerics::float4x4 _matrix4x4;
Windows::Foundation::Numerics::plane _plane;
Windows::Foundation::Numerics::quaternion _quaternion;
Windows::Foundation::Numerics::float2 _vector2;
Windows::Foundation::Numerics::float3 _vector3;
Windows::Foundation::Numerics::float4 _vector4;
Windows::Foundation::TimeSpan _timeSpan{};
Windows::Foundation::DateTime _dateTime{};
winrt::hresult _hr;
Expand Down Expand Up @@ -238,6 +245,20 @@ namespace winrt::TestComponentCSharp::implementation
void RepeatBehaviorProperty(Windows::UI::Xaml::Media::Animation::RepeatBehavior const& value);
Windows::UI::Xaml::Media::Media3D::Matrix3D Matrix3DProperty();
void Matrix3DProperty(Windows::UI::Xaml::Media::Media3D::Matrix3D const& value);
Windows::Foundation::Numerics::float3x2 Matrix3x2Property();
void Matrix3x2Property(Windows::Foundation::Numerics::float3x2 const& value);
Windows::Foundation::Numerics::float4x4 Matrix4x4Property();
void Matrix4x4Property(Windows::Foundation::Numerics::float4x4 const& value);
Windows::Foundation::Numerics::plane PlaneProperty();
void PlaneProperty(Windows::Foundation::Numerics::plane const& value);
Windows::Foundation::Numerics::quaternion QuaternionProperty();
void QuaternionProperty(Windows::Foundation::Numerics::quaternion const& value);
Windows::Foundation::Numerics::float2 Vector2Property();
void Vector2Property(Windows::Foundation::Numerics::float2 const& value);
Windows::Foundation::Numerics::float3 Vector3Property();
void Vector3Property(Windows::Foundation::Numerics::float3 const& value);
Windows::Foundation::Numerics::float4 Vector4Property();
void Vector4Property(Windows::Foundation::Numerics::float4 const& value);
Windows::Foundation::IReference<Windows::Foundation::Point> GetPointReference();
Windows::Foundation::TimeSpan TimeSpanProperty();
void TimeSpanProperty(Windows::Foundation::TimeSpan const& value);
Expand Down
8 changes: 8 additions & 0 deletions TestComponentCSharp/TestComponentCSharp.idl
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,14 @@ namespace TestComponentCSharp
Windows.UI.Xaml.Media.Animation.KeyTime KeyTimeProperty;
Windows.UI.Xaml.Media.Animation.RepeatBehavior RepeatBehaviorProperty;
Windows.UI.Xaml.Media.Media3D.Matrix3D Matrix3DProperty;
Windows.Foundation.Numerics.Matrix3x2 Matrix3x2Property;
Windows.Foundation.Numerics.Matrix4x4 Matrix4x4Property;
Windows.Foundation.Numerics.Plane PlaneProperty;
Windows.Foundation.Numerics.Quaternion QuaternionProperty;
Windows.Foundation.Numerics.Vector2 Vector2Property;
Windows.Foundation.Numerics.Vector3 Vector3Property;
Windows.Foundation.Numerics.Vector4 Vector4Property;

// Structs mapped to equivalent ones in the System namespace
Windows.Foundation.TimeSpan TimeSpanProperty;
Windows.Foundation.IReference<Windows.Foundation.TimeSpan> GetTimeSpanReference();
Expand Down
40 changes: 28 additions & 12 deletions UnitTest/GuidTests.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Numerics;
using Xunit;
using WinRT;

using WF = Windows.Foundation;
using WFC = Windows.Foundation.Collections;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Media.Media3D;


using TestComponentCSharp;

Expand Down Expand Up @@ -85,9 +84,26 @@ public void TestGenerics()
AssertGuid<DateTimeOffset?>("5541d8a7-497c-5aa4-86fc-7713adbf2a2c");
AssertGuid<Point?>("84f14c22-a00a-5272-8d3d-82112e66df00");
AssertGuid<Rect?>("80423f11-054f-5eac-afd3-63b6ce15e77b");
AssertGuid<Size?>("61723086-8e53-5276-9f36-2a4bb93e2b75");

// Enums, structs, IInspectable, classes, and delegates
AssertGuid<Size?>("61723086-8e53-5276-9f36-2a4bb93e2b75");
AssertGuid<Color?>("ab8e5d11-b0c1-5a21-95ae-f16bf3a37624");
AssertGuid<CornerRadius?>("96d922e6-a7ca-5c21-b9f7-e4504e8c7112");
AssertGuid<Duration?>("47bd7ff2-5295-57bb-9212-2b0b8692aa13");
AssertGuid<GridLength?>("b5ecd72b-991b-5ec7-bacc-7ccd6bebe331");
AssertGuid<Thickness?>("a19f7ba8-d8cd-5df2-ab44-fefd26644484");
AssertGuid<GeneratorPosition?>("687a15ce-ae58-5e19-974e-34f17d92b941");
AssertGuid<Matrix?>("704b4ec9-0373-506c-8af3-57e9245f1c4e");
AssertGuid<KeyTime?>("9a16e097-f2e7-5bdc-b9f6-5a3f96ad3b59");
AssertGuid<RepeatBehavior?>("6588d2a6-3972-5247-a870-e217b76de89c");
AssertGuid<Matrix3D?>("e609698d-6aab-5adc-8b3f-a852f9e56fd0");
AssertGuid<Matrix3x2?>("76358cfd-2cbd-525b-a49e-90ee18247b71");
AssertGuid<Matrix4x4?>("dacbffdc-68ef-5fd0-b657-782d0ac9807e");
AssertGuid<Plane?>("46d542a1-52f7-58e7-acfc-9a6d364da022");
AssertGuid<Quaternion?>("b27004bb-c014-5dce-9a21-799c5a3c1461");
AssertGuid<Vector2?>("48f6a69e-8465-57ae-9400-9764087f65ad");
AssertGuid<Vector3?>("1ee770ff-c954-59ca-a754-6199a9be282c");
AssertGuid<Vector4?>("a5e843c9-ed20-5339-8f8d-9fe404cf3654");

// Enums, structs, IInspectable, classes, and delegates
AssertGuid<PropertyType?>("ecebde54-fac0-5aeb-9ba9-9e1fe17e31d5");
AssertGuid<Point?>("84f14c22-a00a-5272-8d3d-82112e66df00");
AssertGuid<IList<object>>("b32bdca4-5e52-5b27-bc5d-d66a1a268c2a");
Expand Down
112 changes: 108 additions & 4 deletions UnitTest/TestComponentCSharp_Tests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Numerics;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
Expand Down Expand Up @@ -1176,6 +1173,113 @@ public void TestMatrix3DTypeMapping()
Assert.True(TestObject.Matrix3DProperty == matrix3D);
}

[Fact]
public void TestMatrix3x2TypeMapping()
{
var matrix3x2 = new Matrix3x2
{
M11 = 11,
M12 = 12,
M21 = 21,
M22 = 22,
M31 = 31,
M32 = 32,
};
TestObject.Matrix3x2Property = matrix3x2;
Assert.Equal(matrix3x2.M11, TestObject.Matrix3x2Property.M11);
Assert.Equal(matrix3x2.M12, TestObject.Matrix3x2Property.M12);
Assert.Equal(matrix3x2.M21, TestObject.Matrix3x2Property.M21);
Assert.Equal(matrix3x2.M22, TestObject.Matrix3x2Property.M22);
Assert.Equal(matrix3x2.M31, TestObject.Matrix3x2Property.M31);
Assert.Equal(matrix3x2.M32, TestObject.Matrix3x2Property.M32);
Assert.True(TestObject.Matrix3x2Property == matrix3x2);
}

[Fact]
public void TestMatrix4x4TypeMapping()
{
var matrix4x4 = new Matrix4x4
{
M11 = 11, M12 = 12, M13 = 13, M14 = 14,
M21 = 21, M22 = 22, M23 = 23, M24 = 24,
M31 = 31, M32 = 32, M33 = 33, M34 = 34,
M41 = 41, M42 = 42, M43 = 43, M44 = 44
};
TestObject.Matrix4x4Property = matrix4x4;
Assert.Equal(matrix4x4.M11, TestObject.Matrix4x4Property.M11);
Assert.Equal(matrix4x4.M12, TestObject.Matrix4x4Property.M12);
Assert.Equal(matrix4x4.M13, TestObject.Matrix4x4Property.M13);
Assert.Equal(matrix4x4.M14, TestObject.Matrix4x4Property.M14);
Assert.Equal(matrix4x4.M21, TestObject.Matrix4x4Property.M21);
Assert.Equal(matrix4x4.M22, TestObject.Matrix4x4Property.M22);
Assert.Equal(matrix4x4.M23, TestObject.Matrix4x4Property.M23);
Assert.Equal(matrix4x4.M24, TestObject.Matrix4x4Property.M24);
Assert.Equal(matrix4x4.M31, TestObject.Matrix4x4Property.M31);
Assert.Equal(matrix4x4.M32, TestObject.Matrix4x4Property.M32);
Assert.Equal(matrix4x4.M33, TestObject.Matrix4x4Property.M33);
Assert.Equal(matrix4x4.M34, TestObject.Matrix4x4Property.M34);
Assert.Equal(matrix4x4.M41, TestObject.Matrix4x4Property.M41);
Assert.Equal(matrix4x4.M42, TestObject.Matrix4x4Property.M42);
Assert.Equal(matrix4x4.M43, TestObject.Matrix4x4Property.M43);
Assert.Equal(matrix4x4.M44, TestObject.Matrix4x4Property.M44);
Assert.True(TestObject.Matrix4x4Property == matrix4x4);
}

[Fact]
public void TestPlaneTypeMapping()
{
var plane = new Plane { D = 3.14F, Normal = new Vector3(1, 2, 3) };
TestObject.PlaneProperty = plane;
Assert.Equal(plane.D, TestObject.PlaneProperty.D);
Assert.Equal(plane.Normal, TestObject.PlaneProperty.Normal);
Assert.True(TestObject.PlaneProperty == plane);
}

[Fact]
public void TestQuaternionTypeMapping()
{
var quaternion = new Quaternion { W = 3.14F, X = 1, Y = 42, Z = 1729 };
TestObject.QuaternionProperty = quaternion;
Assert.Equal(quaternion.W, TestObject.QuaternionProperty.W);
Assert.Equal(quaternion.X, TestObject.QuaternionProperty.X);
Assert.Equal(quaternion.Y, TestObject.QuaternionProperty.Y);
Assert.Equal(quaternion.Z, TestObject.QuaternionProperty.Z);
Assert.True(TestObject.QuaternionProperty == quaternion);
}

[Fact]
public void TestVector2TypeMapping()
{
var vector2 = new Vector2 { X = 1, Y = 42 };
TestObject.Vector2Property = vector2;
Assert.Equal(vector2.X, TestObject.Vector2Property.X);
Assert.Equal(vector2.Y, TestObject.Vector2Property.Y);
Assert.True(TestObject.Vector2Property == vector2);
}

[Fact]
public void TestVector3TypeMapping()
{
var vector3 = new Vector3 { X = 1, Y = 42, Z = 1729 };
TestObject.Vector3Property = vector3;
Assert.Equal(vector3.X, TestObject.Vector3Property.X);
Assert.Equal(vector3.Y, TestObject.Vector3Property.Y);
Assert.Equal(vector3.Z, TestObject.Vector3Property.Z);
Assert.True(TestObject.Vector3Property == vector3);
}

[Fact]
public void TestVector4TypeMapping()
{
var vector4 = new Vector4 { W = 3.14F, X = 1, Y = 42, Z = 1729 };
TestObject.Vector4Property = vector4;
Assert.Equal(vector4.W, TestObject.Vector4Property.W);
Assert.Equal(vector4.X, TestObject.Vector4Property.X);
Assert.Equal(vector4.Y, TestObject.Vector4Property.Y);
Assert.Equal(vector4.Z, TestObject.Vector4Property.Z);
Assert.True(TestObject.Vector4Property == vector4);
}

[Fact]
public void TestTimeSpanMapping()
{
Expand Down
10 changes: 9 additions & 1 deletion WinRT.Runtime/Projections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Threading;
using System.Windows.Input;
Expand Down Expand Up @@ -50,6 +50,14 @@ static Projections()
RegisterCustomAbiTypeMappingNoLock(typeof(NotifyCollectionChangedAction), typeof(ABI.System.Collections.Specialized.NotifyCollectionChangedAction), "Windows.UI.Xaml.Interop.NotifyCollectionChangedAction");
RegisterCustomAbiTypeMappingNoLock(typeof(NotifyCollectionChangedEventArgs), typeof(ABI.System.Collections.Specialized.NotifyCollectionChangedEventArgs), "Windows.UI.Xaml.Interop.NotifyCollectionChangedEventArgs");
RegisterCustomAbiTypeMappingNoLock(typeof(NotifyCollectionChangedEventHandler), typeof(ABI.System.Collections.Specialized.NotifyCollectionChangedEventHandler), "Windows.UI.Xaml.Interop.NotifyCollectionChangedEventHandler");

RegisterCustomAbiTypeMappingNoLock(typeof(Matrix3x2), typeof(ABI.System.Numerics.Matrix3x2), "Windows.Foundation.Numerics.Matrix3x2");
RegisterCustomAbiTypeMappingNoLock(typeof(Matrix4x4), typeof(ABI.System.Numerics.Matrix4x4), "Windows.Foundation.Numerics.Matrix4x4");
RegisterCustomAbiTypeMappingNoLock(typeof(Plane), typeof(ABI.System.Numerics.Plane), "Windows.Foundation.Numerics.Plane");
RegisterCustomAbiTypeMappingNoLock(typeof(Quaternion), typeof(ABI.System.Numerics.Quaternion), "Windows.Foundation.Numerics.Quaternion");
RegisterCustomAbiTypeMappingNoLock(typeof(Vector2), typeof(ABI.System.Numerics.Vector2), "Windows.Foundation.Numerics.Vector2");
RegisterCustomAbiTypeMappingNoLock(typeof(Vector3), typeof(ABI.System.Numerics.Vector3), "Windows.Foundation.Numerics.Vector3");
RegisterCustomAbiTypeMappingNoLock(typeof(Vector4), typeof(ABI.System.Numerics.Vector4), "Windows.Foundation.Numerics.Vector4");
}

public static void RegisterCustomAbiTypeMapping(Type publicType, Type abiType, string winrtTypeName)
Expand Down
56 changes: 56 additions & 0 deletions WinRT.Runtime/Projections/Numerics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace ABI.System.Numerics
{
[StructLayout(LayoutKind.Sequential)]
public struct Matrix3x2
{
public static string GetGuidSignature() =>
"struct(Windows.Foundation.Numerics.Matrix3x2;f4;f4;f4;f4;f4;f4)";
}

[StructLayout(LayoutKind.Sequential)]
public struct Matrix4x4
{
public static string GetGuidSignature() =>
"struct(Windows.Foundation.Numerics.Matrix4x4;f4;f4;f4;f4;f4;f4;f4;f4;f4;f4;f4;f4;f4;f4;f4;f4)";
}

[StructLayout(LayoutKind.Sequential)]
public struct Plane
{
public static string GetGuidSignature() =>
"struct(Windows.Foundation.Numerics.Plane;struct(Windows.Foundation.Numerics.Vector3;f4;f4;f4);f4)";
}

[StructLayout(LayoutKind.Sequential)]
public struct Quaternion
{
public static string GetGuidSignature() =>
"struct(Windows.Foundation.Numerics.Quaternion;f4;f4;f4;f4)";
}

[StructLayout(LayoutKind.Sequential)]
public struct Vector2
{
public static string GetGuidSignature() =>
"struct(Windows.Foundation.Numerics.Vector2;f4;f4)";
}

[StructLayout(LayoutKind.Sequential)]
public struct Vector3
{
public static string GetGuidSignature() =>
"struct(Windows.Foundation.Numerics.Vector3;f4;f4;f4)";
}

[StructLayout(LayoutKind.Sequential)]
public struct Vector4
{
public static string GetGuidSignature() =>
"struct(Windows.Foundation.Numerics.Vector4;f4;f4;f4;f4)";
}
}
11 changes: 11 additions & 0 deletions cswinrt/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,17 @@ namespace cswinrt
{ "AttributeUsageAttribute", "System", "AttributeUsageAttribute" },
}
},
{ "Windows.Foundation.Numerics",
{
{ "Matrix3x2", "System.Numerics", "Matrix3x2" },
{ "Matrix4x4", "System.Numerics", "Matrix4x4" },
{ "Plane", "System.Numerics", "Plane" },
{ "Quaternion", "System.Numerics", "Quaternion" },
{ "Vector2", "System.Numerics", "Vector2" },
{ "Vector3", "System.Numerics", "Vector3" },
{ "Vector4", "System.Numerics", "Vector4" },
}
},
{ "Windows.UI",
{
{ "Color", "Windows.UI", "Color" },
Expand Down
Loading

0 comments on commit 83ad781

Please sign in to comment.