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.

WeakEvent.cs 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using Svelto.DataStructures;
  2. using System;
  3. using System.Reflection;
  4. namespace Svelto.WeakEvents
  5. {
  6. public class WeakEvent
  7. {
  8. public static WeakEvent operator+(WeakEvent c1, Action x)
  9. {
  10. if (c1 == null) c1 = new WeakEvent();
  11. c1.Add(x);
  12. return c1;
  13. }
  14. public static WeakEvent operator-(WeakEvent c1, Action x)
  15. {
  16. DesignByContract.Check.Require(x != null);
  17. c1.Remove(x);
  18. return c1;
  19. }
  20. public void Add(Action x)
  21. {
  22. _subscribers.Add(new WeakActionStruct(x));
  23. }
  24. public void Remove(Action x)
  25. {
  26. RemoveInternal(x.Target, x.GetMethodInfoEx());
  27. }
  28. public void Invoke()
  29. {
  30. for (int i = 0; i < _subscribers.Count; i++)
  31. if (_subscribers[i].Invoke() == false)
  32. _subscribers.UnorderedRemoveAt(i--);
  33. }
  34. void RemoveInternal(object thisObject, MethodInfo thisMethod)
  35. {
  36. for (int i = 0; i < _subscribers.Count; ++i)
  37. {
  38. var otherObject = _subscribers[i];
  39. if (otherObject.IsMatch(thisObject, thisMethod))
  40. {
  41. _subscribers.UnorderedRemoveAt(i);
  42. break;
  43. }
  44. }
  45. }
  46. ~WeakEvent()
  47. {
  48. for (int i = 0; i < _subscribers.Count; i++)
  49. _subscribers[i].Dispose();
  50. }
  51. readonly FasterList<WeakActionStruct>
  52. _subscribers = new FasterList<WeakActionStruct>();
  53. }
  54. public class WeakEvent<T1>
  55. {
  56. public static WeakEvent<T1> operator+(WeakEvent<T1> c1, Action<T1> x)
  57. {
  58. if (c1 == null) c1 = new WeakEvent<T1>();
  59. c1.Add(x);
  60. return c1;
  61. }
  62. public static WeakEvent<T1> operator-(WeakEvent<T1> c1, Action<T1> x)
  63. {
  64. DesignByContract.Check.Require(x != null);
  65. c1.Remove(x);
  66. return c1;
  67. }
  68. public void Add(Action<T1> x)
  69. {
  70. _subscribers.Add(new WeakActionStruct<T1>(x));
  71. }
  72. public void Remove(Action<T1> x)
  73. {
  74. RemoveInternal(x.Target, x.GetMethodInfoEx());
  75. }
  76. public void Invoke(T1 arg1)
  77. {
  78. args[0] = arg1;
  79. for (int i = 0; i < _subscribers.Count; i++)
  80. if (_subscribers[i].Invoke(args) == false)
  81. _subscribers.UnorderedRemoveAt(i--);
  82. }
  83. void RemoveInternal(object thisObject, MethodInfo thisMethod)
  84. {
  85. for (int i = 0; i < _subscribers.Count; ++i)
  86. {
  87. var otherObject = _subscribers[i];
  88. if (otherObject.IsMatch(thisObject, thisMethod))
  89. {
  90. _subscribers.UnorderedRemoveAt(i);
  91. break;
  92. }
  93. }
  94. }
  95. ~WeakEvent()
  96. {
  97. for (int i = 0; i < _subscribers.Count; i++)
  98. _subscribers[i].Dispose();
  99. }
  100. readonly object[] args = new object[1];
  101. readonly FasterList<WeakActionStruct<T1>>
  102. _subscribers = new FasterList<WeakActionStruct<T1>>();
  103. }
  104. public class WeakEvent<T1, T2>
  105. {
  106. public static WeakEvent<T1, T2> operator+(WeakEvent<T1, T2> c1, Action<T1, T2> x)
  107. {
  108. if (c1 == null) c1 = new WeakEvent<T1, T2>();
  109. c1._subscribers.Add(new WeakActionStruct<T1, T2>(x));
  110. return c1;
  111. }
  112. public static WeakEvent<T1, T2> operator-(WeakEvent<T1, T2> c1, Action<T1, T2> x)
  113. {
  114. DesignByContract.Check.Require(x != null);
  115. c1.Remove(x);
  116. return c1;
  117. }
  118. public void Add(Action<T1, T2> x)
  119. {
  120. _subscribers.Add(new WeakActionStruct<T1, T2>(x));
  121. }
  122. public void Remove(Action<T1, T2> x)
  123. {
  124. RemoveInternal(x.Target, x.GetMethodInfoEx());
  125. }
  126. public void Invoke(T1 arg1, T2 arg2)
  127. {
  128. args[0] = arg1;
  129. args[1] = arg2;
  130. for (int i = 0; i < _subscribers.Count; i++)
  131. if (_subscribers[i].Invoke(args) == false)
  132. _subscribers.UnorderedRemoveAt(i--);
  133. }
  134. void RemoveInternal(object thisObject, MethodInfo thisMethod)
  135. {
  136. for (int i = 0; i < _subscribers.Count; ++i)
  137. {
  138. var otherObject = _subscribers[i];
  139. if (otherObject.IsMatch(thisObject, thisMethod))
  140. {
  141. _subscribers.UnorderedRemoveAt(i);
  142. break;
  143. }
  144. }
  145. }
  146. ~WeakEvent()
  147. {
  148. for (int i = 0; i < _subscribers.Count; i++)
  149. _subscribers[i].Dispose();
  150. }
  151. readonly object[] args = new object[2];
  152. readonly FasterList<WeakActionStruct<T1, T2>>
  153. _subscribers = new FasterList<WeakActionStruct<T1, T2>>();
  154. }
  155. }