Skip to content

Commit b096e6e

Browse files
Merge pull request #3323 from Sergio0694/improvement/guard-api-codegen
Improved codegen in Guard APIs
2 parents 8e099de + e063a9c commit b096e6e

11 files changed

+1367
-691
lines changed

Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.g.cs

Lines changed: 384 additions & 192 deletions
Large diffs are not rendered by default.

Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt

Lines changed: 55 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ GenerateTextForItems(EnumerableTypes, item =>
2929
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3030
public static void IsEmpty<T>(<#=item.Type#> <#=item.Name#>, string name)
3131
{
32-
if (<#=item.Name#>.<#=item.Size#> != 0)
32+
if (<#=item.Name#>.<#=item.Size#> == 0)
3333
{
34-
ThrowHelper.ThrowArgumentExceptionForIsEmpty(<#=item.Cast#><#=item.Name#>, name);
34+
return;
3535
}
36+
37+
ThrowHelper.ThrowArgumentExceptionForIsEmpty(<#=item.Cast#><#=item.Name#>, name);
3638
}
3739

3840
/// <summary>
@@ -45,29 +47,31 @@ GenerateTextForItems(EnumerableTypes, item =>
4547
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4648
public static void IsNotEmpty<T>(<#=item.Type#> <#=item.Name#>, string name)
4749
{
48-
if (<#=item.Name#>.<#=item.Size#> == 0)
50+
if (<#=item.Name#>.<#=item.Size#> != 0)
4951
{
52+
return;
53+
}
54+
5055
<#
5156
if (item.Type == "Span<T>")
5257
{
5358
#>
54-
ThrowHelper.ThrowArgumentExceptionForIsNotEmptyWithSpan<T>(name);
59+
ThrowHelper.ThrowArgumentExceptionForIsNotEmptyWithSpan<T>(name);
5560
<#
5661
}
5762
else if (item.Type == "ReadOnlySpan<T>")
5863
{
5964
#>
60-
ThrowHelper.ThrowArgumentExceptionForIsNotEmptyWithReadOnlySpan<T>(name);
65+
ThrowHelper.ThrowArgumentExceptionForIsNotEmptyWithReadOnlySpan<T>(name);
6166
<#
6267
}
6368
else
6469
{
6570
#>
66-
ThrowHelper.ThrowArgumentExceptionForIsNotEmpty<<#=item.Type#>>(name);
71+
ThrowHelper.ThrowArgumentExceptionForIsNotEmpty<<#=item.Type#>>(name);
6772
<#
6873
}
6974
#>
70-
}
7175
}
7276

7377
/// <summary>
@@ -81,10 +85,12 @@ GenerateTextForItems(EnumerableTypes, item =>
8185
[MethodImpl(MethodImplOptions.AggressiveInlining)]
8286
public static void HasSizeEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
8387
{
84-
if (<#=item.Name#>.<#=item.Size#> != size)
88+
if (<#=item.Name#>.<#=item.Size#> == size)
8589
{
86-
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Cast#><#=item.Name#>, size, name);
90+
return;
8791
}
92+
93+
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Cast#><#=item.Name#>, size, name);
8894
}
8995

9096
/// <summary>
@@ -98,10 +104,12 @@ GenerateTextForItems(EnumerableTypes, item =>
98104
[MethodImpl(MethodImplOptions.AggressiveInlining)]
99105
public static void HasSizeNotEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
100106
{
101-
if (<#=item.Name#>.<#=item.Size#> == size)
107+
if (<#=item.Name#>.<#=item.Size#> != size)
102108
{
103-
ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(<#=item.Cast#><#=item.Name#>, size, name);
109+
return;
104110
}
111+
112+
ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(<#=item.Cast#><#=item.Name#>, size, name);
105113
}
106114

107115
/// <summary>
@@ -115,10 +123,12 @@ GenerateTextForItems(EnumerableTypes, item =>
115123
[MethodImpl(MethodImplOptions.AggressiveInlining)]
116124
public static void HasSizeGreaterThan<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
117125
{
118-
if (<#=item.Name#>.<#=item.Size#> <= size)
126+
if (<#=item.Name#>.<#=item.Size#> > size)
119127
{
120-
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThan(<#=item.Cast#><#=item.Name#>, size, name);
128+
return;
121129
}
130+
131+
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThan(<#=item.Cast#><#=item.Name#>, size, name);
122132
}
123133

124134
/// <summary>
@@ -132,10 +142,12 @@ GenerateTextForItems(EnumerableTypes, item =>
132142
[MethodImpl(MethodImplOptions.AggressiveInlining)]
133143
public static void HasSizeGreaterThanOrEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
134144
{
135-
if (<#=item.Name#>.<#=item.Size#> < size)
145+
if (<#=item.Name#>.<#=item.Size#> >= size)
136146
{
137-
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(<#=item.Cast#><#=item.Name#>, size, name);
147+
return;
138148
}
149+
150+
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(<#=item.Cast#><#=item.Name#>, size, name);
139151
}
140152

141153
/// <summary>
@@ -149,10 +161,12 @@ GenerateTextForItems(EnumerableTypes, item =>
149161
[MethodImpl(MethodImplOptions.AggressiveInlining)]
150162
public static void HasSizeLessThan<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
151163
{
152-
if (<#=item.Name#>.<#=item.Size#> >= size)
164+
if (<#=item.Name#>.<#=item.Size#> < size)
153165
{
154-
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(<#=item.Cast#><#=item.Name#>, size, name);
166+
return;
155167
}
168+
169+
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(<#=item.Cast#><#=item.Name#>, size, name);
156170
}
157171

158172
/// <summary>
@@ -166,10 +180,12 @@ GenerateTextForItems(EnumerableTypes, item =>
166180
[MethodImpl(MethodImplOptions.AggressiveInlining)]
167181
public static void HasSizeLessThanOrEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
168182
{
169-
if (<#=item.Name#>.<#=item.Size#> > size)
183+
if (<#=item.Name#>.<#=item.Size#> <= size)
170184
{
171-
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(<#=item.Cast#><#=item.Name#>, size, name);
185+
return;
172186
}
187+
188+
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(<#=item.Cast#><#=item.Name#>, size, name);
173189
}
174190

175191
/// <summary>
@@ -183,23 +199,25 @@ GenerateTextForItems(EnumerableTypes, item =>
183199
[MethodImpl(MethodImplOptions.AggressiveInlining)]
184200
public static void HasSizeEqualTo<T>(<#=item.Type#> source, <#=item.DestinationType#> destination, string name)
185201
{
186-
if (source.<#=item.Size#> != destination.<#=item.Size#>)
202+
if (source.<#=item.Size#> == destination.<#=item.Size#>)
187203
{
204+
return;
205+
}
206+
188207
<#
189208
if (item.HasCountProperty)
190209
{
191210
#>
192-
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Cast#>source, destination.<#=item.Size#>, name);
211+
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Cast#>source, destination.<#=item.Size#>, name);
193212
<#
194213
}
195214
else
196215
{
197216
#>
198-
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(source, <#=item.Cast#>destination, name);
217+
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(source, <#=item.Cast#>destination, name);
199218
<#
200219
}
201220
#>
202-
}
203221
}
204222

205223
/// <summary>
@@ -213,23 +231,25 @@ GenerateTextForItems(EnumerableTypes, item =>
213231
[MethodImpl(MethodImplOptions.AggressiveInlining)]
214232
public static void HasSizeLessThanOrEqualTo<T>(<#=item.Type#> source, <#=item.DestinationType#> destination, string name)
215233
{
216-
if (source.<#=item.Size#> > destination.<#=item.Size#>)
234+
if (source.<#=item.Size#> <= destination.<#=item.Size#>)
217235
{
236+
return;
237+
}
238+
218239
<#
219240
if (item.HasCountProperty)
220241
{
221242
#>
222-
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Cast#>source, destination.<#=item.Size#>, name);
243+
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Cast#>source, destination.<#=item.Size#>, name);
223244
<#
224245
}
225246
else
226247
{
227248
#>
228-
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(source, <#=item.Cast#>destination, name);
249+
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(source, <#=item.Cast#>destination, name);
229250
<#
230251
}
231252
#>
232-
}
233253
}
234254

235255
/// <summary>
@@ -250,10 +270,12 @@ GenerateTextForItems(EnumerableTypes, item =>
250270
// For more info and code sample, see the original conversation here:
251271
// https://github.com/windows-toolkit/WindowsCommunityToolkit/pull/3131#discussion_r390682835
252272
#>
253-
if ((uint)index >= (uint)<#=item.Name#>.<#=item.Size#>)
273+
if ((uint)index < (uint)<#=item.Name#>.<#=item.Size#>)
254274
{
255-
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRangeFor(index, <#=item.Cast#><#=item.Name#>, name);
275+
return;
256276
}
277+
278+
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRangeFor(index, <#=item.Cast#><#=item.Name#>, name);
257279
}
258280

259281
/// <summary>
@@ -267,10 +289,12 @@ GenerateTextForItems(EnumerableTypes, item =>
267289
[MethodImpl(MethodImplOptions.AggressiveInlining)]
268290
public static void IsNotInRangeFor<T>(int index, <#=item.Type#> <#=item.Name#>, string name)
269291
{
270-
if ((uint)index < (uint)<#=item.Name#>.<#=item.Size#>)
292+
if ((uint)index >= (uint)<#=item.Name#>.<#=item.Size#>)
271293
{
272-
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(index, <#=item.Cast#><#=item.Name#>, name);
294+
return;
273295
}
296+
297+
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(index, <#=item.Cast#><#=item.Name#>, name);
274298
}
275299
<#
276300
});

0 commit comments

Comments
 (0)