Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[modules/gui.git] / src / PyInterp / PyInterp_Dispatcher.h
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
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.
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 //  SALOME SALOMEGUI : implementation of desktop and GUI kernel
20 //
21 //  File   : PyInterp_Dispatcher.h
22 //  Author : Sergey Anikin, OCC
23 //  Module : SALOME
24
25 #ifndef _PYINTERP_DISPATCHER_H_
26 #define _PYINTERP_DISPATCHER_H_
27
28 #include "PyInterp.h"
29
30 #include <qthread.h>
31 #include <qevent.h>
32
33 #include <list>
34
35 class QObject;
36
37 class PyInterp_base;
38 class PyInterp_Watcher;
39 class PyInterp_Dispatcher;
40
41 class PYINTERP_EXPORT PyInterp_Request
42 {
43   friend class PyInterp_Dispatcher;
44
45   PyInterp_Request();
46   PyInterp_Request( const PyInterp_Request& );
47
48 protected:
49   virtual         ~PyInterp_Request() {}; 
50   // protected destructor - to control deletion of requests
51
52 public:
53   PyInterp_Request( QObject* listener, bool sync = false )
54     : myIsSync( sync ), myListener( listener ), myEvent( 0 ) {};
55
56   static void     Destroy( PyInterp_Request* );
57   // Deletes a request
58
59   bool            IsSync() const { return myIsSync; }
60   // Returns true if this request should be processed synchronously,
61   // without putting it to a queue
62
63 protected:
64   virtual void    safeExecute();
65
66   virtual void    execute() = 0;
67   // Should be redefined in successors, contains actual request code
68
69   virtual QEvent* createEvent() const;
70   // This method can be overridden to customize notification event creation
71
72 private:
73   void            process();
74   QObject*        getListener() const { return myListener; }
75   void            setListener( QObject* );
76   QEvent*         getEvent();
77   void            postEvent();
78
79 private:
80   bool            myIsSync;
81   QObject*        myListener;
82   QEvent*         myEvent;
83   QMutex          myMutex;
84 };
85
86 class PYINTERP_EXPORT PyInterp_LockRequest : public PyInterp_Request
87 {
88 public:
89   PyInterp_LockRequest( PyInterp_base* interp, QObject* listener = 0, bool sync = false )
90     : PyInterp_Request( listener, sync ), myInterp( interp ) {}
91
92 protected:
93   PyInterp_base*  getInterp() const { return myInterp; }
94
95   virtual void    safeExecute();
96
97 private:
98   PyInterp_base*  myInterp;
99 };
100
101 class PYINTERP_EXPORT PyInterp_Event : public QCustomEvent
102 {
103   PyInterp_Event();
104   PyInterp_Event( const PyInterp_Event& );
105
106 public:
107   enum { NOTIFY = QEvent::User + 5000, OK, ERROR, INCOMPLETE, LAST };
108
109   PyInterp_Event( int type, PyInterp_Request* request )
110     : QCustomEvent( (QEvent::Type)type ), myRequest( request ) {}
111
112   virtual ~PyInterp_Event();
113
114   PyInterp_Request* GetRequest() const { return myRequest; }
115   operator PyInterp_Request*() const { return myRequest; }
116
117 private:
118   PyInterp_Request* myRequest;
119 };
120
121 class PYINTERP_EXPORT PyInterp_Dispatcher : protected QThread
122 {
123   PyInterp_Dispatcher(); // private constructor
124
125 public:
126   static PyInterp_Dispatcher* Get();
127
128   virtual                     ~PyInterp_Dispatcher();
129
130   bool                        IsBusy() const;  
131   void                        Exec( PyInterp_Request* );
132
133 private:
134   virtual void                run();
135   void                        processRequest( PyInterp_Request* );
136   void                        objectDestroyed( const QObject* );
137
138 private:
139   typedef PyInterp_Request*   RequestPtr;
140
141   std::list<RequestPtr>       myQueue;
142   QMutex                      myQueueMutex;
143   PyInterp_Watcher*           myWatcher;
144
145   static PyInterp_Dispatcher* myInstance;
146
147   friend class PyInterp_Watcher;
148 };
149
150 #endif