From 7a5daf0a6171809499794513a9b79b3d4ecdec74 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 25 Feb 2023 17:07:21 +0100 Subject: [PATCH] Add and use ArgumentOutOfRangeException.ThrowIfLessThan --- .../ArgumentOutOfRangeExceptionExtensions.cs | 30 +++++++++++++++++++ .../Resources/Abstract/Texture1D{T}.cs | 5 ++-- .../Resources/Abstract/Texture2D{T}.cs | 5 ++-- .../Resources/Abstract/Texture3D{T}.cs | 5 ++-- 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/ComputeSharp.Core/Polyfills/ArgumentOutOfRangeExceptionExtensions.cs b/src/ComputeSharp.Core/Polyfills/ArgumentOutOfRangeExceptionExtensions.cs index a67c0ba8..3f66f9be 100644 --- a/src/ComputeSharp.Core/Polyfills/ArgumentOutOfRangeExceptionExtensions.cs +++ b/src/ComputeSharp.Core/Polyfills/ArgumentOutOfRangeExceptionExtensions.cs @@ -53,6 +53,36 @@ internal static class ArgumentOutOfRangeExceptionExtensions } } + /// + /// Throws an if is less than . + /// + /// Dummy value to invoke the extension upon (always pass . + /// The argument to validate as less or equal than . + /// The value to compare with . + /// The name of the parameter with which corresponds. + public static void ThrowIfLessThan(this ArgumentOutOfRangeException? _, int value, int other, [CallerArgumentExpression(nameof(value))] string? parameterName = null) + { + if (value < other) + { + Throw(parameterName, value); + } + } + + /// + /// Throws an if is less than . + /// + /// Dummy value to invoke the extension upon (always pass . + /// The argument to validate as less or equal than . + /// The value to compare with . + /// The name of the parameter with which corresponds. + public static void ThrowIfLessThan(this ArgumentOutOfRangeException? _, long value, long other, [CallerArgumentExpression(nameof(value))] string? parameterName = null) + { + if (value < other) + { + Throw(parameterName, value); + } + } + /// /// Throws an if is not in the specified range. /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture1D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture1D{T}.cs index 6d338c1e..85fb6708 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture1D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture1D{T}.cs @@ -1,6 +1,5 @@ using System; using System.Runtime.CompilerServices; -using CommunityToolkit.Diagnostics; using ComputeSharp.__Internals; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Commands; @@ -168,7 +167,7 @@ public abstract unsafe partial class Texture1D : IReferenceTrackedObject, IGr default(ArgumentOutOfRangeException).ThrowIfNotInRange(sourceOffsetX, 0, Width); default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, Width); default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffsetX + width, Width, nameof(sourceOffsetX)); - Guard.IsGreaterThanOrEqualTo(size, width); + default(ArgumentOutOfRangeException).ThrowIfLessThan(size, width); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = GetReferenceTracker().GetLease(); @@ -366,7 +365,7 @@ public abstract unsafe partial class Texture1D : IReferenceTrackedObject, IGr default(ArgumentOutOfRangeException).ThrowIfNotInRange(destinationOffsetX, 0, Width); default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(width, 1, Width); default(ArgumentOutOfRangeException).ThrowIfGreaterThan(destinationOffsetX + width, Width, nameof(destinationOffsetX)); - Guard.IsGreaterThanOrEqualTo(size, width); + default(ArgumentOutOfRangeException).ThrowIfLessThan(size, width); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = GetReferenceTracker().GetLease(); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index 66758d99..668c7208 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -1,6 +1,5 @@ using System; using System.Runtime.CompilerServices; -using CommunityToolkit.Diagnostics; using ComputeSharp.__Internals; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Commands; @@ -183,7 +182,7 @@ public abstract unsafe partial class Texture2D : IReferenceTrackedObject, IGr default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(height, 1, Height); default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffsetX + width, Width, nameof(sourceOffsetX)); default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffsetY + height, Height, nameof(sourceOffsetY)); - Guard.IsGreaterThanOrEqualTo(size, (nint)width * height); + default(ArgumentOutOfRangeException).ThrowIfLessThan(size, (long)width * height); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = GetReferenceTracker().GetLease(); @@ -406,7 +405,7 @@ public abstract unsafe partial class Texture2D : IReferenceTrackedObject, IGr default(ArgumentOutOfRangeException).ThrowIfNotBetweenOrEqual(height, 1, Height); default(ArgumentOutOfRangeException).ThrowIfGreaterThan(destinationOffsetX + width, Width, nameof(destinationOffsetX)); default(ArgumentOutOfRangeException).ThrowIfGreaterThan(destinationOffsetY + height, Height, nameof(destinationOffsetY)); - Guard.IsGreaterThanOrEqualTo(size, (nint)width * height); + default(ArgumentOutOfRangeException).ThrowIfLessThan(size, (long)width * height); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = GetReferenceTracker().GetLease(); diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 7943d589..95bcf9f5 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -1,6 +1,5 @@ using System; using System.Runtime.CompilerServices; -using CommunityToolkit.Diagnostics; using ComputeSharp.__Internals; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Commands; @@ -198,7 +197,7 @@ public abstract unsafe partial class Texture3D : IReferenceTrackedObject, IGr default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffsetX + width, Width, nameof(sourceOffsetX)); default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffsetY + height, Height, nameof(sourceOffsetY)); default(ArgumentOutOfRangeException).ThrowIfGreaterThan(sourceOffsetZ + depth, Depth, nameof(sourceOffsetZ)); - Guard.IsGreaterThanOrEqualTo(size, (nint)width * height * depth); + default(ArgumentOutOfRangeException).ThrowIfLessThan(size, (long)width * height * depth); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = GetReferenceTracker().GetLease(); @@ -447,7 +446,7 @@ public abstract unsafe partial class Texture3D : IReferenceTrackedObject, IGr default(ArgumentOutOfRangeException).ThrowIfGreaterThan(destinationOffsetX + width, Width, nameof(destinationOffsetX)); default(ArgumentOutOfRangeException).ThrowIfGreaterThan(destinationOffsetY + height, Height, nameof(destinationOffsetY)); default(ArgumentOutOfRangeException).ThrowIfGreaterThan(destinationOffsetZ + depth, Depth, nameof(destinationOffsetZ)); - Guard.IsGreaterThanOrEqualTo(size, (nint)width * height * depth); + default(ArgumentOutOfRangeException).ThrowIfLessThan(size, (long)width * height * depth); using ReferenceTracker.Lease _0 = GraphicsDevice.GetReferenceTracker().GetLease(); using ReferenceTracker.Lease _1 = GetReferenceTracker().GetLease();