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 )
42 * This class encapsulates data and functionality required for
43 * posting component-specific events to perform arbitrary operations in main GUI thread.
44 * SALOME_Event objects can be posted by any thread belonging to the GUI process.
46 * It is necessary to derive a custom event class from SALOME_Event and
47 * re-implement virtual Execute() method. This method should actually perform
48 * the desirable operation. To pass all the required data to Execute() and store a return value,
49 * arbitrary data fields can be added to the custom event class. There is
50 * no need to protect such fields with a mutex, for only one thread working with
51 * a SALOME_Event object is active at any moment.
54 * - create SALOME_Event.
55 * Components can derive their own event class from SALOME_Event
56 * in order to pass custom data to the event handler.
57 * - call process() method to post the event. After process() execution
58 * it is possible to examine fields of your custom event object.
59 * - perform delete operator on the event to wake up the desktop (you can also set <autoRelease>
60 * parameter to TRUE to automatically wake up desktop after process()
62 * processed() method is used by the desktop to signal that event processing
71 class EVENT_EXPORT SALOME_Event{
74 virtual ~SALOME_Event();
77 virtual void Execute() = 0;
79 static bool IsSessionThread();
84 friend class SalomeApp_EventFilter;
86 static void GetSessionThread();
87 friend int main(int, char **);
90 QSemaphore* mySemaphore;
96 \brief Template class for member function
98 template<class TObject, typename TRes> class EVENT_EXPORT 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)();
119 \class TVoidMemFunEvent
120 \brief Template class for member function
122 template<class TObject> class EVENT_EXPORT TVoidMemFunEvent: public SALOME_Event{
124 typedef void (TObject::* TAction)();
125 TVoidMemFunEvent(TObject* theObject, TAction theAction):
129 virtual void Execute(){
130 (myObject->*myAction)();
139 \class TMemFun1ArgEvent
140 \brief Template for member function with one argument
142 template<class TObject, typename TRes, typename TArg, typename TStoreArg = TArg> class EVENT_EXPORT TMemFun1ArgEvent:
145 typedef TRes TResult;
147 typedef TResult (TObject::* TAction)(TArg);
148 TMemFun1ArgEvent(TObject* theObject, TAction theAction, TArg theArg,
149 TResult theResult = TResult()):
155 virtual void Execute(){
156 myResult = (myObject->*myAction)(myArg);
166 \class TVoidMemFun1ArgEvent
167 \brief Template for member function with one argument
169 template<class TObject, typename TArg, typename TStoreArg = TArg> class EVENT_EXPORT TVoidMemFun1ArgEvent: public SALOME_Event{
171 typedef void (TObject::* TAction)(TArg);
172 TVoidMemFun1ArgEvent(TObject* theObject, TAction theAction, TArg theArg):
177 virtual void Execute(){
178 (myObject->*myAction)(myArg);
188 \class TMemFun2ArgEvent
189 \brief Template for member function with two arguments
191 template<class TObject, typename TRes, typename TArg, typename TArg1, typename TStoreArg = TArg, typename TStoreArg1 = TArg1> class
192 EVENT_EXPORT TMemFun2ArgEvent: public SALOME_Event{
194 typedef TRes TResult;
196 typedef TResult (TObject::* TAction)(TArg,TArg1);
197 TMemFun2ArgEvent(TObject* theObject, TAction theAction,
198 TArg theArg, TArg1 theArg1,
199 TResult theResult = TResult()):
206 virtual void Execute(){
207 myResult = (myObject->*myAction)(myArg,myArg1);
218 \class TVoidMemFun2ArgEvent
219 \brief Template for member function with two arguments
221 template<class TObject, typename TArg, typename TArg1, typename TStoreArg = TArg, typename TStoreArg1 = TArg1> class
222 EVENT_EXPORT TVoidMemFun2ArgEvent: public SALOME_Event{
224 typedef void (TObject::* TAction)(TArg,TArg1);
225 TVoidMemFun2ArgEvent(TObject* theObject, TAction theAction, TArg theArg, TArg1 theArg1):
231 virtual void Execute(){
232 (myObject->*myAction)(myArg,myArg1);
244 \brief Template function for processing events with result returing
246 template<class TEvent> inline typename TEvent::TResult ProcessEvent(TEvent* theEvent){
247 typename TEvent::TResult aResult;
248 if(SALOME_Event::IsSessionThread()){
250 aResult = theEvent->myResult;
253 aResult = theEvent->myResult;
262 \brief Template function for processing events without result
264 inline void ProcessVoidEvent(SALOME_Event* theEvent){
265 if(SALOME_Event::IsSessionThread()){