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