| | | 1 | | #nullable enable |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Data; |
| | | 6 | | using System.Runtime.CompilerServices; |
| | | 7 | | using JetBrains.Annotations; |
| | | 8 | | using UnityEngine; |
| | | 9 | | |
| | | 10 | | namespace Hertzole.ScriptableValues.Helpers |
| | | 11 | | { |
| | | 12 | | internal static partial class Guard |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Throws an <see cref="ArgumentNullException" /> if the given value is null. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <exception cref="ArgumentNullException" /> |
| | | 18 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 19 | | public static void IsNotNull<T>([System.Diagnostics.CodeAnalysis.NotNull][NoEnumeration] T? value, string name) |
| | 29856 | 20 | | { |
| | 29856 | 21 | | if (!EqualityHelper.IsNull(value)) |
| | 29730 | 22 | | { |
| | 29730 | 23 | | return; |
| | | 24 | | } |
| | | 25 | | |
| | 126 | 26 | | ThrowHelper.ThrowArgumentNullExceptionForIsNotNull<T>(name); |
| | 29730 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Throws a <see cref="ReadOnlyException" /> if the given value is read-only. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <exception cref="ReadOnlyException" /> |
| | | 33 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 34 | | public static void IsNotReadOnly(bool isReadOnly, string name) |
| | 34399 | 35 | | { |
| | | 36 | | #if UNITY_EDITOR |
| | | 37 | | // Read-only does not matter when not playing. |
| | 34399 | 38 | | if (!Application.isPlaying) |
| | 2052 | 39 | | { |
| | 2052 | 40 | | return; |
| | | 41 | | } |
| | | 42 | | #endif |
| | | 43 | | |
| | 32347 | 44 | | if (!isReadOnly) |
| | 31417 | 45 | | { |
| | 31417 | 46 | | return; |
| | | 47 | | } |
| | | 48 | | |
| | 930 | 49 | | ThrowHelper.ThrowIsReadOnlyException(name); |
| | 33469 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <inheritdoc cref="IsNotReadOnly(bool, string)" /> |
| | | 53 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 54 | | public static void IsNotReadOnly<T>(T value, string name) where T : ICanBeReadOnly |
| | 34141 | 55 | | { |
| | 34141 | 56 | | IsNotReadOnly(value.IsReadOnly, name); |
| | 33253 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <inheritdoc cref="IsNotReadOnly" /> |
| | | 60 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 61 | | public static void IsNotReadOnly<T>(ICollection<T> list, string name) |
| | 240 | 62 | | { |
| | 240 | 63 | | IsNotReadOnly(list.IsReadOnly, name); |
| | 210 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Throws an <see cref="ArgumentNullException" /> if the given value is null and T is a non-nullable type. |
| | | 68 | | /// </summary> |
| | | 69 | | /// <exception cref="ArgumentNullException" /> |
| | | 70 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 71 | | public static void ThrowIfNullAndNullsAreIllegal<T>(object? value, string paramName) |
| | 1038 | 72 | | { |
| | 1038 | 73 | | if (default(T) == null || value != null) |
| | 1002 | 74 | | { |
| | 1002 | 75 | | return; |
| | | 76 | | } |
| | | 77 | | |
| | 36 | 78 | | ThrowHelper.ThrowArgumentNullExceptionForIsNotNull<T>(paramName); |
| | 1002 | 79 | | } |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Throws an <see cref="ArgumentOutOfRangeException" /> if the actual value is less than the compare value. |
| | | 83 | | /// </summary> |
| | | 84 | | /// <exception cref="ArgumentOutOfRangeException" /> |
| | | 85 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 86 | | public static void IsGreaterThanOrEqualTo<T>(T actualValue, T compareValue, string paramName) where T : ICompara |
| | 516 | 87 | | { |
| | 516 | 88 | | if (actualValue.CompareTo(compareValue) >= 0) |
| | 360 | 89 | | { |
| | 360 | 90 | | return; |
| | | 91 | | } |
| | | 92 | | |
| | 156 | 93 | | ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsGreaterThanOrEqualTo(actualValue, compareValue, paramName); |
| | 360 | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Throws an <see cref="ArgumentOutOfRangeException" /> if the actual value is not less than the compare va |
| | | 98 | | /// </summary> |
| | | 99 | | /// <exception cref="ArgumentOutOfRangeException" /> |
| | | 100 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 101 | | public static void IsLessThan<T>(T actualValue, T compareValue, string paramName) where T : IComparable<T> |
| | 18 | 102 | | { |
| | 18 | 103 | | if (actualValue.CompareTo(compareValue) < 0) |
| | 6 | 104 | | { |
| | 6 | 105 | | return; |
| | | 106 | | } |
| | | 107 | | |
| | 12 | 108 | | ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsLessThan(compareValue, actualValue, paramName); |
| | 6 | 109 | | } |
| | | 110 | | |
| | | 111 | | /// <summary> |
| | | 112 | | /// Throws an <see cref="ArgumentOutOfRangeException" /> if the value is not in the specified range. |
| | | 113 | | /// </summary> |
| | | 114 | | /// <exception cref="ArgumentOutOfRangeException" /> |
| | | 115 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 116 | | public static void IsInRange<T>(T value, T minValue, T maxValue, string paramName) where T : IComparable<T> |
| | 876 | 117 | | { |
| | 876 | 118 | | if (value.CompareTo(minValue) >= 0 && value.CompareTo(maxValue) <= 0) |
| | 822 | 119 | | { |
| | 822 | 120 | | return; |
| | | 121 | | } |
| | | 122 | | |
| | 54 | 123 | | ThrowHelper.ThrowArgumentOutOfRangeExceptionForIsInRange(value, minValue, maxValue, paramName); |
| | 822 | 124 | | } |
| | | 125 | | } |
| | | 126 | | } |