Salome HOME
Copyright update 2022
[modules/gui.git] / tools / PyInterp / src / PyInterp_Request.h
1 // Copyright (C) 2007-2022  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, or (at your option) any later version.
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 //  File   : PyInterp_Request.h
23 //  Author : Sergey Anikin (OPEN CASCADE S.A.S.), Adrien Bruneton (CEA/DEN)
24
25 #ifndef PYINTERP_REQUEST_H
26 #define PYINTERP_REQUEST_H
27
28 #include "PyInterp.h"
29 #include "PyInterp_Event.h"
30
31 #include <QMutex>
32
33 class QObject;
34 class PyInterp_Interp;
35
36 /**
37    \class PyInterp_Request
38    \brief Base Python interpreter request; does not not acquire GIL during execution.
39  */
40 class PYINTERP_EXPORT PyInterp_Request
41 {
42   friend class PyInterp_Dispatcher;
43
44 private:
45   PyInterp_Request();
46   PyInterp_Request( const PyInterp_Request& );
47
48 protected:
49   // protected destructor - to control deletion of requests
50   virtual ~PyInterp_Request() {};
51
52 public:
53   // Constructor
54   PyInterp_Request( QObject* listener, bool sync = true )
55     : myIsSync( sync ), myListener( listener ) {};
56
57   // Deletes a request
58   static void     Destroy( PyInterp_Request* );
59
60   // Returns true if this request should be processed synchronously,
61   // without putting it to a queue
62   bool            IsSync() const { return myIsSync; }
63
64 protected:
65   // Performs safe execution of the request
66   virtual void    safeExecute();
67
68   // Should be redefined in successors, contains actual request code
69   virtual void    execute() = 0;
70
71   // This method can be overridden to customize notification event creation
72   virtual QEvent* createEvent();
73
74   virtual void    processEvent( QObject* );
75
76   // Provide access to the listener of this request
77   QObject*        listener() const { return myListener; }
78   void            setListener( QObject* );
79
80 private:
81   // Process request, invoked from Dispatcher
82   void            process();
83
84 private:
85   QMutex          myMutex;
86   bool            myIsSync;
87   QObject*        myListener;
88 };
89
90 /**
91    \class PyInterp_LockRequest
92    \brief Python interpreter request; automatically acquires GIL during execution.
93  */
94 class PYINTERP_EXPORT PyInterp_LockRequest : public PyInterp_Request
95 {
96 public:
97   // Constructor
98   PyInterp_LockRequest( PyInterp_Interp* interp, QObject* listener=0, bool sync=true )
99     : PyInterp_Request( listener, sync ), myInterp( interp )
100   {}
101
102 protected:
103   // Get interpreter
104   PyInterp_Interp*  getInterp() const { return myInterp; }
105
106   // Performs safe execution of the request
107   virtual void      safeExecute();
108
109 private:
110   PyInterp_Interp*  myInterp;
111 };
112
113 #endif // PYINTERP_REQUEST_H