From b4057ad78ab8d63bad49a42013c4509102671f6f Mon Sep 17 00:00:00 2001 From: Dirk Lemstra Date: Sat, 18 Feb 2017 19:15:52 +0100 Subject: [PATCH] Added a debug version of the Guard class so we get exceptions for debug asserts instead of a dialog. --- src/ImageSharp/Common/Helpers/DebugGuard.cs | 33 +++++++++++++++++++ src/ImageSharp/Common/Helpers/ThrowHelper.cs | 26 --------------- .../Common/Memory/ArrayPointer{T}.cs | 12 ++----- src/ImageSharp/Image/ImageBase{TColor}.cs | 2 +- src/ImageSharp/MetaData/ImageFrameMetaData.cs | 4 +-- src/ImageSharp/MetaData/ImageMetaData.cs | 3 +- src/ImageSharp/MetaData/ImageProperty.cs | 3 +- 7 files changed, 39 insertions(+), 44 deletions(-) create mode 100644 src/ImageSharp/Common/Helpers/DebugGuard.cs delete mode 100644 src/ImageSharp/Common/Helpers/ThrowHelper.cs diff --git a/src/ImageSharp/Common/Helpers/DebugGuard.cs b/src/ImageSharp/Common/Helpers/DebugGuard.cs new file mode 100644 index 000000000..0ca6f0912 --- /dev/null +++ b/src/ImageSharp/Common/Helpers/DebugGuard.cs @@ -0,0 +1,33 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp +{ + using System; + using System.Diagnostics; + + /// + /// Provides methods to protect against invalid parameters for a DEBUG build. + /// + [DebuggerStepThrough] + internal static class DebugGuard + { + /// + /// Verifies, that the method parameter with specified object value is not null + /// and throws an exception if it is found to be so. + /// + /// The target object, which cannot be null. + /// The name of the parameter that is to be checked. + /// is null + [Conditional("DEBUG")] + public static void NotNull(object target, string parameterName) + { + if (target == null) + { + throw new ArgumentNullException(parameterName); + } + } + } +} \ No newline at end of file diff --git a/src/ImageSharp/Common/Helpers/ThrowHelper.cs b/src/ImageSharp/Common/Helpers/ThrowHelper.cs deleted file mode 100644 index 97d75717a..000000000 --- a/src/ImageSharp/Common/Helpers/ThrowHelper.cs +++ /dev/null @@ -1,26 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp -{ - using System; - using System.Runtime.CompilerServices; - - /// - /// Helps removing exception throwing code from hot path by providing non-inlined exception thrower methods. - /// - internal static class ThrowHelper - { - /// - /// Throws an - /// - /// The parameter name - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowArgumentNullException(string paramName) - { - throw new ArgumentNullException(nameof(paramName)); - } - } -} \ No newline at end of file diff --git a/src/ImageSharp/Common/Memory/ArrayPointer{T}.cs b/src/ImageSharp/Common/Memory/ArrayPointer{T}.cs index 95d2e51e6..1ea7706d4 100644 --- a/src/ImageSharp/Common/Memory/ArrayPointer{T}.cs +++ b/src/ImageSharp/Common/Memory/ArrayPointer{T}.cs @@ -31,11 +31,7 @@ namespace ImageSharp [MethodImpl(MethodImplOptions.AggressiveInlining)] public ArrayPointer(T[] array, void* pointerToArray, int offset) { - // TODO: Use Guard.NotNull() here after optimizing it by eliminating the default argument case and applying ThrowHelper! - if (array == null) - { - ThrowHelper.ThrowArgumentNullException(nameof(array)); - } + DebugGuard.NotNull(array, nameof(array)); this.Array = array; this.Offset = offset; @@ -50,11 +46,7 @@ namespace ImageSharp [MethodImpl(MethodImplOptions.AggressiveInlining)] public ArrayPointer(T[] array, void* pointerToArray) { - // TODO: Use Guard.NotNull() here after optimizing it by eliminating the default argument case and applying ThrowHelper! - if (array == null) - { - ThrowHelper.ThrowArgumentNullException(nameof(array)); - } + DebugGuard.NotNull(array, nameof(array)); this.Array = array; this.Offset = 0; diff --git a/src/ImageSharp/Image/ImageBase{TColor}.cs b/src/ImageSharp/Image/ImageBase{TColor}.cs index f530c0ccf..e4b4485c7 100644 --- a/src/ImageSharp/Image/ImageBase{TColor}.cs +++ b/src/ImageSharp/Image/ImageBase{TColor}.cs @@ -182,7 +182,7 @@ namespace ImageSharp /// protected void CopyProperties(IImageBase other) { - Debug.Assert(other != null); + DebugGuard.NotNull(other, nameof(other)); this.Configuration = other.Configuration; } diff --git a/src/ImageSharp/MetaData/ImageFrameMetaData.cs b/src/ImageSharp/MetaData/ImageFrameMetaData.cs index c2277686f..50119096e 100644 --- a/src/ImageSharp/MetaData/ImageFrameMetaData.cs +++ b/src/ImageSharp/MetaData/ImageFrameMetaData.cs @@ -5,8 +5,6 @@ namespace ImageSharp { - using System.Diagnostics; - /// /// Encapsulates the metadata of an image frame. /// @@ -28,7 +26,7 @@ namespace ImageSharp /// internal ImageFrameMetaData(ImageFrameMetaData other) { - Debug.Assert(other != null); + DebugGuard.NotNull(other, nameof(other)); this.FrameDelay = other.FrameDelay; } diff --git a/src/ImageSharp/MetaData/ImageMetaData.cs b/src/ImageSharp/MetaData/ImageMetaData.cs index a40df3110..de1e42476 100644 --- a/src/ImageSharp/MetaData/ImageMetaData.cs +++ b/src/ImageSharp/MetaData/ImageMetaData.cs @@ -6,7 +6,6 @@ namespace ImageSharp { using System.Collections.Generic; - using System.Diagnostics; /// /// Encapsulates the metadata of an image. @@ -46,7 +45,7 @@ namespace ImageSharp /// internal ImageMetaData(ImageMetaData other) { - Debug.Assert(other != null); + DebugGuard.NotNull(other, nameof(other)); this.HorizontalResolution = other.HorizontalResolution; this.VerticalResolution = other.VerticalResolution; diff --git a/src/ImageSharp/MetaData/ImageProperty.cs b/src/ImageSharp/MetaData/ImageProperty.cs index c8bd0b23d..178794283 100644 --- a/src/ImageSharp/MetaData/ImageProperty.cs +++ b/src/ImageSharp/MetaData/ImageProperty.cs @@ -6,7 +6,6 @@ namespace ImageSharp { using System; - using System.Diagnostics; /// /// Stores meta information about a image, like the name of the author, @@ -37,7 +36,7 @@ namespace ImageSharp /// internal ImageProperty(ImageProperty other) { - Debug.Assert(other != null); + DebugGuard.NotNull(other, nameof(other)); this.Name = other.Name; this.Value = other.Value;