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.

195 lines
5.0KB

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