gloox 1.0
|
00001 /* 00002 Copyright (c) 2008-2009 by Jakob Schroeter <js@camaya.net> 00003 This file is part of the gloox library. http://camaya.net/gloox 00004 00005 This software is distributed under a license. The full license 00006 agreement can be found in the file LICENSE in this distribution. 00007 This software may not be copied, modified, sold or distributed 00008 other than expressed in the named license agreement. 00009 00010 This software is distributed without any warranty. 00011 */ 00012 00013 00014 #include "eventdispatcher.h" 00015 #include "eventhandler.h" 00016 00017 namespace gloox 00018 { 00019 00020 EventDispatcher::EventDispatcher() 00021 { 00022 } 00023 00024 EventDispatcher::~EventDispatcher() 00025 { 00026 } 00027 00028 void EventDispatcher::dispatch( const Event& event, const std::string& context, bool remove ) 00029 { 00030 typedef ContextHandlerMap::iterator Ei; 00031 std::pair<Ei, Ei> g = m_contextHandlers.equal_range( context ); 00032 Ei it = g.first; 00033 Ei it2; 00034 while( it != g.second ) 00035 { 00036 it2 = it++; 00037 (*it2).second->handleEvent( event ); 00038 if( remove ) 00039 m_contextHandlers.erase( it2 ); 00040 } 00041 } 00042 00043 void EventDispatcher::dispatch( const Event& event ) 00044 { 00045 TypeHandlerMap::iterator it = m_typeHandlers.begin(); 00046 for( ; it != m_typeHandlers.end(); ++it ) 00047 { 00048 if( (*it).first == event.eventType() ) 00049 (*it).second->handleEvent( event ); 00050 } 00051 } 00052 00053 void EventDispatcher::registerEventHandler( EventHandler* eh, const std::string& context ) 00054 { 00055 if( !eh || context.empty() ) 00056 return; 00057 00058 m_contextHandlers.insert( std::make_pair( context, eh ) ); 00059 } 00060 00061 void EventDispatcher::removeEventHandler( EventHandler* eh ) 00062 { 00063 ContextHandlerMap::iterator it = m_contextHandlers.begin(); 00064 ContextHandlerMap::iterator it2; 00065 while( it != m_contextHandlers.end() ) 00066 { 00067 it2 = it++; 00068 if( (*it2).second == eh ) 00069 m_contextHandlers.erase( it2 ); 00070 } 00071 } 00072 00073 }