OMKValuedEventListenerCallBack.h

Go to the documentation of this file.
00001 /*
00002  * This file is part of openMask © INRIA, CNRS, Universite de Rennes 1 1993-2002, thereinafter the Software
00003  * 
00004  * The Software has been developped within the Siames Project. 
00005  * INRIA, the University of Rennes 1 and CNRS jointly hold intellectual property rights
00006  * 
00007  * The Software has been registered with the Agence pour la Protection des
00008  * Programmes (APP) under registration number IDDN.FR.001.510008.00.S.P.2001.000.41200
00009  *
00010  * This file may be distributed under the terms of the Q Public License
00011  * version 1.0 as defined by Trolltech AS of Norway and appearing in the file
00012  * LICENSE.QPL included in the packaging of this file.
00013  *
00014  * Licensees holding valid specific licenses issued by INRIA, CNRS or Université de Rennes 1 
00015  * for the software may use this file in accordance with that specific license 
00016  *
00017  */
00018 #ifndef ValuedEventListenerCallBackHEADER
00019 #define ValuedEventListenerCallBackHEADER
00020 
00021 #include "OMKEvent.h"
00022 #include "OMKEventListener.h"
00023 #include "OMKSimulatedObject.h"
00024 
00025 #include <typeinfo>
00026 
00027 namespace OMK
00028 {
00099 template< class CallerClass, class PrmType >
00100 class ValuedEventListenerCallBack :
00101   public EventListener
00102 {
00103 public:
00126   typedef bool (CallerClass::*CallBackFct)( ValuedEvent<PrmType> * event ) ;
00128 
00129 
00130 
00131 
00132 
00133 
00134 
00135 
00136 
00137 
00138 
00139   ValuedEventListenerCallBack( CallerClass & owner, 
00140                                  CallBackFct callBackFct, 
00141                                  const EventIdentifier& eventId )
00142   : EventListener( owner ), _callerInstance( &owner ), _callBackFct( callBackFct ), _eventId( eventId )
00143   { 
00144     // The owner instance and the call back method must be set
00145     OMASSERTM( _callerInstance && _callBackFct, "Needs a valid instance and call back method of this instance" ); 
00146     _owner.addEventListener( *this ) ;     
00147   }
00160   ValuedEventListenerCallBack( SimulatedObject & owner, 
00161                                  CallerClass * callerInstance, CallBackFct callBackFct, 
00162                                  const EventIdentifier& eventId )
00163   : EventListener( owner ), _callerInstance( callerInstance ), _callBackFct( callBackFct ), _eventId( eventId )
00164   { 
00165     // The owner instance and the call back method must be set
00166     OMASSERTM( _callerInstance && _callBackFct, "Needs a valid instance and call back method of this instance" ); 
00167     _owner.addEventListener( *this ) ;     
00168   }
00170   virtual ~ValuedEventListenerCallBack() {}
00172 
00173 protected:
00175 
00176 
00177 
00178 
00179 
00180   virtual bool processEvent( Event *e ) 
00181   {
00182     ValuedEvent< PrmType > *valuedEvent = dynamic_cast< ValuedEvent< PrmType >* >( e ) ;
00183     if( valuedEvent )
00184     { // Ok => call the call back method
00185       return (_callerInstance->*_callBackFct)( valuedEvent ) ;
00186     }
00187     else
00188     { // Unable to cast => error message
00189       OMERROR( ":-( Error to processEvent() of " << debugMsg( &_owner ) << " with the event \"" 
00190                 << ( e ? e->eventId.getCString() : "unknow" ) << "\" send by \"" 
00191                 << ( e ? e->sender.getCString() : "unknow" ) << "\"" << std::endl
00192                 << ">>> Unable to cast the event (waits " << typeid( valuedEvent ).name() << " get " << typeid( e ).name() ) ;
00193       return false ; // Unable to process the event => don't stop the process, maybe something else could process it.
00194     }
00195   }
00197   virtual void registerEvents() 
00198   {
00199     _owner.registerEventListenerForEvent( *this, _eventId ) ;
00200   }
00202   
00203 protected:
00205 
00206 
00207 
00208   CallerClass* _callerInstance ;
00210   CallBackFct _callBackFct ;
00212   EventIdentifier _eventId ;
00214 };
00215 
00216 } //namespace OMK
00217 
00218 #endif //defined ValuedEventListenerCallBackHEADER

logo OpenMask

Documentation generated on Mon Jun 9 11:45:57 2008

Generated with doxygen by Dimitri van Heesch ,   1997-2007