MultiUnlockedDelegate.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #pragma once
  2. //
  3. // UnlockedDelegate
  4. //
  5. class UnlockedDelegate : public Delegate
  6. {
  7. public:
  8. inline UnlockedDelegate ()
  9. {
  10. }
  11. inline UnlockedDelegate (const char * DelegateID) : Delegate (DelegateID)
  12. {
  13. }
  14. inline virtual ~UnlockedDelegate ()
  15. {
  16. }
  17. inline virtual void Invoke (const void * sender, DelegateArgs * arg)
  18. {
  19. int Size = GetSize ();
  20. if (Size == 0)
  21. return;
  22. DSingleLock Lock (& m_handlers.GetMutex (), true);
  23. if (Size <= DelegateMaxHandler)
  24. {
  25. DelegateHandler * arTemp [DelegateMaxHandler];
  26. const Array <DelegateHandler *> & ar = m_handlers;
  27. int NbOfElem = ar.CopyTo (arTemp, DelegateMaxHandler);
  28. Lock.Unlock ();
  29. for (int Index=0; Index<NbOfElem; Index++)
  30. {
  31. DelegateHandler * h = arTemp[Index];
  32. try
  33. {
  34. h->Invoke (sender, arg);
  35. }
  36. catch (...)
  37. {
  38. #ifdef _DEBUG
  39. Beep (1000, 1000);
  40. OnDelegateInvokeFailed (h);
  41. #endif
  42. }
  43. }
  44. }
  45. else
  46. {
  47. FixArray <DelegateHandler *> arTemp (m_handlers.GetSize ());
  48. const Array <DelegateHandler *> & ar = m_handlers;
  49. int NbOfElem = ar.CopyTo (arTemp);
  50. Lock.Unlock ();
  51. for (int Index=0; Index<NbOfElem; Index++)
  52. {
  53. DelegateHandler * h = arTemp[Index];
  54. try
  55. {
  56. h->Invoke (sender, arg);
  57. }
  58. catch (...)
  59. {
  60. #ifdef _DEBUG
  61. Beep (1000, 1000);
  62. OnDelegateInvokeFailed (h);
  63. #endif
  64. }
  65. }
  66. }
  67. }
  68. template <class pfnForEach> void ForEach (pfnForEach pFunction)
  69. {
  70. int Size = GetSize ();
  71. if (Size == 0)
  72. return;
  73. DSingleLock Lock (& m_handlers.GetMutex (), true);
  74. m_ForEachBreak = false;
  75. if (Size <= DelegateMaxHandler)
  76. {
  77. DelegateHandler * arTemp [DelegateMaxHandler];
  78. // int NbOfElem = m_handlers.CopyTo (arTemp, DelegateMaxHandler);
  79. const Array <DelegateHandler *> & ar = m_handlers;
  80. int NbOfElem = ar.CopyTo (arTemp, DelegateMaxHandler);
  81. Lock.Unlock ();
  82. for (int Index=0; Index<NbOfElem; Index++)
  83. {
  84. if (m_ForEachBreak)
  85. break;
  86. DelegateHandler * h = arTemp[Index];
  87. try
  88. {
  89. pFunction (h);
  90. }
  91. catch (...)
  92. {
  93. #ifdef _DEBUG
  94. Beep (1000, 1000);
  95. OnDelegateInvokeFailed (h);
  96. #endif
  97. }
  98. }
  99. }
  100. else
  101. {
  102. FixArray <DelegateHandler *> arTemp (m_handlers.GetSize ());
  103. // int NbOfElem = m_handlers.CopyTo (arTemp, DelegateMaxHandler);
  104. const Array <DelegateHandler *> & ar = m_handlers;
  105. int NbOfElem = ar.CopyTo (arTemp);
  106. Lock.Unlock ();
  107. for (int Index=0; Index<NbOfElem; Index++)
  108. {
  109. if (m_ForEachBreak)
  110. break;
  111. DelegateHandler * h = arTemp[Index];
  112. try
  113. {
  114. pFunction (h);
  115. }
  116. catch (...)
  117. {
  118. #ifdef _DEBUG
  119. Beep (1000, 1000);
  120. OnDelegateInvokeFailed (h);
  121. #endif
  122. }
  123. }
  124. }
  125. }
  126. template <class pfnForEach, class Parameter> void ForEach (pfnForEach pFunction, Parameter para)
  127. {
  128. int Size = GetSize ();
  129. if (Size == 0)
  130. return;
  131. DSingleLock Lock (& m_handlers.GetMutex (), true);
  132. m_ForEachBreak = false;
  133. if (Size <= DelegateMaxHandler)
  134. {
  135. DelegateHandler * arTemp [DelegateMaxHandler];
  136. // int NbOfElem = m_handlers.CopyTo (arTemp, DelegateMaxHandler);
  137. const Array <DelegateHandler *> & ar = m_handlers;
  138. int NbOfElem = ar.CopyTo (arTemp, DelegateMaxHandler);
  139. Lock.Unlock ();
  140. for (int Index=0; Index<NbOfElem; Index++)
  141. {
  142. if (m_ForEachBreak)
  143. break;
  144. DelegateHandler * h = arTemp[Index];
  145. try
  146. {
  147. pFunction (h, para);
  148. }
  149. catch (...)
  150. {
  151. #ifdef _DEBUG
  152. Beep (1000, 1000);
  153. OnDelegateInvokeFailed (h);
  154. #endif
  155. }
  156. }
  157. }
  158. else
  159. {
  160. FixArray <DelegateHandler *> arTemp (m_handlers.GetSize ());
  161. // int NbOfElem = m_handlers.CopyTo (arTemp, DelegateMaxHandler);
  162. const Array <DelegateHandler *> & ar = m_handlers;
  163. int NbOfElem = ar.CopyTo (arTemp);
  164. Lock.Unlock ();
  165. for (int Index=0; Index<NbOfElem; Index++)
  166. {
  167. if (m_ForEachBreak)
  168. break;
  169. DelegateHandler * h = arTemp[Index];
  170. try
  171. {
  172. pFunction (h, para);
  173. }
  174. catch (...)
  175. {
  176. #ifdef _DEBUG
  177. Beep (1000, 1000);
  178. OnDelegateInvokeFailed (h);
  179. #endif
  180. }
  181. }
  182. }
  183. }
  184. template <class pfnForEach> void ForEach (pfnForEach pFunction, Delegate * SThis)
  185. {
  186. int Size = GetSize ();
  187. if (Size == 0)
  188. return;
  189. DSingleLock Lock (& m_handlers.GetMutex (), true);
  190. m_ForEachBreak = false;
  191. if (Size <= DelegateMaxHandler)
  192. {
  193. DelegateHandler * arTemp [DelegateMaxHandler];
  194. // int NbOfElem = m_handlers.CopyTo (arTemp, DelegateMaxHandler);
  195. const Array <DelegateHandler *> & ar = m_handlers;
  196. int NbOfElem = ar.CopyTo (arTemp, DelegateMaxHandler);
  197. Lock.Unlock ();
  198. for (int Index=0; Index<NbOfElem; Index++)
  199. {
  200. if (m_ForEachBreak)
  201. break;
  202. DelegateHandler * h = arTemp[Index];
  203. try
  204. {
  205. pFunction (SThis, h);
  206. }
  207. catch (...)
  208. {
  209. #ifdef _DEBUG
  210. Beep (1000, 1000);
  211. OnDelegateInvokeFailed (h);
  212. #endif
  213. }
  214. }
  215. }
  216. else
  217. {
  218. FixArray <DelegateHandler *> arTemp (m_handlers.GetSize ());
  219. // int NbOfElem = m_handlers.CopyTo (arTemp, DelegateMaxHandler);
  220. const Array <DelegateHandler *> & ar = m_handlers;
  221. int NbOfElem = ar.CopyTo (arTemp);
  222. Lock.Unlock ();
  223. for (int Index=0; Index<NbOfElem; Index++)
  224. {
  225. if (m_ForEachBreak)
  226. break;
  227. DelegateHandler * h = arTemp[Index];
  228. try
  229. {
  230. pFunction (SThis, h);
  231. }
  232. catch (...)
  233. {
  234. #ifdef _DEBUG
  235. Beep (1000, 1000);
  236. OnDelegateInvokeFailed (h);
  237. #endif
  238. }
  239. }
  240. }
  241. }
  242. template <class pfnForEach, class Parameter> void ForEach (pfnForEach pFunction, Delegate * SThis, Parameter para)
  243. {
  244. int Size = GetSize ();
  245. if (Size == 0)
  246. return;
  247. DSingleLock Lock (& m_handlers.GetMutex (), true);
  248. m_ForEachBreak = false;
  249. if (Size <= DelegateMaxHandler)
  250. {
  251. DelegateHandler * arTemp [DelegateMaxHandler];
  252. // int NbOfElem = m_handlers.CopyTo (arTemp, DelegateMaxHandler);
  253. const Array <DelegateHandler *> & ar = m_handlers;
  254. int NbOfElem = ar.CopyTo (arTemp, DelegateMaxHandler);
  255. Lock.Unlock ();
  256. for (int Index=0; Index<NbOfElem; Index++)
  257. {
  258. if (m_ForEachBreak)
  259. break;
  260. DelegateHandler * h = arTemp[Index];
  261. try
  262. {
  263. pFunction (SThis, h, para);
  264. }
  265. catch (...)
  266. {
  267. #ifdef _DEBUG
  268. Beep (1000, 1000);
  269. OnDelegateInvokeFailed (h);
  270. #endif
  271. }
  272. }
  273. }
  274. else
  275. {
  276. FixArray <DelegateHandler *> arTemp (m_handlers.GetSize ());
  277. // int NbOfElem = m_handlers.CopyTo (arTemp, DelegateMaxHandler);
  278. const Array <DelegateHandler *> & ar = m_handlers;
  279. int NbOfElem = ar.CopyTo (arTemp);
  280. Lock.Unlock ();
  281. for (int Index=0; Index<NbOfElem; Index++)
  282. {
  283. if (m_ForEachBreak)
  284. break;
  285. DelegateHandler * h = arTemp[Index];
  286. try
  287. {
  288. pFunction (SThis, h, para);
  289. }
  290. catch (...)
  291. {
  292. #ifdef _DEBUG
  293. Beep (1000, 1000);
  294. OnDelegateInvokeFailed (h);
  295. #endif
  296. }
  297. }
  298. }
  299. }
  300. };