Add and use ArgumentExceptionExtensions.ThrowIf

このコミットが含まれているのは:
Sergio Pedri committed 2023-02-25 17:17:21 +01:00
1 親 7a5daf0a61
コミット 1c6f144064
6ファイル変更
+50 -18

検索条件に一致するファイルはありません

+36
ファイルの表示
@@ -0,0 +1,36 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace System;
/// <summary>
/// Throw helper extensions for <see cref="ArgumentException"/>.
/// </summary>
internal static class ArgumentExceptionExtensions
{
/// <summary>
/// Throws an <see cref="ArgumentException"/> if <paramref name="condition"/> is <see langword="true"/>.
/// </summary>
/// <param name="_">Dummy value to invoke the extension upon (always pass <see langword="null"/>.</param>
/// <param name="condition">The condition to decide whether to throw the exception.</param>
/// <param name="parameterName">The name of the parameter with which <paramref name="condition"/> corresponds.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ThrowIf(this ArgumentException? _, bool condition, string? parameterName = null)
{
if (condition)
{
Throw(parameterName);
}
}
/// <summary>
/// Throws an <see cref="ArgumentException"/> for a given parameter name.
/// </summary>
/// <param name="parameterName">The name of the parameter to report in the exception.</param>
/// <exception cref="ArgumentException">Thrown with <paramref name="parameterName"/>.</exception>
[DoesNotReturn]
private static void Throw(string? parameterName)
{
throw new ArgumentException(parameterName);
}
}
+4 -5
ファイルの表示
@@ -2,7 +2,6 @@ using System;
using System.Buffers;
using System.IO;
using System.Runtime.InteropServices;
using CommunityToolkit.Diagnostics;
using ComputeSharp.Graphics.Helpers;
using ComputeSharp.Resources;
@@ -69,8 +68,8 @@ public static class Texture2DExtensions
{
default(ArgumentNullException).ThrowIfNull(source);
default(ArgumentNullException).ThrowIfNull(destination);
Guard.IsEqualTo(destination.GetLength(0), source.Height, nameof(destination));
Guard.IsEqualTo(destination.GetLength(1), source.Width, nameof(destination));
default(ArgumentException).ThrowIf(destination.GetLength(0) != source.Height, nameof(destination));
default(ArgumentException).ThrowIf(destination.GetLength(1) != source.Width, nameof(destination));
source.CopyTo(ref destination[0, 0], destination.Length, 0, 0, source.Width, source.Height);
}
@@ -359,8 +358,8 @@ public static class Texture2DExtensions
{
default(ArgumentNullException).ThrowIfNull(destination);
default(ArgumentNullException).ThrowIfNull(source);
Guard.IsEqualTo(source.GetLength(0), destination.Height, nameof(source));
Guard.IsEqualTo(source.GetLength(1), destination.Width, nameof(source));
default(ArgumentException).ThrowIf(source.GetLength(0) != destination.Height, nameof(source));
default(ArgumentException).ThrowIf(source.GetLength(1) != destination.Width, nameof(source));
destination.CopyFrom(ref source[0, 0], source.Length, 0, 0, destination.Width, destination.Height);
}
+6 -7
ファイルの表示
@@ -1,6 +1,5 @@
using System;
using System.Runtime.InteropServices;
using CommunityToolkit.Diagnostics;
using ComputeSharp.Resources;
namespace ComputeSharp;
@@ -78,9 +77,9 @@ public static class Texture3DExtensions
{
default(ArgumentNullException).ThrowIfNull(source);
default(ArgumentNullException).ThrowIfNull(destination);
Guard.IsEqualTo(destination.GetLength(0), source.Depth, nameof(destination));
Guard.IsEqualTo(destination.GetLength(1), source.Height, nameof(destination));
Guard.IsEqualTo(destination.GetLength(2), source.Width, nameof(destination));
default(ArgumentException).ThrowIf(destination.GetLength(0) != source.Depth, nameof(destination));
default(ArgumentException).ThrowIf(destination.GetLength(1) != source.Height, nameof(destination));
default(ArgumentException).ThrowIf(destination.GetLength(2) != source.Width, nameof(destination));
source.CopyTo(ref destination[0, 0, 0], destination.Length, 0, 0, 0, source.Width, source.Height, source.Depth);
}
@@ -336,9 +335,9 @@ public static class Texture3DExtensions
{
default(ArgumentNullException).ThrowIfNull(destination);
default(ArgumentNullException).ThrowIfNull(source);
Guard.IsEqualTo(source.GetLength(0), destination.Depth, nameof(source));
Guard.IsEqualTo(source.GetLength(1), destination.Height, nameof(source));
Guard.IsEqualTo(source.GetLength(2), destination.Width, nameof(source));
default(ArgumentException).ThrowIf(source.GetLength(0) != destination.Depth, nameof(source));
default(ArgumentException).ThrowIf(source.GetLength(1) != destination.Height, nameof(source));
default(ArgumentException).ThrowIf(source.GetLength(2) != destination.Width, nameof(source));
destination.CopyFrom(ref source[0, 0, 0], source.Length, 0, 0, 0, destination.Width, destination.Height, destination.Depth);
}
+2 -3
ファイルの表示
@@ -2,7 +2,6 @@ using System;
using System.Buffers;
using System.IO;
using System.Runtime.CompilerServices;
using CommunityToolkit.Diagnostics;
using ComputeSharp.Core.Extensions;
using ComputeSharp.Graphics.Extensions;
using TerraFX.Interop.Windows;
@@ -305,8 +304,8 @@ internal sealed unsafe class WICHelper
wicBitmapFrameDecode.Get()->GetSize(&width, &height).Assert();
// Validate the target texture has the right size
Guard.IsEqualTo((uint)texture.Width, width, nameof(texture.Width));
Guard.IsEqualTo((uint)texture.Height, height, nameof(texture.Height));
default(ArgumentException).ThrowIf((uint)texture.Width != width, nameof(texture.Width));
default(ArgumentException).ThrowIf((uint)texture.Height != height, nameof(texture.Height));
Guid wicTargetPixelFormatGuid = WICFormatHelper.GetForType<T>();
Guid wicActualPixelFormatGuid;
+1 -1
ファイルの表示
@@ -177,7 +177,7 @@ public readonly unsafe ref struct TextureView2D<T>
}
else
{
Guard.HasSizeGreaterThanOrEqualTo(destination, Length);
default(ArgumentException).ThrowIf(destination.Length != Length, nameof(destination));
for (int y = 0, j = 0; y < this.height; y++, j += this.width)
{
+1 -2
ファイルの表示
@@ -1,7 +1,6 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using CommunityToolkit.Diagnostics;
using ComputeSharp.Shaders.Extensions;
using ComputeSharp.__Internals;
using TerraFX.Interop.DirectX;
@@ -34,7 +33,7 @@ internal readonly unsafe struct ShaderDispatchMetadataLoader : IDispatchMetadata
/// <inheritdoc/>
public unsafe void LoadMetadataHandle(ReadOnlySpan<byte> serializedMetadata, ReadOnlySpan<ResourceDescriptor> resourceDescriptors, out IntPtr result)
{
Guard.HasSizeEqualTo(serializedMetadata, 5);
default(ArgumentException).ThrowIf(serializedMetadata.Length != 5, nameof(serializedMetadata));
fixed (IntPtr* p = &result)
{