-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathProjections.CustomTypeMappings.tt
249 lines (228 loc) · 12.4 KB
/
Projections.CustomTypeMappings.tt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#@ output extension=".g.cs"#>
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#if NET
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Numerics;
using System.Threading;
using System.Windows.Input;
using Microsoft.UI.Xaml.Interop;
using Windows.Foundation.Collections;
namespace WinRT
{
/// <inheritdoc cref="Projections"/>
partial class Projections
{
<#
// Helper method to get the generated method name
static string GetMethodName(string name, string hint)
{
var methodTypeName = hint ?? name;
// If the hint isn't available, automatically derive the name. For generic types,
// we strip the '<>' from the name and append "OpenGeneric" as a suffix.
if (hint == null)
{
if (name.Contains("<")) methodTypeName = methodTypeName.Substring(0, methodTypeName.IndexOf('<'));
if (name.Contains("<")) methodTypeName += "OpenGeneric";
}
return $"Register{methodTypeName}Mapping";
}
// Helper to write the guard statements for a given method.
// We need two extra indents as these are inside each method.
void WriteGuardStatements(string name)
{
#>
if (FeatureSwitches.EnableDefaultCustomTypeMappings)
{
return;
}
<#
if (name == "DataErrorsChangedEventArgs" ||
name == "INotifyDataErrorInfo" ||
name == "IServiceProvider")
{
WriteLine("");
#>
if (FeatureSwitches.UseWindowsUIXamlProjections)
{
throw new NotSupportedException("The '<#=name#>' type is only supported for WinUI, and not when using System XAML projections (make sure the 'CsWinRTUseWindowsUIXamlProjections' property is not set to 'true').");
}
<#
}
#>
if (Interlocked.CompareExchange(ref _<#=Regex.Replace(name, "[?<>,]", "_")#>, 1, 0) == 1)
{
return;
}
<#
}
// Types for 'RegisterCustomAbiTypeMapping'
var registerCustomAbiTypeMappings = new (string Public, string Abi, string Name, string Hint, bool IsRuntimeClass)[]
{
("EventRegistrationToken", "ABI.WinRT.EventRegistrationToken", "Windows.Foundation.EventRegistrationToken", null, false),
("Nullable<>", "ABI.System.Nullable<>", "Windows.Foundation.IReference`1", null, false),
("int?", "ABI.System.Nullable_int", "Windows.Foundation.IReference`1<Int32>", "NullableInt", false),
("byte?", "ABI.System.Nullable_byte", "Windows.Foundation.IReference`1<UInt8>", "NullableByte", false),
("sbyte?", "ABI.System.Nullable_sbyte", "Windows.Foundation.IReference`1<Int8>", "NullableSByte", false),
("short?", "ABI.System.Nullable_short", "Windows.Foundation.IReference`1<Int16>", "NullableShort", false),
("ushort?", "ABI.System.Nullable_ushort", "Windows.Foundation.IReference`1<UInt16>", "NullableUShort", false),
("uint?", "ABI.System.Nullable_uint", "Windows.Foundation.IReference`1<UInt32>", "NullableUInt", false),
("long?", "ABI.System.Nullable_long", "Windows.Foundation.IReference`1<Int64>", "NullableLong", false),
("ulong?", "ABI.System.Nullable_ulong", "Windows.Foundation.IReference`1<UInt64>", "NullableULong", false),
("float?", "ABI.System.Nullable_float", "Windows.Foundation.IReference`1<Single>", "NullableFloat", false),
("double?", "ABI.System.Nullable_double", "Windows.Foundation.IReference`1<Double>", "NullableDouble", false),
("char?", "ABI.System.Nullable_char", "Windows.Foundation.IReference`1<Char16>", "NullableChar", false),
("bool?", "ABI.System.Nullable_bool", "Windows.Foundation.IReference`1<Boolean>", "NullableBool", false),
("Guid?", "ABI.System.Nullable_guid", "Windows.Foundation.IReference`1<Guid>", "NullableGuid", false),
("DateTimeOffset?", "ABI.System.Nullable_DateTimeOffset", "Windows.Foundation.IReference`1<Windows.Foundation.DateTime>", "NullableDateTimeOffset", false),
("TimeSpan?", "ABI.System.Nullable_TimeSpan", "Windows.Foundation.IReference`1<TimeSpan>", "NullableTimeSpan", false),
("DateTimeOffset", "ABI.System.DateTimeOffset", "Windows.Foundation.DateTime", null, false),
("Exception", "ABI.System.Exception", "Windows.Foundation.HResult", null, false),
("TimeSpan", "ABI.System.TimeSpan", "Windows.Foundation.TimeSpan", null, false),
("Uri", "ABI.System.Uri", "Windows.Foundation.Uri", null, true),
("DataErrorsChangedEventArgs", "ABI.System.ComponentModel.DataErrorsChangedEventArgs", "Microsoft.UI.Xaml.Data.DataErrorsChangedEventArgs", null, true),
("PropertyChangedEventArgs", "ABI.System.ComponentModel.PropertyChangedEventArgs", "Microsoft.UI.Xaml.Data.PropertyChangedEventArgs", null, true),
("INotifyDataErrorInfo", "ABI.System.ComponentModel.INotifyDataErrorInfo", "Microsoft.UI.Xaml.Data.INotifyDataErrorInfo", null, false),
("INotifyPropertyChanged", "ABI.System.ComponentModel.INotifyPropertyChanged", "Microsoft.UI.Xaml.Data.INotifyPropertyChanged", null, false),
("ICommand", "ABI.System.Windows.Input.ICommand", "Microsoft.UI.Xaml.Interop.ICommand", null, false),
("IServiceProvider", "ABI.System.IServiceProvider", "Microsoft.UI.Xaml.IXamlServiceProvider", null, false),
("EventHandler<>", "ABI.System.EventHandler<>", "Windows.Foundation.EventHandler`1", null, false),
("KeyValuePair<,>", "ABI.System.Collections.Generic.KeyValuePair<,>", "Windows.Foundation.Collections.IKeyValuePair`2", null, false),
("IEnumerable<>", "ABI.System.Collections.Generic.IEnumerable<>", "Windows.Foundation.Collections.IIterable`1", null, false),
("IEnumerator<>", "ABI.System.Collections.Generic.IEnumerator<>", "Windows.Foundation.Collections.IIterator`1", null, false),
("IList<>", "ABI.System.Collections.Generic.IList<>", "Windows.Foundation.Collections.IVector`1", null, false),
("IReadOnlyList<>", "ABI.System.Collections.Generic.IReadOnlyList<>", "Windows.Foundation.Collections.IVectorView`1", null, false),
("IDictionary<,>", "ABI.System.Collections.Generic.IDictionary<,>", "Windows.Foundation.Collections.IMap`2", null, false),
("IReadOnlyDictionary<,>", "ABI.System.Collections.Generic.IReadOnlyDictionary<,>", "Windows.Foundation.Collections.IMapView`2", null, false),
("IDisposable", "ABI.System.IDisposable", "Windows.Foundation.IClosable", null, false),
("IEnumerable", "ABI.System.Collections.IEnumerable", "Microsoft.UI.Xaml.Interop.IBindableIterable", null, false),
("IList", "ABI.System.Collections.IList", "Microsoft.UI.Xaml.Interop.IBindableVector", null, false),
("INotifyCollectionChanged", "ABI.System.Collections.Specialized.INotifyCollectionChanged", "Microsoft.UI.Xaml.Interop.INotifyCollectionChanged", null, false),
("NotifyCollectionChangedAction", "ABI.System.Collections.Specialized.NotifyCollectionChangedAction", "Microsoft.UI.Xaml.Interop.NotifyCollectionChangedAction", null, false),
("NotifyCollectionChangedEventArgs", "ABI.System.Collections.Specialized.NotifyCollectionChangedEventArgs", "Microsoft.UI.Xaml.Interop.NotifyCollectionChangedEventArgs", null, true),
("Matrix3x2", "ABI.System.Numerics.Matrix3x2", "Windows.Foundation.Numerics.Matrix3x2", null, false),
("Matrix4x4", "ABI.System.Numerics.Matrix4x4", "Windows.Foundation.Numerics.Matrix4x4", null, false),
("Plane", "ABI.System.Numerics.Plane", "Windows.Foundation.Numerics.Plane", null, false),
("Quaternion", "ABI.System.Numerics.Quaternion", "Windows.Foundation.Numerics.Quaternion", null, false),
("Vector2", "ABI.System.Numerics.Vector2", "Windows.Foundation.Numerics.Vector2", null, false),
("Vector3", "ABI.System.Numerics.Vector3", "Windows.Foundation.Numerics.Vector3", null, false),
("Vector4", "ABI.System.Numerics.Vector4", "Windows.Foundation.Numerics.Vector4", null, false)
};
// Types for 'RegisterCustomTypeToHelperTypeMapping'
var registerCustomTypeToHelperTypeMapping = new (string Public, string Helper)[]
{
("IMap<,>", "ABI.System.Collections.Generic.IDictionary<,>"),
("IVector<>", "ABI.System.Collections.Generic.IList<>"),
("IMapView<,>", "ABI.System.Collections.Generic.IReadOnlyDictionary<,>"),
("IVectorView<>", "ABI.System.Collections.Generic.IReadOnlyList<>"),
("IBindableVector", "ABI.System.Collections.IList"),
("ICollection<>", "ABI.System.Collections.Generic.ICollection<>"),
("IReadOnlyCollection<>", "ABI.System.Collections.Generic.IReadOnlyCollection<>"),
("ICollection", "ABI.System.Collections.ICollection")
};
// Types that have different projections for System XAML
var systemXamlProjectionTypeMapping = new(string Public, string Name)[]
{
("PropertyChangedEventArgs", "Windows.UI.Xaml.Data.PropertyChangedEventArgs"),
("PropertyChangedEventHandler", "Windows.UI.Xaml.Data.PropertyChangedEventHandler"),
("INotifyPropertyChanged", "Windows.UI.Xaml.Data.INotifyPropertyChanged"),
("ICommand", "Windows.UI.Xaml.Interop.ICommand"),
("IEnumerable", "Windows.UI.Xaml.Interop.IBindableIterable"),
("IList", "Windows.UI.Xaml.Interop.IBindableVector"),
("INotifyCollectionChanged", "Windows.UI.Xaml.Interop.INotifyCollectionChanged"),
("NotifyCollectionChangedAction", "Windows.UI.Xaml.Interop.NotifyCollectionChangedAction"),
("NotifyCollectionChangedEventArgs", "Windows.UI.Xaml.Interop.NotifyCollectionChangedEventArgs"),
("NotifyCollectionChangedEventHandler", "Windows.UI.Xaml.Interop.NotifyCollectionChangedEventHandler")
};
// Declare all fields
foreach (string fieldName in
registerCustomAbiTypeMappings
.Select(t => t.Public)
.Concat(
registerCustomTypeToHelperTypeMapping
.Select(t => t.Public))
.Select(s => Regex.Replace(s, "[?<>,]", "_")))
{
#>
private static int _<#=fieldName#>;
<#
}
// 'RegisterCustomAbiTypeMapping' methods
foreach (var type in registerCustomAbiTypeMappings)
{
WriteLine("");
#>
/// <summary>Registers the custom ABI type mapping for the <c>"<#=type.Name#>"</c> WinRT type.</summary>
public static void <#=GetMethodName(type.Public, type.Hint)#>()
{
<#
WriteGuardStatements(type.Public);
WriteLine("");
int indexOfSystemXamlTypeMapping = Array.FindIndex(systemXamlProjectionTypeMapping, t => t.Public == type.Public);
if (indexOfSystemXamlTypeMapping != -1)
{
#>
if (FeatureSwitches.UseWindowsUIXamlProjections)
{
RegisterCustomAbiTypeMapping(
typeof(<#=type.Public#>),
typeof(<#=type.Abi#>),
"<#=systemXamlProjectionTypeMapping[indexOfSystemXamlTypeMapping].Name#>",
isRuntimeClass: <#=type.IsRuntimeClass.ToString().ToLowerInvariant()#>);
}
else
{
RegisterCustomAbiTypeMapping(
typeof(<#=type.Public#>),
typeof(<#=type.Abi#>),
"<#=type.Name#>",
isRuntimeClass: <#=type.IsRuntimeClass.ToString().ToLowerInvariant()#>);
}
<#
}
else
{
#>
RegisterCustomAbiTypeMapping(
typeof(<#=type.Public#>),
typeof(<#=type.Abi#>),
"<#=type.Name#>",
isRuntimeClass: <#=type.IsRuntimeClass.ToString().ToLowerInvariant()#>);
<#
}
#>
}
<#
}
// 'RegisterCustomTypeToHelperTypeMapping' methods
foreach (var type in registerCustomTypeToHelperTypeMapping)
{
string xmlName = type.Public;
if (type.Public.EndsWith("<>")) xmlName = $"{xmlName.Substring(0, xmlName.IndexOf('<'))}{{T}}";
if (type.Public.EndsWith("<,>")) xmlName = $"{xmlName.Substring(0, xmlName.IndexOf('<'))}{{K, V}}";
WriteLine("");
#>
/// <summary>Registers the custom ABI type mapping for the <see cref="<#=xmlName#>"/> type.</summary>
public static void <#=GetMethodName(type.Public, null)#>()
{
<#
WriteGuardStatements(type.Public);
#>
RegisterCustomTypeToHelperTypeMapping(
typeof(<#=type.Public#>),
typeof(<#=type.Helper#>));
}
<#
}
#>
}
}
#endif