gloox  1.0.1
eventdispatcher.cpp
1 /*
2  Copyright (c) 2008-2012 by Jakob Schroeter <js@camaya.net>
3  This file is part of the gloox library. http://camaya.net/gloox
4 
5  This software is distributed under a license. The full license
6  agreement can be found in the file LICENSE in this distribution.
7  This software may not be copied, modified, sold or distributed
8  other than expressed in the named license agreement.
9 
10  This software is distributed without any warranty.
11 */
12 
13 
14 #include "eventdispatcher.h"
15 #include "eventhandler.h"
16 
17 namespace gloox
18 {
19 
21  {
22  }
23 
25  {
26  }
27 
28  void EventDispatcher::dispatch( const Event& event, const std::string& context, bool remove )
29  {
30  typedef ContextHandlerMap::iterator Ei;
31  std::pair<Ei, Ei> g = m_contextHandlers.equal_range( context );
32  Ei it = g.first;
33  Ei it2;
34  while( it != g.second )
35  {
36  it2 = it++;
37  (*it2).second->handleEvent( event );
38  if( remove )
39  m_contextHandlers.erase( it2 );
40  }
41  }
42 
43  void EventDispatcher::dispatch( const Event& event )
44  {
45  TypeHandlerMap::iterator it = m_typeHandlers.begin();
46  for( ; it != m_typeHandlers.end(); ++it )
47  {
48  if( (*it).first == event.eventType() )
49  (*it).second->handleEvent( event );
50  }
51  }
52 
53  void EventDispatcher::registerEventHandler( EventHandler* eh, const std::string& context )
54  {
55  if( !eh || context.empty() )
56  return;
57 
58  m_contextHandlers.insert( std::make_pair( context, eh ) );
59  }
60 
62  {
63  ContextHandlerMap::iterator it = m_contextHandlers.begin();
64  ContextHandlerMap::iterator it2;
65  while( it != m_contextHandlers.end() )
66  {
67  it2 = it++;
68  if( (*it2).second == eh )
69  m_contextHandlers.erase( it2 );
70  }
71  }
72 
73 }