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