From ebeec1c3970019cf491b50c9aacfe1228043f3d9 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 3 Nov 2021 22:07:21 +0100 Subject: [PATCH] Removed leftover ThrowHelper class --- .../Resources/Views/TextureView2D{T}.cs | 19 ++---- .../Resources/Views/TextureView3D{T}.cs | 22 ++----- .../Graphics/Resources/Views/ThrowHelper.cs | 61 ------------------- 3 files changed, 11 insertions(+), 91 deletions(-) delete mode 100644 src/ComputeSharp/Graphics/Resources/Views/ThrowHelper.cs diff --git a/src/ComputeSharp/Graphics/Resources/Views/TextureView2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Views/TextureView2D{T}.cs index a6556148..cb02a1e1 100644 --- a/src/ComputeSharp/Graphics/Resources/Views/TextureView2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Views/TextureView2D{T}.cs @@ -2,7 +2,7 @@ using System.ComponentModel; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; -using ComputeSharp.Resources.Views; +using Microsoft.Toolkit.Diagnostics; #pragma warning disable CS0809 @@ -128,11 +128,8 @@ public readonly unsafe ref partial struct TextureView2D [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if ((uint)x >= (uint)this.width || - (uint)y >= (uint)this.height) - { - ThrowHelper.ThrowIndexOutOfRangeException(); - } + Guard.IsInRange(x, 0, this.width, nameof(x)); + Guard.IsInRange(y, 0, this.height, nameof(y)); return ref *((T*)((byte*)this.pointer + (y * this.pitchInBytes)) + x); } @@ -181,10 +178,7 @@ public readonly unsafe ref partial struct TextureView2D } else { - if (Length > destination.Length) - { - ThrowHelper.ThrowArgumentExceptionForDestinationTooShort(); - } + Guard.HasSizeGreaterThanOrEqualTo(destination, Length, nameof(destination)); for (int y = 0, j = 0; y < this.height; y++, j += this.width) { @@ -308,10 +302,7 @@ public readonly unsafe ref partial struct TextureView2D [Pure] public Span GetRowSpan(int y) { - if ((uint)y >= (uint)this.height) - { - ThrowHelper.ThrowArgumentOutOfRangeExceptionForRow(); - } + Guard.IsInRange(y, 0, this.height, nameof(y)); return new((byte*)this.pointer + (y * this.pitchInBytes), this.width); } diff --git a/src/ComputeSharp/Graphics/Resources/Views/TextureView3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Views/TextureView3D{T}.cs index 8ff26a42..eca1a8c1 100644 --- a/src/ComputeSharp/Graphics/Resources/Views/TextureView3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Views/TextureView3D{T}.cs @@ -2,7 +2,7 @@ using System.ComponentModel; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; -using ComputeSharp.Resources.Views; +using Microsoft.Toolkit.Diagnostics; #pragma warning disable CS0809 @@ -153,12 +153,9 @@ public readonly unsafe ref partial struct TextureView3D [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if ((uint)x >= (uint)this.width || - (uint)y >= (uint)this.height || - (uint)z >= (uint)this.depth) - { - ThrowHelper.ThrowIndexOutOfRangeException(); - } + Guard.IsInRange(x, 0, this.width, nameof(x)); + Guard.IsInRange(y, 0, this.height, nameof(y)); + Guard.IsInRange(z, 0, this.depth, nameof(z)); return ref *((T*)((byte*)this.pointer + (z * this.height * this.strideInBytes) + (y * this.strideInBytes)) + x); } @@ -344,15 +341,8 @@ public readonly unsafe ref partial struct TextureView3D [Pure] public Span GetRowSpan(int y, int z) { - if ((uint)y >= (uint)this.height) - { - ThrowHelper.ThrowArgumentOutOfRangeExceptionForRow(); - } - - if ((uint)z >= (uint)this.depth) - { - ThrowHelper.ThrowArgumentOutOfRangeExceptionForZ(); - } + Guard.IsInRange(y, 0, this.height, nameof(y)); + Guard.IsInRange(z, 0, this.depth, nameof(z)); return new((byte*)this.pointer + (z * this.height * this.strideInBytes) + (y * this.strideInBytes), this.width); } diff --git a/src/ComputeSharp/Graphics/Resources/Views/ThrowHelper.cs b/src/ComputeSharp/Graphics/Resources/Views/ThrowHelper.cs deleted file mode 100644 index 8f9bad1b..00000000 --- a/src/ComputeSharp/Graphics/Resources/Views/ThrowHelper.cs +++ /dev/null @@ -1,61 +0,0 @@ -using System; - -namespace ComputeSharp.Resources.Views; - -/// -/// A helper class to throw exceptions for view types. -/// -internal static class ThrowHelper -{ - /// - /// Throws an when the target span is too short. - /// - public static void ThrowArgumentExceptionForDestinationTooShort() - { - throw new ArgumentException("The target span is too short to copy all the current items to"); - } - - /// - /// Throws an when the a given coordinate is invalid. - /// - /// - /// Throwing is technically discouraged in the docs, but - /// we're doing that here for consistency with the official type(s) from the BCL. - /// - public static void ThrowIndexOutOfRangeException() - { - throw new IndexOutOfRangeException(); - } - - /// - /// Throws an when more than one parameter are invalid. - /// - public static void ThrowArgumentException() - { - throw new ArgumentException("One or more input parameters were invalid"); - } - - /// - /// Throws an when the "row" parameter is invalid. - /// - public static void ThrowArgumentOutOfRangeExceptionForRow() - { - throw new ArgumentOutOfRangeException("row"); - } - - /// - /// Throws an when the "column" parameter is invalid. - /// - public static void ThrowArgumentOutOfRangeExceptionForColumn() - { - throw new ArgumentOutOfRangeException("column"); - } - - /// - /// Throws an when the "z" parameter is invalid. - /// - public static void ThrowArgumentOutOfRangeExceptionForZ() - { - throw new ArgumentOutOfRangeException("z"); - } -}