src/Event/SALOME_Event.hxx

Go to the documentation of this file.
00001 //  KERNEL SALOME_Event : Define event posting mechanism
00002 //
00003 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
00005 // 
00006 //  This library is free software; you can redistribute it and/or 
00007 //  modify it under the terms of the GNU Lesser General Public 
00008 //  License as published by the Free Software Foundation; either 
00009 //  version 2.1 of the License. 
00010 // 
00011 //  This library is distributed in the hope that it will be useful, 
00012 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
00013 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
00014 //  Lesser General Public License for more details. 
00015 // 
00016 //  You should have received a copy of the GNU Lesser General Public 
00017 //  License along with this library; if not, write to the Free Software 
00018 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
00019 // 
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 //
00023 //
00024 //  File   : SALOME_Event.hxx
00025 //  Author : Sergey ANIKIN
00026 //  Module : KERNEL
00027 //  $Header: /home/server/cvs/GUI/GUI_SRC/src/Event/SALOME_Event.hxx,v 1.3 2006/06/01 11:27:47 jfa Exp $
00028 
00029 
00030 #ifndef SALOME_Event_HeaderFile
00031 #define SALOME_Event_HeaderFile
00032 
00033 #include <qevent.h>
00034 
00035 #define SALOME_EVENT QEvent::Type( QEvent::User + 10000 )
00036 
00037 class QSemaphore;
00038 
00069 #include <Event.h>
00070 
00071 class EVENT_EXPORT SALOME_Event{
00072 public:
00073   SALOME_Event();
00074   virtual ~SALOME_Event();
00075 
00076   // To do real work
00077   virtual void Execute() = 0;
00078 
00079   static bool IsSessionThread();
00080   void process();
00081 
00082 protected:
00083   void processed();
00084   friend class SalomeApp_EventFilter;
00085 
00086   static void GetSessionThread();
00087   friend int main(int, char **);
00088 
00089 private:
00090   QSemaphore* mySemaphore;
00091 };
00092 
00093 
00098 template<class TObject, typename TRes> class EVENT_EXPORT TMemFunEvent: public SALOME_Event{
00099 public:
00100   typedef TRes TResult;
00101   TResult myResult;
00102   typedef TResult (TObject::* TAction)();
00103   TMemFunEvent(TObject* theObject, TAction theAction, 
00104             TResult theResult = TResult()):
00105     myObject(theObject),
00106     myAction(theAction),
00107     myResult(theResult)
00108   {}
00109   virtual void Execute(){
00110     myResult = (myObject->*myAction)();
00111   }
00112 private:
00113   TObject* myObject;
00114   TAction myAction;
00115 };
00116 
00117 
00122 template<class TObject> class EVENT_EXPORT TVoidMemFunEvent: public SALOME_Event{
00123 public:
00124   typedef void (TObject::* TAction)();
00125   TVoidMemFunEvent(TObject* theObject, TAction theAction):
00126     myObject(theObject),
00127     myAction(theAction)
00128   {}
00129   virtual void Execute(){
00130     (myObject->*myAction)();
00131   }
00132 private:
00133   TObject* myObject;
00134   TAction myAction;
00135 };
00136 
00137 
00142 template<class TObject, typename TRes, typename TArg, typename TStoreArg = TArg> class EVENT_EXPORT TMemFun1ArgEvent:
00143 public SALOME_Event{
00144 public:
00145   typedef TRes TResult;
00146   TResult myResult;
00147   typedef TResult (TObject::* TAction)(TArg);
00148   TMemFun1ArgEvent(TObject* theObject, TAction theAction, TArg theArg, 
00149              TResult theResult = TResult()):
00150     myObject(theObject),
00151     myAction(theAction),
00152     myResult(theResult),
00153     myArg(theArg)
00154   {}
00155   virtual void Execute(){
00156     myResult = (myObject->*myAction)(myArg);
00157   }
00158 private:
00159   TObject* myObject;
00160   TAction myAction;
00161   TStoreArg myArg;
00162 };
00163 
00164 
00169 template<class TObject, typename TArg, typename TStoreArg = TArg> class EVENT_EXPORT TVoidMemFun1ArgEvent: public SALOME_Event{
00170 public:
00171   typedef void (TObject::* TAction)(TArg);
00172   TVoidMemFun1ArgEvent(TObject* theObject, TAction theAction, TArg theArg):
00173     myObject(theObject),
00174     myAction(theAction),
00175     myArg(theArg)
00176   {}
00177   virtual void Execute(){
00178     (myObject->*myAction)(myArg);
00179   }
00180 private:
00181   TObject* myObject;
00182   TAction myAction;
00183   TStoreArg myArg;
00184 };
00185 
00186 
00191 template<class TObject, typename TRes, typename TArg, typename TArg1, typename TStoreArg = TArg, typename TStoreArg1 = TArg1> class
00192 EVENT_EXPORT TMemFun2ArgEvent: public SALOME_Event{
00193 public:
00194   typedef TRes TResult;
00195   TResult myResult;
00196   typedef TResult (TObject::* TAction)(TArg,TArg1);
00197   TMemFun2ArgEvent(TObject* theObject, TAction theAction, 
00198              TArg theArg, TArg1 theArg1,
00199              TResult theResult = TResult()):
00200     myObject(theObject),
00201     myAction(theAction),
00202     myResult(theResult),
00203     myArg(theArg),
00204     myArg1(theArg1)
00205   {}
00206   virtual void Execute(){
00207     myResult = (myObject->*myAction)(myArg,myArg1);
00208   }
00209 private:
00210   TObject* myObject;
00211   TAction myAction;
00212   TStoreArg myArg;
00213   TStoreArg1 myArg1;
00214 };
00215 
00216 
00221 template<class TObject, typename TArg, typename TArg1, typename TStoreArg = TArg, typename TStoreArg1 = TArg1> class
00222 EVENT_EXPORT TVoidMemFun2ArgEvent: public SALOME_Event{
00223 public:
00224   typedef void (TObject::* TAction)(TArg,TArg1);
00225   TVoidMemFun2ArgEvent(TObject* theObject, TAction theAction, TArg theArg, TArg1 theArg1):
00226     myObject(theObject),
00227     myAction(theAction),
00228     myArg(theArg),
00229     myArg1(theArg1)
00230   {}
00231   virtual void Execute(){
00232     (myObject->*myAction)(myArg,myArg1);
00233   }
00234 private:
00235   TObject* myObject;
00236   TAction myAction;
00237   TStoreArg myArg;
00238   TStoreArg1 myArg1;
00239 };
00240 
00241 
00246 template<class TEvent> inline typename TEvent::TResult ProcessEvent(TEvent* theEvent){
00247   typename TEvent::TResult aResult;
00248   if(SALOME_Event::IsSessionThread()){
00249     theEvent->Execute();
00250     aResult = theEvent->myResult;
00251   }else{
00252     theEvent->process();
00253     aResult = theEvent->myResult;
00254   }
00255   delete theEvent;
00256   return aResult;
00257 }
00258 
00259 
00264 inline void ProcessVoidEvent(SALOME_Event* theEvent){
00265   if(SALOME_Event::IsSessionThread()){
00266     theEvent->Execute();
00267   }else{
00268     theEvent->process();
00269   }
00270   delete theEvent;
00271 }
00272 
00273 
00274 #endif