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