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.

72 lines
2.1 KiB

7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
​
​
7 years ago
​
​
​
2 years ago
​
​
​
​
7 years ago
​
​
​
​
  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. if (0 == _instance) {
  11. _instance = new basicEventHeap;
  12. };
  13. return _instance;
  14. };
  15. void basicEventHeap::addHandler( basicEventHandler *handler, bool atBottom ){
  16. m_pHandlerMutex->lock();
  17. m_vHandlers.push_back( handler );
  18. m_pHandlerMutex->unlock();
  19. };
  20. void basicEventHeap::removeHandler( basicEventHandler *handler ){
  21. m_pHandlerMutex->lock();
  22. for( V_EVT_HANDLER::iterator idx = m_vHandlers.begin(); idx< m_vHandlers.end();idx++) {
  23. if( (*idx) == handler ) {
  24. m_vHandlers.erase( idx );
  25. break;
  26. };
  27. };
  28. m_pHandlerMutex->unlock();
  29. };
  30. void basicEventHeap::m_fThreadProc( void ){
  31. basicEventHandler *mpHandler = NULL;
  32. EVENT_CONTAINER *mpEvent = NULL;
  33. m_bThreadStatus = TH_WORKING;
  34. while( m_bThreadStatus < TH_ENDED ){
  35. if( m_bThreadStatus == TH_WORKING ){
  36. mpEvent = NULL;
  37. m_pDispatchingMutex->lock();
  38. if (!m_vEventQueue.empty()){
  39. mpEvent = m_vEventQueue.front();
  40. m_vEventQueue.pop();
  41. };
  42. m_pDispatchingMutex->unlock();
  43. if( mpEvent != NULL ){
  44. m_pHandlerMutex->lock();
  45. for( V_EVT_HANDLER::iterator idx = m_vHandlers.begin(); idx< m_vHandlers.end();idx++) {
  46. mpHandler = (*idx);
  47. if( mpHandler != NULL ) if( !mpHandler->dispatchEvent( mpEvent ) ) break; // Stop Propogation if false
  48. };
  49. m_pHandlerMutex->unlock();
  50. utilizeBasicEvent( mpEvent );
  51. } else SLEEP(1);
  52. };
  53. };
  54. };
  55. void basicEventHeap::broadcast( EVENT_CONTAINER *Evt ){
  56. if ( Evt ) {
  57. m_pDispatchingMutex->lock();
  58. m_vEventQueue.push( Evt );
  59. m_pDispatchingMutex->unlock();
  60. };
  61. };
  62. #undef LOG
  63. #undef LOG_ERROR