1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
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.
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/>.
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"
31 /*------------------------------------------------------------------*\
33 \*------------------------------------------------------------------*/
34 CEventServer::CEventServer()
39 CEventServer::~CEventServer()
41 std::list
<CEvent
*>::iterator itev
= _Events
.begin();
42 while(itev
!=_Events
.end())
45 itev
=_Events
.erase (itev
);
50 /*------------------------------------------------------------------*\
52 \*------------------------------------------------------------------*/
53 void CEventServer::postEvent(CEvent
* event
)
55 _Events
.push_back(event
);
60 /*------------------------------------------------------------------*\
62 \*------------------------------------------------------------------*/
63 void CEventServer::pump(bool allWindows
)
65 // Avoid recurse (can arise if the process of an event decide to pump the server again....)
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
);
80 // **** process to listeners
81 std::list
<CEvent
*>::iterator itev
= _Events
.begin();
82 while(itev
!=_Events
.end())
85 bool bDelete
=pumpEvent(*itev
);
88 itev
=_Events
.erase (itev
);
96 /*------------------------------------------------------------------*\
98 \*------------------------------------------------------------------*/
99 bool CEventServer::pumpEvent(CEvent
* event
)
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
);
115 // delete the pointer
121 /*------------------------------------------------------------------*\
123 \*------------------------------------------------------------------*/
124 void CEventServer::addListener(CClassId id
, IEventListener
* listener
)
126 _Listeners
.insert( mapListener::value_type(id
, listener
));
130 /*------------------------------------------------------------------*\
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
)
144 _Listeners
.erase(it
);
152 /*------------------------------------------------------------------*\
154 \*------------------------------------------------------------------*/
155 void CEventServer::addEmitter(IEventEmitter
* emitter
)
157 _Emitters
.push_back(emitter
);
161 /*------------------------------------------------------------------*\
163 \*------------------------------------------------------------------*/
164 void CEventServer::removeEmitter(IEventEmitter
* emitter
)
166 _Emitters
.remove(emitter
);