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.

128 lines
2.7 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. if( atBottom ) {
  21. m_vHandlers.push_back( handler );
  22. } else {
  23. m_vHandlers.insert( m_vHandlers.begin(), handler );
  24. };
  25. m_pHandlerMutex->unlock();
  26. }
  27. void basicEventHeap::removeHandler(basicEventHandler * handler)
  28. {
  29. m_pHandlerMutex->lock();
  30. for (V_EVT_HANDLER::iterator idx = m_vHandlers.begin(); idx != m_vHandlers.end(); idx++)
  31. {
  32. if ((*idx) == handler)
  33. {
  34. m_vHandlers.erase(idx);
  35. break;
  36. };
  37. };
  38. m_pHandlerMutex->unlock();
  39. }
  40. void basicEventHeap::moveHandlerBottom( basicEventHandler * handler ){
  41. m_pHandlerMutex->lock();
  42. for (V_EVT_HANDLER::iterator idx = m_vHandlers.begin(); idx != m_vHandlers.end(); idx++)
  43. {
  44. if ((*idx) == handler)
  45. {
  46. m_vHandlers.erase(idx);
  47. break;
  48. };
  49. };
  50. m_vHandlers.push_back( handler );
  51. m_pHandlerMutex->unlock();
  52. }
  53. void basicEventHeap::moveHandlerTop( basicEventHandler * handler ){
  54. m_pHandlerMutex->lock();
  55. for (V_EVT_HANDLER::iterator idx = m_vHandlers.begin(); idx != m_vHandlers.end(); idx++)
  56. {
  57. if ((*idx) == handler)
  58. {
  59. m_vHandlers.erase(idx);
  60. break;
  61. };
  62. };
  63. m_vHandlers.insert( m_vHandlers.begin(), handler );
  64. m_pHandlerMutex->unlock();
  65. }
  66. void basicEventHeap::m_fThreadProc(void)
  67. {
  68. basicEventHandler * mpHandler = NULL;
  69. EVENT_CONTAINER * mpEvent = NULL;
  70. m_bThreadStatus = TH_WORKING;
  71. while (m_bThreadStatus < TH_ENDED)
  72. {
  73. if (m_bThreadStatus == TH_WORKING)
  74. {
  75. mpEvent = NULL;
  76. m_pDispatchingMutex->lock();
  77. if (! m_vEventQueue.empty())
  78. {
  79. mpEvent = m_vEventQueue.front();
  80. m_vEventQueue.pop();
  81. };
  82. m_pDispatchingMutex->unlock();
  83. if (mpEvent != NULL)
  84. {
  85. m_pHandlerMutex->lock();
  86. for (V_EVT_HANDLER::iterator idx = m_vHandlers.begin(); idx < m_vHandlers.end(); idx++)
  87. {
  88. mpHandler = (*idx);
  89. if (mpHandler != NULL)
  90. {
  91. if (! mpHandler->dispatchEvent(mpEvent))
  92. {
  93. break; // Stop Propogation if false
  94. }
  95. }
  96. };
  97. m_pHandlerMutex->unlock();
  98. utilizeBasicEvent(mpEvent);
  99. }
  100. else
  101. {
  102. SLEEP(1);
  103. }
  104. };
  105. };
  106. }
  107. void basicEventHeap::broadcast(EVENT_CONTAINER * Evt)
  108. {
  109. if (Evt)
  110. {
  111. m_pDispatchingMutex->lock();
  112. m_vEventQueue.push(Evt);
  113. m_pDispatchingMutex->unlock();
  114. };
  115. }
  116. #undef LOG
  117. #undef LOG_ERROR