diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2018-06-10 12:06:29 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2018-06-10 12:06:29 -0400 |
commit | 235d67623de648499db521606e4b9033d35388e5 (patch) | |
tree | bae7a46e7de77270ff09d893e0187b4bc9e9a72f /src/SMAPI/Framework/StateTracking | |
parent | e27ada0f6187cdfd90b2812ba61de1e2e4f4b12d (diff) | |
download | SMAPI-235d67623de648499db521606e4b9033d35388e5.tar.gz SMAPI-235d67623de648499db521606e4b9033d35388e5.tar.bz2 SMAPI-235d67623de648499db521606e4b9033d35388e5.zip |
create watcher core (#310)
Diffstat (limited to 'src/SMAPI/Framework/StateTracking')
-rw-r--r-- | src/SMAPI/Framework/StateTracking/Comparers/GenericEqualsComparer.cs | 31 | ||||
-rw-r--r-- | src/SMAPI/Framework/StateTracking/FieldWatchers/WatcherFactory.cs | 8 |
2 files changed, 39 insertions, 0 deletions
diff --git a/src/SMAPI/Framework/StateTracking/Comparers/GenericEqualsComparer.cs b/src/SMAPI/Framework/StateTracking/Comparers/GenericEqualsComparer.cs new file mode 100644 index 00000000..cc1d6553 --- /dev/null +++ b/src/SMAPI/Framework/StateTracking/Comparers/GenericEqualsComparer.cs @@ -0,0 +1,31 @@ +using System.Collections.Generic; +using System.Runtime.CompilerServices; + +namespace StardewModdingAPI.Framework.StateTracking.Comparers +{ + /// <summary>Compares values using their <see cref="object.Equals(object)"/> method. This should only be used when <see cref="EquatableComparer{T}"/> won't work, since this doesn't validate whether they're comparable.</summary> + /// <typeparam name="T">The value type.</typeparam> + internal class GenericEqualsComparer<T> : IEqualityComparer<T> + { + /********* + ** Public methods + *********/ + /// <summary>Determines whether the specified objects are equal.</summary> + /// <returns>true if the specified objects are equal; otherwise, false.</returns> + /// <param name="x">The first object to compare.</param> + /// <param name="y">The second object to compare.</param> + public bool Equals(T x, T y) + { + if (x == null) + return y == null; + return x.Equals(y); + } + + /// <summary>Get a hash code for the specified object.</summary> + /// <param name="obj">The value.</param> + public int GetHashCode(T obj) + { + return RuntimeHelpers.GetHashCode(obj); + } + } +} diff --git a/src/SMAPI/Framework/StateTracking/FieldWatchers/WatcherFactory.cs b/src/SMAPI/Framework/StateTracking/FieldWatchers/WatcherFactory.cs index 4f1ac9f4..d7a02668 100644 --- a/src/SMAPI/Framework/StateTracking/FieldWatchers/WatcherFactory.cs +++ b/src/SMAPI/Framework/StateTracking/FieldWatchers/WatcherFactory.cs @@ -12,6 +12,14 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers /********* ** Public methods *********/ + /// <summary>Get a watcher which compares values using their <see cref="object.Equals(object)"/> method. This method should only be used when <see cref="ForEquatable{T}"/> won't work, since this doesn't validate whether they're comparable.</summary> + /// <typeparam name="T">The value type.</typeparam> + /// <param name="getValue">Get the current value.</param> + public static ComparableWatcher<T> ForGenericEquality<T>(Func<T> getValue) where T : struct + { + return new ComparableWatcher<T>(getValue, new GenericEqualsComparer<T>()); + } + /// <summary>Get a watcher for an <see cref="IEquatable{T}"/> value.</summary> /// <typeparam name="T">The value type.</typeparam> /// <param name="getValue">Get the current value.</param> |