MultiDelegate.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. #pragma once
  2. //
  3. // a delegate
  4. //
  5. class Delegate
  6. {
  7. public:
  8. inline Delegate () : m_handlers (NULL)
  9. {
  10. m_DelegateID [0] = 0;
  11. m_MoveFirst = NULL;
  12. m_MoveLast = NULL;
  13. m_ForEachBreak = false;
  14. }
  15. inline Delegate (const char * DelegateID) : m_handlers (NULL)
  16. {
  17. strncpy (m_DelegateID, DelegateID, sizeof(m_DelegateID));
  18. m_MoveFirst = NULL;
  19. m_MoveLast = NULL;
  20. m_ForEachBreak = false;
  21. }
  22. inline virtual ~Delegate ()
  23. {
  24. RemoveAll ();
  25. }
  26. inline virtual void AddDelegateHandler (DelegateHandler * handler)
  27. {
  28. try
  29. {
  30. DSingleLock Lock (& ((DMutex &)m_handlers), true);
  31. if (! IsExist (handler))
  32. {
  33. m_handlers.Add (handler);
  34. AdjustPosition ();
  35. }
  36. else
  37. {
  38. delete handler;
  39. }
  40. }
  41. catch (...)
  42. {
  43. }
  44. }
  45. inline Delegate & operator += (DelegateHandler * handler)
  46. {
  47. AddDelegateHandler (handler);
  48. return *this;
  49. }
  50. inline virtual void RemoveDelegateHandler (DelegateHandler * handler)
  51. {
  52. try
  53. {
  54. RemoveHandler (handler);
  55. delete handler;
  56. }
  57. catch (...)
  58. {
  59. }
  60. }
  61. inline Delegate & operator -= (DelegateHandler * handler)
  62. {
  63. RemoveDelegateHandler (handler);
  64. return *this;
  65. }
  66. inline void operator () (const void * sender, DelegateArgs * arg)
  67. {
  68. Invoke (sender, arg);
  69. }
  70. inline virtual void Invoke (const void * sender, DelegateArgs * arg)
  71. {
  72. int Size = GetSize ();
  73. if (Size == 0)
  74. return;
  75. DSingleLock Lock (& m_handlers.GetMutex (), true);
  76. if (Size <= DelegateMaxHandler)
  77. {
  78. DelegateHandler * arTemp [DelegateMaxHandler];
  79. const Array <DelegateHandler *> & ar = m_handlers;
  80. int NbOfElem = ar.CopyTo (arTemp, DelegateMaxHandler);
  81. for (int Index=0; Index<NbOfElem; Index++)
  82. {
  83. DelegateHandler * h = arTemp[Index];
  84. #ifndef _DEBUG
  85. try
  86. #endif
  87. {
  88. h->Invoke (sender, arg);
  89. }
  90. #ifndef _DEBUG
  91. catch (...)
  92. {
  93. Beep (1000, 1000);
  94. OnDelegateInvokeFailed (h);
  95. }
  96. #endif
  97. }
  98. }
  99. else
  100. {
  101. FixArray <DelegateHandler *> arTemp (m_handlers.GetSize ());
  102. const Array <DelegateHandler *> & ar = m_handlers;
  103. int NbOfElem = ar.CopyTo (arTemp);
  104. for (int Index=0; Index<NbOfElem; Index++)
  105. {
  106. DelegateHandler * h = arTemp[Index];
  107. #ifndef _DEBUG
  108. try
  109. #endif
  110. {
  111. h->Invoke (sender, arg);
  112. }
  113. #ifndef _DEBUG
  114. catch (...)
  115. {
  116. Beep (1000, 1000);
  117. OnDelegateInvokeFailed (h);
  118. }
  119. #endif
  120. }
  121. }
  122. }
  123. inline int GetSize () const
  124. {
  125. const Array <DelegateHandler *> * ar = & m_handlers;
  126. return ar->GetSize ();
  127. }
  128. inline bool IsEmpty () const
  129. {
  130. const Array <DelegateHandler *> * ar = & m_handlers;
  131. return ar->IsEmpty ();
  132. }
  133. inline bool IsExist (const DelegateHandler * handler) const
  134. {
  135. for (ExclusiveArrayOfPtr <DelegateHandler *>::Iterator Iter (m_handlers); Iter; Iter++)
  136. {
  137. DelegateHandler * curHandler = Iter ();
  138. if (curHandler->IsEqual (* handler))
  139. return true;
  140. }
  141. return false;
  142. }
  143. inline void RemoveAll ()
  144. {
  145. m_handlers.RemoveAll ();
  146. }
  147. inline virtual void AddFirst (DelegateHandler * handler)
  148. {
  149. try
  150. {
  151. DSingleLock Lock (& ((DMutex &)m_handlers), true);
  152. if (! IsExist (handler))
  153. m_handlers.InsertFirst (handler);
  154. else
  155. delete handler;
  156. }
  157. catch (...)
  158. {
  159. }
  160. }
  161. inline virtual void AddLast (DelegateHandler * handler)
  162. {
  163. try
  164. {
  165. DSingleLock Lock (& ((DMutex &)m_handlers), true);
  166. if (! IsExist (handler))
  167. m_handlers.InsertLast (handler);
  168. else
  169. delete handler;
  170. }
  171. catch (...)
  172. {
  173. }
  174. }
  175. template <class pfnForEach> void ForEach (pfnForEach pFunction)
  176. {
  177. int Size = GetSize ();
  178. if (Size == 0)
  179. return;
  180. DSingleLock Lock (& m_handlers.GetMutex (), true);
  181. m_ForEachBreak = false;
  182. if (Size <= DelegateMaxHandler)
  183. {
  184. DelegateHandler * arTemp [DelegateMaxHandler];
  185. // int NbOfElem = m_handlers.CopyTo (arTemp, DelegateMaxHandler);
  186. const Array <DelegateHandler *> & ar = m_handlers;
  187. int NbOfElem = ar.CopyTo (arTemp, DelegateMaxHandler);
  188. for (int Index=0; Index<NbOfElem; Index++)
  189. {
  190. if (m_ForEachBreak)
  191. break;
  192. DelegateHandler * h = arTemp[Index];
  193. try
  194. {
  195. pFunction (h);
  196. }
  197. catch (...)
  198. {
  199. #ifdef _DEBUG
  200. Beep (1000, 1000);
  201. OnDelegateInvokeFailed (h);
  202. #endif
  203. }
  204. }
  205. }
  206. else
  207. {
  208. FixArray <DelegateHandler *> arTemp (m_handlers.GetSize ());
  209. // int NbOfElem = m_handlers.CopyTo (arTemp, DelegateMaxHandler);
  210. const Array <DelegateHandler *> & ar = m_handlers;
  211. int NbOfElem = ar.CopyTo (arTemp);
  212. for (int Index=0; Index<NbOfElem; Index++)
  213. {
  214. if (m_ForEachBreak)
  215. break;
  216. DelegateHandler * h = arTemp[Index];
  217. try
  218. {
  219. pFunction (h);
  220. }
  221. catch (...)
  222. {
  223. #ifdef _DEBUG
  224. Beep (1000, 1000);
  225. OnDelegateInvokeFailed (h);
  226. #endif
  227. }
  228. }
  229. }
  230. }
  231. template <class pfnForEach, class Parameter> void ForEach (pfnForEach pFunction, Parameter para)
  232. {
  233. int Size = GetSize ();
  234. if (Size == 0)
  235. return;
  236. DSingleLock Lock (& m_handlers.GetMutex (), true);
  237. m_ForEachBreak = false;
  238. if (Size <= DelegateMaxHandler)
  239. {
  240. DelegateHandler * arTemp [DelegateMaxHandler];
  241. // int NbOfElem = m_handlers.CopyTo (arTemp, DelegateMaxHandler);
  242. const Array <DelegateHandler *> & ar = m_handlers;
  243. int NbOfElem = ar.CopyTo (arTemp, DelegateMaxHandler);
  244. for (int Index=0; Index<NbOfElem; Index++)
  245. {
  246. if (m_ForEachBreak)
  247. break;
  248. DelegateHandler * h = arTemp[Index];
  249. try
  250. {
  251. pFunction (h, para);
  252. }
  253. catch (...)
  254. {
  255. #ifdef _DEBUG
  256. Beep (1000, 1000);
  257. OnDelegateInvokeFailed (h);
  258. #endif
  259. }
  260. }
  261. }
  262. else
  263. {
  264. FixArray <DelegateHandler *> arTemp (m_handlers.GetSize ());
  265. // int NbOfElem = m_handlers.CopyTo (arTemp, DelegateMaxHandler);
  266. const Array <DelegateHandler *> & ar = m_handlers;
  267. int NbOfElem = ar.CopyTo (arTemp);
  268. for (int Index=0; Index<NbOfElem; Index++)
  269. {
  270. if (m_ForEachBreak)
  271. break;
  272. DelegateHandler * h = arTemp[Index];
  273. try
  274. {
  275. pFunction (h, para);
  276. }
  277. catch (...)
  278. {
  279. #ifdef _DEBUG
  280. Beep (1000, 1000);
  281. OnDelegateInvokeFailed (h);
  282. #endif
  283. }
  284. }
  285. }
  286. }
  287. template <class pfnForEach> void ForEach (pfnForEach pFunction, Delegate * SThis)
  288. {
  289. int Size = GetSize ();
  290. if (Size == 0)
  291. return;
  292. DSingleLock Lock (& m_handlers.GetMutex (), true);
  293. m_ForEachBreak = false;
  294. if (Size <= DelegateMaxHandler)
  295. {
  296. DelegateHandler * arTemp [DelegateMaxHandler];
  297. // int NbOfElem = m_handlers.CopyTo (arTemp, DelegateMaxHandler);
  298. const Array <DelegateHandler *> & ar = m_handlers;
  299. int NbOfElem = ar.CopyTo (arTemp, DelegateMaxHandler);
  300. for (int Index=0; Index<NbOfElem; Index++)
  301. {
  302. if (m_ForEachBreak)
  303. break;
  304. DelegateHandler * h = arTemp[Index];
  305. try
  306. {
  307. pFunction (SThis, h);
  308. }
  309. catch (...)
  310. {
  311. #ifdef _DEBUG
  312. Beep (1000, 1000);
  313. OnDelegateInvokeFailed (h);
  314. #endif
  315. }
  316. }
  317. }
  318. else
  319. {
  320. FixArray <DelegateHandler *> arTemp (m_handlers.GetSize ());
  321. // int NbOfElem = m_handlers.CopyTo (arTemp, DelegateMaxHandler);
  322. const Array <DelegateHandler *> & ar = m_handlers;
  323. int NbOfElem = ar.CopyTo (arTemp);
  324. for (int Index=0; Index<NbOfElem; Index++)
  325. {
  326. if (m_ForEachBreak)
  327. break;
  328. DelegateHandler * h = arTemp[Index];
  329. try
  330. {
  331. pFunction (SThis, h);
  332. }
  333. catch (...)
  334. {
  335. #ifdef _DEBUG
  336. Beep (1000, 1000);
  337. OnDelegateInvokeFailed (h);
  338. #endif
  339. }
  340. }
  341. }
  342. }
  343. template <class pfnForEach, class Parameter> void ForEach (pfnForEach pFunction, Delegate * SThis, Parameter para)
  344. {
  345. int Size = GetSize ();
  346. if (Size == 0)
  347. return;
  348. DSingleLock Lock (& m_handlers.GetMutex (), true);
  349. m_ForEachBreak = false;
  350. if (Size <= DelegateMaxHandler)
  351. {
  352. DelegateHandler * arTemp [DelegateMaxHandler];
  353. // int NbOfElem = m_handlers.CopyTo (arTemp, DelegateMaxHandler);
  354. const Array <DelegateHandler *> & ar = m_handlers;
  355. int NbOfElem = ar.CopyTo (arTemp, DelegateMaxHandler);
  356. for (int Index=0; Index<NbOfElem; Index++)
  357. {
  358. if (m_ForEachBreak)
  359. break;
  360. DelegateHandler * h = arTemp[Index];
  361. try
  362. {
  363. pFunction (SThis, h, para);
  364. }
  365. catch (...)
  366. {
  367. #ifdef _DEBUG
  368. Beep (1000, 1000);
  369. OnDelegateInvokeFailed (h);
  370. #endif
  371. }
  372. }
  373. }
  374. else
  375. {
  376. FixArray <DelegateHandler *> arTemp (m_handlers.GetSize ());
  377. // int NbOfElem = m_handlers.CopyTo (arTemp, DelegateMaxHandler);
  378. const Array <DelegateHandler *> & ar = m_handlers;
  379. int NbOfElem = ar.CopyTo (arTemp);
  380. for (int Index=0; Index<NbOfElem; Index++)
  381. {
  382. if (m_ForEachBreak)
  383. break;
  384. DelegateHandler * h = arTemp[Index];
  385. try
  386. {
  387. pFunction (SThis, h, para);
  388. }
  389. catch (...)
  390. {
  391. #ifdef _DEBUG
  392. Beep (1000, 1000);
  393. OnDelegateInvokeFailed (h);
  394. #endif
  395. }
  396. }
  397. }
  398. }
  399. inline int TransferTo (Delegate & toDelegate)
  400. {
  401. m_handlers.TransferTo (toDelegate.m_handlers);
  402. return toDelegate.m_handlers.GetSize ();
  403. }
  404. inline int TransferFrom (Delegate & fromDelegate)
  405. {
  406. return fromDelegate.TransferTo (*this);
  407. }
  408. inline int TransferTo (Array <DelegateHandler *> & toArray)
  409. {
  410. m_handlers.TransferTo (toArray);
  411. return toArray.GetSize ();
  412. }
  413. inline int TransferFrom (Array <DelegateHandler *> & fromArray)
  414. {
  415. fromArray.TransferTo (m_handlers);
  416. return m_handlers.GetSize ();
  417. }
  418. inline int CopyTo (Array <DelegateHandler *> & toArray) const
  419. {
  420. for (ExclusiveArrayOfPtr <DelegateHandler *>::Iterator Iter (m_handlers); Iter; Iter++)
  421. {
  422. DelegateHandler * h = Iter ();
  423. toArray.Add (h->Clone ());
  424. }
  425. return toArray.GetSize ();
  426. }
  427. inline int CopyTo (Delegate & toDelegate) const
  428. {
  429. for (ExclusiveArrayOfPtr <DelegateHandler *>::Iterator Iter (m_handlers); Iter; Iter++)
  430. {
  431. DelegateHandler * h = Iter ();
  432. toDelegate.AddDelegateHandler (h->Clone ());
  433. }
  434. return toDelegate.GetSize ();
  435. }
  436. inline int IndexOf (DelegateHandler * h) const
  437. {
  438. return m_handlers.IndexOf (h);
  439. }
  440. inline int IndexOf (const char * Name) const
  441. {
  442. if (! Name)
  443. return -1;
  444. if (Name[0] == 0)
  445. return -1;
  446. for (ExclusiveArrayOfPtr <DelegateHandler *>::Iterator Iter (m_handlers); Iter; Iter++)
  447. {
  448. DelegateHandler * h = Iter ();
  449. if (h->m_Name[0] == 0)
  450. continue;
  451. if (strcmp (h->m_Name, Name) == 0)
  452. return Iter.Index ();
  453. }
  454. return -1;
  455. }
  456. const DelegateHandler * Find (const char * Name) const
  457. {
  458. if (! Name)
  459. return NULL;
  460. if (Name[0] == 0)
  461. return NULL;
  462. for (ExclusiveArrayOfPtr <DelegateHandler *>::Iterator Iter (m_handlers); Iter; Iter++)
  463. {
  464. DelegateHandler * h = Iter ();
  465. if (h->m_Name[0] == 0)
  466. continue;
  467. if (strcmp (h->m_Name, Name) == 0)
  468. return h;
  469. }
  470. return NULL;
  471. }
  472. DelegateHandler * Find (const char * Name)
  473. {
  474. if (! Name)
  475. return NULL;
  476. if (Name[0] == 0)
  477. return NULL;
  478. for (ExclusiveArrayOfPtr <DelegateHandler *>::Iterator Iter (m_handlers); Iter; Iter++)
  479. {
  480. DelegateHandler * h = Iter ();
  481. if (h->m_Name[0] == 0)
  482. continue;
  483. if (strcmp (h->m_Name, Name) == 0)
  484. return h;
  485. }
  486. return NULL;
  487. }
  488. template <typename class T> DelegateHandler * Find (const T * This)
  489. {
  490. if (! This)
  491. return NULL;
  492. for (ExclusiveArrayOfPtr <DelegateHandler *>::Iterator Iter (m_handlers); Iter; Iter++)
  493. {
  494. try
  495. {
  496. ClassDelegateHandler <T> * h = (ClassDelegateHandler <T> * ) Iter ();
  497. if (h->GetClassInstance () == This)
  498. return h;
  499. }
  500. catch (...)
  501. {
  502. }
  503. }
  504. return NULL;
  505. }
  506. template <typename class T> const DelegateHandler * Find (const T * This) const
  507. {
  508. if (! This)
  509. return NULL;
  510. for (ExclusiveArrayOfPtr <DelegateHandler *>::Iterator Iter (m_handlers); Iter; Iter++)
  511. {
  512. try
  513. {
  514. ClassDelegateHandler <T> * h = (ClassDelegateHandler <T> * ) Iter ();
  515. if (h->GetClassInstance () == This)
  516. return h;
  517. }
  518. catch (...)
  519. {
  520. }
  521. }
  522. return NULL;
  523. }
  524. template <typename class T> int FindAll (const T * This, Array <DelegateHandler *> * pArray)
  525. {
  526. if (! This)
  527. return 0;
  528. if (! pArray)
  529. return 0;
  530. for (ExclusiveArrayOfPtr <DelegateHandler *>::Iterator Iter (m_handlers); Iter; Iter++)
  531. {
  532. try
  533. {
  534. ClassDelegateHandler <T> * h = (ClassDelegateHandler <T> * ) Iter ();
  535. if (h->GetClassInstance () == This)
  536. pArray->Add (h);
  537. }
  538. catch (...)
  539. {
  540. }
  541. }
  542. return pArray->GetSize ();
  543. }
  544. inline DString GetDelegateID (void) const
  545. {
  546. return m_DelegateID;
  547. }
  548. DString ToString (void) const;
  549. void MoveToFirst (const DelegateHandler * h)
  550. {
  551. m_MoveFirst = h;
  552. AdjustPosition ();
  553. }
  554. void MoveLast (const DelegateHandler * h)
  555. {
  556. m_MoveLast = h;
  557. AdjustPosition ();
  558. }
  559. template <typename class T> void MoveToFirst (const T * This)
  560. {
  561. const DelegateHandler * h = Find (This);
  562. if (h)
  563. {
  564. m_MoveFirst = h;
  565. AdjustPosition ();
  566. }
  567. }
  568. template <typename class T> void MoveToLast (const T * This)
  569. {
  570. const DelegateHandler * h = Find (This);
  571. if (h)
  572. {
  573. m_MoveLast = h;
  574. AdjustPosition ();
  575. }
  576. }
  577. void SetForEachBreak (bool b)
  578. {
  579. m_ForEachBreak = b;
  580. }
  581. protected:
  582. inline bool RemoveHandler (const DelegateHandler * handler)
  583. {
  584. for (ExclusiveArrayOfPtr <DelegateHandler *>::Iterator Iter (m_handlers); Iter; Iter++)
  585. {
  586. DelegateHandler * curHandler = Iter ();
  587. if (curHandler->IsEqual (* handler))
  588. {
  589. m_handlers.RemoveAt (Iter);
  590. return true;
  591. }
  592. }
  593. return false;
  594. }
  595. inline void AdjustPosition (void)
  596. {
  597. if (m_handlers.IsEmpty ())
  598. return;
  599. if (m_MoveFirst)
  600. {
  601. DelegateHandler * const nh = (DelegateHandler * const) (m_MoveFirst);
  602. int Index = m_handlers.IndexOf (nh);
  603. if (Index > 0)
  604. m_handlers.Swap (0, Index);
  605. }
  606. if (m_MoveLast)
  607. {
  608. DelegateHandler * const nh = (DelegateHandler * const) (m_MoveLast);
  609. int Index = m_handlers.IndexOf (nh);
  610. if (Index < m_handlers.GetSize ()-1)
  611. m_handlers.Swap (0, Index);
  612. }
  613. }
  614. protected:
  615. char m_DelegateID [64];
  616. ExclusiveArrayOfPtr <DelegateHandler *> m_handlers;
  617. const DelegateHandler * m_MoveFirst;
  618. const DelegateHandler * m_MoveLast;
  619. bool m_ForEachBreak;
  620. protected:
  621. static const int DelegateMaxHandler = 64;
  622. };