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

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

+60
ファイルの表示
@@ -0,0 +1,60 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace System;
/// <summary>
/// Throw helper extensions for <see cref="ObjectDisposedException"/>.
/// </summary>
internal static class ObjectDisposedExceptionExtensions
{
/// <summary>
/// Throws an <see cref="ObjectDisposedException"/> 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 indicating whether <paramref name="disposableInstance"/> has been disposed.</param>
/// <param name="disposableInstance">The instance that can be signaled as being disposed.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ThrowIf(this ObjectDisposedException? _, [DoesNotReturnIf(true)] bool condition, object disposableInstance)
{
if (condition)
{
Throw(disposableInstance);
}
}
/// <summary>
/// Throws an <see cref="ObjectDisposedException"/> if <paramref name="validationObject"/> is <see langword="null"/>.
/// </summary>
/// <param name="_">Dummy value to invoke the extension upon (always pass <see langword="null"/>.</param>
/// <param name="validationObject">The instance to check to determine whether the target instance has been disposed.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ThrowIfNull(this ObjectDisposedException? _, [NotNull] object? validationObject)
{
if (validationObject is null)
{
Throw();
}
}
/// <summary>
/// Throws an <see cref="ObjectDisposedException"/>.
/// </summary>
/// <exception cref="ObjectDisposedException">Thrown with no parameters.</exception>
[DoesNotReturn]
private static void Throw()
{
throw new ObjectDisposedException(null);
}
/// <summary>
/// Throws an <see cref="ObjectDisposedException"/> for a given target instance.
/// </summary>
/// <param name="targetObject">The target instance that was disposed.</param>
/// <exception cref="ObjectDisposedException">Thrown for <paramref name="targetObject"/>.</exception>
[DoesNotReturn]
private static void Throw(object targetObject)
{
throw new ObjectDisposedException(targetObject.ToString());
}
}
+1
ファイルの表示
@@ -22,6 +22,7 @@
<Compile Include="..\ComputeSharp.Core\Intrinsics\Hlsl.cs" Link="ComputeSharp.Core\Intrinsics\Hlsl.cs" />
<Compile Include="..\ComputeSharp.Core\Intrinsics\Hlsl.g.cs" Link="ComputeSharp.Core\Intrinsics\Hlsl.g.cs" />
<Compile Include="..\ComputeSharp.Core\Intrinsics\Hlsl.Void.g.cs" Link="ComputeSharp.Core\Intrinsics\Hlsl.Void.g.cs" />
<Compile Include="..\ComputeSharp.Core\Polyfills\Win32ExceptionExtensions.cs" Link="ComputeSharp.Core\Polyfills\Win32ExceptionExtensions.cs" />
<Compile Include="..\ComputeSharp.Core\Primitives\Bool\Bool.cs" Link="ComputeSharp.Core\Primitives\Bool\Bool.cs" />
<Compile Include="..\ComputeSharp.Core\Primitives\Bool\Bool2.cs" Link="ComputeSharp.Core\Primitives\Bool\Bool2.cs" />
<Compile Include="..\ComputeSharp.Core\Primitives\Bool\Bool3.cs" Link="ComputeSharp.Core\Primitives\Bool\Bool3.cs" />
+1 -14
ファイルの表示
@@ -1,5 +1,4 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Numerics;
using System.Runtime.InteropServices;
using ABI.Microsoft.Graphics.Canvas;
@@ -69,10 +68,7 @@ unsafe partial class CanvasEffect
{
lock (this.lockObject)
{
if (this.isDisposed)
{
ThrowObjectDisposedException();
}
default(ObjectDisposedException).ThrowIf(this.isDisposed, this);
this.canvasImage ??= CreateCanvasImage();
@@ -99,13 +95,4 @@ unsafe partial class CanvasEffect
canvasImageUnknown.CopyTo(canvasImageInterop).Assert();
}
/// <summary>
/// Throws a new <see cref="ObjectDisposedException"/>.
/// </summary>
[DoesNotReturn]
public void ThrowObjectDisposedException()
{
throw new ObjectDisposedException(ToString(), "The current effect has been disposed and can no longer be used.");
}
}
@@ -108,30 +108,30 @@ partial class PixelShaderEffect<T>
bool ICollection.IsSynchronized => true;
/// <inheritdoc/>
object ICollection.SyncRoot => throw new NotSupportedException("ICollection.SyncRoot is not supported for ResourceTextureManagerCollection.");
object ICollection.SyncRoot => default(NotSupportedException).Throw<object>();
/// <inheritdoc/>
void ICollection<D2D1ResourceTextureManager?>.Add(D2D1ResourceTextureManager? item)
{
throw new NotSupportedException("ICollection<T>.Add is not supported for ResourceTextureManagerCollection.");
default(NotSupportedException).Throw();
}
/// <inheritdoc/>
int IList.Add(object value)
{
throw new NotSupportedException("IList.Add is not supported for ResourceTextureManagerCollection.");
return default(NotSupportedException).Throw<int>();
}
/// <inheritdoc/>
void ICollection<D2D1ResourceTextureManager?>.Clear()
{
throw new NotSupportedException("ICollection<T>.Clear is not supported for ResourceTextureManagerCollection.");
default(NotSupportedException).Throw();
}
/// <inheritdoc/>
void IList.Clear()
{
throw new NotSupportedException("IList.Clear is not supported for ResourceTextureManagerCollection.");
default(NotSupportedException).Throw();
}
/// <inheritdoc/>
@@ -185,37 +185,37 @@ partial class PixelShaderEffect<T>
/// <inheritdoc/>
void IList<D2D1ResourceTextureManager?>.Insert(int index, D2D1ResourceTextureManager? item)
{
throw new NotSupportedException("IList<T>.Insert is not supported for ResourceTextureManagerCollection.");
default(NotSupportedException).Throw();
}
/// <inheritdoc/>
void IList.Insert(int index, object value)
{
throw new NotSupportedException("IList.Insert is not supported for ResourceTextureManagerCollection.");
default(NotSupportedException).Throw();
}
/// <inheritdoc/>
bool ICollection<D2D1ResourceTextureManager?>.Remove(D2D1ResourceTextureManager? item)
{
throw new NotSupportedException("ICollection<T>.Remove is not supported for ResourceTextureManagerCollection.");
return default(NotSupportedException).Throw<bool>();
}
/// <inheritdoc/>
void IList.Remove(object value)
{
throw new NotSupportedException("IList.Remove is not supported for ResourceTextureManagerCollection.");
default(NotSupportedException).Throw();
}
/// <inheritdoc/>
void IList<D2D1ResourceTextureManager?>.RemoveAt(int index)
{
throw new NotSupportedException("IList<T>.RemoveAt is not supported for ResourceTextureManagerCollection.");
default(NotSupportedException).Throw();
}
/// <inheritdoc/>
void IList.RemoveAt(int index)
{
throw new NotSupportedException("IList.RemoveAt is not supported for ResourceTextureManagerCollection.");
default(NotSupportedException).Throw();
}
/// <summary>
+11 -11
ファイルの表示
@@ -94,30 +94,30 @@ partial class PixelShaderEffect<T>
bool ICollection.IsSynchronized => true;
/// <inheritdoc/>
object ICollection.SyncRoot => throw new NotSupportedException("ICollection.SyncRoot is not supported for SourceCollection.");
object ICollection.SyncRoot => default(NotSupportedException).Throw<object>();
/// <inheritdoc/>
void ICollection<IGraphicsEffectSource?>.Add(IGraphicsEffectSource? item)
{
throw new NotSupportedException("ICollection<T>.Add is not supported for SourceCollection.");
default(NotSupportedException).Throw();
}
/// <inheritdoc/>
int IList.Add(object value)
{
throw new NotSupportedException("IList.Add is not supported for SourceCollection.");
return default(NotSupportedException).Throw<int>();
}
/// <inheritdoc/>
void ICollection<IGraphicsEffectSource?>.Clear()
{
throw new NotSupportedException("ICollection<T>.Clear is not supported for SourceCollection.");
default(NotSupportedException).Throw();
}
/// <inheritdoc/>
void IList.Clear()
{
throw new NotSupportedException("IList.Clear is not supported for SourceCollection.");
default(NotSupportedException).Throw();
}
/// <inheritdoc/>
@@ -171,37 +171,37 @@ partial class PixelShaderEffect<T>
/// <inheritdoc/>
void IList<IGraphicsEffectSource?>.Insert(int index, IGraphicsEffectSource? item)
{
throw new NotSupportedException("IList<T>.Insert is not supported for SourceCollection.");
default(NotSupportedException).Throw();
}
/// <inheritdoc/>
void IList.Insert(int index, object value)
{
throw new NotSupportedException("IList.Insert is not supported for SourceCollection.");
default(NotSupportedException).Throw();
}
/// <inheritdoc/>
bool ICollection<IGraphicsEffectSource?>.Remove(IGraphicsEffectSource? item)
{
throw new NotSupportedException("ICollection<T>.Remove is not supported for SourceCollection.");
return default(NotSupportedException).Throw<bool>();
}
/// <inheritdoc/>
void IList.Remove(object value)
{
throw new NotSupportedException("IList.Remove is not supported for SourceCollection.");
default(NotSupportedException).Throw();
}
/// <inheritdoc/>
void IList<IGraphicsEffectSource?>.RemoveAt(int index)
{
throw new NotSupportedException("IList<T>.RemoveAt is not supported for SourceCollection.");
default(NotSupportedException).Throw();
}
/// <inheritdoc/>
void IList.RemoveAt(int index)
{
throw new NotSupportedException("IList.RemoveAt is not supported for SourceCollection.");
default(NotSupportedException).Throw();
}
/// <summary>
+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();
+1 -1
ファイルの表示
@@ -206,7 +206,7 @@ partial class DeviceHelper
/// <inheritdoc/>
public void Reset()
{
throw new NotSupportedException();
default(NotSupportedException).Throw();
}
/// <inheritdoc/>