]> SALOME platform Git repositories - modules/gui.git/blob - src/PyInterp/PyInterp_Dispatcher.h
Salome HOME
cd970dd0fc025c9ddcd5f458c4e2eff5b4bf7ba0
[modules/gui.git] / src / PyInterp / PyInterp_Dispatcher.h
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  File   : PyInterp_Dispatcher.h
23 //  Author : Sergey Anikin, OCC
24 //  Module : SALOME
25 //
26 #ifndef PYINTERP_DISPATCHER_H
27 #define PYINTERP_DISPATCHER_H
28
29 #include "PyInterp.h"   // !!! WARNING !!! THIS INCLUDE MUST BE THE VERY FIRST !!!
30
31 #include <QMutex>
32 #include <QThread>
33 #include <QEvent>
34 #include <QQueue>
35
36 class QObject;
37
38 class PyInterp_Interp;
39 class PyInterp_Watcher;
40 class PyInterp_Dispatcher;
41 class PyInterp_ExecuteEvent;
42
43 class PYINTERP_EXPORT PyInterp_Request
44 {
45   friend class PyInterp_Dispatcher;
46   friend class PyInterp_ExecuteEvent;
47
48   PyInterp_Request();
49   PyInterp_Request( const PyInterp_Request& );
50
51 protected:
52   virtual ~PyInterp_Request() {};
53   // protected destructor - to control deletion of requests
54
55 public:
56   PyInterp_Request( QObject* listener, bool sync = false )
57     : myIsSync( sync ), myListener( listener ) {};
58
59   static void     Destroy( PyInterp_Request* );
60   // Deletes a request
61
62   bool            IsSync() const { return myIsSync; }
63   // Returns true if this request should be processed synchronously,
64   // without putting it to a queue
65
66 protected:
67   virtual void    safeExecute();
68
69   virtual void    execute() = 0;
70   // Should be redefined in successors, contains actual request code
71
72   virtual QEvent* createEvent() const;
73   // This method can be overridden to customize notification event creation
74
75   virtual void    processEvent( QObject* );
76
77   QObject*        listener() const { return myListener; }
78   void            setListener( QObject* );
79
80 private:
81   void            process();
82
83 private:
84   QMutex          myMutex;
85   bool            myIsSync;
86   QObject*        myListener;
87 };
88
89 class PYINTERP_EXPORT PyInterp_LockRequest : public PyInterp_Request
90 {
91 public:
92   PyInterp_LockRequest( PyInterp_Interp* interp, QObject* listener = 0, bool sync = false )
93     : PyInterp_Request( listener, sync ), myInterp( interp ) {}
94
95 protected:
96   PyInterp_Interp*  getInterp() const { return myInterp; }
97
98   virtual void      safeExecute();
99
100 private:
101   PyInterp_Interp*  myInterp;
102 };
103
104 class PYINTERP_EXPORT PyInterp_Event : public QEvent
105 {
106   PyInterp_Event();
107   PyInterp_Event( const PyInterp_Event& );
108
109 public:
110   //Execution state
111   enum { ES_NOTIFY = QEvent::User + 5000, ES_OK, ES_ERROR, ES_INCOMPLETE, ES_LAST };
112
113   PyInterp_Event( int type, PyInterp_Request* request )
114     : QEvent( (QEvent::Type)type ), myRequest( request ) {}
115
116   virtual ~PyInterp_Event();
117
118   PyInterp_Request* GetRequest() const { return myRequest; }
119   operator PyInterp_Request*() const { return myRequest; }
120
121 private:
122   PyInterp_Request* myRequest;
123 };
124
125 class PYINTERP_EXPORT PyInterp_Dispatcher : protected QThread
126 {
127   PyInterp_Dispatcher(); // private constructor
128
129 public:
130   static PyInterp_Dispatcher* Get();
131
132   virtual                     ~PyInterp_Dispatcher();
133
134   bool                        IsBusy() const;  
135   void                        Exec( PyInterp_Request* );
136
137 private:
138   virtual void                run();
139   void                        processRequest( PyInterp_Request* );
140   void                        objectDestroyed( const QObject* );
141
142 private:
143   typedef PyInterp_Request*   RequestPtr;
144
145   QQueue<RequestPtr>          myQueue;
146   QMutex                      myQueueMutex;
147   PyInterp_Watcher*           myWatcher;
148
149   static PyInterp_Dispatcher* myInstance;
150
151   friend class PyInterp_Watcher;
152 };
153
154 #endif // PYINTERP_DISPATCHER_H