mirror of
https://github.com/chylex/.NET-Community-Toolkit.git
synced 2025-04-10 11:15:45 +02:00
Improved codegen in generated Guard APIs
This commit is contained in:
parent
44fd676ee9
commit
e2a2e93a4a
Microsoft.Toolkit/Diagnostics/Generated
File diff suppressed because it is too large
Load Diff
@ -29,10 +29,12 @@ GenerateTextForItems(EnumerableTypes, item =>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsEmpty<T>(<#=item.Type#> <#=item.Name#>, string name)
|
||||
{
|
||||
if (<#=item.Name#>.<#=item.Size#> != 0)
|
||||
if (<#=item.Name#>.<#=item.Size#> == 0)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsEmpty(<#=item.Cast#><#=item.Name#>, name);
|
||||
return;
|
||||
}
|
||||
|
||||
ThrowHelper.ThrowArgumentExceptionForIsEmpty(<#=item.Cast#><#=item.Name#>, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -45,29 +47,31 @@ GenerateTextForItems(EnumerableTypes, item =>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotEmpty<T>(<#=item.Type#> <#=item.Name#>, string name)
|
||||
{
|
||||
if (<#=item.Name#>.<#=item.Size#> == 0)
|
||||
if (<#=item.Name#>.<#=item.Size#> != 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
<#
|
||||
if (item.Type == "Span<T>")
|
||||
{
|
||||
#>
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotEmptyWithSpan<T>(name);
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotEmptyWithSpan<T>(name);
|
||||
<#
|
||||
}
|
||||
else if (item.Type == "ReadOnlySpan<T>")
|
||||
{
|
||||
#>
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotEmptyWithReadOnlySpan<T>(name);
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotEmptyWithReadOnlySpan<T>(name);
|
||||
<#
|
||||
}
|
||||
else
|
||||
{
|
||||
#>
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotEmpty<<#=item.Type#>>(name);
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotEmpty<<#=item.Type#>>(name);
|
||||
<#
|
||||
}
|
||||
#>
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -81,10 +85,12 @@ GenerateTextForItems(EnumerableTypes, item =>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void HasSizeEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
|
||||
{
|
||||
if (<#=item.Name#>.<#=item.Size#> != size)
|
||||
if (<#=item.Name#>.<#=item.Size#> == size)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Cast#><#=item.Name#>, size, name);
|
||||
return;
|
||||
}
|
||||
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Cast#><#=item.Name#>, size, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -98,10 +104,12 @@ GenerateTextForItems(EnumerableTypes, item =>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void HasSizeNotEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
|
||||
{
|
||||
if (<#=item.Name#>.<#=item.Size#> == size)
|
||||
if (<#=item.Name#>.<#=item.Size#> != size)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(<#=item.Cast#><#=item.Name#>, size, name);
|
||||
return;
|
||||
}
|
||||
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(<#=item.Cast#><#=item.Name#>, size, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -115,10 +123,12 @@ GenerateTextForItems(EnumerableTypes, item =>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void HasSizeGreaterThan<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
|
||||
{
|
||||
if (<#=item.Name#>.<#=item.Size#> <= size)
|
||||
if (<#=item.Name#>.<#=item.Size#> > size)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThan(<#=item.Cast#><#=item.Name#>, size, name);
|
||||
return;
|
||||
}
|
||||
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThan(<#=item.Cast#><#=item.Name#>, size, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -132,10 +142,12 @@ GenerateTextForItems(EnumerableTypes, item =>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void HasSizeGreaterThanOrEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
|
||||
{
|
||||
if (<#=item.Name#>.<#=item.Size#> < size)
|
||||
if (<#=item.Name#>.<#=item.Size#> >= size)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(<#=item.Cast#><#=item.Name#>, size, name);
|
||||
return;
|
||||
}
|
||||
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(<#=item.Cast#><#=item.Name#>, size, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -149,10 +161,12 @@ GenerateTextForItems(EnumerableTypes, item =>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void HasSizeLessThan<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
|
||||
{
|
||||
if (<#=item.Name#>.<#=item.Size#> >= size)
|
||||
if (<#=item.Name#>.<#=item.Size#> < size)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(<#=item.Cast#><#=item.Name#>, size, name);
|
||||
return;
|
||||
}
|
||||
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(<#=item.Cast#><#=item.Name#>, size, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -166,10 +180,12 @@ GenerateTextForItems(EnumerableTypes, item =>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void HasSizeLessThanOrEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
|
||||
{
|
||||
if (<#=item.Name#>.<#=item.Size#> > size)
|
||||
if (<#=item.Name#>.<#=item.Size#> <= size)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(<#=item.Cast#><#=item.Name#>, size, name);
|
||||
return;
|
||||
}
|
||||
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(<#=item.Cast#><#=item.Name#>, size, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -183,23 +199,25 @@ GenerateTextForItems(EnumerableTypes, item =>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void HasSizeEqualTo<T>(<#=item.Type#> source, <#=item.DestinationType#> destination, string name)
|
||||
{
|
||||
if (source.<#=item.Size#> != destination.<#=item.Size#>)
|
||||
if (source.<#=item.Size#> == destination.<#=item.Size#>)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
<#
|
||||
if (item.HasCountProperty)
|
||||
{
|
||||
#>
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Cast#>source, destination.<#=item.Size#>, name);
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Cast#>source, destination.<#=item.Size#>, name);
|
||||
<#
|
||||
}
|
||||
else
|
||||
{
|
||||
#>
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(source, <#=item.Cast#>destination, name);
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(source, <#=item.Cast#>destination, name);
|
||||
<#
|
||||
}
|
||||
#>
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -213,23 +231,25 @@ GenerateTextForItems(EnumerableTypes, item =>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void HasSizeLessThanOrEqualTo<T>(<#=item.Type#> source, <#=item.DestinationType#> destination, string name)
|
||||
{
|
||||
if (source.<#=item.Size#> > destination.<#=item.Size#>)
|
||||
if (source.<#=item.Size#> <= destination.<#=item.Size#>)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
<#
|
||||
if (item.HasCountProperty)
|
||||
{
|
||||
#>
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Cast#>source, destination.<#=item.Size#>, name);
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Cast#>source, destination.<#=item.Size#>, name);
|
||||
<#
|
||||
}
|
||||
else
|
||||
{
|
||||
#>
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(source, <#=item.Cast#>destination, name);
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(source, <#=item.Cast#>destination, name);
|
||||
<#
|
||||
}
|
||||
#>
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -250,10 +270,12 @@ GenerateTextForItems(EnumerableTypes, item =>
|
||||
// For more info and code sample, see the original conversation here:
|
||||
// https://github.com/windows-toolkit/WindowsCommunityToolkit/pull/3131#discussion_r390682835
|
||||
#>
|
||||
if ((uint)index >= (uint)<#=item.Name#>.<#=item.Size#>)
|
||||
if ((uint)index < (uint)<#=item.Name#>.<#=item.Size#>)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRangeFor(index, <#=item.Cast#><#=item.Name#>, name);
|
||||
return;
|
||||
}
|
||||
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRangeFor(index, <#=item.Cast#><#=item.Name#>, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -267,10 +289,12 @@ GenerateTextForItems(EnumerableTypes, item =>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotInRangeFor<T>(int index, <#=item.Type#> <#=item.Name#>, string name)
|
||||
{
|
||||
if ((uint)index < (uint)<#=item.Name#>.<#=item.Size#>)
|
||||
if ((uint)index >= (uint)<#=item.Name#>.<#=item.Size#>)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(index, <#=item.Cast#><#=item.Name#>, name);
|
||||
return;
|
||||
}
|
||||
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(index, <#=item.Cast#><#=item.Name#>, name);
|
||||
}
|
||||
<#
|
||||
});
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -28,10 +28,12 @@ GenerateTextForItems(NumericTypes, type =>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsEqualTo(<#=type#> value, <#=type#> target, string name)
|
||||
{
|
||||
if (value != target)
|
||||
if (value == target)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsEqualTo(value, target, name);
|
||||
return;
|
||||
}
|
||||
|
||||
ThrowHelper.ThrowArgumentExceptionForIsEqualTo(value, target, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -45,10 +47,12 @@ GenerateTextForItems(NumericTypes, type =>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotEqualTo(<#=type#> value, <#=type#> target, string name)
|
||||
{
|
||||
if (value == target)
|
||||
if (value != target)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotEqualTo(value, target, name);
|
||||
return;
|
||||
}
|
||||
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotEqualTo(value, target, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -62,10 +66,12 @@ GenerateTextForItems(NumericTypes, type =>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsLessThan(<#=type#> value, <#=type#> maximum, string name)
|
||||
{
|
||||
if (value >= maximum)
|
||||
if (value < maximum)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsLessThan(value, maximum, name);
|
||||
return;
|
||||
}
|
||||
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsLessThan(value, maximum, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -79,10 +85,12 @@ GenerateTextForItems(NumericTypes, type =>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsLessThanOrEqualTo(<#=type#> value, <#=type#> maximum, string name)
|
||||
{
|
||||
if (value > maximum)
|
||||
if (value <= maximum)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsLessThanOrEqualTo(value, maximum, name);
|
||||
return;
|
||||
}
|
||||
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsLessThanOrEqualTo(value, maximum, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -96,10 +104,12 @@ GenerateTextForItems(NumericTypes, type =>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsGreaterThan(<#=type#> value, <#=type#> minimum, string name)
|
||||
{
|
||||
if (value <= minimum)
|
||||
if (value > minimum)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsGreaterThan(value, minimum, name);
|
||||
return;
|
||||
}
|
||||
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsGreaterThan(value, minimum, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -113,10 +123,12 @@ GenerateTextForItems(NumericTypes, type =>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsGreaterThanOrEqualTo(<#=type#> value, <#=type#> minimum, string name)
|
||||
{
|
||||
if (value < minimum)
|
||||
if (value >= minimum)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsGreaterThanOrEqualTo(value, minimum, name);
|
||||
return;
|
||||
}
|
||||
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsGreaterThanOrEqualTo(value, minimum, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -134,10 +146,12 @@ GenerateTextForItems(NumericTypes, type =>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsInRange(<#=type#> value, <#=type#> minimum, <#=type#> maximum, string name)
|
||||
{
|
||||
if (value < minimum || value >= maximum)
|
||||
if (value >= minimum && value < maximum)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRange(value, minimum, maximum, name);
|
||||
return;
|
||||
}
|
||||
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRange(value, minimum, maximum, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -155,10 +169,12 @@ GenerateTextForItems(NumericTypes, type =>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotInRange(<#=type#> value, <#=type#> minimum, <#=type#> maximum, string name)
|
||||
{
|
||||
if (value >= minimum && value < maximum)
|
||||
if (value < minimum || value >= maximum)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotInRange(value, minimum, maximum, name);
|
||||
return;
|
||||
}
|
||||
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotInRange(value, minimum, maximum, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -176,10 +192,12 @@ GenerateTextForItems(NumericTypes, type =>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsBetween(<#=type#> value, <#=type#> minimum, <#=type#> maximum, string name)
|
||||
{
|
||||
if (value <= minimum || value >= maximum)
|
||||
if (value > minimum && value < maximum)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsBetween(value, minimum, maximum, name);
|
||||
return;
|
||||
}
|
||||
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsBetween(value, minimum, maximum, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -197,10 +215,12 @@ GenerateTextForItems(NumericTypes, type =>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotBetween(<#=type#> value, <#=type#> minimum, <#=type#> maximum, string name)
|
||||
{
|
||||
if (value > minimum && value < maximum)
|
||||
if (value <= minimum || value >= maximum)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotBetween(value, minimum, maximum, name);
|
||||
return;
|
||||
}
|
||||
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotBetween(value, minimum, maximum, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -218,10 +238,12 @@ GenerateTextForItems(NumericTypes, type =>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsBetweenOrEqualTo(<#=type#> value, <#=type#> minimum, <#=type#> maximum, string name)
|
||||
{
|
||||
if (value < minimum || value > maximum)
|
||||
if (value >= minimum && value <= maximum)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsBetweenOrEqualTo(value, minimum, maximum, name);
|
||||
return;
|
||||
}
|
||||
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsBetweenOrEqualTo(value, minimum, maximum, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -239,10 +261,12 @@ GenerateTextForItems(NumericTypes, type =>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotBetweenOrEqualTo(<#=type#> value, <#=type#> minimum, <#=type#> maximum, string name)
|
||||
{
|
||||
if (value >= minimum && value <= maximum)
|
||||
if (value < minimum || value > maximum)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotBetweenOrEqualTo(value, minimum, maximum, name);
|
||||
return;
|
||||
}
|
||||
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotBetweenOrEqualTo(value, minimum, maximum, name);
|
||||
}
|
||||
<#
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user