// 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 { /// /// Helper methods to verify conditions when running code. /// public static partial class Guard { <# GenerateTextForItems(NumericTypes, typeInfo => { var (type, prefix) = typeInfo; #> /// /// Asserts that the input value must be equal to a specified value. /// /// The input ="<#=type#>"/> value to test. /// The target ="<#=type#>"/> value to test for. /// The name of the input parameter being tested. /// Thrown if is != . [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsEqualTo(<#=type#> value, <#=type#> target, [CallerArgumentExpression("value")] string name = "") { if (value == target) { return; } ThrowHelper.ThrowArgumentExceptionForIsEqualTo(value, target, name); } /// /// Asserts that the input value must be not equal to a specified value. /// /// The input ="<#=type#>"/> value to test. /// The target ="<#=type#>"/> value to test for. /// The name of the input parameter being tested. /// Thrown if is == . /// The method is generic to avoid boxing the parameters, if they are value types. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsNotEqualTo(<#=type#> value, <#=type#> target, [CallerArgumentExpression("value")] string name = "") { if (value != target) { return; } ThrowHelper.ThrowArgumentExceptionForIsNotEqualTo(value, target, name); } /// /// Asserts that the input value must be less than a specified value. /// /// The input ="<#=type#>"/> value to test. /// The exclusive maximum ="<#=type#>"/> value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= . /// The method is generic to avoid boxing the parameters, if they are value types. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsLessThan(<#=type#> value, <#=type#> maximum, [CallerArgumentExpression("value")] string name = "") { if (value < maximum) { return; } ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsLessThan(value, maximum, name); } /// /// Asserts that the input value must be less than or equal to a specified value. /// /// The input ="<#=type#>"/> value to test. /// The inclusive maximum ="<#=type#>"/> value that is accepted. /// The name of the input parameter being tested. /// Thrown if is > . /// The method is generic to avoid boxing the parameters, if they are value types. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsLessThanOrEqualTo(<#=type#> value, <#=type#> maximum, [CallerArgumentExpression("value")] string name = "") { if (value <= maximum) { return; } ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsLessThanOrEqualTo(value, maximum, name); } /// /// Asserts that the input value must be greater than a specified value. /// /// The input ="<#=type#>"/> value to test. /// The exclusive minimum ="<#=type#>"/> value that is accepted. /// The name of the input parameter being tested. /// Thrown if is <= . /// The method is generic to avoid boxing the parameters, if they are value types. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsGreaterThan(<#=type#> value, <#=type#> minimum, [CallerArgumentExpression("value")] string name = "") { if (value > minimum) { return; } ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsGreaterThan(value, minimum, name); } /// /// Asserts that the input value must be greater than or equal to a specified value. /// /// The input ="<#=type#>"/> value to test. /// The inclusive minimum ="<#=type#>"/> value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < . /// The method is generic to avoid boxing the parameters, if they are value types. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsGreaterThanOrEqualTo(<#=type#> value, <#=type#> minimum, [CallerArgumentExpression("value")] string name = "") { if (value >= minimum) { return; } ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsGreaterThanOrEqualTo(value, minimum, name); } /// /// Asserts that the input value must be in a given range. /// /// The input ="<#=type#>"/> value to test. /// The inclusive minimum ="<#=type#>"/> value that is accepted. /// The exclusive maximum ="<#=type#>"/> value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < or >= . /// /// This API asserts the equivalent of " in [, )", using arithmetic notation. /// The method is generic to avoid boxing the parameters, if they are value types. /// [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); } /// /// Asserts that the input value must not be in a given range. /// /// The input ="<#=type#>"/> value to test. /// The inclusive minimum ="<#=type#>"/> value that is accepted. /// The exclusive maximum ="<#=type#>"/> value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= or < . /// /// This API asserts the equivalent of " not in [, )", using arithmetic notation. /// The method is generic to avoid boxing the parameters, if they are value types. /// [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); } /// /// Asserts that the input value must be in a given interval. /// /// The input ="<#=type#>"/> value to test. /// The exclusive minimum ="<#=type#>"/> value that is accepted. /// The exclusive maximum ="<#=type#>"/> value that is accepted. /// The name of the input parameter being tested. /// Thrown if is <= or >= . /// /// This API asserts the equivalent of " in (, )", using arithmetic notation. /// The method is generic to avoid boxing the parameters, if they are value types. /// [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); } /// /// Asserts that the input value must not be in a given interval. /// /// The input ="<#=type#>"/> value to test. /// The exclusive minimum ="<#=type#>"/> value that is accepted. /// The exclusive maximum ="<#=type#>"/> value that is accepted. /// The name of the input parameter being tested. /// Thrown if is > or < . /// /// This API asserts the equivalent of " not in (, )", using arithmetic notation. /// The method is generic to avoid boxing the parameters, if they are value types. /// [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); } /// /// Asserts that the input value must be in a given interval. /// /// The input ="<#=type#>"/> value to test. /// The inclusive minimum ="<#=type#>"/> value that is accepted. /// The inclusive maximum ="<#=type#>"/> value that is accepted. /// The name of the input parameter being tested. /// Thrown if is < or > . /// /// This API asserts the equivalent of " in [, ]", using arithmetic notation. /// The method is generic to avoid boxing the parameters, if they are value types. /// [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); } /// /// Asserts that the input value must not be in a given interval. /// /// The input ="<#=type#>"/> value to test. /// The inclusive minimum ="<#=type#>"/> value that is accepted. /// The inclusive maximum ="<#=type#>"/> value that is accepted. /// The name of the input parameter being tested. /// Thrown if is >= or <= . /// /// This API asserts the equivalent of " not in [, ]", using arithmetic notation. /// The method is generic to avoid boxing the parameters, if they are value types. /// [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); } <# }); #> } }