# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

event_server.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 2000 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #include "stdmisc.h"
00027 
00028 #include "nel/misc/event_server.h"
00029 #include "nel/misc/event_listener.h"
00030 #include "nel/misc/event_emitter.h"
00031 #include "nel/misc/events.h"
00032 
00033 
00034 namespace NLMISC {
00035 
00036 
00037 
00038 /*------------------------------------------------------------------*\
00039                                                         postEvent()
00040 \*------------------------------------------------------------------*/
00041 void CEventServer::postEvent(CEvent * event)
00042 {
00043         _Events.push_back(event);
00044 }
00045 
00046 
00047 
00048 /*------------------------------------------------------------------*\
00049                                                         pump()
00050 \*------------------------------------------------------------------*/
00051 void CEventServer::pump()
00052 {
00053         std::list<IEventEmitter*>::iterator item = _Emitters.begin();
00054         
00055         // getting events from emitters
00056         while(item!=_Emitters.end())
00057         {
00058                 // ask emitters to submit their events to server
00059                 (*item)->submitEvents(*this);
00060                 item++;
00061         }
00062 
00063         
00064         std::list<CEvent*>::iterator itev = _Events.begin();
00065         
00066         while(itev!=_Events.end())
00067         {
00068                 // pump event
00069                 bool bDelete=pumpEvent(*itev);
00070                 if (bDelete)
00071                         delete *itev;
00072                 itev=_Events.erase (itev);
00073         }
00074 }
00075 
00076 
00077 /*------------------------------------------------------------------*\
00078                                                         pumpEvent()
00079 \*------------------------------------------------------------------*/
00080 bool CEventServer::pumpEvent(CEvent* event)
00081 {
00082         // taking id
00083         uint64 id = (uint64) *event;
00084 
00085         // looking for the first occurence of id
00086         mapListener::iterator it = _Listeners.find(id);
00087 
00088         // calling every callbacks
00089         while(it!=_Listeners.end() && (uint64)(*it).first == id)
00090         {
00091                 (*((*it).second)) (*event);
00092                 it++;
00093         }
00094 
00095         // delete the pointer
00096         return true;
00097 }
00098 
00099 
00100 
00101 /*------------------------------------------------------------------*\
00102                                                         addListener()
00103 \*------------------------------------------------------------------*/
00104 void CEventServer::addListener(CClassId id, IEventListener* listener )
00105 {
00106         _Listeners.insert( mapListener::value_type(id, listener));
00107 }
00108 
00109 
00110 /*------------------------------------------------------------------*\
00111                                                         removeListener()
00112 \*------------------------------------------------------------------*/
00113 void CEventServer::removeListener(CClassId id, IEventListener* listener )
00114 {
00115         // looking for the first occurence of id
00116         mapListener::iterator it = _Listeners.find(id);
00117 
00118         // looking for occurence with the right callback
00119         while(it!=_Listeners.end() && (*it).first == id)
00120         {
00121                 if((*it).second==listener)
00122                 {
00123                         // erasing pair
00124                         _Listeners.erase(it);
00125                         return;
00126                 }
00127                 it++;
00128         }
00129 }
00130 
00131 
00132 /*------------------------------------------------------------------*\
00133                                                         addEmitter()
00134 \*------------------------------------------------------------------*/
00135 void CEventServer::addEmitter(IEventEmitter * emitter)
00136 {
00137         _Emitters.push_back(emitter);
00138 }
00139 
00140 
00141 /*------------------------------------------------------------------*\
00142                                                         removeEmitter()
00143 \*------------------------------------------------------------------*/
00144 void CEventServer::removeEmitter(IEventEmitter * emitter)
00145 {
00146         _Emitters.remove(emitter);
00147 }
00148 
00149 
00150 } // NLMISC