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.

WeakActionStruct.cs 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Reflection;
  3. using System.Runtime.CompilerServices;
  4. using System.Runtime.InteropServices;
  5. //careful, you must handle the destruction of the GCHandles!
  6. namespace BetterWeakEvents
  7. {
  8. public struct WeakAction<T1, T2> : IEquatable<WeakAction<T1, T2>>
  9. {
  10. public WeakAction(Action<T1, T2> listener)
  11. {
  12. ObjectRef = GCHandle.Alloc(listener.Target, GCHandleType.Weak);
  13. Method = listener.Method;
  14. if (Method.DeclaringType.GetCustomAttributes(typeof(CompilerGeneratedAttribute), false).Length != 0)
  15. throw new ArgumentException("Cannot create weak event to anonymous method with closure.");
  16. }
  17. public bool Invoke(T1 data1, T2 data2)
  18. {
  19. if (ObjectRef.IsAllocated && ObjectRef.Target != null)
  20. {
  21. Method.Invoke(ObjectRef.Target, new object[] { data1, data2 });
  22. return true;
  23. }
  24. Release();
  25. return false;
  26. }
  27. public bool Equals(WeakAction<T1, T2> other)
  28. {
  29. return (Method.Equals(other.Method));
  30. }
  31. public void Release()
  32. {
  33. ObjectRef.Free();
  34. }
  35. readonly GCHandle ObjectRef;
  36. readonly MethodInfo Method;
  37. }
  38. public struct WeakAction<T> : IEquatable<WeakAction<T>>
  39. {
  40. public WeakAction(Action<T> listener)
  41. {
  42. ObjectRef = GCHandle.Alloc(listener.Target, GCHandleType.Weak);
  43. Method = listener.Method;
  44. if (Method.DeclaringType.GetCustomAttributes(typeof(CompilerGeneratedAttribute), false).Length != 0)
  45. throw new ArgumentException("Cannot create weak event to anonymous method with closure.");
  46. }
  47. public bool Invoke(T data)
  48. {
  49. if (ObjectRef.IsAllocated && ObjectRef.Target != null)
  50. {
  51. Method.Invoke(ObjectRef.Target, new object[] { data });
  52. return true;
  53. }
  54. Release();
  55. return false;
  56. }
  57. public bool Equals(WeakAction<T> other)
  58. {
  59. return (Method.Equals(other.Method));
  60. }
  61. public void Release()
  62. {
  63. ObjectRef.Free();
  64. }
  65. readonly GCHandle ObjectRef;
  66. readonly MethodInfo Method;
  67. }
  68. public struct WeakAction : IEquatable<WeakAction>
  69. {
  70. public WeakAction(Action listener)
  71. {
  72. ObjectRef = GCHandle.Alloc(listener.Target, GCHandleType.Weak);
  73. Method = listener.Method;
  74. if (Method.DeclaringType.GetCustomAttributes(typeof(CompilerGeneratedAttribute), false).Length != 0)
  75. throw new ArgumentException("Cannot create weak event to anonymous method with closure.");
  76. }
  77. public bool Invoke()
  78. {
  79. if (ObjectRef.IsAllocated && ObjectRef.Target != null)
  80. {
  81. Method.Invoke(ObjectRef.Target, null);
  82. return true;
  83. }
  84. Release();
  85. return false;
  86. }
  87. public bool Equals(WeakAction other)
  88. {
  89. return (Method.Equals(other.Method));
  90. }
  91. public void Release()
  92. {
  93. ObjectRef.Free();
  94. }
  95. readonly GCHandle ObjectRef;
  96. readonly MethodInfo Method;
  97. }
  98. }