Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef PYINTERP_DISPATCHER_H
00028 #define PYINTERP_DISPATCHER_H
00029
00030 #include "PyInterp.h"
00031
00032 #include <QMutex>
00033 #include <QThread>
00034 #include <QEvent>
00035 #include <QQueue>
00036
00037 class QObject;
00038
00039 class PyInterp_Interp;
00040 class PyInterp_Watcher;
00041 class PyInterp_Dispatcher;
00042 class PyInterp_ExecuteEvent;
00043
00044 class PYINTERP_EXPORT PyInterp_Request
00045 {
00046 friend class PyInterp_Dispatcher;
00047 friend class PyInterp_ExecuteEvent;
00048
00049 PyInterp_Request();
00050 PyInterp_Request( const PyInterp_Request& );
00051
00052 protected:
00053 virtual ~PyInterp_Request() {};
00054
00055
00056 public:
00057 PyInterp_Request( QObject* listener, bool sync = false )
00058 : myIsSync( sync ), myListener( listener ) {};
00059
00060 static void Destroy( PyInterp_Request* );
00061
00062
00063 bool IsSync() const { return myIsSync; }
00064
00065
00066
00067 protected:
00068 virtual void safeExecute();
00069
00070 virtual void execute() = 0;
00071
00072
00073 virtual QEvent* createEvent() const;
00074
00075
00076 virtual void processEvent( QObject* );
00077
00078 QObject* listener() const { return myListener; }
00079 void setListener( QObject* );
00080
00081 private:
00082 void process();
00083
00084 private:
00085 QMutex myMutex;
00086 bool myIsSync;
00087 QObject* myListener;
00088 };
00089
00090 class PYINTERP_EXPORT PyInterp_LockRequest : public PyInterp_Request
00091 {
00092 public:
00093 PyInterp_LockRequest( PyInterp_Interp* interp, QObject* listener = 0, bool sync = false )
00094 : PyInterp_Request( listener, sync ), myInterp( interp ) {}
00095
00096 protected:
00097 PyInterp_Interp* getInterp() const { return myInterp; }
00098
00099 virtual void safeExecute();
00100
00101 private:
00102 PyInterp_Interp* myInterp;
00103 };
00104
00105 class PYINTERP_EXPORT PyInterp_Event : public QEvent
00106 {
00107 PyInterp_Event();
00108 PyInterp_Event( const PyInterp_Event& );
00109
00110 public:
00111
00112 enum { ES_NOTIFY = QEvent::User + 5000, ES_OK, ES_ERROR, ES_INCOMPLETE, ES_LAST };
00113
00114 PyInterp_Event( int type, PyInterp_Request* request )
00115 : QEvent( (QEvent::Type)type ), myRequest( request ) {}
00116
00117 virtual ~PyInterp_Event();
00118
00119 PyInterp_Request* GetRequest() const { return myRequest; }
00120 operator PyInterp_Request*() const { return myRequest; }
00121
00122 private:
00123 PyInterp_Request* myRequest;
00124 };
00125
00126 class PYINTERP_EXPORT PyInterp_Dispatcher : protected QThread
00127 {
00128 PyInterp_Dispatcher();
00129
00130 public:
00131 static PyInterp_Dispatcher* Get();
00132
00133 virtual ~PyInterp_Dispatcher();
00134
00135 bool IsBusy() const;
00136 void Exec( PyInterp_Request* );
00137
00138 private:
00139 virtual void run();
00140 void processRequest( PyInterp_Request* );
00141 void objectDestroyed( const QObject* );
00142
00143 private:
00144 typedef PyInterp_Request* RequestPtr;
00145
00146 QQueue<RequestPtr> myQueue;
00147 QMutex myQueueMutex;
00148 PyInterp_Watcher* myWatcher;
00149
00150 static PyInterp_Dispatcher* myInstance;
00151
00152 friend class PyInterp_Watcher;
00153 };
00154
00155 #endif // PYINTERP_DISPATCHER_H