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.
54 * It is possible to make the thread that creates SALOME_Event
55 * wait until the event is processed by the component GUI, SALOME_Event
56 * should be constructed with <wait> == TRUE in such a case.
58 * SALOME_Event objects should be created on the heap. QAD_Desktop deletes
59 * these objects as soon as they have been processed.
62 * - create SALOME_Event object on the heap with proper <type> and <wait> parameters.
63 * Components can derive their own event class from SALOME_Event
64 * in order to pass custom data to the event handler.
65 * - call process() method to post the event. Between process() and release()
66 * it is possible to examine fields of your custom event object.
67 * - call release() method to wake up the desktop (you can also set <autoRelease>
68 * parameter to TRUE to automatically wake up desktop after process()
70 * processed() method is used by the desktop to signal that event processing
74 * 1. Never create SALOME_Event with <wait> == TRUE in code that is
75 * supposed to be called within main GUI thread, for this will result
76 * in GUI process deadlock.
77 * 2. Always call release() method after process() if you use <wait> parameters as TRUE,
78 * otherwise processed() method will never return and main GUI thread will be blocked!
79 * 3. Never use pointers to the event after it has been released (either by calling release()
80 * or automatically by process() if <autoRelease> == TRUE) to avoid application crashes!
82 //===========================================================
89 virtual ~SALOME_Event();
92 virtual void Execute() = 0;
102 QSemaphore* mySemaphore;
106 // Template classes for member function
107 //-------------------------------------
108 template<class TObject, typename TRes>
109 class TMemFunEvent: public SALOME_Event{
111 typedef TRes TResult;
113 typedef TResult (TObject::* TAction)();
114 TMemFunEvent(TObject* theObject, TAction theAction,
115 TResult theResult = TResult()):
120 virtual void Execute(){
121 myResult = (myObject->*myAction)();
129 template<class TObject>
130 class TVoidMemFunEvent: public SALOME_Event{
132 typedef void (TObject::* TAction)();
133 TVoidMemFunEvent(TObject* theObject, TAction theAction):
137 virtual void Execute(){
138 (myObject->*myAction)();
146 // Template for member function with one argument
147 //-----------------------------------------------
148 template<class TObject, typename TRes,
149 typename TArg, typename TStoreArg = TArg>
150 class TMemFun1ArgEvent: public SALOME_Event{
152 typedef TRes TResult;
154 typedef TResult (TObject::* TAction)(TArg);
155 TMemFun1ArgEvent(TObject* theObject, TAction theAction, TArg theArg,
156 TResult theResult = TResult()):
162 virtual void Execute(){
163 myResult = (myObject->*myAction)(myArg);
172 template<class TObject, typename TArg, typename TStoreArg = TArg>
173 class TVoidMemFun1ArgEvent: public SALOME_Event{
175 typedef void (TObject::* TAction)(TArg);
176 TVoidMemFun1ArgEvent(TObject* theObject, TAction theAction, TArg theArg):
181 virtual void Execute(){
182 (myObject->*myAction)(myArg);
191 // Template for member function with one argument
192 //-----------------------------------------------
193 template<class TObject, typename TRes,
194 typename TArg, typename TArg1,
195 typename TStoreArg = TArg, typename TStoreArg1 = TArg1>
196 class TMemFun2ArgEvent: public SALOME_Event{
198 typedef TRes TResult;
200 typedef TResult (TObject::* TAction)(TArg,TArg1);
201 TMemFun2ArgEvent(TObject* theObject, TAction theAction,
202 TArg theArg, TArg1 theArg1,
203 TResult theResult = TResult()):
210 virtual void Execute(){
211 myResult = (myObject->*myAction)(myArg,myArg1);
221 template<class TObject, typename TArg, typename TArg1,
222 typename TStoreArg = TArg, typename TStoreArg1 = TArg1>
223 class TVoidMemFun2ArgEvent: public SALOME_Event{
225 typedef void (TObject::* TAction)(TArg,TArg1);
226 TVoidMemFun2ArgEvent(TObject* theObject, TAction theAction, TArg theArg, TArg1 theArg1):
232 virtual void Execute(){
233 (myObject->*myAction)(myArg,myArg1);
243 // Template function for processing events with result returing
244 template<class TEvent> inline typename TEvent::TResult ProcessEvent(TEvent* theEvent){
246 typename TEvent::TResult aResult = theEvent->myResult;
252 // Template function for processing events without result
253 inline void ProcessVoidEvent(SALOME_Event* theEvent){