]> SALOME platform Git repositories - modules/gui.git/blob - src/Event/SALOME_Event.h
Salome HOME
8493680737e0547b1e21bb79088f5c4177b3fd69
[modules/gui.git] / src / Event / SALOME_Event.h
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  KERNEL SALOME_Event : Define event posting mechanism
23 //  File   : SALOME_Event.h
24 //  Author : Sergey ANIKIN
25 //
26 #ifndef SALOME_EVENT_H
27 #define SALOME_EVENT_H
28
29 #include "Event.h"
30
31 #include <QEvent>
32
33 //! SALOME custom event type
34 #define SALOME_EVENT QEvent::Type( QEvent::User + 10000 )
35
36 class EVENT_EXPORT SALOME_CustomEvent : public QEvent
37 {
38 public:
39   SALOME_CustomEvent( int type );
40   SALOME_CustomEvent( QEvent::Type type, void* data );
41
42   void* data() const;
43   void  setData( void* data );
44
45 private:
46   void *d;   //!< internal data
47 };
48
49 class QSemaphore;
50
51 class EVENT_EXPORT SALOME_Event
52 {
53 public:
54   SALOME_Event();
55   virtual ~SALOME_Event();
56
57   void            ExecutePostedEvent();
58   virtual void    Execute() = 0;
59
60   static bool     IsSessionThread();
61   void            process();
62
63 protected:
64   void            processed();
65   static void     GetSessionThread();
66
67 private:
68   QSemaphore*     mySemaphore;     //!< internal semaphore
69 };
70
71 template<class TObject, typename TRes> class TMemFunEvent : public SALOME_Event
72 {
73 public:
74   typedef TRes TResult;
75   TResult myResult;
76   typedef TResult (TObject::* TAction)();
77   TMemFunEvent(TObject* theObject, TAction theAction, 
78                TResult theResult = TResult()):
79     myObject(theObject),
80     myAction(theAction),
81     myResult(theResult)
82   {}
83   virtual void Execute()
84   {
85     myResult = (myObject->*myAction)();
86   }
87 private:
88   TObject* myObject;
89   TAction  myAction;
90 };
91
92 template<class TObject> class TVoidMemFunEvent : public SALOME_Event
93 {
94 public:
95   typedef void (TObject::* TAction)();
96   TVoidMemFunEvent(TObject* theObject, TAction theAction):
97     myObject(theObject),
98     myAction(theAction)
99   {}
100   virtual void Execute()
101   {
102     (myObject->*myAction)();
103   }
104 private:
105   TObject* myObject;
106   TAction myAction;
107 };
108
109 template<class TObject, typename TRes, typename TArg, typename TStoreArg = TArg> 
110 class TMemFun1ArgEvent : public SALOME_Event
111 {
112 public:
113   typedef TRes TResult;
114   TResult myResult;
115   typedef TResult (TObject::* TAction)(TArg);
116   TMemFun1ArgEvent(TObject* theObject, TAction theAction, TArg theArg, 
117                    TResult theResult = TResult()):
118     myObject(theObject),
119     myAction(theAction),
120     myResult(theResult),
121     myArg(theArg)
122   {}
123   virtual void Execute()
124   {
125     myResult = (myObject->*myAction)(myArg);
126   }
127 private:
128   TObject* myObject;
129   TAction myAction;
130   TStoreArg myArg;
131 };
132
133 template<class TObject, typename TArg, typename TStoreArg = TArg> 
134 class TVoidMemFun1ArgEvent : public SALOME_Event
135 {
136 public:
137   typedef void (TObject::* TAction)(TArg);
138   TVoidMemFun1ArgEvent(TObject* theObject, TAction theAction, TArg theArg):
139     myObject(theObject),
140     myAction(theAction),
141     myArg(theArg)
142   {}
143   virtual void Execute()
144   {
145     (myObject->*myAction)(myArg);
146   }
147 private:
148   TObject* myObject;
149   TAction myAction;
150   TStoreArg myArg;
151 };
152
153 template<class TObject, typename TRes, typename TArg, typename TArg1, typename TStoreArg = TArg, typename TStoreArg1 = TArg1>
154 class TMemFun2ArgEvent: public SALOME_Event
155 {
156 public:
157   typedef TRes TResult;
158   TResult myResult;
159   typedef TResult (TObject::* TAction)(TArg,TArg1);
160   TMemFun2ArgEvent(TObject* theObject, TAction theAction, 
161                    TArg theArg, TArg1 theArg1,
162                    TResult theResult = TResult()):
163     myObject(theObject),
164     myAction(theAction),
165     myResult(theResult),
166     myArg(theArg),
167     myArg1(theArg1)
168   {}
169   virtual void Execute()
170   {
171     myResult = (myObject->*myAction)(myArg,myArg1);
172   }
173 private:
174   TObject* myObject;
175   TAction myAction;
176   TStoreArg myArg;
177   TStoreArg1 myArg1;
178 };
179
180 template<class TObject, typename TArg, typename TArg1, typename TStoreArg = TArg, typename TStoreArg1 = TArg1>
181 class TVoidMemFun2ArgEvent : public SALOME_Event
182 {
183 public:
184   typedef void (TObject::* TAction)(TArg,TArg1);
185   TVoidMemFun2ArgEvent(TObject* theObject, TAction theAction, TArg theArg, TArg1 theArg1):
186     myObject(theObject),
187     myAction(theAction),
188     myArg(theArg),
189     myArg1(theArg1)
190   {}
191   virtual void Execute()
192   {
193     (myObject->*myAction)(myArg,myArg1);
194   }
195 private:
196   TObject* myObject;
197   TAction myAction;
198   TStoreArg myArg;
199   TStoreArg1 myArg1;
200 };
201
202 template<class TEvent> inline typename TEvent::TResult ProcessEvent(TEvent* theEvent)
203 {
204   typename TEvent::TResult aResult;
205   if(SALOME_Event::IsSessionThread()) {
206     theEvent->Execute();
207     aResult = theEvent->myResult;
208   }
209   else {
210     theEvent->process();
211     aResult = theEvent->myResult;
212   }
213   delete theEvent;
214   return aResult;
215 }
216
217 inline void ProcessVoidEvent(SALOME_Event* theEvent)
218 {
219   if(SALOME_Event::IsSessionThread()) {
220     theEvent->Execute();
221   }
222   else {
223     theEvent->process();
224   }
225   delete theEvent;
226 }
227
228 #endif // SALOME_EVENT_H