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.

64 lines
2.6KB

  1. using System;
  2. using System.Runtime.Serialization;
  3. /// <span class="code-SummaryComment"><summary></span>
  4. /// Represents a weak reference, which references an object while still allowing
  5. /// that object to be reclaimed by garbage collection.
  6. /// <span class="code-SummaryComment"></summary></span>
  7. /// <span class="code-SummaryComment"><typeparam name="T">The type of the object that is referenced.</typeparam></span>
  8. namespace Svelto.DataStructures
  9. {
  10. [Serializable]
  11. public class WeakReference<T>
  12. : WeakReference where T : class
  13. {
  14. public bool IsValid { get { return Target != null && IsAlive == true; } }
  15. /// <span class="code-SummaryComment"><summary></span>
  16. /// Gets or sets the object (the target) referenced by the
  17. /// current WeakReference{T} object.
  18. /// <span class="code-SummaryComment"></summary></span>
  19. public new T Target
  20. {
  21. get
  22. {
  23. return (T)base.Target;
  24. }
  25. set
  26. {
  27. base.Target = value;
  28. }
  29. }
  30. /// <span class="code-SummaryComment"><summary></span>
  31. /// Initializes a new instance of the WeakReference{T} class, referencing
  32. /// the specified object.
  33. /// <span class="code-SummaryComment"></summary></span>
  34. /// <span class="code-SummaryComment"><param name="target">The object to reference.</param></span>
  35. public WeakReference(T target)
  36. : base(target)
  37. { }
  38. /// <span class="code-SummaryComment"><summary></span>
  39. /// Initializes a new instance of the WeakReference{T} class, referencing
  40. /// the specified object and using the specified resurrection tracking.
  41. /// <span class="code-SummaryComment"></summary></span>
  42. /// <span class="code-SummaryComment"><param name="target">An object to track.</param></span>
  43. /// <span class="code-SummaryComment"><param name="trackResurrection">Indicates when to stop tracking the object. </span>
  44. /// If true, the object is tracked
  45. /// after finalization; if false, the object is only tracked
  46. /// until finalization.<span class="code-SummaryComment"></param></span>
  47. public WeakReference(T target, bool trackResurrection)
  48. : base(target, trackResurrection)
  49. { }
  50. protected WeakReference(SerializationInfo info, StreamingContext context)
  51. : base(info, context)
  52. { }
  53. }
  54. public static class WeakReferenceUtility
  55. {
  56. public static bool IsValid(this WeakReference obj) { return obj != null && obj.IsAlive == true && obj.Target != null; }
  57. }
  58. }