Guest User

Untitled

a guest
Jul 19th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApplication1
  4. {
  5. public delegate void SmartAction<T>(SmartAction<T> self, T data);
  6.  
  7. class EventSource<T>
  8. {
  9. private event SmartAction<T> actions;
  10.  
  11. public void AddListener(SmartAction<T> eventHandler)
  12. {
  13. actions += eventHandler;
  14. }
  15.  
  16. public void RemoveListener(SmartAction<T> eventHandler)
  17. {
  18. actions -= eventHandler;
  19. }
  20.  
  21. public void Fire(T eventData)
  22. {
  23. if (actions == null)
  24. {
  25. return;
  26. }
  27. Delegate[] delegates = actions.GetInvocationList();
  28. for (int i = delegates.Length - 1; i >= 0; --i)
  29. {
  30. var action = (SmartAction<T>) delegates[i];
  31. action(action, eventData);
  32. }
  33. }
  34. }
  35.  
  36. internal class Program
  37. {
  38. public event SmartAction<int> ProgressChanged;
  39.  
  40. public static event Action EventA;
  41. public static event Action EventB;
  42.  
  43. public static void Main(string[] args)
  44. {
  45. new Program().TestSequence();
  46. }
  47.  
  48. public void TestSequence()
  49. {
  50. EventB += () => Console.WriteLine("b");
  51.  
  52. EventB();
  53. SmartActionExt.Copy2(out EventB);
  54. EventB();
  55. }
  56.  
  57. public void TestFirst()
  58. {
  59. Action a = () => Console.WriteLine("a");
  60.  
  61. EventA += a.First();
  62.  
  63. EventA();
  64. EventA();
  65. EventA();
  66. }
  67.  
  68. public void TestUntilEvent()
  69. {
  70. Action a = () => Console.WriteLine("a");
  71. Action b = () => Console.WriteLine("b");
  72.  
  73. EventA += a.Until(ref EventB);
  74.  
  75. EventA();
  76. EventA();
  77. EventB();
  78. EventA();
  79. }
  80.  
  81. public void TestConcurentModification()
  82. {
  83. // no exception
  84. Action b = () =>
  85. {
  86. Console.WriteLine("b");
  87. };
  88. Action a = () =>
  89. {
  90. Console.WriteLine("a");
  91. b();
  92. };
  93. Action c = () =>
  94. {
  95. Console.WriteLine("c");
  96. b();
  97. };
  98.  
  99. a = (a + c).Until(ref b);
  100.  
  101. a();
  102. b();
  103. a();
  104. }
  105.  
  106. public void TestUntil()
  107. {
  108. // result is b
  109. Action b = () => Console.WriteLine("b");
  110. Action a = () => Console.WriteLine("a");
  111.  
  112. a = a.Until(ref b);
  113.  
  114. a();
  115. b();
  116. a();
  117. }
  118.  
  119. public void Test3()
  120. {
  121. EventA += () => Console.WriteLine("a");
  122. Action b = () => Console.WriteLine("b");
  123. Action c = () => Console.WriteLine("c");
  124.  
  125. EventA += (b + c);
  126. EventA();
  127. }
  128.  
  129. public void Test1()
  130. {
  131. var source = new EventSource<int>();
  132. source.AddListener((self, n) =>
  133. {
  134. source.RemoveListener(self);
  135. Console.WriteLine(n);
  136. });
  137. source.Fire(5);
  138. source.Fire(25);
  139. }
  140.  
  141. public void DoSome()
  142. {
  143. ProgressChanged += (self, progress) => { ProgressChanged -= self; };
  144. }
  145. }
  146.  
  147. public static class SmartActionExt
  148. {
  149. public static void DoNothing() {}
  150.  
  151. public static Action First(this Action a)
  152. {
  153. Action sub = null;
  154. sub = () =>
  155. {
  156. sub = DoNothing;
  157. a();
  158. };
  159. return () => sub();
  160. }
  161.  
  162. // TODO CombineEventSource.FromSequence(ref eventA, ref eventB)
  163. // pass to Until as second param. Via overload of Until (CombineEventSource must implement += operator)
  164. public static Action Until(this Action a, ref Action b)
  165. {
  166. Action sub = () => a();
  167. Action result = () => sub();
  168. b += () =>
  169. {
  170. sub = DoNothing;
  171. };
  172. return result;
  173. }
  174.  
  175. public static Action Copy(ref Action a)
  176. {
  177. return a;
  178. }
  179.  
  180. public static void Copy2(out Action a)
  181. {
  182. a = null;
  183. }
  184.  
  185. /* public static Action Sequence(ref Action event1, ref Action event2)
  186. {
  187.  
  188. return a += b;
  189. }*/
  190. }
  191. }
Add Comment
Please, Sign In to add comment