// 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 /// The type of the object to verify [Conditional("DEBUG")] public static void NotNull(T target, string parameterName) where T : class { if (target is 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 `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); } } } }