// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Diagnostics;
namespace SixLabors
{
///
/// 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);
}
}
///
/// Verifies that the specified value is less than a maximum value
/// and throws an exception if it is not.
///
/// The target value, which should be validated.
/// The maximum value.
/// The name of the parameter that is to be checked.
/// The type of the value.
///
/// is greater than the maximum value.
///
[Conditional("DEBUG")]
public static void MustBeLessThan(TValue value, TValue max, string parameterName)
where TValue : IComparable
{
if (value.CompareTo(max) >= 0)
{
throw new ArgumentOutOfRangeException(parameterName, $"Value must be less than {max}.");
}
}
///
/// Verifies that the specified value is less than or equal to a maximum value
/// and throws an exception if it is not.
///
/// The target value, which should be validated.
/// The maximum value.
/// The name of the parameter that is to be checked.
/// The type of the value.
///
/// is greater than the maximum value.
///
[Conditional("DEBUG")]
public static void MustBeLessThanOrEqualTo(TValue value, TValue max, string parameterName)
where TValue : IComparable
{
if (value.CompareTo(max) > 0)
{
throw new ArgumentOutOfRangeException(parameterName, $"Value must be less than or equal to {max}.");
}
}
///
/// Verifies that the specified value is greater than a minimum value
/// and throws an exception if it is not.
///
/// The target value, which should be validated.
/// The minimum value.
/// The name of the parameter that is to be checked.
/// The type of the value.
///
/// is less than the minimum value.
///
[Conditional("DEBUG")]
public static void MustBeGreaterThan(TValue value, TValue min, string parameterName)
where TValue : IComparable
{
if (value.CompareTo(min) <= 0)
{
throw new ArgumentOutOfRangeException(
parameterName,
$"Value must be greater than {min}.");
}
}
///
/// Verifies that the specified value is greater than or equal to a minimum value
/// and throws an exception if it is not.
///
/// The target value, which should be validated.
/// The minimum value.
/// The name of the parameter that is to be checked.
/// The type of the value.
///
/// is less than the minimum value.
///
[Conditional("DEBUG")]
public static void MustBeGreaterThanOrEqualTo(TValue value, TValue min, string parameterName)
where TValue : IComparable
{
if (value.CompareTo(min) < 0)
{
throw new ArgumentOutOfRangeException(parameterName, $"Value must be greater than or equal to {min}.");
}
}
///
/// Verifies, that the method parameter with specified target value is true
/// and throws an exception if it is found to be so.
///
///
/// The target value, which cannot be false.
///
///
/// The name of the parameter that is to be checked.
///
///
/// The error message, if any to add to the exception.
///
///
/// is false
///
[Conditional("DEBUG")]
public static void IsTrue(bool target, string parameterName, string message)
{
if (!target)
{
throw new ArgumentException(message, parameterName);
}
}
///
/// Verifies, that the method parameter with specified target value is false
/// and throws an exception if it is found to be so.
///
/// The target value, which cannot be true.
/// The name of the parameter that is to be checked.
/// The error message, if any to add to the exception.
///
/// is true
///
[Conditional("DEBUG")]
public static void IsFalse(bool target, string parameterName, string message)
{
if (target)
{
throw new ArgumentException(message, parameterName);
}
}
///
/// Verifies, that the target span is of same size than the 'other' span.
///
/// The element type of the spans
/// The target span.
/// The 'other' span to compare 'target' to.
/// The name of the parameter that is to be checked.
///
/// is true
///
[Conditional("DEBUG")]
public static void MustBeSameSized(ReadOnlySpan target, ReadOnlySpan other, string parameterName)
where T : struct
{
if (target.Length != other.Length)
{
throw new ArgumentException("Span-s must be the same size!", parameterName);
}
}
///
/// Verifies, that the target span is of same size than the 'other' span.
///
/// The element type of the spans
/// The target span.
/// The 'other' span to compare 'target' to.
/// The name of the parameter that is to be checked.
///
/// is true
///
[Conditional("DEBUG")]
public static void MustBeSameSized(Span target, Span other, string parameterName)
where T : struct
{
if (target.Length != other.Length)
{
throw new ArgumentException("Span-s must be the same size!", parameterName);
}
}
///
/// Verifies, that the `target` span has the length of 'minSpan', or longer.
///
/// The element type of the spans
/// The target span.
/// The 'minSpan' span to compare 'target' to.
/// The name of the parameter that is to be checked.
///
/// is true
///
[Conditional("DEBUG")]
public static void MustBeSizedAtLeast(ReadOnlySpan target, ReadOnlySpan minSpan, string parameterName)
where T : struct
{
if (target.Length < minSpan.Length)
{
throw new ArgumentException($"Span-s must be at least of length {minSpan.Length}!", parameterName);
}
}
///
/// Verifies, that the `target` span has the length of 'minSpan', or longer.
///
/// The element type of the spans
/// The target span.
/// The 'minSpan' span to compare 'target' to.
/// The name of the parameter that is to be checked.
///
/// is true
///
[Conditional("DEBUG")]
public static void MustBeSizedAtLeast(Span target, Span minSpan, string parameterName)
where T : struct
{
if (target.Length < minSpan.Length)
{
throw new ArgumentException($"Span-s must be at least of length {minSpan.Length}!", parameterName);
}
}
///
/// Verifies, that the `target` array has declared the length or longer.
///
/// The element type of the spans
/// The target array.
/// The min length the array must have.
/// The name of the parameter that is to be checked.
///
/// is true
///
[Conditional("DEBUG")]
public static void MustBeSizedAtLeast(T[] target, int minLength, string parameterName)
where T : struct
{
if (target.Length < minLength)
{
throw new ArgumentException($"The size must be at least {minLength}.", parameterName);
}
}
}
}