Back to index

salome-gui  6.5.0
PyInterp_Dispatcher.h
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  File   : PyInterp_Dispatcher.h
00024 //  Author : Sergey Anikin, OCC
00025 //  Module : SALOME
00026 //
00027 #ifndef PYINTERP_DISPATCHER_H
00028 #define PYINTERP_DISPATCHER_H
00029 
00030 #include "PyInterp.h"   // !!! WARNING !!! THIS INCLUDE MUST BE THE VERY FIRST !!!
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   // protected destructor - to control deletion of requests
00055 
00056 public:
00057   PyInterp_Request( QObject* listener, bool sync = false )
00058     : myIsSync( sync ), myListener( listener ) {};
00059 
00060   static void     Destroy( PyInterp_Request* );
00061   // Deletes a request
00062 
00063   bool            IsSync() const { return myIsSync; }
00064   // Returns true if this request should be processed synchronously,
00065   // without putting it to a queue
00066 
00067 protected:
00068   virtual void    safeExecute();
00069 
00070   virtual void    execute() = 0;
00071   // Should be redefined in successors, contains actual request code
00072 
00073   virtual QEvent* createEvent() const;
00074   // This method can be overridden to customize notification event creation
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   //Execution state
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(); // private constructor
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