Added FisheryLib.Collections.FishTable and Mono.Cecil extension methods
A bit of refactoring has been done too
This commit is contained in:
@@ -10,287 +10,287 @@ namespace PerformanceFish.Utility.Diagnostics;
|
||||
partial class Guard
|
||||
{
|
||||
<#
|
||||
GenerateTextForItems(EnumerableTypes, item =>
|
||||
{
|
||||
GenerateTextForItems(EnumerableTypes, item =>
|
||||
{
|
||||
#>
|
||||
/// <summary>
|
||||
/// Asserts that the input <#=item.XmlType#> instance must be empty.
|
||||
/// Asserts that the input <#= item.XmlType #> instance must be empty.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The item of items in the input <#=item.XmlType#> instance.</typeparam>
|
||||
/// <param name="<#=item.Name#>">The input <#=item.XmlType#> instance to check the size for.</param>
|
||||
/// <typeparam name="T">The item of items in the input <#= item.XmlType #> instance.</typeparam>
|
||||
/// <param name="<#= item.Name #>">The input <#= item.XmlType #> instance to check the size for.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="<#=item.Name#>"/> is != 0.</exception>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="<#= item.Name #>"/> is != 0.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsEmpty<T>(<#=item.Type#> <#=item.Name#>, [CallerArgumentExpression("<#=item.Name#>")] string name = "")
|
||||
public static void IsEmpty<T>(<#= item.Type #> <#= item.Name #>, [CallerArgumentExpression("<#= item.Name #>")] string name = "")
|
||||
{
|
||||
if (<#=item.Name#>.<#=item.Size#> == 0)
|
||||
if (<#= item.Name #>.<#= item.Size #> == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ThrowHelper.ThrowArgumentExceptionForIsEmpty(<#=item.Cast#><#=item.Name#>, name);
|
||||
ThrowHelper.ThrowArgumentExceptionForIsEmpty(<#= item.Cast #><#= item.Name #>, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <#=item.XmlType#> instance must not be empty.
|
||||
/// Asserts that the input <#= item.XmlType #> instance must not be empty.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The item of items in the input <#=item.XmlType#> instance.</typeparam>
|
||||
/// <param name="<#=item.Name#>">The input <#=item.XmlType#> instance to check the size for.</param>
|
||||
/// <typeparam name="T">The item of items in the input <#= item.XmlType #> instance.</typeparam>
|
||||
/// <param name="<#= item.Name #>">The input <#= item.XmlType #> instance to check the size for.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="<#=item.Name#>"/> is == 0.</exception>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="<#= item.Name #>"/> is == 0.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotEmpty<T>(<#=item.Type#> <#=item.Name#>, [CallerArgumentExpression("<#=item.Name#>")] string name = "")
|
||||
public static void IsNotEmpty<T>(<#= item.Type #> <#= item.Name #>, [CallerArgumentExpression("<#= item.Name #>")] string name = "")
|
||||
{
|
||||
if (<#=item.Name#>.<#=item.Size#> != 0)
|
||||
if (<#= item.Name #>.<#= item.Size #> != 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
<#
|
||||
if (item.Type == "Span<T>")
|
||||
{
|
||||
if (item.Type == "Span<T>")
|
||||
{
|
||||
#>
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotEmptyWithSpan<T>(name);
|
||||
<#
|
||||
}
|
||||
else if (item.Type == "ReadOnlySpan<T>")
|
||||
{
|
||||
}
|
||||
else if (item.Type == "ReadOnlySpan<T>")
|
||||
{
|
||||
#>
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotEmptyWithReadOnlySpan<T>(name);
|
||||
<#
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
#>
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotEmpty<<#=item.Type#>>(name);
|
||||
ThrowHelper.ThrowArgumentExceptionForIsNotEmpty<<#= item.Type #>>(name);
|
||||
<#
|
||||
}
|
||||
}
|
||||
#>
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <#=item.XmlType#> instance must have a size of a specified value.
|
||||
/// Asserts that the input <#= item.XmlType #> instance must have a size of a specified value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The item of items in the input <#=item.XmlType#> instance.</typeparam>
|
||||
/// <param name="<#=item.Name#>">The input <#=item.XmlType#> instance to check the size for.</param>
|
||||
/// <typeparam name="T">The item of items in the input <#= item.XmlType #> instance.</typeparam>
|
||||
/// <param name="<#= item.Name #>">The input <#= item.XmlType #> instance to check the size for.</param>
|
||||
/// <param name="size">The target size to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="<#=item.Name#>"/> is != <paramref name="size"/>.</exception>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="<#= item.Name #>"/> is != <paramref name="size"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void HasSizeEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, [CallerArgumentExpression("<#=item.Name#>")] string name = "")
|
||||
public static void HasSizeEqualTo<T>(<#= item.Type #> <#= item.Name #>, int size, [CallerArgumentExpression("<#= item.Name #>")] string name = "")
|
||||
{
|
||||
if (<#=item.Name#>.<#=item.Size#> == size)
|
||||
if (<#= item.Name #>.<#= item.Size #> == size)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Cast#><#=item.Name#>, size, name);
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(<#= item.Cast #><#= item.Name #>, size, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <#=item.XmlType#> instance must have a size not equal to a specified value.
|
||||
/// Asserts that the input <#= item.XmlType #> instance must have a size not equal to a specified value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The item of items in the input <#=item.XmlType#> instance.</typeparam>
|
||||
/// <param name="<#=item.Name#>">The input <#=item.XmlType#> instance to check the size for.</param>
|
||||
/// <typeparam name="T">The item of items in the input <#= item.XmlType #> instance.</typeparam>
|
||||
/// <param name="<#= item.Name #>">The input <#= item.XmlType #> instance to check the size for.</param>
|
||||
/// <param name="size">The target size to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="<#=item.Name#>"/> is == <paramref name="size"/>.</exception>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="<#= item.Name #>"/> is == <paramref name="size"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void HasSizeNotEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, [CallerArgumentExpression("<#=item.Name#>")] string name = "")
|
||||
public static void HasSizeNotEqualTo<T>(<#= item.Type #> <#= item.Name #>, int size, [CallerArgumentExpression("<#= item.Name #>")] string name = "")
|
||||
{
|
||||
if (<#=item.Name#>.<#=item.Size#> != size)
|
||||
if (<#= item.Name #>.<#= item.Size #> != size)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(<#=item.Cast#><#=item.Name#>, size, name);
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(<#= item.Cast #><#= item.Name #>, size, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <#=item.XmlType#> instance must have a size over a specified value.
|
||||
/// Asserts that the input <#= item.XmlType #> instance must have a size over a specified value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The item of items in the input <#=item.XmlType#> instance.</typeparam>
|
||||
/// <param name="<#=item.Name#>">The input <#=item.XmlType#> instance to check the size for.</param>
|
||||
/// <typeparam name="T">The item of items in the input <#= item.XmlType #> instance.</typeparam>
|
||||
/// <param name="<#= item.Name #>">The input <#= item.XmlType #> instance to check the size for.</param>
|
||||
/// <param name="size">The target size to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="<#=item.Name#>"/> is <= <paramref name="size"/>.</exception>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="<#= item.Name #>"/> is <= <paramref name="size"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void HasSizeGreaterThan<T>(<#=item.Type#> <#=item.Name#>, int size, [CallerArgumentExpression("<#=item.Name#>")] string name = "")
|
||||
public static void HasSizeGreaterThan<T>(<#= item.Type #> <#= item.Name #>, int size, [CallerArgumentExpression("<#= item.Name #>")] string name = "")
|
||||
{
|
||||
if (<#=item.Name#>.<#=item.Size#> > size)
|
||||
if (<#= item.Name #>.<#= item.Size #> > size)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThan(<#=item.Cast#><#=item.Name#>, size, name);
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThan(<#= item.Cast #><#= item.Name #>, size, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <#=item.XmlType#> instance must have a size of at least or equal to a specified value.
|
||||
/// Asserts that the input <#= item.XmlType #> instance must have a size of at least or equal to a specified value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The item of items in the input <#=item.XmlType#> instance.</typeparam>
|
||||
/// <param name="<#=item.Name#>">The input <#=item.XmlType#> instance to check the size for.</param>
|
||||
/// <typeparam name="T">The item of items in the input <#= item.XmlType #> instance.</typeparam>
|
||||
/// <param name="<#= item.Name #>">The input <#= item.XmlType #> instance to check the size for.</param>
|
||||
/// <param name="size">The target size to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="<#=item.Name#>"/> is < <paramref name="size"/>.</exception>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="<#= item.Name #>"/> is < <paramref name="size"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void HasSizeGreaterThanOrEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, [CallerArgumentExpression("<#=item.Name#>")] string name = "")
|
||||
public static void HasSizeGreaterThanOrEqualTo<T>(<#= item.Type #> <#= item.Name #>, int size, [CallerArgumentExpression("<#= item.Name #>")] string name = "")
|
||||
{
|
||||
if (<#=item.Name#>.<#=item.Size#> >= size)
|
||||
if (<#= item.Name #>.<#= item.Size #> >= size)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(<#=item.Cast#><#=item.Name#>, size, name);
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo(<#= item.Cast #><#= item.Name #>, size, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <#=item.XmlType#> instance must have a size of less than a specified value.
|
||||
/// Asserts that the input <#= item.XmlType #> instance must have a size of less than a specified value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The item of items in the input <#=item.XmlType#> instance.</typeparam>
|
||||
/// <param name="<#=item.Name#>">The input <#=item.XmlType#> instance to check the size for.</param>
|
||||
/// <typeparam name="T">The item of items in the input <#= item.XmlType #> instance.</typeparam>
|
||||
/// <param name="<#= item.Name #>">The input <#= item.XmlType #> instance to check the size for.</param>
|
||||
/// <param name="size">The target size to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="<#=item.Name#>"/> is >= <paramref name="size"/>.</exception>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="<#= item.Name #>"/> is >= <paramref name="size"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void HasSizeLessThan<T>(<#=item.Type#> <#=item.Name#>, int size, [CallerArgumentExpression("<#=item.Name#>")] string name = "")
|
||||
public static void HasSizeLessThan<T>(<#= item.Type #> <#= item.Name #>, int size, [CallerArgumentExpression("<#= item.Name #>")] string name = "")
|
||||
{
|
||||
if (<#=item.Name#>.<#=item.Size#> < size)
|
||||
if (<#= item.Name #>.<#= item.Size #> < size)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(<#=item.Cast#><#=item.Name#>, size, name);
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(<#= item.Cast #><#= item.Name #>, size, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input <#=item.XmlType#> instance must have a size of less than or equal to a specified value.
|
||||
/// Asserts that the input <#= item.XmlType #> instance must have a size of less than or equal to a specified value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The item of items in the input <#=item.XmlType#> instance.</typeparam>
|
||||
/// <param name="<#=item.Name#>">The input <#=item.XmlType#> instance to check the size for.</param>
|
||||
/// <typeparam name="T">The item of items in the input <#= item.XmlType #> instance.</typeparam>
|
||||
/// <param name="<#= item.Name #>">The input <#= item.XmlType #> instance to check the size for.</param>
|
||||
/// <param name="size">The target size to test.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="<#=item.Name#>"/> is > <paramref name="size"/>.</exception>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="<#= item.Name #>"/> is > <paramref name="size"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void HasSizeLessThanOrEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, [CallerArgumentExpression("<#=item.Name#>")] string name = "")
|
||||
public static void HasSizeLessThanOrEqualTo<T>(<#= item.Type #> <#= item.Name #>, int size, [CallerArgumentExpression("<#= item.Name #>")] string name = "")
|
||||
{
|
||||
if (<#=item.Name#>.<#=item.Size#> <= size)
|
||||
if (<#= item.Name #>.<#= item.Size #> <= size)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(<#=item.Cast#><#=item.Name#>, size, name);
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(<#= item.Cast #><#= item.Name #>, size, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the source <#=item.XmlType#> instance must have the same size of a destination <#=item.XmlType#> instance.
|
||||
/// Asserts that the source <#= item.XmlType #> instance must have the same size of a destination <#= item.XmlType #> instance.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The item of items in the input <#=item.XmlType#> instance.</typeparam>
|
||||
/// <param name="source">The source <#=item.XmlType#> instance to check the size for.</param>
|
||||
/// <param name="destination">The destination <#=item.XmlType#> instance to check the size for.</param>
|
||||
/// <typeparam name="T">The item of items in the input <#= item.XmlType #> instance.</typeparam>
|
||||
/// <param name="source">The source <#= item.XmlType #> instance to check the size for.</param>
|
||||
/// <param name="destination">The destination <#= item.XmlType #> instance to check the size for.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="source"/> is != the one of <paramref name="destination"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void HasSizeEqualTo<T>(<#=item.Type#> source, <#=item.DestinationType#> destination, [CallerArgumentExpression("source")] string name = "")
|
||||
public static void HasSizeEqualTo<T>(<#= item.Type #> source, <#= item.DestinationType #> destination, [CallerArgumentExpression("source")] string name = "")
|
||||
{
|
||||
if (source.<#=item.Size#> == destination.<#=item.Size#>)
|
||||
if (source.<#= item.Size #> == destination.<#= item.Size #>)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
<#
|
||||
if (item.HasCountProperty)
|
||||
{
|
||||
if (item.HasCountProperty)
|
||||
{
|
||||
#>
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Cast#>source, destination.<#=item.Size#>, name);
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(<#= item.Cast #>source, destination.<#= item.Size #>, name);
|
||||
<#
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
#>
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(source, <#=item.Cast#>destination, name);
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(source, <#= item.Cast #>destination, name);
|
||||
<#
|
||||
}
|
||||
}
|
||||
#>
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the source <#=item.XmlType#> instance must have a size of less than or equal to that of a destination <#=item.XmlType#> instance.
|
||||
/// Asserts that the source <#= item.XmlType #> instance must have a size of less than or equal to that of a destination <#= item.XmlType #> instance.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The item of items in the input <#=item.XmlType#> instance.</typeparam>
|
||||
/// <param name="source">The source <#=item.XmlType#> instance to check the size for.</param>
|
||||
/// <param name="destination">The destination <#=item.XmlType#> instance to check the size for.</param>
|
||||
/// <typeparam name="T">The item of items in the input <#= item.XmlType #> instance.</typeparam>
|
||||
/// <param name="source">The source <#= item.XmlType #> instance to check the size for.</param>
|
||||
/// <param name="destination">The destination <#= item.XmlType #> instance to check the size for.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if the size of <paramref name="source"/> is > the one of <paramref name="destination"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void HasSizeLessThanOrEqualTo<T>(<#=item.Type#> source, <#=item.DestinationType#> destination, [CallerArgumentExpression("source")] string name = "")
|
||||
public static void HasSizeLessThanOrEqualTo<T>(<#= item.Type #> source, <#= item.DestinationType #> destination, [CallerArgumentExpression("source")] string name = "")
|
||||
{
|
||||
if (source.<#=item.Size#> <= destination.<#=item.Size#>)
|
||||
if (source.<#= item.Size #> <= destination.<#= item.Size #>)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
<#
|
||||
if (item.HasCountProperty)
|
||||
{
|
||||
if (item.HasCountProperty)
|
||||
{
|
||||
#>
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Cast#>source, destination.<#=item.Size#>, name);
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(<#= item.Cast #>source, destination.<#= item.Size #>, name);
|
||||
<#
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
#>
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(source, <#=item.Cast#>destination, name);
|
||||
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(source, <#= item.Cast #>destination, name);
|
||||
<#
|
||||
}
|
||||
}
|
||||
#>
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input index is valid for a given <#=item.XmlType#> instance.
|
||||
/// Asserts that the input index is valid for a given <#= item.XmlType #> instance.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The item of items in the input <#=item.XmlType#> instance.</typeparam>
|
||||
/// <param name="index">The input index to be used to access <paramref name="<#=item.Name#>"/>.</param>
|
||||
/// <param name="<#=item.Name#>">The input <#=item.XmlType#> instance to use to validate <paramref name="index"/>.</param>
|
||||
/// <typeparam name="T">The item of items in the input <#= item.XmlType #> instance.</typeparam>
|
||||
/// <param name="index">The input index to be used to access <paramref name="<#= item.Name #>"/>.</param>
|
||||
/// <param name="<#= item.Name #>">The input <#= item.XmlType #> instance to use to validate <paramref name="index"/>.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="index"/> is not valid to access <paramref name="<#=item.Name#>"/>.</exception>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="index"/> is not valid to access <paramref name="<#= item.Name #>"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsInRangeFor<T>(int index, <#=item.Type#> <#=item.Name#>, [CallerArgumentExpression("index")] string name = "")
|
||||
public static void IsInRangeFor<T>(int index, <#= item.Type #> <#= item.Name #>, [CallerArgumentExpression("index")] string name = "")
|
||||
{
|
||||
<#
|
||||
// Here we're leveraging the fact that signed integers are represented
|
||||
// in 2-complement to perform the bounds check with a single compare operation.
|
||||
// This is the same trick used throughout CoreCLR as well.
|
||||
// For more info and code sample, see the original conversation here:
|
||||
// https://github.com/CommunityToolkit/WindowsCommunityToolkit/pull/3131#discussion_r390682835
|
||||
// Here we're leveraging the fact that signed integers are represented
|
||||
// in 2-complement to perform the bounds check with a single compare operation.
|
||||
// This is the same trick used throughout CoreCLR as well.
|
||||
// For more info and code sample, see the original conversation here:
|
||||
// https://github.com/CommunityToolkit/WindowsCommunityToolkit/pull/3131#discussion_r390682835
|
||||
#>
|
||||
if ((uint)index < (uint)<#=item.Name#>.<#=item.Size#>)
|
||||
if ((uint)index < (uint)<#= item.Name #>.<#= item.Size #>)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRangeFor(index, <#=item.Cast#><#=item.Name#>, name);
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRangeFor(index, <#= item.Cast #><#= item.Name #>, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the input index is not valid for a given <#=item.XmlType#> instance.
|
||||
/// Asserts that the input index is not valid for a given <#= item.XmlType #> instance.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The item of items in the input <#=item.XmlType#> instance.</typeparam>
|
||||
/// <param name="index">The input index to be used to access <paramref name="<#=item.Name#>"/>.</param>
|
||||
/// <param name="<#=item.Name#>">The input <#=item.XmlType#> instance to use to validate <paramref name="index"/>.</param>
|
||||
/// <typeparam name="T">The item of items in the input <#= item.XmlType #> instance.</typeparam>
|
||||
/// <param name="index">The input index to be used to access <paramref name="<#= item.Name #>"/>.</param>
|
||||
/// <param name="<#= item.Name #>">The input <#= item.XmlType #> instance to use to validate <paramref name="index"/>.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="index"/> is valid to access <paramref name="<#=item.Name#>"/>.</exception>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="index"/> is valid to access <paramref name="<#= item.Name #>"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotInRangeFor<T>(int index, <#=item.Type#> <#=item.Name#>, [CallerArgumentExpression("index")] string name = "")
|
||||
public static void IsNotInRangeFor<T>(int index, <#= item.Type #> <#= item.Name #>, [CallerArgumentExpression("index")] string name = "")
|
||||
{
|
||||
if ((uint)index >= (uint)<#=item.Name#>.<#=item.Size#>)
|
||||
if ((uint)index >= (uint)<#= item.Name #>.<#= item.Size #>)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(index, <#=item.Cast#><#=item.Name#>, name);
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(index, <#= item.Cast #><#= item.Name #>, name);
|
||||
}
|
||||
<#
|
||||
});
|
||||
});
|
||||
#>
|
||||
}
|
||||
|
||||
@@ -10,19 +10,19 @@ namespace PerformanceFish.Utility.Diagnostics;
|
||||
partial class Guard
|
||||
{
|
||||
<#
|
||||
GenerateTextForItems(NumericTypes, typeInfo =>
|
||||
{
|
||||
var (type, prefix) = typeInfo;
|
||||
GenerateTextForItems(NumericTypes, typeInfo =>
|
||||
{
|
||||
var (type, prefix) = typeInfo;
|
||||
#>
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be equal to a specified value.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see <#=prefix#>="<#=type#>"/> value to test.</param>
|
||||
/// <param name="target">The target <see <#=prefix#>="<#=type#>"/> value to test for.</param>
|
||||
/// <param name="value">The input <see <#= prefix #>="<#= type #>"/> value to test.</param>
|
||||
/// <param name="target">The target <see <#= prefix #>="<#= type #>"/> value to test for.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is != <paramref name="target"/>.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsEqualTo(<#=type#> value, <#=type#> target, [CallerArgumentExpression("value")] string name = "")
|
||||
public static void IsEqualTo(<#= type #> value, <#= type #> target, [CallerArgumentExpression("value")] string name = "")
|
||||
{
|
||||
if (value == target)
|
||||
{
|
||||
@@ -35,13 +35,13 @@ var (type, prefix) = typeInfo;
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be not equal to a specified value.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see <#=prefix#>="<#=type#>"/> value to test.</param>
|
||||
/// <param name="target">The target <see <#=prefix#>="<#=type#>"/> value to test for.</param>
|
||||
/// <param name="value">The input <see <#= prefix #>="<#= type #>"/> value to test.</param>
|
||||
/// <param name="target">The target <see <#= prefix #>="<#= type #>"/> value to test for.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is == <paramref name="target"/>.</exception>
|
||||
/// <remarks>The method is generic to avoid boxing the parameters, if they are value types.</remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotEqualTo(<#=type#> value, <#=type#> target, [CallerArgumentExpression("value")] string name = "")
|
||||
public static void IsNotEqualTo(<#= type #> value, <#= type #> target, [CallerArgumentExpression("value")] string name = "")
|
||||
{
|
||||
if (value != target)
|
||||
{
|
||||
@@ -54,13 +54,13 @@ var (type, prefix) = typeInfo;
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be less than a specified value.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see <#=prefix#>="<#=type#>"/> value to test.</param>
|
||||
/// <param name="maximum">The exclusive maximum <see <#=prefix#>="<#=type#>"/> value that is accepted.</param>
|
||||
/// <param name="value">The input <see <#= prefix #>="<#= type #>"/> value to test.</param>
|
||||
/// <param name="maximum">The exclusive maximum <see <#= prefix #>="<#= type #>"/> value that is accepted.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is >= <paramref name="maximum"/>.</exception>
|
||||
/// <remarks>The method is generic to avoid boxing the parameters, if they are value types.</remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsLessThan(<#=type#> value, <#=type#> maximum, [CallerArgumentExpression("value")] string name = "")
|
||||
public static void IsLessThan(<#= type #> value, <#= type #> maximum, [CallerArgumentExpression("value")] string name = "")
|
||||
{
|
||||
if (value < maximum)
|
||||
{
|
||||
@@ -73,13 +73,13 @@ var (type, prefix) = typeInfo;
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be less than or equal to a specified value.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see <#=prefix#>="<#=type#>"/> value to test.</param>
|
||||
/// <param name="maximum">The inclusive maximum <see <#=prefix#>="<#=type#>"/> value that is accepted.</param>
|
||||
/// <param name="value">The input <see <#= prefix #>="<#= type #>"/> value to test.</param>
|
||||
/// <param name="maximum">The inclusive maximum <see <#= prefix #>="<#= type #>"/> value that is accepted.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is > <paramref name="maximum"/>.</exception>
|
||||
/// <remarks>The method is generic to avoid boxing the parameters, if they are value types.</remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsLessThanOrEqualTo(<#=type#> value, <#=type#> maximum, [CallerArgumentExpression("value")] string name = "")
|
||||
public static void IsLessThanOrEqualTo(<#= type #> value, <#= type #> maximum, [CallerArgumentExpression("value")] string name = "")
|
||||
{
|
||||
if (value <= maximum)
|
||||
{
|
||||
@@ -92,13 +92,13 @@ var (type, prefix) = typeInfo;
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be greater than a specified value.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see <#=prefix#>="<#=type#>"/> value to test.</param>
|
||||
/// <param name="minimum">The exclusive minimum <see <#=prefix#>="<#=type#>"/> value that is accepted.</param>
|
||||
/// <param name="value">The input <see <#= prefix #>="<#= type #>"/> value to test.</param>
|
||||
/// <param name="minimum">The exclusive minimum <see <#= prefix #>="<#= type #>"/> value that is accepted.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is <= <paramref name="minimum"/>.</exception>
|
||||
/// <remarks>The method is generic to avoid boxing the parameters, if they are value types.</remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsGreaterThan(<#=type#> value, <#=type#> minimum, [CallerArgumentExpression("value")] string name = "")
|
||||
public static void IsGreaterThan(<#= type #> value, <#= type #> minimum, [CallerArgumentExpression("value")] string name = "")
|
||||
{
|
||||
if (value > minimum)
|
||||
{
|
||||
@@ -111,13 +111,13 @@ var (type, prefix) = typeInfo;
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be greater than or equal to a specified value.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see <#=prefix#>="<#=type#>"/> value to test.</param>
|
||||
/// <param name="minimum">The inclusive minimum <see <#=prefix#>="<#=type#>"/> value that is accepted.</param>
|
||||
/// <param name="value">The input <see <#= prefix #>="<#= type #>"/> value to test.</param>
|
||||
/// <param name="minimum">The inclusive minimum <see <#= prefix #>="<#= type #>"/> value that is accepted.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is < <paramref name="minimum"/>.</exception>
|
||||
/// <remarks>The method is generic to avoid boxing the parameters, if they are value types.</remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsGreaterThanOrEqualTo(<#=type#> value, <#=type#> minimum, [CallerArgumentExpression("value")] string name = "")
|
||||
public static void IsGreaterThanOrEqualTo(<#= type #> value, <#= type #> minimum, [CallerArgumentExpression("value")] string name = "")
|
||||
{
|
||||
if (value >= minimum)
|
||||
{
|
||||
@@ -130,9 +130,9 @@ var (type, prefix) = typeInfo;
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be in a given range.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see <#=prefix#>="<#=type#>"/> value to test.</param>
|
||||
/// <param name="minimum">The inclusive minimum <see <#=prefix#>="<#=type#>"/> value that is accepted.</param>
|
||||
/// <param name="maximum">The exclusive maximum <see <#=prefix#>="<#=type#>"/> value that is accepted.</param>
|
||||
/// <param name="value">The input <see <#= prefix #>="<#= type #>"/> value to test.</param>
|
||||
/// <param name="minimum">The inclusive minimum <see <#= prefix #>="<#= type #>"/> value that is accepted.</param>
|
||||
/// <param name="maximum">The exclusive maximum <see <#= prefix #>="<#= type #>"/> value that is accepted.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is < <paramref name="minimum"/> or >= <paramref name="maximum"/>.</exception>
|
||||
/// <remarks>
|
||||
@@ -140,7 +140,7 @@ var (type, prefix) = typeInfo;
|
||||
/// The method is generic to avoid boxing the parameters, if they are value types.
|
||||
/// </remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsInRange(<#=type#> value, <#=type#> minimum, <#=type#> maximum, [CallerArgumentExpression("value")] string name = "")
|
||||
public static void IsInRange(<#= type #> value, <#= type #> minimum, <#= type #> maximum, [CallerArgumentExpression("value")] string name = "")
|
||||
{
|
||||
if (value >= minimum && value < maximum)
|
||||
{
|
||||
@@ -153,9 +153,9 @@ var (type, prefix) = typeInfo;
|
||||
/// <summary>
|
||||
/// Asserts that the input value must not be in a given range.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see <#=prefix#>="<#=type#>"/> value to test.</param>
|
||||
/// <param name="minimum">The inclusive minimum <see <#=prefix#>="<#=type#>"/> value that is accepted.</param>
|
||||
/// <param name="maximum">The exclusive maximum <see <#=prefix#>="<#=type#>"/> value that is accepted.</param>
|
||||
/// <param name="value">The input <see <#= prefix #>="<#= type #>"/> value to test.</param>
|
||||
/// <param name="minimum">The inclusive minimum <see <#= prefix #>="<#= type #>"/> value that is accepted.</param>
|
||||
/// <param name="maximum">The exclusive maximum <see <#= prefix #>="<#= type #>"/> value that is accepted.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is >= <paramref name="minimum"/> or < <paramref name="maximum"/>.</exception>
|
||||
/// <remarks>
|
||||
@@ -163,7 +163,7 @@ var (type, prefix) = typeInfo;
|
||||
/// The method is generic to avoid boxing the parameters, if they are value types.
|
||||
/// </remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotInRange(<#=type#> value, <#=type#> minimum, <#=type#> maximum, [CallerArgumentExpression("value")] string name = "")
|
||||
public static void IsNotInRange(<#= type #> value, <#= type #> minimum, <#= type #> maximum, [CallerArgumentExpression("value")] string name = "")
|
||||
{
|
||||
if (value < minimum || value >= maximum)
|
||||
{
|
||||
@@ -176,9 +176,9 @@ var (type, prefix) = typeInfo;
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be in a given interval.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see <#=prefix#>="<#=type#>"/> value to test.</param>
|
||||
/// <param name="minimum">The exclusive minimum <see <#=prefix#>="<#=type#>"/> value that is accepted.</param>
|
||||
/// <param name="maximum">The exclusive maximum <see <#=prefix#>="<#=type#>"/> value that is accepted.</param>
|
||||
/// <param name="value">The input <see <#= prefix #>="<#= type #>"/> value to test.</param>
|
||||
/// <param name="minimum">The exclusive minimum <see <#= prefix #>="<#= type #>"/> value that is accepted.</param>
|
||||
/// <param name="maximum">The exclusive maximum <see <#= prefix #>="<#= type #>"/> value that is accepted.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is <= <paramref name="minimum"/> or >= <paramref name="maximum"/>.</exception>
|
||||
/// <remarks>
|
||||
@@ -186,7 +186,7 @@ var (type, prefix) = typeInfo;
|
||||
/// The method is generic to avoid boxing the parameters, if they are value types.
|
||||
/// </remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsBetween(<#=type#> value, <#=type#> minimum, <#=type#> maximum, [CallerArgumentExpression("value")] string name = "")
|
||||
public static void IsBetween(<#= type #> value, <#= type #> minimum, <#= type #> maximum, [CallerArgumentExpression("value")] string name = "")
|
||||
{
|
||||
if (value > minimum && value < maximum)
|
||||
{
|
||||
@@ -199,9 +199,9 @@ var (type, prefix) = typeInfo;
|
||||
/// <summary>
|
||||
/// Asserts that the input value must not be in a given interval.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see <#=prefix#>="<#=type#>"/> value to test.</param>
|
||||
/// <param name="minimum">The exclusive minimum <see <#=prefix#>="<#=type#>"/> value that is accepted.</param>
|
||||
/// <param name="maximum">The exclusive maximum <see <#=prefix#>="<#=type#>"/> value that is accepted.</param>
|
||||
/// <param name="value">The input <see <#= prefix #>="<#= type #>"/> value to test.</param>
|
||||
/// <param name="minimum">The exclusive minimum <see <#= prefix #>="<#= type #>"/> value that is accepted.</param>
|
||||
/// <param name="maximum">The exclusive maximum <see <#= prefix #>="<#= type #>"/> value that is accepted.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is > <paramref name="minimum"/> or < <paramref name="maximum"/>.</exception>
|
||||
/// <remarks>
|
||||
@@ -209,7 +209,7 @@ var (type, prefix) = typeInfo;
|
||||
/// The method is generic to avoid boxing the parameters, if they are value types.
|
||||
/// </remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotBetween(<#=type#> value, <#=type#> minimum, <#=type#> maximum, [CallerArgumentExpression("value")] string name = "")
|
||||
public static void IsNotBetween(<#= type #> value, <#= type #> minimum, <#= type #> maximum, [CallerArgumentExpression("value")] string name = "")
|
||||
{
|
||||
if (value <= minimum || value >= maximum)
|
||||
{
|
||||
@@ -222,9 +222,9 @@ var (type, prefix) = typeInfo;
|
||||
/// <summary>
|
||||
/// Asserts that the input value must be in a given interval.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see <#=prefix#>="<#=type#>"/> value to test.</param>
|
||||
/// <param name="minimum">The inclusive minimum <see <#=prefix#>="<#=type#>"/> value that is accepted.</param>
|
||||
/// <param name="maximum">The inclusive maximum <see <#=prefix#>="<#=type#>"/> value that is accepted.</param>
|
||||
/// <param name="value">The input <see <#= prefix #>="<#= type #>"/> value to test.</param>
|
||||
/// <param name="minimum">The inclusive minimum <see <#= prefix #>="<#= type #>"/> value that is accepted.</param>
|
||||
/// <param name="maximum">The inclusive maximum <see <#= prefix #>="<#= type #>"/> value that is accepted.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is < <paramref name="minimum"/> or > <paramref name="maximum"/>.</exception>
|
||||
/// <remarks>
|
||||
@@ -232,7 +232,7 @@ var (type, prefix) = typeInfo;
|
||||
/// The method is generic to avoid boxing the parameters, if they are value types.
|
||||
/// </remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsBetweenOrEqualTo(<#=type#> value, <#=type#> minimum, <#=type#> maximum, [CallerArgumentExpression("value")] string name = "")
|
||||
public static void IsBetweenOrEqualTo(<#= type #> value, <#= type #> minimum, <#= type #> maximum, [CallerArgumentExpression("value")] string name = "")
|
||||
{
|
||||
if (value >= minimum && value <= maximum)
|
||||
{
|
||||
@@ -245,9 +245,9 @@ var (type, prefix) = typeInfo;
|
||||
/// <summary>
|
||||
/// Asserts that the input value must not be in a given interval.
|
||||
/// </summary>
|
||||
/// <param name="value">The input <see <#=prefix#>="<#=type#>"/> value to test.</param>
|
||||
/// <param name="minimum">The inclusive minimum <see <#=prefix#>="<#=type#>"/> value that is accepted.</param>
|
||||
/// <param name="maximum">The inclusive maximum <see <#=prefix#>="<#=type#>"/> value that is accepted.</param>
|
||||
/// <param name="value">The input <see <#= prefix #>="<#= type #>"/> value to test.</param>
|
||||
/// <param name="minimum">The inclusive minimum <see <#= prefix #>="<#= type #>"/> value that is accepted.</param>
|
||||
/// <param name="maximum">The inclusive maximum <see <#= prefix #>="<#= type #>"/> value that is accepted.</param>
|
||||
/// <param name="name">The name of the input parameter being tested.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is >= <paramref name="minimum"/> or <= <paramref name="maximum"/>.</exception>
|
||||
/// <remarks>
|
||||
@@ -255,7 +255,7 @@ var (type, prefix) = typeInfo;
|
||||
/// The method is generic to avoid boxing the parameters, if they are value types.
|
||||
/// </remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void IsNotBetweenOrEqualTo(<#=type#> value, <#=type#> minimum, <#=type#> maximum, [CallerArgumentExpression("value")] string name = "")
|
||||
public static void IsNotBetweenOrEqualTo(<#= type #> value, <#= type #> minimum, <#= type #> maximum, [CallerArgumentExpression("value")] string name = "")
|
||||
{
|
||||
if (value < minimum || value > maximum)
|
||||
{
|
||||
@@ -265,6 +265,6 @@ var (type, prefix) = typeInfo;
|
||||
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotBetweenOrEqualTo(value, minimum, maximum, name);
|
||||
}
|
||||
<#
|
||||
});
|
||||
});
|
||||
#>
|
||||
}
|
||||
|
||||
@@ -1,17 +1,29 @@
|
||||
# T4 templates and generated APIs
|
||||
|
||||
This folder contains a number of template files (with the `.tt` or `.ttinclude` extensions) and the generated `.g.cs` files generated by those templates. The template files use the T4 format, which is natively supported by Visual Studio (more info is available [here](https://docs.microsoft.com/visualstudio/modeling/code-generation-and-t4-text-templates)).
|
||||
This folder contains a number of template files (with the `.tt` or `.ttinclude` extensions) and the generated `.g.cs`
|
||||
files generated by those templates. The template files use the T4 format, which is natively supported by Visual Studio (
|
||||
more info is available [here](https://docs.microsoft.com/visualstudio/modeling/code-generation-and-t4-text-templates)).
|
||||
|
||||
## Why is this needed?
|
||||
|
||||
There are a few reasons why T4 templates are used for the `Guard` class:
|
||||
|
||||
- Especially when many overloads are available for an API, using templates makes it much easier to maintain code and spot mistakes, as the actual number of lines of code to review is much smaller: it's just the same code from each template!
|
||||
- Using type-specific overloads instead of generic methods can result in faster code. For instance, T4 templates are used to generate overloads for comparison APIs (eg. `Guard.IsGreaterThan(int, int, string)`). This results in more compact and optimized code as opposed to a generic method using `where T : IComparable<T>` as type constraint.
|
||||
- In some cases, using generic methods just isn't possible. For instance, types like `Span<T>` and `ReadOnlySpan<T>` can't be used as generic type parameters, and even if that had been possible, they don't implement an interface we could use in the generic type constraint. Using T4 templates solves this issue, as we can just have specialized method for each supported type or collection type.
|
||||
- Especially when many overloads are available for an API, using templates makes it much easier to maintain code and
|
||||
spot mistakes, as the actual number of lines of code to review is much smaller: it's just the same code from each
|
||||
template!
|
||||
- Using type-specific overloads instead of generic methods can result in faster code. For instance, T4 templates are
|
||||
used to generate overloads for comparison APIs (eg. `Guard.IsGreaterThan(int, int, string)`). This results in more
|
||||
compact and optimized code as opposed to a generic method using `where T : IComparable<T>` as type constraint.
|
||||
- In some cases, using generic methods just isn't possible. For instance, types like `Span<T>` and `ReadOnlySpan<T>`
|
||||
can't be used as generic type parameters, and even if that had been possible, they don't implement an interface we
|
||||
could use in the generic type constraint. Using T4 templates solves this issue, as we can just have specialized method
|
||||
for each supported type or collection type.
|
||||
|
||||
## How to make changes
|
||||
|
||||
If you need to change an API that is declared in a template, or to add a new one, just edit the right `.tt` file and save it: Visual Studio will take care of updating the generated `.g.cs` file automatically. Don't make changes to those generated `.g.cs` files directly, as those will be overwritten as soon as their source template is updated.
|
||||
If you need to change an API that is declared in a template, or to add a new one, just edit the right `.tt` file and
|
||||
save it: Visual Studio will take care of updating the generated `.g.cs` file automatically. Don't make changes to those
|
||||
generated `.g.cs` files directly, as those will be overwritten as soon as their source template is updated.
|
||||
|
||||
Note that all the `.g.cs` files are checked in into the repository, so if you do make changes to a template file, make sure to also include the updated `.g.cs` file in your commits.
|
||||
Note that all the `.g.cs` files are checked in into the repository, so if you do make changes to a template file, make
|
||||
sure to also include the updated `.g.cs` file in your commits.
|
||||
|
||||
@@ -13,109 +13,109 @@ partial class Guard
|
||||
partial class ThrowHelper
|
||||
{
|
||||
<#
|
||||
GenerateTextForItems(EnumerableTypes, item =>
|
||||
{
|
||||
GenerateTextForItems(EnumerableTypes, item =>
|
||||
{
|
||||
#>
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsEmpty{T}(T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[DoesNotReturn]
|
||||
public static void ThrowArgumentExceptionForIsEmpty<T>(<#=item.Type#> <#=item.Name#>, string name)
|
||||
public static void ThrowArgumentExceptionForIsEmpty<T>(<#= item.Type #> <#= item.Name #>, string name)
|
||||
{
|
||||
throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(<#=item.Type#>).ToTypeString()}) must be empty, had a size of {AssertString(<#=item.Name#>.<#=item.Size#>)}.", name);
|
||||
throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(<#= item.Type #>).ToTypeString()}) must be empty, had a size of {AssertString(<#= item.Name #>.<#= item.Size #>)}.", name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeEqualTo{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[DoesNotReturn]
|
||||
public static void ThrowArgumentExceptionForHasSizeEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
|
||||
public static void ThrowArgumentExceptionForHasSizeEqualTo<T>(<#= item.Type #> <#= item.Name #>, int size, string name)
|
||||
{
|
||||
throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size equal to {size}, had a size of {AssertString(<#=item.Name#>.<#=item.Size#>)}.", name);
|
||||
throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(<#= item.Type #>).ToTypeString()}) must have a size equal to {size}, had a size of {AssertString(<#= item.Name #>.<#= item.Size #>)}.", name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeNotEqualTo{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[DoesNotReturn]
|
||||
public static void ThrowArgumentExceptionForHasSizeNotEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
|
||||
public static void ThrowArgumentExceptionForHasSizeNotEqualTo<T>(<#= item.Type #> <#= item.Name #>, int size, string name)
|
||||
{
|
||||
throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size not equal to {size}, had a size of {AssertString(<#=item.Name#>.<#=item.Size#>)}.", name);
|
||||
throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(<#= item.Type #>).ToTypeString()}) must have a size not equal to {size}, had a size of {AssertString(<#= item.Name #>.<#= item.Size #>)}.", name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeGreaterThan{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[DoesNotReturn]
|
||||
public static void ThrowArgumentExceptionForHasSizeGreaterThan<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
|
||||
public static void ThrowArgumentExceptionForHasSizeGreaterThan<T>(<#= item.Type #> <#= item.Name #>, int size, string name)
|
||||
{
|
||||
throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size over {size}, had a size of {AssertString(<#=item.Name#>.<#=item.Size#>)}.", name);
|
||||
throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(<#= item.Type #>).ToTypeString()}) must have a size over {size}, had a size of {AssertString(<#= item.Name #>.<#= item.Size #>)}.", name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeGreaterThanOrEqualTo{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[DoesNotReturn]
|
||||
public static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
|
||||
public static void ThrowArgumentExceptionForHasSizeGreaterThanOrEqualTo<T>(<#= item.Type #> <#= item.Name #>, int size, string name)
|
||||
{
|
||||
throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size of at least {size}, had a size of {AssertString(<#=item.Name#>.<#=item.Size#>)}.", name);
|
||||
throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(<#= item.Type #>).ToTypeString()}) must have a size of at least {size}, had a size of {AssertString(<#= item.Name #>.<#= item.Size #>)}.", name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThan{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[DoesNotReturn]
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThan<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThan<T>(<#= item.Type #> <#= item.Name #>, int size, string name)
|
||||
{
|
||||
throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size less than {size}, had a size of {AssertString(<#=item.Name#>.<#=item.Size#>)}.", name);
|
||||
throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(<#= item.Type #>).ToTypeString()}) must have a size less than {size}, had a size of {AssertString(<#= item.Name #>.<#= item.Size #>)}.", name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThanOrEqualTo{T}(T[],int,string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[DoesNotReturn]
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo<T>(<#= item.Type #> <#= item.Name #>, int size, string name)
|
||||
{
|
||||
throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size less than or equal to {size}, had a size of {AssertString(<#=item.Name#>.<#=item.Size#>)}.", name);
|
||||
throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(<#= item.Type #>).ToTypeString()}) must have a size less than or equal to {size}, had a size of {AssertString(<#= item.Name #>.<#= item.Size #>)}.", name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeEqualTo{T}(T[],T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[DoesNotReturn]
|
||||
public static void ThrowArgumentExceptionForHasSizeEqualTo<T>(<#=item.Type#> source, <#=item.DestinationType#> destination, string name)
|
||||
public static void ThrowArgumentExceptionForHasSizeEqualTo<T>(<#= item.Type #> source, <#= item.DestinationType #> destination, string name)
|
||||
{
|
||||
throw new ArgumentException($"The source {AssertString(name)} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size equal to {AssertString(destination.<#=item.Size#>)} (the destination), had a size of {AssertString(source.<#=item.Size#>)}.", name);
|
||||
throw new ArgumentException($"The source {AssertString(name)} ({typeof(<#= item.Type #>).ToTypeString()}) must have a size equal to {AssertString(destination.<#= item.Size #>)} (the destination), had a size of {AssertString(source.<#= item.Size #>)}.", name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.HasSizeLessThanOrEqualTo{T}(T[],T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[DoesNotReturn]
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo<T>(<#=item.Type#> source, <#=item.DestinationType#> destination, string name)
|
||||
public static void ThrowArgumentExceptionForHasSizeLessThanOrEqualTo<T>(<#= item.Type #> source, <#= item.DestinationType #> destination, string name)
|
||||
{
|
||||
throw new ArgumentException($"The source {AssertString(name)} ({typeof(<#=item.Type#>).ToTypeString()}) must have a size less than or equal to {AssertString(destination.<#=item.Size#>)} (the destination), had a size of {AssertString(source.<#=item.Size#>)}.", name);
|
||||
throw new ArgumentException($"The source {AssertString(name)} ({typeof(<#= item.Type #>).ToTypeString()}) must have a size less than or equal to {AssertString(destination.<#= item.Size #>)} (the destination), had a size of {AssertString(source.<#= item.Size #>)}.", name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsInRangeFor{T}(int,T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[DoesNotReturn]
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor<T>(int index, <#=item.Type#> <#=item.Name#>, string name)
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIsInRangeFor<T>(int index, <#= item.Type #> <#= item.Name #>, string name)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(name, index, $"Parameter {AssertString(name)} (int) must be in the range given by <0> and {AssertString(<#=item.Name#>.<#=item.Size#>)} to be a valid index for the target collection ({typeof(<#=item.Type#>).ToTypeString()}), was {AssertString(index)}.");
|
||||
throw new ArgumentOutOfRangeException(name, index, $"Parameter {AssertString(name)} (int) must be in the range given by <0> and {AssertString(<#= item.Name #>.<#= item.Size #>)} to be a valid index for the target collection ({typeof(<#= item.Type #>).ToTypeString()}), was {AssertString(index)}.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentOutOfRangeException"/> when <see cref="Guard.IsNotInRangeFor{T}(int,T[],string)"/> (or an overload) fails.
|
||||
/// </summary>
|
||||
[DoesNotReturn]
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor<T>(int index, <#=item.Type#> <#=item.Name#>, string name)
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor<T>(int index, <#= item.Type #> <#= item.Name #>, string name)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(name, index, $"Parameter {AssertString(name)} (int) must not be in the range given by <0> and {AssertString(<#=item.Name#>.<#=item.Size#>)} to be an invalid index for the target collection ({typeof(<#=item.Type#>).ToTypeString()}), was {AssertString(index)}.");
|
||||
throw new ArgumentOutOfRangeException(name, index, $"Parameter {AssertString(name)} (int) must not be in the range given by <0> and {AssertString(<#= item.Name #>.<#= item.Size #>)} to be an invalid index for the target collection ({typeof(<#= item.Type #>).ToTypeString()}), was {AssertString(index)}.");
|
||||
}
|
||||
<#
|
||||
});
|
||||
});
|
||||
#>
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,11 +71,14 @@
|
||||
new EnumerableTypeInfo("Span<T>", "<see cref=\"Span{T}\"/>", "span", "Length", "Span<T>", ""),
|
||||
new EnumerableTypeInfo("ReadOnlySpan<T>", "<see cref=\"ReadOnlySpan{T}\"/>", "span", "Length", "Span<T>", ""),
|
||||
new EnumerableTypeInfo("Memory<T>", "<see cref=\"Memory{T}\"/>", "memory", "Length", "Memory<T>", ""),
|
||||
new EnumerableTypeInfo("ReadOnlyMemory<T>", "<see cref=\"ReadOnlyMemory{T}\"/>", "memory", "Length", "Memory<T>", ""),
|
||||
new EnumerableTypeInfo("ReadOnlyMemory<T>", "<see cref=\"ReadOnlyMemory{T}\"/>", "memory", "Length", "Memory<T>",
|
||||
""),
|
||||
new EnumerableTypeInfo("T[]", "<see typeparamref=\"T\"/> array", "array", "Length", "T[]", ""),
|
||||
new EnumerableTypeInfo("List<T>", "<see cref=\"List{T}\"/>", "list", "Count", "List<T>", "(ICollection<T>)"),
|
||||
new EnumerableTypeInfo("ICollection<T>", "<see cref=\"ICollection{T}\"/>", "collection", "Count", "ICollection<T>", ""),
|
||||
new EnumerableTypeInfo("IReadOnlyCollection<T>", "<see cref=\"IReadOnlyCollection{T}\"/>", "collection", "Count", "ICollection<T>", ""),
|
||||
new EnumerableTypeInfo("ICollection<T>", "<see cref=\"ICollection{T}\"/>", "collection", "Count",
|
||||
"ICollection<T>", ""),
|
||||
new EnumerableTypeInfo("IReadOnlyCollection<T>", "<see cref=\"IReadOnlyCollection{T}\"/>", "collection", "Count",
|
||||
"ICollection<T>", "")
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
@@ -107,10 +110,11 @@
|
||||
for (int i = 0; i < items.Count; i++)
|
||||
{
|
||||
// Insert a blank line after the first item
|
||||
if (i > 0) WriteLine("");
|
||||
if (i > 0)
|
||||
WriteLine("");
|
||||
|
||||
// Invoke the factory with the current item
|
||||
factory(items [i]);
|
||||
factory(items[i]);
|
||||
}
|
||||
}
|
||||
#>
|
||||
Reference in New Issue
Block a user