| | | 1 | | #nullable enable |
| | | 2 | | |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Diagnostics.CodeAnalysis; |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | using UnityEngine; |
| | | 7 | | |
| | | 8 | | namespace Hertzole.ScriptableValues.Helpers |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Helper methods for ScriptableValues. |
| | | 12 | | /// </summary> |
| | | 13 | | public static class EqualityHelper |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Helper method for checking if two types are the same. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <remarks>This takes Unity objects into consideration.</remarks> |
| | | 19 | | /// <param name="x">The first object to compare.</param> |
| | | 20 | | /// <param name="y">The second object to compare.</param> |
| | | 21 | | /// <typeparam name="T">The type of the object.</typeparam> |
| | | 22 | | /// <returns><c>true</c> if the specified objects are equal; otherwise, <c>false</c>.</returns> |
| | | 23 | | public static bool Equals<T>(T x, T y) |
| | 197403 | 24 | | { |
| | | 25 | | // Do special checking on Unity objects. |
| | 197403 | 26 | | if (typeof(T).IsSubclassOf(typeof(Object)) || typeof(T) == typeof(Object)) |
| | 1042 | 27 | | { |
| | 1042 | 28 | | Object? xUnityObject = x as Object; |
| | 1042 | 29 | | Object? yUnityObject = y as Object; |
| | | 30 | | |
| | 1042 | 31 | | return xUnityObject == yUnityObject; |
| | | 32 | | } |
| | | 33 | | |
| | 196361 | 34 | | return EqualityComparer<T>.Default.Equals(x, y); |
| | 197403 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Helper method for checking if an object is null. |
| | | 39 | | /// </summary> |
| | | 40 | | /// <remarks>This takes Unity objects into consideration.</remarks> |
| | | 41 | | /// <param name="obj">The object to check.</param> |
| | | 42 | | /// <typeparam name="T">The type of the object.</typeparam> |
| | | 43 | | /// <returns><c>true</c> if the object is null; otherwise <c>false</c>.</returns> |
| | | 44 | | public static bool IsNull<T>([NotNullWhen(false)] T? obj) |
| | 31368 | 45 | | { |
| | | 46 | | // Do special checking on Unity objects. |
| | 31368 | 47 | | if (typeof(T).IsSubclassOf(typeof(Object)) || typeof(T) == typeof(Object)) |
| | 26160 | 48 | | { |
| | 26160 | 49 | | Object? unityObject = obj as Object; |
| | 26160 | 50 | | return unityObject == null; |
| | | 51 | | } |
| | | 52 | | |
| | 5208 | 53 | | return obj == null; |
| | 31368 | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Helper method to check if the given object is the same type as the provided generic type. |
| | | 58 | | /// </summary> |
| | | 59 | | /// <param name="value">The object value to check.</param> |
| | | 60 | | /// <param name="newValue">The value as the generic value.</param> |
| | | 61 | | /// <typeparam name="TType">The type to match.</typeparam> |
| | | 62 | | /// <returns><c>true</c> if the value is the same type; otherwise <c>false</c>.</returns> |
| | | 63 | | /// <exception cref="System.ArgumentNullException"><paramref name="value" /> is <c>null</c>.</exception> |
| | | 64 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 65 | | public static bool IsSameType<TType>(object? value, [NotNullWhen(true)] out TType? newValue) |
| | 222 | 66 | | { |
| | 222 | 67 | | if (value is TType newValueT) |
| | 204 | 68 | | { |
| | 204 | 69 | | newValue = newValueT; |
| | 204 | 70 | | return true; |
| | | 71 | | } |
| | | 72 | | |
| | 18 | 73 | | Guard.IsNotNull(value, nameof(value)); |
| | | 74 | | |
| | | 75 | | #if DEBUG |
| | 18 | 76 | | Debug.LogError($"{typeof(TType)} is not assignable from {value.GetType()}."); |
| | | 77 | | #endif |
| | 18 | 78 | | newValue = default; |
| | 18 | 79 | | return false; |
| | 222 | 80 | | } |
| | | 81 | | } |
| | | 82 | | } |