Version: 8.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SALOME_Event.h
Go to the documentation of this file.
1 // Copyright (C) 2007-2016 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 
32 #define SALOME_EVENT QEvent::Type( QEvent::User + 10000 )
33 
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;
45 };
46 
47 class QSemaphore;
48 
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;
67 };
68 
69 template<class TObject, typename TRes> class TMemFunEvent : public SALOME_Event
70 {
71 public:
72  typedef TRes TResult;
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;
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;
105 };
106 
107 template<class TObject, typename TRes, typename TArg, typename TStoreArg = TArg>
109 {
110 public:
111  typedef TRes TResult;
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  {
124  }
125 private:
126  TObject* myObject;
128  TStoreArg myArg;
129 };
130 
131 template<class TObject, typename TArg, typename TStoreArg = TArg>
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;
148  TStoreArg myArg;
149 };
150 
151 template<class TObject, typename TRes, typename TArg, typename TArg1, typename TStoreArg = TArg, typename TStoreArg1 = TArg1>
153 {
154 public:
155  typedef TRes TResult;
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  {
170  }
171 private:
172  TObject* myObject;
174  TStoreArg myArg;
175  TStoreArg1 myArg1;
176 };
177 
178 template<class TObject, typename TArg, typename TArg1, typename TStoreArg = TArg, typename TStoreArg1 = TArg1>
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  {
192  }
193 private:
194  TObject* myObject;
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;
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 {
218  theEvent->Execute();
219  }
220  else {
221  theEvent->process();
222  }
223  delete theEvent;
224 }
225 
226 #endif // SALOME_EVENT_H