1 // KERNEL SALOME_Event : Define event posting mechanism
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SALOME_Event.hxx
25 // Author : Sergey ANIKIN
30 #ifndef SALOME_Event_HeaderFile
31 #define SALOME_Event_HeaderFile
35 #define SALOME_EVENT QEvent::Type( QEvent::User + 10000 )
39 //===========================================================
43 * This class encapsulates data and functionality required for
44 * posting component-specific events to perform arbitrary operations in main GUI thread.
45 * SALOME_Event objects can be posted by any thread belonging to the GUI process.
47 * It is necessary to derive a custom event class from SALOME_Event and
48 * re-implement virtual Execute() method. This method should actually perform
49 * the desirable operation. To pass all the required data to Execute() and store a return value,
50 * arbitrary data fields can be added to the custom event class. There is
51 * no need to protect such fields with a mutex, for only one thread working with
52 * a SALOME_Event object is active at any moment.
55 * - create SALOME_Event.
56 * Components can derive their own event class from SALOME_Event
57 * in order to pass custom data to the event handler.
58 * - call process() method to post the event. After process() execution
59 * it is possible to examine fields of your custom event object.
60 * - perform delete operator on the event to wake up the desktop (you can also set <autoRelease>
61 * parameter to TRUE to automatically wake up desktop after process()
63 * processed() method is used by the desktop to signal that event processing
69 //===========================================================
75 virtual ~SALOME_Event();
78 virtual void Execute() = 0;
80 static bool IsSessionThread();
85 friend class QAD_Desktop;
87 static void GetSessionThread();
88 friend int main(int, char **);
91 QSemaphore* mySemaphore;
95 // Template classes for member function
96 //-------------------------------------
97 template<class TObject, typename TRes>
98 class TMemFunEvent: public SALOME_Event{
100 typedef TRes TResult;
102 typedef TResult (TObject::* TAction)();
103 TMemFunEvent(TObject* theObject, TAction theAction,
104 TResult theResult = TResult()):
109 virtual void Execute(){
110 myResult = (myObject->*myAction)();
118 template<class TObject>
119 class TVoidMemFunEvent: public SALOME_Event{
121 typedef void (TObject::* TAction)();
122 TVoidMemFunEvent(TObject* theObject, TAction theAction):
126 virtual void Execute(){
127 (myObject->*myAction)();
135 // Template for member function with one argument
136 //-----------------------------------------------
137 template<class TObject, typename TRes,
138 typename TArg, typename TStoreArg = TArg>
139 class TMemFun1ArgEvent: public SALOME_Event{
141 typedef TRes TResult;
143 typedef TResult (TObject::* TAction)(TArg);
144 TMemFun1ArgEvent(TObject* theObject, TAction theAction, TArg theArg,
145 TResult theResult = TResult()):
151 virtual void Execute(){
152 myResult = (myObject->*myAction)(myArg);
161 template<class TObject, typename TArg, typename TStoreArg = TArg>
162 class TVoidMemFun1ArgEvent: public SALOME_Event{
164 typedef void (TObject::* TAction)(TArg);
165 TVoidMemFun1ArgEvent(TObject* theObject, TAction theAction, TArg theArg):
170 virtual void Execute(){
171 (myObject->*myAction)(myArg);
180 // Template for member function with one argument
181 //-----------------------------------------------
182 template<class TObject, typename TRes,
183 typename TArg, typename TArg1,
184 typename TStoreArg = TArg, typename TStoreArg1 = TArg1>
185 class TMemFun2ArgEvent: public SALOME_Event{
187 typedef TRes TResult;
189 typedef TResult (TObject::* TAction)(TArg,TArg1);
190 TMemFun2ArgEvent(TObject* theObject, TAction theAction,
191 TArg theArg, TArg1 theArg1,
192 TResult theResult = TResult()):
199 virtual void Execute(){
200 myResult = (myObject->*myAction)(myArg,myArg1);
210 template<class TObject, typename TArg, typename TArg1,
211 typename TStoreArg = TArg, typename TStoreArg1 = TArg1>
212 class TVoidMemFun2ArgEvent: public SALOME_Event{
214 typedef void (TObject::* TAction)(TArg,TArg1);
215 TVoidMemFun2ArgEvent(TObject* theObject, TAction theAction, TArg theArg, TArg1 theArg1):
221 virtual void Execute(){
222 (myObject->*myAction)(myArg,myArg1);
232 // Template function for processing events with result returing
233 template<class TEvent> inline typename TEvent::TResult ProcessEvent(TEvent* theEvent){
234 typename TEvent::TResult aResult;
235 if(SALOME_Event::IsSessionThread()){
237 aResult = theEvent->myResult;
240 aResult = theEvent->myResult;
247 // Template function for processing events without result
248 inline void ProcessVoidEvent(SALOME_Event* theEvent){
249 if(SALOME_Event::IsSessionThread()){