mirror of
https://github.com/chylex/.NET-Community-Toolkit.git
synced 2024-11-25 01:42:46 +01:00
272 lines
14 KiB
Plaintext
272 lines
14 KiB
Plaintext
// Licensed to the .NET Foundation under one or more agreements.
|
|
// The .NET Foundation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
<#@include file="TypeInfo.ttinclude" #>
|
|
using System;
|
|
using System.Runtime.CompilerServices;
|
|
|
|
namespace CommunityToolkit.Diagnostics;
|
|
|
|
/// <inheritdoc/>
|
|
partial class Guard
|
|
{
|
|
<#
|
|
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="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 = "")
|
|
{
|
|
if (value == target)
|
|
{
|
|
return;
|
|
}
|
|
|
|
ThrowHelper.ThrowArgumentExceptionForIsEqualTo(value, target, name);
|
|
}
|
|
|
|
/// <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="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 = "")
|
|
{
|
|
if (value != target)
|
|
{
|
|
return;
|
|
}
|
|
|
|
ThrowHelper.ThrowArgumentExceptionForIsNotEqualTo(value, target, name);
|
|
}
|
|
|
|
/// <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="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 = "")
|
|
{
|
|
if (value < maximum)
|
|
{
|
|
return;
|
|
}
|
|
|
|
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsLessThan(value, maximum, name);
|
|
}
|
|
|
|
/// <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="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 = "")
|
|
{
|
|
if (value <= maximum)
|
|
{
|
|
return;
|
|
}
|
|
|
|
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsLessThanOrEqualTo(value, maximum, name);
|
|
}
|
|
|
|
/// <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="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 = "")
|
|
{
|
|
if (value > minimum)
|
|
{
|
|
return;
|
|
}
|
|
|
|
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsGreaterThan(value, minimum, name);
|
|
}
|
|
|
|
/// <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="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 = "")
|
|
{
|
|
if (value >= minimum)
|
|
{
|
|
return;
|
|
}
|
|
|
|
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsGreaterThanOrEqualTo(value, minimum, name);
|
|
}
|
|
|
|
/// <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="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>
|
|
/// This API asserts the equivalent of "<paramref name="value"/> in [<paramref name="minimum"/>, <paramref name="maximum"/>)", using arithmetic notation.
|
|
/// 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 = "")
|
|
{
|
|
if (value >= minimum && value < maximum)
|
|
{
|
|
return;
|
|
}
|
|
|
|
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRange(value, minimum, maximum, name);
|
|
}
|
|
|
|
/// <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="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>
|
|
/// This API asserts the equivalent of "<paramref name="value"/> not in [<paramref name="minimum"/>, <paramref name="maximum"/>)", using arithmetic notation.
|
|
/// 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 = "")
|
|
{
|
|
if (value < minimum || value >= maximum)
|
|
{
|
|
return;
|
|
}
|
|
|
|
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotInRange(value, minimum, maximum, name);
|
|
}
|
|
|
|
/// <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="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>
|
|
/// This API asserts the equivalent of "<paramref name="value"/> in (<paramref name="minimum"/>, <paramref name="maximum"/>)", using arithmetic notation.
|
|
/// 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 = "")
|
|
{
|
|
if (value > minimum && value < maximum)
|
|
{
|
|
return;
|
|
}
|
|
|
|
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsBetween(value, minimum, maximum, name);
|
|
}
|
|
|
|
/// <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="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>
|
|
/// This API asserts the equivalent of "<paramref name="value"/> not in (<paramref name="minimum"/>, <paramref name="maximum"/>)", using arithmetic notation.
|
|
/// 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 = "")
|
|
{
|
|
if (value <= minimum || value >= maximum)
|
|
{
|
|
return;
|
|
}
|
|
|
|
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotBetween(value, minimum, maximum, name);
|
|
}
|
|
|
|
/// <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="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>
|
|
/// This API asserts the equivalent of "<paramref name="value"/> in [<paramref name="minimum"/>, <paramref name="maximum"/>]", using arithmetic notation.
|
|
/// 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 = "")
|
|
{
|
|
if (value >= minimum && value <= maximum)
|
|
{
|
|
return;
|
|
}
|
|
|
|
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsBetweenOrEqualTo(value, minimum, maximum, name);
|
|
}
|
|
|
|
/// <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="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>
|
|
/// This API asserts the equivalent of "<paramref name="value"/> not in [<paramref name="minimum"/>, <paramref name="maximum"/>]", using arithmetic notation.
|
|
/// 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 = "")
|
|
{
|
|
if (value < minimum || value > maximum)
|
|
{
|
|
return;
|
|
}
|
|
|
|
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotBetweenOrEqualTo(value, minimum, maximum, name);
|
|
}
|
|
<#
|
|
});
|
|
#>
|
|
}
|