Browse Source

Improve Guard

pull/744/head
Anton Firszov 8 years ago
parent
commit
cf9476be96
  1. 73
      src/ImageSharp/Common/Helpers/Guard.cs

73
src/ImageSharp/Common/Helpers/Guard.cs

@ -21,12 +21,13 @@ namespace SixLabors.ImageSharp
/// <param name="parameterName">The name of the parameter that is to be checked.</param>
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null</exception>
[MethodImpl(InliningOptions.ShortMethod)]
[DebuggerStepThrough]
public static void NotNull<T>(T value, string parameterName)
where T : class
{
if (value is null)
{
throw new ArgumentNullException(parameterName);
ThrowArgumentNullException(parameterName);
}
}
@ -38,16 +39,17 @@ namespace SixLabors.ImageSharp
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
/// <exception cref="ArgumentException"><paramref name="value"/> is empty or contains only blanks.</exception>
[MethodImpl(InliningOptions.ShortMethod)]
[DebuggerStepThrough]
public static void NotNullOrWhiteSpace(string value, string parameterName)
{
if (value is null)
{
throw new ArgumentNullException(parameterName);
ThrowArgumentNullException(parameterName);
}
if (string.IsNullOrWhiteSpace(value))
{
throw new ArgumentException("Must not be empty or whitespace.", parameterName);
ThrowArgumentException("Must not be empty or whitespace.", parameterName);
}
}
@ -60,16 +62,17 @@ namespace SixLabors.ImageSharp
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
/// <exception cref="ArgumentException"><paramref name="value"/> is empty.</exception>
[MethodImpl(InliningOptions.ShortMethod)]
[DebuggerStepThrough]
public static void NotNullOrEmpty<T>(ICollection<T> value, string parameterName)
{
if (value is null)
{
throw new ArgumentNullException(parameterName);
ThrowArgumentNullException(parameterName);
}
if (value.Count == 0)
{
throw new ArgumentException("Must not be empty.", parameterName);
ThrowArgumentException("Must not be empty.", parameterName);
}
}
@ -84,12 +87,13 @@ namespace SixLabors.ImageSharp
/// <paramref name="value"/> is greater than the maximum value.
/// </exception>
[MethodImpl(InliningOptions.ShortMethod)]
[DebuggerStepThrough]
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 {value} must be less than {max}.");
ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be less than {max}.");
}
}
@ -105,12 +109,13 @@ namespace SixLabors.ImageSharp
/// <paramref name="value"/> is greater than the maximum value.
/// </exception>
[MethodImpl(InliningOptions.ShortMethod)]
[DebuggerStepThrough]
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 {value} must be less than or equal to {max}.");
ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be less than or equal to {max}.");
}
}
@ -126,12 +131,13 @@ namespace SixLabors.ImageSharp
/// <paramref name="value"/> is less than the minimum value.
/// </exception>
[MethodImpl(InliningOptions.ShortMethod)]
[DebuggerStepThrough]
public static void MustBeGreaterThan<TValue>(TValue value, TValue min, string parameterName)
where TValue : IComparable<TValue>
{
if (value.CompareTo(min) <= 0)
{
throw new ArgumentOutOfRangeException(
ThrowArgumentOutOfRangeException(
parameterName,
$"Value {value} must be greater than {min}.");
}
@ -149,12 +155,13 @@ namespace SixLabors.ImageSharp
/// <paramref name="value"/> is less than the minimum value.
/// </exception>
[MethodImpl(InliningOptions.ShortMethod)]
[DebuggerStepThrough]
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 {value} must be greater than or equal to {min}.");
ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be greater than or equal to {min}.");
}
}
@ -171,12 +178,13 @@ namespace SixLabors.ImageSharp
/// <paramref name="value"/> is less than the minimum value of greater than the maximum value.
/// </exception>
[MethodImpl(InliningOptions.ShortMethod)]
[DebuggerStepThrough]
public static void MustBeBetweenOrEqualTo<TValue>(TValue value, TValue min, TValue max, string parameterName)
where TValue : IComparable<TValue>
{
if (value.CompareTo(min) < 0 || value.CompareTo(max) > 0)
{
throw new ArgumentOutOfRangeException(parameterName, $"Value {value} must be greater than or equal to {min} and less than or equal to {max}.");
ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be greater than or equal to {min} and less than or equal to {max}.");
}
}
@ -191,11 +199,12 @@ namespace SixLabors.ImageSharp
/// <paramref name="target"/> is false
/// </exception>
[MethodImpl(InliningOptions.ShortMethod)]
[DebuggerStepThrough]
public static void IsTrue(bool target, string parameterName, string message)
{
if (!target)
{
throw new ArgumentException(message, parameterName);
ThrowArgumentException(message, parameterName);
}
}
@ -210,11 +219,12 @@ namespace SixLabors.ImageSharp
/// <paramref name="target"/> is true
/// </exception>
[MethodImpl(InliningOptions.ShortMethod)]
[DebuggerStepThrough]
public static void IsFalse(bool target, string parameterName, string message)
{
if (target)
{
throw new ArgumentException(message, parameterName);
ThrowArgumentException(message, parameterName);
}
}
@ -229,11 +239,12 @@ namespace SixLabors.ImageSharp
/// <paramref name="source"/> has less than <paramref name="minLength"/> items
/// </exception>
[MethodImpl(InliningOptions.ShortMethod)]
[DebuggerStepThrough]
public static void MustBeSizedAtLeast<T>(ReadOnlySpan<T> source, int minLength, string parameterName)
{
if (source.Length < minLength)
{
throw new ArgumentException($"Span-s must be at least of length {minLength}!", parameterName);
ThrowArgumentException($"Span-s must be at least of length {minLength}!", parameterName);
}
}
@ -246,6 +257,7 @@ namespace SixLabors.ImageSharp
/// <param name="destination">The destination span</param>
/// <param name="destinationParamName">The name of the argument for 'destination'</param>
[MethodImpl(InliningOptions.ShortMethod)]
[DebuggerStepThrough]
public static void DestinationShouldNotBeTooShort<TSource, TDest>(
ReadOnlySpan<TSource> source,
Span<TDest> destination,
@ -253,7 +265,7 @@ namespace SixLabors.ImageSharp
{
if (destination.Length < source.Length)
{
throw new ArgumentException($"Destination span is too short!", destinationParamName);
ThrowArgumentException($"Destination span is too short!", destinationParamName);
}
}
@ -268,46 +280,31 @@ namespace SixLabors.ImageSharp
/// <paramref name="source"/> has less than <paramref name="minLength"/> items
/// </exception>
[MethodImpl(InliningOptions.ShortMethod)]
[DebuggerStepThrough]
public static void MustBeSizedAtLeast<T>(Span<T> source, int minLength, string parameterName)
{
if (source.Length < minLength)
{
throw new ArgumentException($"Span-s must be at least of length {minLength}!", parameterName);
ThrowArgumentException($"Span-s must be at least of length {minLength}!", parameterName);
}
}
/// <summary>
/// Verifies that the given 'source' and 'dest' spans are at least of 'minLength' size.
/// Throwing an <see cref="ArgumentException"/> if the condition is not met.
/// </summary>
/// <typeparam name="TSource">The source element type</typeparam>
/// <typeparam name="TDest">The destination element type</typeparam>
/// <param name="source">The source span</param>
/// <param name="sourceParamName">The source parameter name</param>
/// <param name="dest">The destination span</param>
/// <param name="destParamName">The destination parameter name</param>
/// <param name="minLength">The minimum length</param>
public static void SpansMustBeSizedAtLeast<TSource, TDest>(
ReadOnlySpan<TSource> source,
string sourceParamName,
Span<TDest> dest,
string destParamName,
int minLength)
[MethodImpl(InliningOptions.ColdPath)]
private static void ThrowArgumentException(string message, string parameterName)
{
MustBeSizedAtLeast(source, minLength, sourceParamName);
MustBeSizedAtLeast(dest, minLength, destParamName);
throw new ArgumentException(message, parameterName);
}
[MethodImpl(InliningOptions.ColdPath)]
private static void ThrowArgumentException(string message, string parameterName)
private static void ThrowArgumentOutOfRangeException(string parameterName, string message)
{
throw new ArgumentException(message, parameterName);
throw new ArgumentOutOfRangeException(parameterName, message);
}
[MethodImpl(InliningOptions.ColdPath)]
private static void ThrowArgumentNullException(string message)
private static void ThrowArgumentNullException(string parameterName)
{
throw new ArgumentException(message);
throw new ArgumentNullException(parameterName);
}
}
}

Loading…
Cancel
Save