mirror of https://github.com/SixLabors/ImageSharp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
6.1 KiB
139 lines
6.1 KiB
// Copyright (c) Six Labors and contributors.
|
|
// Licensed under the Apache License, Version 2.0.
|
|
|
|
using System;
|
|
using System.Diagnostics;
|
|
|
|
namespace SixLabors
|
|
{
|
|
/// <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>
|
|
/// <typeparam name="T">The type of the object to verify</typeparam>
|
|
[Conditional("DEBUG")]
|
|
public static void NotNull<T>(T target, string parameterName)
|
|
where T : class
|
|
{
|
|
if (target is null)
|
|
{
|
|
throw new ArgumentNullException(parameterName);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Verifies that the specified value is less than a maximum value
|
|
/// and throws an exception if it is not.
|
|
/// </summary>
|
|
/// <param name="value">The target value, which should be validated.</param>
|
|
/// <param name="max">The maximum value.</param>
|
|
/// <param name="parameterName">The name of the parameter that is to be checked.</param>
|
|
/// <typeparam name="TValue">The type of the value.</typeparam>
|
|
/// <exception cref="ArgumentException">
|
|
/// <paramref name="value"/> is greater than the maximum value.
|
|
/// </exception>
|
|
[Conditional("DEBUG")]
|
|
public static void MustBeLessThan<TValue>(TValue value, TValue max, string parameterName)
|
|
where TValue : IComparable<TValue>
|
|
{
|
|
if (value.CompareTo(max) >= 0)
|
|
{
|
|
throw new ArgumentOutOfRangeException(parameterName, $"Value must be less than {max}.");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Verifies that the specified value is less than or equal to a maximum value
|
|
/// and throws an exception if it is not.
|
|
/// </summary>
|
|
/// <param name="value">The target value, which should be validated.</param>
|
|
/// <param name="max">The maximum value.</param>
|
|
/// <param name="parameterName">The name of the parameter that is to be checked.</param>
|
|
/// <typeparam name="TValue">The type of the value.</typeparam>
|
|
/// <exception cref="ArgumentException">
|
|
/// <paramref name="value"/> is greater than the maximum value.
|
|
/// </exception>
|
|
[Conditional("DEBUG")]
|
|
public static void MustBeLessThanOrEqualTo<TValue>(TValue value, TValue max, string parameterName)
|
|
where TValue : IComparable<TValue>
|
|
{
|
|
if (value.CompareTo(max) > 0)
|
|
{
|
|
throw new ArgumentOutOfRangeException(parameterName, $"Value must be less than or equal to {max}.");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Verifies that the specified value is greater than a minimum value
|
|
/// and throws an exception if it is not.
|
|
/// </summary>
|
|
/// <param name="value">The target value, which should be validated.</param>
|
|
/// <param name="min">The minimum value.</param>
|
|
/// <param name="parameterName">The name of the parameter that is to be checked.</param>
|
|
/// <typeparam name="TValue">The type of the value.</typeparam>
|
|
/// <exception cref="ArgumentException">
|
|
/// <paramref name="value"/> is less than the minimum value.
|
|
/// </exception>
|
|
[Conditional("DEBUG")]
|
|
public static void MustBeGreaterThan<TValue>(TValue value, TValue min, string parameterName)
|
|
where TValue : IComparable<TValue>
|
|
{
|
|
if (value.CompareTo(min) <= 0)
|
|
{
|
|
throw new ArgumentOutOfRangeException(
|
|
parameterName,
|
|
$"Value must be greater than {min}.");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Verifies that the specified value is greater than or equal to a minimum value
|
|
/// and throws an exception if it is not.
|
|
/// </summary>
|
|
/// <param name="value">The target value, which should be validated.</param>
|
|
/// <param name="min">The minimum value.</param>
|
|
/// <param name="parameterName">The name of the parameter that is to be checked.</param>
|
|
/// <typeparam name="TValue">The type of the value.</typeparam>
|
|
/// <exception cref="ArgumentException">
|
|
/// <paramref name="value"/> is less than the minimum value.
|
|
/// </exception>
|
|
[Conditional("DEBUG")]
|
|
public static void MustBeGreaterThanOrEqualTo<TValue>(TValue value, TValue min, string parameterName)
|
|
where TValue : IComparable<TValue>
|
|
{
|
|
if (value.CompareTo(min) < 0)
|
|
{
|
|
throw new ArgumentOutOfRangeException(parameterName, $"Value must be greater than or equal to {min}.");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Verifies, that the `target` array has declared the length or longer.
|
|
/// </summary>
|
|
/// <typeparam name="T">The element type of the spans</typeparam>
|
|
/// <param name="target">The target array.</param>
|
|
/// <param name="minLength">The min length the array must have.</param>
|
|
/// <param name="parameterName">The name of the parameter that is to be checked.</param>
|
|
/// <exception cref="ArgumentException">
|
|
/// <paramref name="target"/> is true
|
|
/// </exception>
|
|
[Conditional("DEBUG")]
|
|
public static void MustBeSizedAtLeast<T>(T[] target, int minLength, string parameterName)
|
|
where T : struct
|
|
{
|
|
if (target.Length < minLength)
|
|
{
|
|
throw new ArgumentException($"The size must be at least {minLength}.", parameterName);
|
|
}
|
|
}
|
|
}
|
|
}
|