#include "basicEventHeap.h" #include "basicEventHandler.h" #define LOG( X ) m_pLog->Log( "basicEventHeap", this, X ) #define LOG_ERROR( X ) m_pLog->Log( "basicEventHeap", this, _LOG_ERROR, X ) basicEventHeap* basicEventHeap::_instance = 0; /** * Returns the single instance of the object. */ basicEventHeap* basicEventHeap::getInstance() { if (0 == _instance) { _instance = new basicEventHeap; }; return _instance; }; void basicEventHeap::addHandler( basicEventHandler *handler, bool atBottom ){ m_vHandlers.push_back( handler ); }; void basicEventHeap::removeHandler( basicEventHandler *handler ){ for( V_EVT_HANDLER::iterator idx = m_vHandlers.begin(); idx< m_vHandlers.end();idx++) { if( (*idx) == handler ) { m_vHandlers.erase( idx ); break; }; }; }; void basicEventHeap::m_fThreadProc( void ){ basicEventHandler *mpHandler = NULL; EVENT_CONTAINER *mpEvent = NULL; m_bThreadStatus = TH_WORKING; while( m_bThreadStatus < TH_ENDED ){ if( m_bThreadStatus == TH_SLIPS ){ SLEEP( 10 ); }; if( m_bThreadStatus == TH_WORKING ){ mpEvent = NULL; m_pDispatchingMutex->lock(); if (!m_vEventQueue.empty()){ mpEvent = m_vEventQueue.front(); m_vEventQueue.pop(); }; m_pDispatchingMutex->unlock(); if( mpEvent!= NULL ){ m_pHandlerMutex->lock(); for( V_EVT_HANDLER::iterator idx = m_vHandlers.begin(); idx< m_vHandlers.end();idx++) { mpHandler = (*idx); if( mpHandler != NULL ) { mpHandler->dispatchEvent( mpEvent ); }; }; m_pHandlerMutex->unlock(); utilizeBasicEvent( mpEvent ); } else SLEEP(1); }; }; }; void basicEventHeap::broadcast( EVENT_CONTAINER *Evt ){ m_pDispatchingMutex->lock(); m_vEventQueue.push( Evt ); m_pDispatchingMutex->unlock(); }; #undef LOG #undef LOG_ERROR