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 //===========================================================
73 class EVENT_EXPORT SALOME_Event{
76 virtual ~SALOME_Event();
79 virtual void Execute() = 0;
81 static bool IsSessionThread();
86 friend class SalomeApp_EventFilter;
88 static void GetSessionThread();
89 friend int main(int, char **);
92 QSemaphore* mySemaphore;
96 // Template classes for member function
97 //-------------------------------------
98 template<class TObject, typename TRes>
99 class EVENT_EXPORT TMemFunEvent: public SALOME_Event{
101 typedef TRes TResult;
103 typedef TResult (TObject::* TAction)();
104 TMemFunEvent(TObject* theObject, TAction theAction,
105 TResult theResult = TResult()):
110 virtual void Execute(){
111 myResult = (myObject->*myAction)();
119 template<class TObject>
120 class EVENT_EXPORT TVoidMemFunEvent: public SALOME_Event{
122 typedef void (TObject::* TAction)();
123 TVoidMemFunEvent(TObject* theObject, TAction theAction):
127 virtual void Execute(){
128 (myObject->*myAction)();
136 // Template for member function with one argument
137 //-----------------------------------------------
138 template<class TObject, typename TRes,
139 typename TArg, typename TStoreArg = TArg>
140 class EVENT_EXPORT TMemFun1ArgEvent: public SALOME_Event{
142 typedef TRes TResult;
144 typedef TResult (TObject::* TAction)(TArg);
145 TMemFun1ArgEvent(TObject* theObject, TAction theAction, TArg theArg,
146 TResult theResult = TResult()):
152 virtual void Execute(){
153 myResult = (myObject->*myAction)(myArg);
162 template<class TObject, typename TArg, typename TStoreArg = TArg>
163 class EVENT_EXPORT TVoidMemFun1ArgEvent: public SALOME_Event{
165 typedef void (TObject::* TAction)(TArg);
166 TVoidMemFun1ArgEvent(TObject* theObject, TAction theAction, TArg theArg):
171 virtual void Execute(){
172 (myObject->*myAction)(myArg);
181 // Template for member function with one argument
182 //-----------------------------------------------
183 template<class TObject, typename TRes,
184 typename TArg, typename TArg1,
185 typename TStoreArg = TArg, typename TStoreArg1 = TArg1>
186 class EVENT_EXPORT TMemFun2ArgEvent: public SALOME_Event{
188 typedef TRes TResult;
190 typedef TResult (TObject::* TAction)(TArg,TArg1);
191 TMemFun2ArgEvent(TObject* theObject, TAction theAction,
192 TArg theArg, TArg1 theArg1,
193 TResult theResult = TResult()):
200 virtual void Execute(){
201 myResult = (myObject->*myAction)(myArg,myArg1);
211 template<class TObject, typename TArg, typename TArg1,
212 typename TStoreArg = TArg, typename TStoreArg1 = TArg1>
213 class EVENT_EXPORT TVoidMemFun2ArgEvent: public SALOME_Event{
215 typedef void (TObject::* TAction)(TArg,TArg1);
216 TVoidMemFun2ArgEvent(TObject* theObject, TAction theAction, TArg theArg, TArg1 theArg1):
222 virtual void Execute(){
223 (myObject->*myAction)(myArg,myArg1);
233 // Template function for processing events with result returing
234 template<class TEvent> inline typename TEvent::TResult ProcessEvent(TEvent* theEvent){
235 typename TEvent::TResult aResult;
236 if(SALOME_Event::IsSessionThread()){
238 aResult = theEvent->myResult;
241 aResult = theEvent->myResult;
248 // Template function for processing events without result
249 inline void ProcessVoidEvent(SALOME_Event* theEvent){
250 if(SALOME_Event::IsSessionThread()){