mirror of
https://github.com/CommunityToolkit/dotnet.git
synced 2026-10-21 22:56:44 +08:00
8acc8b7308
* Initial guard APIs added * Added EqualTo and NotEqualTo APIs * Added more Guard APIs * New interval Guard APIs added * Added Guard APIs for IEnumerable<T> values * Renamed APIs * Added Guard APIs for Stream values * Added Guard APIs for ReadOnlySpan<T> values * Added Guard APIs for ReadOnlyMemory<T> values * Code refactoring * Minor code refactoring * Added Guard.IsBitwiseEqualTo<T> API * Added Guard.IsNull API * Code refactoring and optimizations * Code refactoring * Added Guard.HasSizeNotEqualTo<T> API * Added Guard size API overloads for the string type * Added Guard size API overloads for T[] arrays * Code refactoring * Removed unnecessary using directives * Added Guard reference check APIs * Added new Guaard APIs for string values * Improved Guard.IsBitwiseEqualTo<T> API * Improved Guard.IsNull APIs * Added new size APIs for copy operations * Added new type test APIs, minor code tweaks * Minor bug fixes * Added Guard APIs for Task values * Minor code tweak * Added new Guard.IsEmpty APIs * Refactored code to remove unsafe requirement * Added Guard.IsInRange<T> APIs * Added missing type checks in Guard.IsBitwiseEqualTo API * Added ValueTypeExtensions.ToHexString API * Code refactoring * Added ValueTypeExtensions tests * Added general Guard tests * Bug fixes in the Guard class * Minor speed improvements * Code refactoring * Removed unnecessary using directives * More speed improvements and API refactoring * Refactored/fixed some Guard APIs * More bug fixes * Added Guard tests for array APIs * Fixed the Guard.IsNotEmpty<T> array API * Moved exception throwers to separate class * Moved general Guard throwers to separate class * Disabled warning for XML overload resolution * Moved array Guard throwers to separate class * Moved stream Guard throwers to separate class * Fixed some XML docs * Moved enumerable Guard throwers to separate class * Moved task Guard throwers to separate class * Added new Guard APIs for tasks * Moved string Guard throwers to separate class * Moved ReadOnlySpan<T> Guard throwers to separate class * Added Span<T> APIs to the Guard class * Moved ReadOnlyMemory<T> Guard throwers to separate class * Added Memory<T> APIs to the Guard class * Minor code refactoring * Update file headers * Removed unnecessary methods * Added TypeExtensions.ToTypeString extension * Improved error messages for general Guard APIs * Improved error messages for Enumerable Guard APIs * Improved error messages for stream Guard APIs * Improved error messages for array Guard APIs * Improved error messages for string Guard APIs * Improved error messages for task Guard APIs * Improved error messages for span Guard APIs * Improved error messages for memory Guard APIs * Improved type string for nullable types * Improved type string for value tuple types * Minor performance improvement * Added numeric comparison T4 file * Updated .gitignore to skip generated files * Moved comparison Guard APIs to separate file * Renamed template file, fixed incorrect XML doc * Fixed missing blank line * Removed unnecessary type parameters * Moved enumerable Guard APIs to separate file * Code refactoring in the enumerable T4 template * Renamed a file * Added empty template for ThrowHelper.Collection * Code refactoring in the T4 templates * Switched collection throw helpers to T4 generation * Code refactoring * Removed incorrect file header * Added new Guard.IsInRangeFor API * Updated .gitignore, added generated files to source control * Added tests for Guard.IsInRangeFor * Improved formatting of values in error messages * Improved error messages * More improvements to the error messages * Minor code tweaks * Added Guard.IsNotOfType APIs * Added Guard.IsNotAssignableToType APIs * Added Guard.IsDefault<T> APIs * Fixed some XML docs * Added missing [Pure] attribute * Fixed typo in a comment * Removed unnecessary comment after code changes * Added XML comment for the T4 service * Added more comments to ToHexString * Suppressed an incorrect code style warning * Added more tests for Guard.IsInRange * Added Guard.IsNotInRangeFor APIs * Added readme file for the T4 templates * Changed generated files extension to .g.cs to avoid conflicts * Added Guard.IsCloseTo and Guard.IsNotCloseTo APIs * Added tests for IsCloseTo * Updated ".g.cs" extension for generated files Co-Authored-By: Michael Hawker MSFT (XAML Llama) <michael.hawker@outlook.com> * Removed extra space (typo) Co-Authored-By: Michael Hawker MSFT (XAML Llama) <michael.hawker@outlook.com> * More tweakes to the Guard.md file * Excluded TypeInfo.g.cs file from .ttinclude from checkout * Added more info about target ranges for some APIs * Added IsCloseTo overloads for long type * Minor code tweaks * Added comment to describe (uint) cast range check trick * Added a nore about the .g.cs files being checked in * Fixed IsCloseTo tests * Fixed IsNull tests Co-authored-by: Michael Hawker MSFT (XAML Llama) <michael.hawker@outlook.com>
284 lines
14 KiB
Plaintext
284 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" #>
|
|
/* ========================
|
|
* Auto generated file
|
|
* ===================== */
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Runtime.CompilerServices;
|
|
|
|
#nullable enable
|
|
|
|
namespace Microsoft.Toolkit.Diagnostics
|
|
{
|
|
/// <summary>
|
|
/// Helper methods to verify conditions when running code.
|
|
/// </summary>
|
|
public static partial class Guard
|
|
{
|
|
<#
|
|
GenerateTextForItems(EnumerableTypes, item =>
|
|
{
|
|
#>
|
|
/// <summary>
|
|
/// 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>
|
|
/// <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>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static void IsEmpty<T>(<#=item.Type#> <#=item.Name#>, string name)
|
|
{
|
|
if (<#=item.Name#>.<#=item.Size#> != 0)
|
|
{
|
|
ThrowHelper.ThrowArgumentExceptionForIsEmpty(<#=item.Cast#><#=item.Name#>, name);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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>
|
|
/// <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>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static void IsNotEmpty<T>(<#=item.Type#> <#=item.Name#>, string name)
|
|
{
|
|
if (<#=item.Name#>.<#=item.Size#> == 0)
|
|
{
|
|
<#
|
|
if (item.Type == "Span<T>")
|
|
{
|
|
#>
|
|
ThrowHelper.ThrowArgumentExceptionForIsNotEmptyWithSpan<T>(name);
|
|
<#
|
|
}
|
|
else if (item.Type == "ReadOnlySpan<T>")
|
|
{
|
|
#>
|
|
ThrowHelper.ThrowArgumentExceptionForIsNotEmptyWithReadOnlySpan<T>(name);
|
|
<#
|
|
}
|
|
else
|
|
{
|
|
#>
|
|
ThrowHelper.ThrowArgumentExceptionForIsNotEmpty<<#=item.Type#>>(name);
|
|
<#
|
|
}
|
|
#>
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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>
|
|
/// <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>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static void HasSizeEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
|
|
{
|
|
if (<#=item.Name#>.<#=item.Size#> != size)
|
|
{
|
|
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.
|
|
/// </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>
|
|
/// <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>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static void HasSizeNotEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
|
|
{
|
|
if (<#=item.Name#>.<#=item.Size#> == size)
|
|
{
|
|
ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(<#=item.Cast#><#=item.Name#>, size, name);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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>
|
|
/// <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>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static void HasSizeOver<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
|
|
{
|
|
if (<#=item.Name#>.<#=item.Size#> <= size)
|
|
{
|
|
ThrowHelper.ThrowArgumentExceptionForHasSizeOver(<#=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.
|
|
/// </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>
|
|
/// <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>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static void HasSizeAtLeast<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
|
|
{
|
|
if (<#=item.Name#>.<#=item.Size#> < size)
|
|
{
|
|
ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(<#=item.Cast#><#=item.Name#>, size, name);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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>
|
|
/// <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>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static void HasSizeLessThan<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
|
|
{
|
|
if (<#=item.Name#>.<#=item.Size#> >= size)
|
|
{
|
|
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.
|
|
/// </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>
|
|
/// <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>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static void HasSizeLessThanOrEqualTo<T>(<#=item.Type#> <#=item.Name#>, int size, string name)
|
|
{
|
|
if (<#=item.Name#>.<#=item.Size#> > size)
|
|
{
|
|
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.
|
|
/// </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>
|
|
/// <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, string name)
|
|
{
|
|
if (source.<#=item.Size#> != destination.<#=item.Size#>)
|
|
{
|
|
<#
|
|
if (item.HasCountProperty)
|
|
{
|
|
#>
|
|
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Cast#>source, destination.<#=item.Size#>, name);
|
|
<#
|
|
}
|
|
else
|
|
{
|
|
#>
|
|
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.
|
|
/// </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>
|
|
/// <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, string name)
|
|
{
|
|
if (source.<#=item.Size#> > destination.<#=item.Size#>)
|
|
{
|
|
<#
|
|
if (item.HasCountProperty)
|
|
{
|
|
#>
|
|
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(<#=item.Cast#>source, destination.<#=item.Size#>, name);
|
|
<#
|
|
}
|
|
else
|
|
{
|
|
#>
|
|
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(source, <#=item.Cast#>destination, name);
|
|
<#
|
|
}
|
|
#>
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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>
|
|
/// <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>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static void IsInRangeFor<T>(int index, <#=item.Type#> <#=item.Name#>, 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/windows-toolkit/WindowsCommunityToolkit/pull/3131#discussion_r390682835 */
|
|
#>
|
|
if ((uint)index >= (uint)<#=item.Name#>.<#=item.Size#>)
|
|
{
|
|
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRangeFor(index, <#=item.Cast#><#=item.Name#>, name);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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>
|
|
/// <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>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static void IsNotInRangeFor<T>(int index, <#=item.Type#> <#=item.Name#>, string name)
|
|
{
|
|
if ((uint)index < (uint)<#=item.Name#>.<#=item.Size#>)
|
|
{
|
|
ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsNotInRangeFor(index, <#=item.Cast#><#=item.Name#>, name);
|
|
}
|
|
}
|
|
<#
|
|
});
|
|
#>
|
|
}
|
|
}
|