Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guard APIs #3131

Merged
merged 128 commits into from
Mar 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
128 commits
Select commit Hold shift + click to select a range
1c79157
Initial guard APIs added
Sergio0694 Feb 13, 2020
0554d2f
Added EqualTo and NotEqualTo APIs
Sergio0694 Feb 13, 2020
6dc701f
Merge branch 'master' into feature/guard-apis
Sergio0694 Feb 13, 2020
018e5c4
Added more Guard APIs
Sergio0694 Feb 13, 2020
8d78ba3
New interval Guard APIs added
Sergio0694 Feb 13, 2020
0b76fb9
Added Guard APIs for IEnumerable<T> values
Sergio0694 Feb 13, 2020
8028741
Renamed APIs
Sergio0694 Feb 13, 2020
cd23275
Added Guard APIs for Stream values
Sergio0694 Feb 13, 2020
a54441c
Added Guard APIs for ReadOnlySpan<T> values
Sergio0694 Feb 13, 2020
8dee59a
Added Guard APIs for ReadOnlyMemory<T> values
Sergio0694 Feb 13, 2020
2d32333
Code refactoring
Sergio0694 Feb 13, 2020
19e3bcc
Minor code refactoring
Sergio0694 Feb 13, 2020
0ff1f00
Added Guard.IsBitwiseEqualTo<T> API
Sergio0694 Feb 13, 2020
9dd8a80
Added Guard.IsNull API
Sergio0694 Feb 13, 2020
f2a4500
Code refactoring and optimizations
Sergio0694 Feb 13, 2020
b79daa7
Code refactoring
Sergio0694 Feb 13, 2020
d44e633
Added Guard.HasSizeNotEqualTo<T> API
Sergio0694 Feb 13, 2020
0292f9d
Added Guard size API overloads for the string type
Sergio0694 Feb 13, 2020
073a205
Added Guard size API overloads for T[] arrays
Sergio0694 Feb 13, 2020
5edef16
Code refactoring
Sergio0694 Feb 13, 2020
1a19fea
Removed unnecessary using directives
Sergio0694 Feb 13, 2020
d38ad13
Added Guard reference check APIs
Sergio0694 Feb 13, 2020
e9108fc
Added new Guaard APIs for string values
Sergio0694 Feb 13, 2020
18bb2d4
Improved Guard.IsBitwiseEqualTo<T> API
Sergio0694 Feb 13, 2020
2d9536f
Improved Guard.IsNull APIs
Sergio0694 Feb 13, 2020
a927dad
Added new size APIs for copy operations
Sergio0694 Feb 13, 2020
56a3a18
Added new type test APIs, minor code tweaks
Sergio0694 Feb 13, 2020
bbd10a8
Minor bug fixes
Sergio0694 Feb 13, 2020
fbb5801
Added Guard APIs for Task values
Sergio0694 Feb 14, 2020
eba232e
Minor code tweak
Sergio0694 Feb 14, 2020
9071e12
Added new Guard.IsEmpty APIs
Sergio0694 Feb 14, 2020
edb1d41
Refactored code to remove unsafe requirement
Sergio0694 Feb 14, 2020
d9c83f4
Added Guard.IsInRange<T> APIs
Sergio0694 Feb 14, 2020
e63402b
Added missing type checks in Guard.IsBitwiseEqualTo API
Sergio0694 Feb 17, 2020
87056c9
Added ValueTypeExtensions.ToHexString API
Sergio0694 Feb 18, 2020
0d34c0a
Code refactoring
Sergio0694 Feb 18, 2020
497d149
Added ValueTypeExtensions tests
Sergio0694 Feb 18, 2020
bc0fc1b
Added general Guard tests
Sergio0694 Feb 18, 2020
606cbde
Bug fixes in the Guard class
Sergio0694 Feb 18, 2020
f29cd93
Minor speed improvements
Sergio0694 Feb 18, 2020
79d2dd4
Code refactoring
Sergio0694 Feb 18, 2020
793c769
Removed unnecessary using directives
Sergio0694 Feb 18, 2020
8e2f2d8
More speed improvements and API refactoring
Sergio0694 Feb 18, 2020
5b58e5e
Refactored/fixed some Guard APIs
Sergio0694 Feb 18, 2020
b664c90
More bug fixes
Sergio0694 Feb 18, 2020
7f65184
Added Guard tests for array APIs
Sergio0694 Feb 18, 2020
4304edd
Fixed the Guard.IsNotEmpty<T> array API
Sergio0694 Feb 18, 2020
4802936
Moved exception throwers to separate class
Sergio0694 Feb 23, 2020
ed5315a
Moved general Guard throwers to separate class
Sergio0694 Feb 23, 2020
1772a0f
Disabled warning for XML overload resolution
Sergio0694 Feb 23, 2020
9814d91
Moved array Guard throwers to separate class
Sergio0694 Feb 23, 2020
1b78344
Moved stream Guard throwers to separate class
Sergio0694 Feb 23, 2020
bc0dc18
Fixed some XML docs
Sergio0694 Feb 23, 2020
e000fe0
Moved enumerable Guard throwers to separate class
Sergio0694 Feb 23, 2020
21cf16a
Moved task Guard throwers to separate class
Sergio0694 Feb 23, 2020
c1b9c8b
Added new Guard APIs for tasks
Sergio0694 Feb 23, 2020
c784d4e
Moved string Guard throwers to separate class
Sergio0694 Feb 23, 2020
8cf87c3
Moved ReadOnlySpan<T> Guard throwers to separate class
Sergio0694 Feb 23, 2020
24e03f5
Added Span<T> APIs to the Guard class
Sergio0694 Feb 23, 2020
6f1679b
Moved ReadOnlyMemory<T> Guard throwers to separate class
Sergio0694 Feb 23, 2020
7e80e43
Added Memory<T> APIs to the Guard class
Sergio0694 Feb 23, 2020
662bb83
Minor code refactoring
Sergio0694 Feb 23, 2020
1c334df
Update file headers
Sergio0694 Feb 23, 2020
eccc2a6
Removed unnecessary methods
Sergio0694 Feb 23, 2020
15ef6a3
Added TypeExtensions.ToTypeString extension
Sergio0694 Feb 24, 2020
317fc99
Improved error messages for general Guard APIs
Sergio0694 Feb 24, 2020
cb1f3b0
Improved error messages for Enumerable Guard APIs
Sergio0694 Feb 24, 2020
23f1504
Improved error messages for stream Guard APIs
Sergio0694 Feb 24, 2020
920e69f
Improved error messages for array Guard APIs
Sergio0694 Feb 24, 2020
18e661f
Improved error messages for string Guard APIs
Sergio0694 Feb 24, 2020
d00e0e3
Improved error messages for task Guard APIs
Sergio0694 Feb 24, 2020
6aeedaa
Improved error messages for span Guard APIs
Sergio0694 Feb 24, 2020
acafd8a
Improved error messages for memory Guard APIs
Sergio0694 Feb 24, 2020
8c7757a
Improved type string for nullable types
Sergio0694 Feb 24, 2020
c2fd97c
Improved type string for value tuple types
Sergio0694 Feb 24, 2020
8facb7e
Minor performance improvement
Sergio0694 Feb 24, 2020
2952e23
Added numeric comparison T4 file
Sergio0694 Feb 29, 2020
4991a5c
Updated .gitignore to skip generated files
Sergio0694 Feb 29, 2020
714cf31
Moved comparison Guard APIs to separate file
Sergio0694 Feb 29, 2020
fa8cc56
Renamed template file, fixed incorrect XML doc
Sergio0694 Feb 29, 2020
effb2ed
Fixed missing blank line
Sergio0694 Mar 1, 2020
e2e5171
Removed unnecessary type parameters
Sergio0694 Mar 1, 2020
4482217
Moved enumerable Guard APIs to separate file
Sergio0694 Mar 1, 2020
94872b9
Code refactoring in the enumerable T4 template
Sergio0694 Mar 1, 2020
756c842
Renamed a file
Sergio0694 Mar 1, 2020
0ecef12
Added empty template for ThrowHelper.Collection
Sergio0694 Mar 1, 2020
e093427
Code refactoring in the T4 templates
Sergio0694 Mar 1, 2020
9db9cd0
Switched collection throw helpers to T4 generation
Sergio0694 Mar 1, 2020
f6e3248
Code refactoring
Sergio0694 Mar 1, 2020
deb302b
Merge pull request #7 from Sergio0694/feature/guard-apis-T4
Sergio0694 Mar 1, 2020
fcc3c05
Removed incorrect file header
Sergio0694 Mar 1, 2020
760972f
Added new Guard.IsInRangeFor API
Sergio0694 Mar 1, 2020
8d6cc8a
Updated .gitignore, added generated files to source control
Sergio0694 Mar 3, 2020
e3d245f
Added tests for Guard.IsInRangeFor
Sergio0694 Mar 3, 2020
d367add
Improved formatting of values in error messages
Sergio0694 Mar 4, 2020
6d97f33
Improved error messages
Sergio0694 Mar 4, 2020
ec67152
More improvements to the error messages
Sergio0694 Mar 4, 2020
b2c3acb
Minor code tweaks
Sergio0694 Mar 5, 2020
43b22f0
Added Guard.IsNotOfType APIs
Sergio0694 Mar 5, 2020
c8af095
Added Guard.IsNotAssignableToType APIs
Sergio0694 Mar 5, 2020
7adfa79
Added Guard.IsDefault<T> APIs
Sergio0694 Mar 8, 2020
95b9fd1
Merge branch 'master' into feature/guard-apis
Sergio0694 Mar 9, 2020
d4c2d05
Fixed some XML docs
Sergio0694 Mar 11, 2020
afd973e
Added missing [Pure] attribute
Sergio0694 Mar 11, 2020
96ce238
Fixed typo in a comment
Sergio0694 Mar 11, 2020
cb98a02
Removed unnecessary comment after code changes
Sergio0694 Mar 11, 2020
5f25e18
Added XML comment for the T4 service
Sergio0694 Mar 11, 2020
cf5785c
Added more comments to ToHexString
Sergio0694 Mar 11, 2020
801dc93
Suppressed an incorrect code style warning
Sergio0694 Mar 11, 2020
b840201
Added more tests for Guard.IsInRange
Sergio0694 Mar 11, 2020
818c1eb
Added Guard.IsNotInRangeFor APIs
Sergio0694 Mar 11, 2020
caad2f7
Added readme file for the T4 templates
Sergio0694 Mar 11, 2020
7134f98
Changed generated files extension to .g.cs to avoid conflicts
Sergio0694 Mar 11, 2020
01a7257
Added Guard.IsCloseTo and Guard.IsNotCloseTo APIs
Sergio0694 Mar 11, 2020
f33c984
Added tests for IsCloseTo
Sergio0694 Mar 11, 2020
1b0f2c9
Updated ".g.cs" extension for generated files
Sergio0694 Mar 11, 2020
ef189ba
Removed extra space (typo)
Sergio0694 Mar 11, 2020
ac44dad
More tweakes to the Guard.md file
Sergio0694 Mar 11, 2020
21115f6
Excluded TypeInfo.g.cs file from .ttinclude from checkout
Sergio0694 Mar 11, 2020
67fcd47
Added more info about target ranges for some APIs
Sergio0694 Mar 11, 2020
41fea84
Added IsCloseTo overloads for long type
Sergio0694 Mar 11, 2020
132d240
Minor code tweaks
Sergio0694 Mar 11, 2020
f17aacd
Added comment to describe (uint) cast range check trick
Sergio0694 Mar 11, 2020
949134b
Added a nore about the .g.cs files being checked in
Sergio0694 Mar 13, 2020
c9283d1
Merge branch 'master' into feature/guard-apis
michael-hawker Mar 13, 2020
de701b6
Merge branch 'master' into feature/guard-apis
Sergio0694 Mar 13, 2020
51b45e2
Fixed IsCloseTo tests
Sergio0694 Mar 13, 2020
244da99
Fixed IsNull tests
Sergio0694 Mar 13, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,6 @@ msbuild.binlog
*.project.lock.json
/build/tools/**
!/build/tools/packages.config

# Generated file from .ttinclude
**/Generated/TypeInfo.g.cs
4 changes: 0 additions & 4 deletions Microsoft.Toolkit/Converters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Text;

namespace Microsoft.Toolkit
{
/// <summary>
Expand Down
1,638 changes: 1,638 additions & 0 deletions Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.g.cs

Large diffs are not rendered by default.

283 changes: 283 additions & 0 deletions Microsoft.Toolkit/Diagnostics/Generated/Guard.Collection.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
// Licensed to the .NET Foundation under one or more agreements.
Sergio0694 marked this conversation as resolved.
Show resolved Hide resolved
// 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)
michael-hawker marked this conversation as resolved.
Show resolved Hide resolved
{
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 &lt;= <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 &lt; <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#>)
michael-hawker marked this conversation as resolved.
Show resolved Hide resolved
{
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);
}
}
<#
});
#>
}
Sergio0694 marked this conversation as resolved.
Show resolved Hide resolved
}
Loading