Browse Source

Added a debug version of the Guard class so we get exceptions for debug asserts instead of a dialog.

af/merge-core
Dirk Lemstra 9 years ago
parent
commit
b4057ad78a
  1. 33
      src/ImageSharp/Common/Helpers/DebugGuard.cs
  2. 26
      src/ImageSharp/Common/Helpers/ThrowHelper.cs
  3. 12
      src/ImageSharp/Common/Memory/ArrayPointer{T}.cs
  4. 2
      src/ImageSharp/Image/ImageBase{TColor}.cs
  5. 4
      src/ImageSharp/MetaData/ImageFrameMetaData.cs
  6. 3
      src/ImageSharp/MetaData/ImageMetaData.cs
  7. 3
      src/ImageSharp/MetaData/ImageProperty.cs

33
src/ImageSharp/Common/Helpers/DebugGuard.cs

@ -0,0 +1,33 @@
// <copyright file="DebugGuard.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp
{
using System;
using System.Diagnostics;
/// <summary>
/// Provides methods to protect against invalid parameters for a DEBUG build.
/// </summary>
[DebuggerStepThrough]
internal static class DebugGuard
{
/// <summary>
/// Verifies, that the method parameter with specified object value is not null
/// and throws an exception if it is found to be so.
/// </summary>
/// <param name="target">The target object, which cannot be null.</param>
/// <param name="parameterName">The name of the parameter that is to be checked.</param>
/// <exception cref="ArgumentNullException"><paramref name="target"/> is null</exception>
[Conditional("DEBUG")]
public static void NotNull(object target, string parameterName)
{
if (target == null)
{
throw new ArgumentNullException(parameterName);
}
}
}
}

26
src/ImageSharp/Common/Helpers/ThrowHelper.cs

@ -1,26 +0,0 @@
// <copyright file="ThrowHelper.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp
{
using System;
using System.Runtime.CompilerServices;
/// <summary>
/// Helps removing exception throwing code from hot path by providing non-inlined exception thrower methods.
/// </summary>
internal static class ThrowHelper
{
/// <summary>
/// Throws an <see cref="ArgumentNullException"/>
/// </summary>
/// <param name="paramName">The parameter name</param>
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentNullException(string paramName)
{
throw new ArgumentNullException(nameof(paramName));
}
}
}

12
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;

2
src/ImageSharp/Image/ImageBase{TColor}.cs

@ -182,7 +182,7 @@ namespace ImageSharp
/// </param>
protected void CopyProperties(IImageBase other)
{
Debug.Assert(other != null);
DebugGuard.NotNull(other, nameof(other));
this.Configuration = other.Configuration;
}

4
src/ImageSharp/MetaData/ImageFrameMetaData.cs

@ -5,8 +5,6 @@
namespace ImageSharp
{
using System.Diagnostics;
/// <summary>
/// Encapsulates the metadata of an image frame.
/// </summary>
@ -28,7 +26,7 @@ namespace ImageSharp
/// </param>
internal ImageFrameMetaData(ImageFrameMetaData other)
{
Debug.Assert(other != null);
DebugGuard.NotNull(other, nameof(other));
this.FrameDelay = other.FrameDelay;
}

3
src/ImageSharp/MetaData/ImageMetaData.cs

@ -6,7 +6,6 @@
namespace ImageSharp
{
using System.Collections.Generic;
using System.Diagnostics;
/// <summary>
/// Encapsulates the metadata of an image.
@ -46,7 +45,7 @@ namespace ImageSharp
/// </param>
internal ImageMetaData(ImageMetaData other)
{
Debug.Assert(other != null);
DebugGuard.NotNull(other, nameof(other));
this.HorizontalResolution = other.HorizontalResolution;
this.VerticalResolution = other.VerticalResolution;

3
src/ImageSharp/MetaData/ImageProperty.cs

@ -6,7 +6,6 @@
namespace ImageSharp
{
using System;
using System.Diagnostics;
/// <summary>
/// Stores meta information about a image, like the name of the author,
@ -37,7 +36,7 @@ namespace ImageSharp
/// </param>
internal ImageProperty(ImageProperty other)
{
Debug.Assert(other != null);
DebugGuard.NotNull(other, nameof(other));
this.Name = other.Name;
this.Value = other.Value;

Loading…
Cancel
Save