Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / PyEvent / PyEvent_Event.h
1
2 #ifndef PyEvent_PYEVENT_H
3 #define PyEvent_PYEVENT_H
4
5 #include "PyEvent.h"
6
7 #include <QEvent>
8
9 //! SALOME custom event type
10 #define PyEvent_EVENT QEvent::Type( QEvent::User + 10000 )
11
12 class PYEVENT_EXPORT PyEvent_CustomEvent : public QEvent
13 {
14 public:
15   PyEvent_CustomEvent( int type );
16   PyEvent_CustomEvent( QEvent::Type type, void* data );
17
18   void* data() const;
19   void  setData( void* data );
20
21 private:
22   void *d;   //!< internal data
23 };
24
25 class QSemaphore;
26
27 class PYEVENT_EXPORT PyEvent_Event
28 {
29 public:
30   PyEvent_Event();
31   virtual ~PyEvent_Event();
32
33   void            ExecutePostedEvent();
34   virtual void    Execute() = 0;
35
36   static bool     IsSessionThread();
37   void            process();
38
39 protected:
40   void            processed();
41   static void     GetSessionThread();
42
43 private:
44   QSemaphore*     mySemaphore;     //!< internal semaphore
45 };
46
47 template<class TObject, typename TRes> class TMemFunEvent : public PyEvent_Event
48 {
49 public:
50   typedef TRes TResult;
51   TResult myResult;
52   typedef TResult (TObject::* TAction)();
53   TMemFunEvent(TObject* theObject, TAction theAction, 
54                TResult theResult = TResult()):
55     myObject(theObject),
56     myAction(theAction),
57     myResult(theResult)
58   {}
59   virtual void Execute()
60   {
61     myResult = (myObject->*myAction)();
62   }
63 private:
64   TObject* myObject;
65   TAction  myAction;
66 };
67
68 template<class TObject> class TVoidMemFunEvent : public PyEvent_Event
69 {
70 public:
71   typedef void (TObject::* TAction)();
72   TVoidMemFunEvent(TObject* theObject, TAction theAction):
73     myObject(theObject),
74     myAction(theAction)
75   {}
76   virtual void Execute()
77   {
78     (myObject->*myAction)();
79   }
80 private:
81   TObject* myObject;
82   TAction myAction;
83 };
84
85 template<class TObject, typename TRes, typename TArg, typename TStoreArg = TArg> 
86 class TMemFun1ArgEvent : public PyEvent_Event
87 {
88 public:
89   typedef TRes TResult;
90   TResult myResult;
91   typedef TResult (TObject::* TAction)(TArg);
92   TMemFun1ArgEvent(TObject* theObject, TAction theAction, TArg theArg, 
93                    TResult theResult = TResult()):
94     myObject(theObject),
95     myAction(theAction),
96     myResult(theResult),
97     myArg(theArg)
98   {}
99   virtual void Execute()
100   {
101     myResult = (myObject->*myAction)(myArg);
102   }
103 private:
104   TObject* myObject;
105   TAction myAction;
106   TStoreArg myArg;
107 };
108
109 template<class TObject, typename TArg, typename TStoreArg = TArg> 
110 class TVoidMemFun1ArgEvent : public PyEvent_Event
111 {
112 public:
113   typedef void (TObject::* TAction)(TArg);
114   TVoidMemFun1ArgEvent(TObject* theObject, TAction theAction, TArg theArg):
115     myObject(theObject),
116     myAction(theAction),
117     myArg(theArg)
118   {}
119   virtual void Execute()
120   {
121     (myObject->*myAction)(myArg);
122   }
123 private:
124   TObject* myObject;
125   TAction myAction;
126   TStoreArg myArg;
127 };
128
129 template<class TObject, typename TRes, typename TArg, typename TArg1, typename TStoreArg = TArg, typename TStoreArg1 = TArg1>
130 class TMemFun2ArgEvent: public PyEvent_Event
131 {
132 public:
133   typedef TRes TResult;
134   TResult myResult;
135   typedef TResult (TObject::* TAction)(TArg,TArg1);
136   TMemFun2ArgEvent(TObject* theObject, TAction theAction, 
137                    TArg theArg, TArg1 theArg1,
138                    TResult theResult = TResult()):
139     myObject(theObject),
140     myAction(theAction),
141     myResult(theResult),
142     myArg(theArg),
143     myArg1(theArg1)
144   {}
145   virtual void Execute()
146   {
147     myResult = (myObject->*myAction)(myArg,myArg1);
148   }
149 private:
150   TObject* myObject;
151   TAction myAction;
152   TStoreArg myArg;
153   TStoreArg1 myArg1;
154 };
155
156 template<class TObject, typename TArg, typename TArg1, typename TStoreArg = TArg, typename TStoreArg1 = TArg1>
157 class TVoidMemFun2ArgEvent : public PyEvent_Event
158 {
159 public:
160   typedef void (TObject::* TAction)(TArg,TArg1);
161   TVoidMemFun2ArgEvent(TObject* theObject, TAction theAction, TArg theArg, TArg1 theArg1):
162     myObject(theObject),
163     myAction(theAction),
164     myArg(theArg),
165     myArg1(theArg1)
166   {}
167   virtual void Execute()
168   {
169     (myObject->*myAction)(myArg,myArg1);
170   }
171 private:
172   TObject* myObject;
173   TAction myAction;
174   TStoreArg myArg;
175   TStoreArg1 myArg1;
176 };
177
178 template<class TEvent> inline typename TEvent::TResult ProcessEvent(TEvent* theEvent)
179 {
180   typename TEvent::TResult aResult;
181   if(PyEvent_Event::IsSessionThread()) {
182     theEvent->Execute();
183     aResult = theEvent->myResult;
184   }
185   else {
186     theEvent->process();
187     aResult = theEvent->myResult;
188   }
189   delete theEvent;
190   return aResult;
191 }
192
193 inline void ProcessVoidEvent(PyEvent_Event* theEvent)
194 {
195   if(PyEvent_Event::IsSessionThread()) {
196     theEvent->Execute();
197   }
198   else {
199     theEvent->process();
200   }
201   delete theEvent;
202 }
203
204 #endif // PyEvent_PYEVENT_H