Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.4KB

  1. using System;
  2. namespace Svelto.DataStructures
  3. {
  4. class HashableWeakRef<T> : IEquatable<HashableWeakRef<T>> where T : class
  5. {
  6. public bool isAlive { get { return _weakRef.IsAlive; } }
  7. public T Target { get { return (T)_weakRef.Target; } }
  8. public HashableWeakRef(T target)
  9. {
  10. _weakRef = new WeakReference(target);
  11. _hash = target.GetHashCode();
  12. }
  13. public static bool operator !=(HashableWeakRef<T> a, HashableWeakRef<T> b)
  14. {
  15. return !(a == b);
  16. }
  17. public static bool operator ==(HashableWeakRef<T> a, HashableWeakRef<T> b)
  18. {
  19. if (a._hash != b._hash)
  20. return false;
  21. var tmpTargetA = (T) a._weakRef.Target;
  22. var tmpTargetB = (T) b._weakRef.Target;
  23. if (tmpTargetA == null || tmpTargetB == null)
  24. return false;
  25. return tmpTargetA == tmpTargetB;
  26. }
  27. public override bool Equals(object other)
  28. {
  29. if (other is HashableWeakRef<T>)
  30. return this.Equals((HashableWeakRef<T>)other);
  31. return false;
  32. }
  33. public bool Equals(HashableWeakRef<T> other)
  34. {
  35. return (this == other);
  36. }
  37. public override int GetHashCode()
  38. {
  39. return _hash;
  40. }
  41. int _hash;
  42. WeakReference _weakRef;
  43. }
  44. }