Use throw helpers in more places, add ObjectDisposedExceptionExtensions

このコミットが含まれているのは:
Sergio Pedri committed 2023-02-26 14:06:07 +01:00
1 親 19eab4d7af
コミット c35b2d9394
9ファイル変更
+97 -130

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

+2 -19
ファイルの表示
@@ -1,6 +1,5 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using TerraFX.Interop.Windows;
@@ -21,10 +20,7 @@ internal static class HRESULTExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Assert(this HRESULT result)
{
if ((int)result < 0)
{
ThrowWin32Exception(result);
}
default(Win32Exception).ThrowIfFailed((int)result);
}
/// <summary>
@@ -35,19 +31,6 @@ internal static class HRESULTExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Assert(this int result)
{
if (result < 0)
{
ThrowWin32Exception(result);
}
}
/// <summary>
/// Throws a <see cref="Win32Exception"/>.
/// </summary>
/// <param name="result">The input return code.</param>
[DoesNotReturn]
private static void ThrowWin32Exception(int result)
{
throw new Win32Exception(result);
default(Win32Exception).ThrowIfFailed(result);
}
}
+7 -73
ファイルの表示
@@ -57,10 +57,7 @@ internal ref struct ArrayPoolBufferWriter<T>
{
T[]? array = this.array;
if (array is null)
{
ArrayPoolBufferWriterHelpers.ThrowObjectDisposedException();
}
default(ObjectDisposedException).ThrowIfNull(array);
return array.AsSpan(0, this.index);
}
@@ -70,28 +67,14 @@ internal ref struct ArrayPoolBufferWriter<T>
/// Advances the current writer.
/// </summary>
/// <param name="count">The amount to advance.</param>
/// <remarks>
/// <para>Must be called after <see cref="GetSpan"/>.</para>
/// <para>This and the methods below are <see langword="readonly"/> to enable mutating extensions.</para>
/// </remarks>
/// <remarks>Must be called after <see cref="GetSpan"/>.</remarks>
internal void Advance(int count)
{
T[]? array = this.array;
if (array is null)
{
ArrayPoolBufferWriterHelpers.ThrowObjectDisposedException();
}
if (count < 0)
{
ArrayPoolBufferWriterHelpers.ThrowArgumentOutOfRangeExceptionForNegativeCount();
}
if (this.index > array.Length - count)
{
ArrayPoolBufferWriterHelpers.ThrowArgumentExceptionForAdvancedTooFar();
}
default(ObjectDisposedException).ThrowIfNull(array);
default(ArgumentOutOfRangeException).ThrowIfNegative(count);
default(ArgumentException).ThrowIf(this.index > array.Length - count);
this.index += count;
}
@@ -118,15 +101,8 @@ internal ref struct ArrayPoolBufferWriter<T>
{
T[]? array = this.array;
if (array is null)
{
ArrayPoolBufferWriterHelpers.ThrowObjectDisposedException();
}
if (sizeHint < 0)
{
ArrayPoolBufferWriterHelpers.ThrowArgumentOutOfRangeExceptionForNegativeSizeHint();
}
default(ObjectDisposedException).ThrowIfNull(array);
default(ArgumentOutOfRangeException).ThrowIfNegative(sizeHint);
if (sizeHint == 0)
{
@@ -181,46 +157,4 @@ internal ref struct ArrayPoolBufferWriter<T>
ArrayPool<T>.Shared.Return(array);
}
}
/// <summary>
/// Private helpers for <see cref="ArrayPoolBufferWriter{T}"/>.
/// </summary>
file static class ArrayPoolBufferWriterHelpers
{
/// <summary>
/// Throws an <see cref="ArgumentOutOfRangeException"/> when the requested count is negative.
/// </summary>
[DoesNotReturn]
public static void ThrowArgumentOutOfRangeExceptionForNegativeCount()
{
throw new ArgumentOutOfRangeException("count", "The count can't be a negative value.");
}
/// <summary>
/// Throws an <see cref="ArgumentOutOfRangeException"/> when the size hint is negative.
/// </summary>
[DoesNotReturn]
public static void ThrowArgumentOutOfRangeExceptionForNegativeSizeHint()
{
throw new ArgumentOutOfRangeException("sizeHint", "The size hint can't be a negative value.");
}
/// <summary>
/// Throws an <see cref="ArgumentOutOfRangeException"/> when the requested count is negative.
/// </summary>
[DoesNotReturn]
public static void ThrowArgumentExceptionForAdvancedTooFar()
{
throw new ArgumentException("The buffer writer has advanced too far.");
}
/// <summary>
/// Throws an <see cref="ObjectDisposedException"/> when the array is <see langword="null"/>.
/// </summary>
[DoesNotReturn]
public static void ThrowObjectDisposedException()
{
throw new ObjectDisposedException("The current buffer has already been disposed.");
}
}
+3 -1
ファイルの表示
@@ -95,7 +95,9 @@ internal abstract class D2D1AffineTransformMapper<T> : D2D1TransformMapper<T, Ma
{
static void Throw()
{
throw new ArgumentException("The input transform matrix can't be inverted, so it can't be used for a transform mapper.");
// The parameter name is "matrix" and it's passed by users from D2D1TransformMapperFactory<T>.Transform.
// Additionally, we explicitly don't use a throw helper here to avoid loading the two string literals.
throw new ArgumentException("The input transform matrix can't be inverted, so it can't be used for a transform mapper.", "matrix");
}
Throw();