Merge branch 'ryzom/ark-features' into main/gingo-test
[ryzomcore.git] / nel / src / misc / event_server.cpp
blobeceb783fe1100445dbb9aceab7d1d101106f1786
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "stdmisc.h"
19 #include "nel/misc/event_server.h"
20 #include "nel/misc/event_listener.h"
21 #include "nel/misc/event_emitter.h"
22 #include "nel/misc/events.h"
24 #ifdef DEBUG_NEW
25 #define new DEBUG_NEW
26 #endif
28 namespace NLMISC {
31 /*------------------------------------------------------------------*\
32 CEventServer()
33 \*------------------------------------------------------------------*/
34 CEventServer::CEventServer()
36 _Pumping= false;
39 CEventServer::~CEventServer()
41 std::list<CEvent*>::iterator itev = _Events.begin();
42 while(itev!=_Events.end())
44 delete *itev;
45 itev=_Events.erase (itev);
50 /*------------------------------------------------------------------*\
51 postEvent()
52 \*------------------------------------------------------------------*/
53 void CEventServer::postEvent(CEvent * event)
55 _Events.push_back(event);
60 /*------------------------------------------------------------------*\
61 pump()
62 \*------------------------------------------------------------------*/
63 void CEventServer::pump(bool allWindows)
65 // Avoid recurse (can arise if the process of an event decide to pump the server again....)
66 nlassert(!_Pumping);
67 _Pumping= true;
69 // **** submit emitters events
70 std::list<IEventEmitter*>::iterator item = _Emitters.begin();
72 // getting events from emitters
73 while(item!=_Emitters.end())
75 // ask emitters to submit their events to server
76 (*item)->submitEvents(*this, allWindows);
77 item++;
80 // **** process to listeners
81 std::list<CEvent*>::iterator itev = _Events.begin();
82 while(itev!=_Events.end())
84 // pump event
85 bool bDelete=pumpEvent(*itev);
86 if (bDelete)
87 delete *itev;
88 itev=_Events.erase (itev);
91 // end of pumping
92 _Pumping= false;
96 /*------------------------------------------------------------------*\
97 pumpEvent()
98 \*------------------------------------------------------------------*/
99 bool CEventServer::pumpEvent(CEvent* event)
101 // taking id
102 uint64 id = (uint64) *event;
104 // looking for the first occurence of id
105 mapListener::iterator it = _Listeners.find(id);
107 // calling every callbacks
108 while(it!=_Listeners.end() && (uint64)(*it).first == id)
110 IEventListener *a = (IEventListener *)((*it).second);
111 a->process(*event);
112 it++;
115 // delete the pointer
116 return true;
121 /*------------------------------------------------------------------*\
122 addListener()
123 \*------------------------------------------------------------------*/
124 void CEventServer::addListener(CClassId id, IEventListener* listener )
126 _Listeners.insert( mapListener::value_type(id, listener));
130 /*------------------------------------------------------------------*\
131 removeListener()
132 \*------------------------------------------------------------------*/
133 void CEventServer::removeListener(CClassId id, IEventListener* listener )
135 // looking for the first occurence of id
136 mapListener::iterator it = _Listeners.find(id);
138 // looking for occurence with the right callback
139 while(it!=_Listeners.end() && (*it).first == id)
141 if((*it).second==listener)
143 // erasing pair
144 _Listeners.erase(it);
145 return;
147 it++;
152 /*------------------------------------------------------------------*\
153 addEmitter()
154 \*------------------------------------------------------------------*/
155 void CEventServer::addEmitter(IEventEmitter * emitter)
157 _Emitters.push_back(emitter);
161 /*------------------------------------------------------------------*\
162 removeEmitter()
163 \*------------------------------------------------------------------*/
164 void CEventServer::removeEmitter(IEventEmitter * emitter)
166 _Emitters.remove(emitter);
170 } // NLMISC