using System; namespace Svelto.DataStructures { class HashableWeakRef : IEquatable> where T : class { public bool isAlive { get { return _weakRef.IsAlive; } } public T Target { get { return (T)_weakRef.Target; } } public HashableWeakRef(T target) { _weakRef = new WeakReference(target); _hash = target.GetHashCode(); } public static bool operator !=(HashableWeakRef a, HashableWeakRef b) { return !(a == b); } public static bool operator ==(HashableWeakRef a, HashableWeakRef b) { if (a._hash != b._hash) return false; var tmpTargetA = (T) a._weakRef.Target; var tmpTargetB = (T) b._weakRef.Target; if (tmpTargetA == null || tmpTargetB == null) return false; return tmpTargetA == tmpTargetB; } public override bool Equals(object other) { if (other is HashableWeakRef) return this.Equals((HashableWeakRef)other); return false; } public bool Equals(HashableWeakRef other) { return (this == other); } public override int GetHashCode() { return _hash; } int _hash; WeakReference _weakRef; } }