Back to index

salome-gui  6.5.0
SALOME_Event.h
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 //  KERNEL SALOME_Event : Define event posting mechanism
00021 //  File   : SALOME_Event.h
00022 //  Author : Sergey ANIKIN
00023 //
00024 #ifndef SALOME_EVENT_H
00025 #define SALOME_EVENT_H
00026 
00027 #include "Event.h"
00028 
00029 #include <QEvent>
00030 
00032 #define SALOME_EVENT QEvent::Type( QEvent::User + 10000 )
00033 
00034 class EVENT_EXPORT SALOME_CustomEvent : public QEvent
00035 {
00036 public:
00037   SALOME_CustomEvent( int type );
00038   SALOME_CustomEvent( QEvent::Type type, void* data );
00039 
00040   void* data() const;
00041   void  setData( void* data );
00042 
00043 private:
00044   void *d;   
00045 };
00046 
00047 class QSemaphore;
00048 
00049 class EVENT_EXPORT SALOME_Event
00050 {
00051 public:
00052   SALOME_Event();
00053   virtual ~SALOME_Event();
00054 
00055   void            ExecutePostedEvent();
00056   virtual void    Execute() = 0;
00057 
00058   static bool     IsSessionThread();
00059   void            process();
00060 
00061 protected:
00062   void            processed();
00063   static void     GetSessionThread();
00064 
00065 private:
00066   QSemaphore*     mySemaphore;     
00067 };
00068 
00069 template<class TObject, typename TRes> class TMemFunEvent : public SALOME_Event
00070 {
00071 public:
00072   typedef TRes TResult;
00073   TResult myResult;
00074   typedef TResult (TObject::* TAction)();
00075   TMemFunEvent(TObject* theObject, TAction theAction, 
00076                TResult theResult = TResult()):
00077     myObject(theObject),
00078     myAction(theAction),
00079     myResult(theResult)
00080   {}
00081   virtual void Execute()
00082   {
00083     myResult = (myObject->*myAction)();
00084   }
00085 private:
00086   TObject* myObject;
00087   TAction  myAction;
00088 };
00089 
00090 template<class TObject> class TVoidMemFunEvent : public SALOME_Event
00091 {
00092 public:
00093   typedef void (TObject::* TAction)();
00094   TVoidMemFunEvent(TObject* theObject, TAction theAction):
00095     myObject(theObject),
00096     myAction(theAction)
00097   {}
00098   virtual void Execute()
00099   {
00100     (myObject->*myAction)();
00101   }
00102 private:
00103   TObject* myObject;
00104   TAction myAction;
00105 };
00106 
00107 template<class TObject, typename TRes, typename TArg, typename TStoreArg = TArg> 
00108 class TMemFun1ArgEvent : public SALOME_Event
00109 {
00110 public:
00111   typedef TRes TResult;
00112   TResult myResult;
00113   typedef TResult (TObject::* TAction)(TArg);
00114   TMemFun1ArgEvent(TObject* theObject, TAction theAction, TArg theArg, 
00115                    TResult theResult = TResult()):
00116     myObject(theObject),
00117     myAction(theAction),
00118     myResult(theResult),
00119     myArg(theArg)
00120   {}
00121   virtual void Execute()
00122   {
00123     myResult = (myObject->*myAction)(myArg);
00124   }
00125 private:
00126   TObject* myObject;
00127   TAction myAction;
00128   TStoreArg myArg;
00129 };
00130 
00131 template<class TObject, typename TArg, typename TStoreArg = TArg> 
00132 class TVoidMemFun1ArgEvent : public SALOME_Event
00133 {
00134 public:
00135   typedef void (TObject::* TAction)(TArg);
00136   TVoidMemFun1ArgEvent(TObject* theObject, TAction theAction, TArg theArg):
00137     myObject(theObject),
00138     myAction(theAction),
00139     myArg(theArg)
00140   {}
00141   virtual void Execute()
00142   {
00143     (myObject->*myAction)(myArg);
00144   }
00145 private:
00146   TObject* myObject;
00147   TAction myAction;
00148   TStoreArg myArg;
00149 };
00150 
00151 template<class TObject, typename TRes, typename TArg, typename TArg1, typename TStoreArg = TArg, typename TStoreArg1 = TArg1>
00152 class TMemFun2ArgEvent: public SALOME_Event
00153 {
00154 public:
00155   typedef TRes TResult;
00156   TResult myResult;
00157   typedef TResult (TObject::* TAction)(TArg,TArg1);
00158   TMemFun2ArgEvent(TObject* theObject, TAction theAction, 
00159                    TArg theArg, TArg1 theArg1,
00160                    TResult theResult = TResult()):
00161     myObject(theObject),
00162     myAction(theAction),
00163     myResult(theResult),
00164     myArg(theArg),
00165     myArg1(theArg1)
00166   {}
00167   virtual void Execute()
00168   {
00169     myResult = (myObject->*myAction)(myArg,myArg1);
00170   }
00171 private:
00172   TObject* myObject;
00173   TAction myAction;
00174   TStoreArg myArg;
00175   TStoreArg1 myArg1;
00176 };
00177 
00178 template<class TObject, typename TArg, typename TArg1, typename TStoreArg = TArg, typename TStoreArg1 = TArg1>
00179 class TVoidMemFun2ArgEvent : public SALOME_Event
00180 {
00181 public:
00182   typedef void (TObject::* TAction)(TArg,TArg1);
00183   TVoidMemFun2ArgEvent(TObject* theObject, TAction theAction, TArg theArg, TArg1 theArg1):
00184     myObject(theObject),
00185     myAction(theAction),
00186     myArg(theArg),
00187     myArg1(theArg1)
00188   {}
00189   virtual void Execute()
00190   {
00191     (myObject->*myAction)(myArg,myArg1);
00192   }
00193 private:
00194   TObject* myObject;
00195   TAction myAction;
00196   TStoreArg myArg;
00197   TStoreArg1 myArg1;
00198 };
00199 
00200 template<class TEvent> inline typename TEvent::TResult ProcessEvent(TEvent* theEvent)
00201 {
00202   typename TEvent::TResult aResult;
00203   if(SALOME_Event::IsSessionThread()) {
00204     theEvent->Execute();
00205     aResult = theEvent->myResult;
00206   }
00207   else {
00208     theEvent->process();
00209     aResult = theEvent->myResult;
00210   }
00211   delete theEvent;
00212   return aResult;
00213 }
00214 
00215 inline void ProcessVoidEvent(SALOME_Event* theEvent)
00216 {
00217   if(SALOME_Event::IsSessionThread()) {
00218     theEvent->Execute();
00219   }
00220   else {
00221     theEvent->process();
00222   }
00223   delete theEvent;
00224 }
00225 
00226 #endif // SALOME_EVENT_H