|
|
|
@ -38,108 +38,128 @@ |
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Diagnostics; |
|
|
|
using System.Diagnostics.CodeAnalysis; |
|
|
|
using System.Runtime.CompilerServices; |
|
|
|
using System.Runtime.Serialization; |
|
|
|
|
|
|
|
#nullable enable |
|
|
|
|
|
|
|
namespace Avalonia.Collections.Pooled |
|
|
|
{ |
|
|
|
internal static class ThrowHelper |
|
|
|
{ |
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowArrayTypeMismatchException() |
|
|
|
{ |
|
|
|
throw new ArrayTypeMismatchException(); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowIndexOutOfRangeException() |
|
|
|
{ |
|
|
|
throw new IndexOutOfRangeException(); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowArgumentOutOfRangeException() |
|
|
|
{ |
|
|
|
throw new ArgumentOutOfRangeException(); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowArgumentException_DestinationTooShort() |
|
|
|
{ |
|
|
|
throw new ArgumentException("Destination too short."); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowArgumentException_OverlapAlignmentMismatch() |
|
|
|
{ |
|
|
|
throw new ArgumentException("Overlap alignment mismatch."); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowArgumentOutOfRange_IndexException() |
|
|
|
{ |
|
|
|
throw GetArgumentOutOfRangeException(ExceptionArgument.index, |
|
|
|
ExceptionResource.ArgumentOutOfRange_Index); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowIndexArgumentOutOfRange_NeedNonNegNumException() |
|
|
|
{ |
|
|
|
throw GetArgumentOutOfRangeException(ExceptionArgument.index, |
|
|
|
ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowValueArgumentOutOfRange_NeedNonNegNumException() |
|
|
|
{ |
|
|
|
throw GetArgumentOutOfRangeException(ExceptionArgument.value, |
|
|
|
ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowLengthArgumentOutOfRange_ArgumentOutOfRange_NeedNonNegNum() |
|
|
|
{ |
|
|
|
throw GetArgumentOutOfRangeException(ExceptionArgument.length, |
|
|
|
ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index() |
|
|
|
{ |
|
|
|
throw GetArgumentOutOfRangeException(ExceptionArgument.startIndex, |
|
|
|
ExceptionResource.ArgumentOutOfRange_Index); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count() |
|
|
|
{ |
|
|
|
throw GetArgumentOutOfRangeException(ExceptionArgument.count, |
|
|
|
ExceptionResource.ArgumentOutOfRange_Count); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowWrongKeyTypeArgumentException<T>(T key, Type targetType) |
|
|
|
{ |
|
|
|
// Generic key to move the boxing to the right hand side of throw
|
|
|
|
throw GetWrongKeyTypeArgumentException((object)key, targetType); |
|
|
|
throw GetWrongKeyTypeArgumentException((object?)key, targetType); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowWrongValueTypeArgumentException<T>(T value, Type targetType) |
|
|
|
{ |
|
|
|
// Generic key to move the boxing to the right hand side of throw
|
|
|
|
throw GetWrongValueTypeArgumentException((object)value, targetType); |
|
|
|
throw GetWrongValueTypeArgumentException((object?)value, targetType); |
|
|
|
} |
|
|
|
|
|
|
|
private static ArgumentException GetAddingDuplicateWithKeyArgumentException(object key) |
|
|
|
private static ArgumentException GetAddingDuplicateWithKeyArgumentException(object? key) |
|
|
|
{ |
|
|
|
return new ArgumentException($"Error adding duplicate with key: {key}."); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowAddingDuplicateWithKeyArgumentException<T>(T key) |
|
|
|
{ |
|
|
|
// Generic key to move the boxing to the right hand side of throw
|
|
|
|
throw GetAddingDuplicateWithKeyArgumentException((object)key); |
|
|
|
throw GetAddingDuplicateWithKeyArgumentException((object?)key); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowKeyNotFoundException<T>(T key) |
|
|
|
{ |
|
|
|
// Generic key to move the boxing to the right hand side of throw
|
|
|
|
throw GetKeyNotFoundException((object)key); |
|
|
|
throw GetKeyNotFoundException((object?)key); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowArgumentException(ExceptionResource resource) |
|
|
|
{ |
|
|
|
throw GetArgumentException(resource); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowArgumentException(ExceptionResource resource, ExceptionArgument argument) |
|
|
|
{ |
|
|
|
throw GetArgumentException(resource, argument); |
|
|
|
@ -150,141 +170,169 @@ namespace Avalonia.Collections.Pooled |
|
|
|
return new ArgumentNullException(GetArgumentName(argument)); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowArgumentNullException(ExceptionArgument argument) |
|
|
|
{ |
|
|
|
throw GetArgumentNullException(argument); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowArgumentNullException(ExceptionResource resource) |
|
|
|
{ |
|
|
|
throw new ArgumentNullException(GetResourceString(resource)); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowArgumentNullException(ExceptionArgument argument, ExceptionResource resource) |
|
|
|
{ |
|
|
|
throw new ArgumentNullException(GetArgumentName(argument), GetResourceString(resource)); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowArgumentOutOfRangeException(ExceptionArgument argument) |
|
|
|
{ |
|
|
|
throw new ArgumentOutOfRangeException(GetArgumentName(argument)); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource) |
|
|
|
{ |
|
|
|
throw GetArgumentOutOfRangeException(argument, resource); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowArgumentOutOfRangeException(ExceptionArgument argument, int paramNumber, ExceptionResource resource) |
|
|
|
{ |
|
|
|
throw GetArgumentOutOfRangeException(argument, paramNumber, resource); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowInvalidOperationException(ExceptionResource resource) |
|
|
|
{ |
|
|
|
throw GetInvalidOperationException(resource); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowInvalidOperationException(ExceptionResource resource, Exception e) |
|
|
|
{ |
|
|
|
throw new InvalidOperationException(GetResourceString(resource), e); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowSerializationException(ExceptionResource resource) |
|
|
|
{ |
|
|
|
throw new SerializationException(GetResourceString(resource)); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowSecurityException(ExceptionResource resource) |
|
|
|
{ |
|
|
|
throw new System.Security.SecurityException(GetResourceString(resource)); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowRankException(ExceptionResource resource) |
|
|
|
{ |
|
|
|
throw new RankException(GetResourceString(resource)); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowNotSupportedException(ExceptionResource resource) |
|
|
|
{ |
|
|
|
throw new NotSupportedException(GetResourceString(resource)); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowUnauthorizedAccessException(ExceptionResource resource) |
|
|
|
{ |
|
|
|
throw new UnauthorizedAccessException(GetResourceString(resource)); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowObjectDisposedException(string objectName, ExceptionResource resource) |
|
|
|
{ |
|
|
|
throw new ObjectDisposedException(objectName, GetResourceString(resource)); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowObjectDisposedException(ExceptionResource resource) |
|
|
|
{ |
|
|
|
throw new ObjectDisposedException(null, GetResourceString(resource)); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowNotSupportedException() |
|
|
|
{ |
|
|
|
throw new NotSupportedException(); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowAggregateException(List<Exception> exceptions) |
|
|
|
{ |
|
|
|
throw new AggregateException(exceptions); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowOutOfMemoryException() |
|
|
|
{ |
|
|
|
throw new OutOfMemoryException(); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowArgumentException_Argument_InvalidArrayType() |
|
|
|
{ |
|
|
|
throw new ArgumentException("Invalid array type."); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowInvalidOperationException_InvalidOperation_EnumNotStarted() |
|
|
|
{ |
|
|
|
throw new InvalidOperationException("Enumeration has not started."); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowInvalidOperationException_InvalidOperation_EnumEnded() |
|
|
|
{ |
|
|
|
throw new InvalidOperationException("Enumeration has ended."); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowInvalidOperationException_EnumCurrent(int index) |
|
|
|
{ |
|
|
|
throw GetInvalidOperationException_EnumCurrent(index); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion() |
|
|
|
{ |
|
|
|
throw new InvalidOperationException("Collection was modified during enumeration."); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowInvalidOperationException_InvalidOperation_EnumOpCantHappen() |
|
|
|
{ |
|
|
|
throw new InvalidOperationException("Invalid enumerator state: enumeration cannot proceed."); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowInvalidOperationException_InvalidOperation_NoValue() |
|
|
|
{ |
|
|
|
throw new InvalidOperationException("No value provided."); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowInvalidOperationException_ConcurrentOperationsNotSupported() |
|
|
|
{ |
|
|
|
throw new InvalidOperationException("Concurrent operations are not supported."); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowInvalidOperationException_HandleIsNotInitialized() |
|
|
|
{ |
|
|
|
throw new InvalidOperationException("Handle is not initialized."); |
|
|
|
} |
|
|
|
|
|
|
|
[DoesNotReturn] |
|
|
|
internal static void ThrowFormatException_BadFormatSpecifier() |
|
|
|
{ |
|
|
|
throw new FormatException("Bad format specifier."); |
|
|
|
@ -300,17 +348,17 @@ namespace Avalonia.Collections.Pooled |
|
|
|
return new InvalidOperationException(GetResourceString(resource)); |
|
|
|
} |
|
|
|
|
|
|
|
private static ArgumentException GetWrongKeyTypeArgumentException(object key, Type targetType) |
|
|
|
private static ArgumentException GetWrongKeyTypeArgumentException(object? key, Type targetType) |
|
|
|
{ |
|
|
|
return new ArgumentException($"Wrong key type. Expected {targetType}, got: '{key}'.", nameof(key)); |
|
|
|
} |
|
|
|
|
|
|
|
private static ArgumentException GetWrongValueTypeArgumentException(object value, Type targetType) |
|
|
|
private static ArgumentException GetWrongValueTypeArgumentException(object? value, Type targetType) |
|
|
|
{ |
|
|
|
return new ArgumentException($"Wrong value type. Expected {targetType}, got: '{value}'.", nameof(value)); |
|
|
|
} |
|
|
|
|
|
|
|
private static KeyNotFoundException GetKeyNotFoundException(object key) |
|
|
|
private static KeyNotFoundException GetKeyNotFoundException(object? key) |
|
|
|
{ |
|
|
|
return new KeyNotFoundException($"Key not found: {key}"); |
|
|
|
} |
|
|
|
@ -342,7 +390,7 @@ namespace Avalonia.Collections.Pooled |
|
|
|
// Aggressively inline so the jit evaluates the if in place and either drops the call altogether
|
|
|
|
// Or just leaves null test and call to the Non-returning ThrowHelper.ThrowArgumentNullException
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
|
|
internal static void IfNullAndNullsAreIllegalThenThrow<T>(object value, ExceptionArgument argName) |
|
|
|
internal static void IfNullAndNullsAreIllegalThenThrow<T>(object? value, ExceptionArgument argName) |
|
|
|
{ |
|
|
|
// Note that default(T) is not equal to null for value types except when T is Nullable<U>.
|
|
|
|
if (!(default(T) == null) && value == null) |
|
|
|
|