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