diff --git a/src/ComputeSharp.Core/Polyfills/ObjectDisposedExceptionExtensions.cs b/src/ComputeSharp.Core/Polyfills/ObjectDisposedExceptionExtensions.cs new file mode 100644 index 00000000..21335f80 --- /dev/null +++ b/src/ComputeSharp.Core/Polyfills/ObjectDisposedExceptionExtensions.cs @@ -0,0 +1,60 @@ +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; + +namespace System; + +/// +/// Throw helper extensions for . +/// +internal static class ObjectDisposedExceptionExtensions +{ + /// + /// Throws an if is . + /// + /// Dummy value to invoke the extension upon (always pass . + /// The condition indicating whether has been disposed. + /// The instance that can be signaled as being disposed. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowIf(this ObjectDisposedException? _, [DoesNotReturnIf(true)] bool condition, object disposableInstance) + { + if (condition) + { + Throw(disposableInstance); + } + } + + /// + /// Throws an if is . + /// + /// Dummy value to invoke the extension upon (always pass . + /// The instance to check to determine whether the target instance has been disposed. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowIfNull(this ObjectDisposedException? _, [NotNull] object? validationObject) + { + if (validationObject is null) + { + Throw(); + } + } + + /// + /// Throws an . + /// + /// Thrown with no parameters. + [DoesNotReturn] + private static void Throw() + { + throw new ObjectDisposedException(null); + } + + /// + /// Throws an for a given target instance. + /// + /// The target instance that was disposed. + /// Thrown for . + [DoesNotReturn] + private static void Throw(object targetObject) + { + throw new ObjectDisposedException(targetObject.ToString()); + } +} diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj index e04c8b81..f3fd8190 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj +++ b/src/ComputeSharp.D2D1.SourceGenerators/ComputeSharp.D2D1.SourceGenerators.csproj @@ -22,6 +22,7 @@ + diff --git a/src/ComputeSharp.D2D1.Uwp/CanvasEffect.Interop.cs b/src/ComputeSharp.D2D1.Uwp/CanvasEffect.Interop.cs index 694bf1a0..4d630cb2 100644 --- a/src/ComputeSharp.D2D1.Uwp/CanvasEffect.Interop.cs +++ b/src/ComputeSharp.D2D1.Uwp/CanvasEffect.Interop.cs @@ -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(); } - - /// - /// Throws a new . - /// - [DoesNotReturn] - public void ThrowObjectDisposedException() - { - throw new ObjectDisposedException(ToString(), "The current effect has been disposed and can no longer be used."); - } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.ResourceTextureManagerCollection.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.ResourceTextureManagerCollection.cs index b83600cf..e458491f 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.ResourceTextureManagerCollection.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.ResourceTextureManagerCollection.cs @@ -108,30 +108,30 @@ partial class PixelShaderEffect bool ICollection.IsSynchronized => true; /// - object ICollection.SyncRoot => throw new NotSupportedException("ICollection.SyncRoot is not supported for ResourceTextureManagerCollection."); + object ICollection.SyncRoot => default(NotSupportedException).Throw(); /// void ICollection.Add(D2D1ResourceTextureManager? item) { - throw new NotSupportedException("ICollection.Add is not supported for ResourceTextureManagerCollection."); + default(NotSupportedException).Throw(); } /// int IList.Add(object value) { - throw new NotSupportedException("IList.Add is not supported for ResourceTextureManagerCollection."); + return default(NotSupportedException).Throw(); } /// void ICollection.Clear() { - throw new NotSupportedException("ICollection.Clear is not supported for ResourceTextureManagerCollection."); + default(NotSupportedException).Throw(); } /// void IList.Clear() { - throw new NotSupportedException("IList.Clear is not supported for ResourceTextureManagerCollection."); + default(NotSupportedException).Throw(); } /// @@ -185,37 +185,37 @@ partial class PixelShaderEffect /// void IList.Insert(int index, D2D1ResourceTextureManager? item) { - throw new NotSupportedException("IList.Insert is not supported for ResourceTextureManagerCollection."); + default(NotSupportedException).Throw(); } /// void IList.Insert(int index, object value) { - throw new NotSupportedException("IList.Insert is not supported for ResourceTextureManagerCollection."); + default(NotSupportedException).Throw(); } /// bool ICollection.Remove(D2D1ResourceTextureManager? item) { - throw new NotSupportedException("ICollection.Remove is not supported for ResourceTextureManagerCollection."); + return default(NotSupportedException).Throw(); } /// void IList.Remove(object value) { - throw new NotSupportedException("IList.Remove is not supported for ResourceTextureManagerCollection."); + default(NotSupportedException).Throw(); } /// void IList.RemoveAt(int index) { - throw new NotSupportedException("IList.RemoveAt is not supported for ResourceTextureManagerCollection."); + default(NotSupportedException).Throw(); } /// void IList.RemoveAt(int index) { - throw new NotSupportedException("IList.RemoveAt is not supported for ResourceTextureManagerCollection."); + default(NotSupportedException).Throw(); } /// diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Sources.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Sources.cs index 5c313d8c..3d36a9ea 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Sources.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Sources.cs @@ -94,30 +94,30 @@ partial class PixelShaderEffect bool ICollection.IsSynchronized => true; /// - object ICollection.SyncRoot => throw new NotSupportedException("ICollection.SyncRoot is not supported for SourceCollection."); + object ICollection.SyncRoot => default(NotSupportedException).Throw(); /// void ICollection.Add(IGraphicsEffectSource? item) { - throw new NotSupportedException("ICollection.Add is not supported for SourceCollection."); + default(NotSupportedException).Throw(); } /// int IList.Add(object value) { - throw new NotSupportedException("IList.Add is not supported for SourceCollection."); + return default(NotSupportedException).Throw(); } /// void ICollection.Clear() { - throw new NotSupportedException("ICollection.Clear is not supported for SourceCollection."); + default(NotSupportedException).Throw(); } /// void IList.Clear() { - throw new NotSupportedException("IList.Clear is not supported for SourceCollection."); + default(NotSupportedException).Throw(); } /// @@ -171,37 +171,37 @@ partial class PixelShaderEffect /// void IList.Insert(int index, IGraphicsEffectSource? item) { - throw new NotSupportedException("IList.Insert is not supported for SourceCollection."); + default(NotSupportedException).Throw(); } /// void IList.Insert(int index, object value) { - throw new NotSupportedException("IList.Insert is not supported for SourceCollection."); + default(NotSupportedException).Throw(); } /// bool ICollection.Remove(IGraphicsEffectSource? item) { - throw new NotSupportedException("ICollection.Remove is not supported for SourceCollection."); + return default(NotSupportedException).Throw(); } /// void IList.Remove(object value) { - throw new NotSupportedException("IList.Remove is not supported for SourceCollection."); + default(NotSupportedException).Throw(); } /// void IList.RemoveAt(int index) { - throw new NotSupportedException("IList.RemoveAt is not supported for SourceCollection."); + default(NotSupportedException).Throw(); } /// void IList.RemoveAt(int index) { - throw new NotSupportedException("IList.RemoveAt is not supported for SourceCollection."); + default(NotSupportedException).Throw(); } /// diff --git a/src/ComputeSharp.D2D1/Extensions/HRESULTExtensions.cs b/src/ComputeSharp.D2D1/Extensions/HRESULTExtensions.cs index 656d8d96..ed1cb658 100644 --- a/src/ComputeSharp.D2D1/Extensions/HRESULTExtensions.cs +++ b/src/ComputeSharp.D2D1/Extensions/HRESULTExtensions.cs @@ -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); } /// @@ -35,19 +31,6 @@ internal static class HRESULTExtensions [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Assert(this int result) { - if (result < 0) - { - ThrowWin32Exception(result); - } - } - - /// - /// Throws a . - /// - /// The input return code. - [DoesNotReturn] - private static void ThrowWin32Exception(int result) - { - throw new Win32Exception(result); + default(Win32Exception).ThrowIfFailed(result); } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriter{T}.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriter{T}.cs index b84c8372..ba7c3cf5 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriter{T}.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriter{T}.cs @@ -57,10 +57,7 @@ internal ref struct ArrayPoolBufferWriter { 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 /// Advances the current writer. /// /// The amount to advance. - /// - /// Must be called after . - /// This and the methods below are to enable mutating extensions. - /// + /// Must be called after . 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[]? 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 ArrayPool.Shared.Return(array); } -} - -/// -/// Private helpers for . -/// -file static class ArrayPoolBufferWriterHelpers -{ - /// - /// Throws an when the requested count is negative. - /// - [DoesNotReturn] - public static void ThrowArgumentOutOfRangeExceptionForNegativeCount() - { - throw new ArgumentOutOfRangeException("count", "The count can't be a negative value."); - } - - /// - /// Throws an when the size hint is negative. - /// - [DoesNotReturn] - public static void ThrowArgumentOutOfRangeExceptionForNegativeSizeHint() - { - throw new ArgumentOutOfRangeException("sizeHint", "The size hint can't be a negative value."); - } - - /// - /// Throws an when the requested count is negative. - /// - [DoesNotReturn] - public static void ThrowArgumentExceptionForAdvancedTooFar() - { - throw new ArgumentException("The buffer writer has advanced too far."); - } - - /// - /// Throws an when the array is . - /// - [DoesNotReturn] - public static void ThrowObjectDisposedException() - { - throw new ObjectDisposedException("The current buffer has already been disposed."); - } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Factories/D2D1AffineTransformMapper{T}.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Factories/D2D1AffineTransformMapper{T}.cs index c7663a96..a87f958d 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Factories/D2D1AffineTransformMapper{T}.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Factories/D2D1AffineTransformMapper{T}.cs @@ -95,7 +95,9 @@ internal abstract class D2D1AffineTransformMapper : D2D1TransformMapper.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(); diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs index d9e1c2f1..05eaf2e9 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.Enumerator.cs @@ -206,7 +206,7 @@ partial class DeviceHelper /// public void Reset() { - throw new NotSupportedException(); + default(NotSupportedException).Throw(); } ///