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.

96 lines
1.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "basicEventHeap.h"
  2. #include "basicEventHandler.h"
  3. #define LOG(X) m_pLog->Log("basicEventHeap", this, X)
  4. #define LOG_ERROR(X) m_pLog->Log("basicEventHeap", this, _LOG_ERROR, X)
  5. basicEventHeap * basicEventHeap::_instance = 0;
  6. /**
  7. * Returns the single instance of the object.
  8. */
  9. basicEventHeap * basicEventHeap::getInstance()
  10. {
  11. if (0 == _instance)
  12. {
  13. _instance = new basicEventHeap;
  14. };
  15. return _instance;
  16. }
  17. void basicEventHeap::addHandler(basicEventHandler * handler, bool atBottom)
  18. {
  19. m_pHandlerMutex->lock();
  20. m_vHandlers.push_back(handler);
  21. m_pHandlerMutex->unlock();
  22. }
  23. void basicEventHeap::removeHandler(basicEventHandler * handler)
  24. {
  25. m_pHandlerMutex->lock();
  26. for (V_EVT_HANDLER::iterator idx = m_vHandlers.begin(); idx < m_vHandlers.end(); idx++)
  27. {
  28. if ((*idx) == handler)
  29. {
  30. m_vHandlers.erase(idx);
  31. break;
  32. };
  33. };
  34. m_pHandlerMutex->unlock();
  35. }
  36. void basicEventHeap::m_fThreadProc(void)
  37. {
  38. basicEventHandler * mpHandler = NULL;
  39. EVENT_CONTAINER * mpEvent = NULL;
  40. m_bThreadStatus = TH_WORKING;
  41. while (m_bThreadStatus < TH_ENDED)
  42. {
  43. if (m_bThreadStatus == TH_WORKING)
  44. {
  45. mpEvent = NULL;
  46. m_pDispatchingMutex->lock();
  47. if (! m_vEventQueue.empty())
  48. {
  49. mpEvent = m_vEventQueue.front();
  50. m_vEventQueue.pop();
  51. };
  52. m_pDispatchingMutex->unlock();
  53. if (mpEvent != NULL)
  54. {
  55. m_pHandlerMutex->lock();
  56. for (V_EVT_HANDLER::iterator idx = m_vHandlers.begin(); idx < m_vHandlers.end(); idx++)
  57. {
  58. mpHandler = (*idx);
  59. if (mpHandler != NULL)
  60. {
  61. if (! mpHandler->dispatchEvent(mpEvent))
  62. {
  63. break; // Stop Propogation if false
  64. }
  65. }
  66. };
  67. m_pHandlerMutex->unlock();
  68. utilizeBasicEvent(mpEvent);
  69. }
  70. else
  71. {
  72. SLEEP(1);
  73. }
  74. };
  75. };
  76. }
  77. void basicEventHeap::broadcast(EVENT_CONTAINER * Evt)
  78. {
  79. if (Evt)
  80. {
  81. m_pDispatchingMutex->lock();
  82. m_vEventQueue.push(Evt);
  83. m_pDispatchingMutex->unlock();
  84. };
  85. }
  86. #undef LOG
  87. #undef LOG_ERROR