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 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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._subscribers.Add(new WeakActionStruct(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.Target, x.GetMethodInfoEx());
  18. return c1;
  19. }
  20. public void Invoke()
  21. {
  22. for (int i = 0; i < _subscribers.Count; i++)
  23. if (_subscribers[i].Invoke() == false)
  24. _subscribers.UnorderedRemoveAt(i--);
  25. }
  26. void Remove(object thisObject, MethodInfo thisMethod)
  27. {
  28. for (int i = 0; i < _subscribers.Count; ++i)
  29. {
  30. var otherObject = _subscribers[i];
  31. if (otherObject.IsMatch(thisObject, thisMethod))
  32. {
  33. _subscribers.UnorderedRemoveAt(i);
  34. break;
  35. }
  36. }
  37. }
  38. ~WeakEvent()
  39. {
  40. for (int i = 0; i < _subscribers.Count; i++)
  41. _subscribers[i].Dispose();
  42. }
  43. readonly FasterList<WeakActionStruct>
  44. _subscribers = new FasterList<WeakActionStruct>();
  45. }
  46. public class WeakEvent<T1>
  47. {
  48. public static WeakEvent<T1> operator+(WeakEvent<T1> c1, Action<T1> x)
  49. {
  50. if (c1 == null) c1 = new WeakEvent<T1>();
  51. c1._subscribers.Add(new WeakActionStruct<T1>(x));
  52. return c1;
  53. }
  54. public static WeakEvent<T1> operator-(WeakEvent<T1> c1, Action<T1> x)
  55. {
  56. DesignByContract.Check.Require(x != null);
  57. c1.Remove(x.Target, x.GetMethodInfoEx());
  58. return c1;
  59. }
  60. public void Invoke(T1 arg1)
  61. {
  62. args[0] = arg1;
  63. for (int i = 0; i < _subscribers.Count; i++)
  64. if (_subscribers[i].Invoke(args) == false)
  65. _subscribers.UnorderedRemoveAt(i--);
  66. }
  67. void Remove(object thisObject, MethodInfo thisMethod)
  68. {
  69. for (int i = 0; i < _subscribers.Count; ++i)
  70. {
  71. var otherObject = _subscribers[i];
  72. if (otherObject.IsMatch(thisObject, thisMethod))
  73. {
  74. _subscribers.UnorderedRemoveAt(i);
  75. break;
  76. }
  77. }
  78. }
  79. ~WeakEvent()
  80. {
  81. for (int i = 0; i < _subscribers.Count; i++)
  82. _subscribers[i].Dispose();
  83. }
  84. readonly object[] args = new object[1];
  85. readonly FasterList<WeakActionStruct<T1>>
  86. _subscribers = new FasterList<WeakActionStruct<T1>>();
  87. }
  88. public class WeakEvent<T1, T2>
  89. {
  90. public static WeakEvent<T1, T2> operator+(WeakEvent<T1, T2> c1, Action<T1, T2> x)
  91. {
  92. if (c1 == null) c1 = new WeakEvent<T1, T2>();
  93. c1._subscribers.Add(new WeakActionStruct<T1, T2>(x));
  94. return c1;
  95. }
  96. public static WeakEvent<T1, T2> operator-(WeakEvent<T1, T2> c1, Action<T1, T2> x)
  97. {
  98. DesignByContract.Check.Require(x != null);
  99. c1.Remove(x.Target, x.GetMethodInfoEx());
  100. return c1;
  101. }
  102. public void Invoke(T1 arg1, T2 arg2)
  103. {
  104. args[0] = arg1;
  105. args[1] = arg2;
  106. for (int i = 0; i < _subscribers.Count; i++)
  107. if (_subscribers[i].Invoke(args) == false)
  108. _subscribers.UnorderedRemoveAt(i--);
  109. }
  110. void Remove(object thisObject, MethodInfo thisMethod)
  111. {
  112. for (int i = 0; i < _subscribers.Count; ++i)
  113. {
  114. var otherObject = _subscribers[i];
  115. if (otherObject.IsMatch(thisObject, thisMethod))
  116. {
  117. _subscribers.UnorderedRemoveAt(i);
  118. break;
  119. }
  120. }
  121. }
  122. ~WeakEvent()
  123. {
  124. for (int i = 0; i < _subscribers.Count; i++)
  125. _subscribers[i].Dispose();
  126. }
  127. readonly object[] args = new object[2];
  128. readonly FasterList<WeakActionStruct<T1, T2>>
  129. _subscribers = new FasterList<WeakActionStruct<T1, T2>>();
  130. }
  131. }