Salome HOME
Popup item "Refresh"
[modules/gui.git] / src / PyInterp / PyInterp_base.h
1 //  SALOME SALOMEGUI : implementation of desktop and GUI kernel
2 //
3 //  Copyright (C) 2003  CEA/DEN, EDF R&D
4 //
5 //
6 //
7 //  File   : PyInterp_base.h
8 //  Author : Christian CAREMOLI, Paul RASCLE, EDF
9 //  Module : SALOME
10 //  $Header$
11
12 #ifndef _PYINTERP_BASE_H_
13 #define _PYINTERP_BASE_H_
14
15 #include "PyInterp.h"
16
17 #include <list>
18 #include <string>
19 #include <iostream>
20
21 // include order important!
22 // pthread then python then qt
23 //#include <pthread.h>  // must be before Python.h !
24
25 #include <Python.h>   // must be before qt includes ...
26
27 class QSemaphore;
28 class QMutex;
29
30 extern "C" PyObject * PyEval_EvalCode(PyObject *co, PyObject *g, PyObject *l);
31
32 #define TOP_HISTORY_PY "--- top of history ---"
33 #define BEGIN_HISTORY_PY "--- begin of history ---"
34
35
36 class SemaphoreLock{
37   QSemaphore* mySemaphore;
38   std::string myComment;
39  public:
40   SemaphoreLock(QSemaphore* theSemaphore, const char* theComment = "");
41   ~SemaphoreLock();
42 };
43
44
45 class PYINTERP_EXPORT PyLockWrapper
46 {
47   PyThreadState* myThreadState;
48   PyThreadState* mySaveThreadState;
49  public:
50   PyLockWrapper(PyThreadState* theThreadState);
51   ~PyLockWrapper();
52 };
53
54
55 class ThreadLock
56 {
57   QMutex* myMutex;
58   std::string myComment;
59 public:
60   ThreadLock(QMutex* theMutex, const char* theComment = "");
61   ~ThreadLock();
62 };
63
64
65 bool IsPyLocked();
66
67 ThreadLock GetPyThreadLock(const char* theComment = "");
68
69 class PYINTERP_EXPORT PyInterp_base{
70  public:
71 //  static PyThreadState *_gtstate;
72   static int _argc;
73   static char* _argv[];
74   static PyObject *builtinmodule;
75   
76   PyInterp_base();
77   ~PyInterp_base();
78   
79   virtual void initialize();
80   static void init_python();
81
82   virtual int run(const char *command); 
83
84   PyLockWrapper GetLockWrapper();
85
86   std::string getbanner(); 
87   std::string getverr();
88   std::string getvout();  
89
90   const char * getPrevious();
91   const char * getNext();    
92
93  protected:
94   PyThreadState * _tstate;
95   PyObject * _vout;
96   PyObject * _verr;
97   PyObject * _g;
98   PyObject * _codeop;
99   std::list<std::string> _history;
100   std::list<std::string>::iterator _ith;
101   bool _atFirst;
102
103   int simpleRun(const char* command);
104   int initRun();
105
106   virtual bool initState() = 0;
107   virtual bool initContext() = 0;  
108 };
109
110
111 class PYINTERP_EXPORT PyObjWrapper{
112   PyObject* myObject;
113 public:
114   PyObjWrapper(PyObject* theObject): myObject(theObject) {}
115   PyObjWrapper(): myObject(0) {}
116   operator PyObject*(){
117     return myObject;
118   }
119   PyObject* operator->(){
120     return myObject;
121   }
122   PyObject* get(){
123     return myObject;
124   }
125   bool operator!(){
126     return !myObject;
127   }
128   bool operator==(PyObject* theObject){
129     return myObject == theObject;
130   }
131   PyObject** operator&(){
132     return &myObject;
133   }
134   PyObjWrapper& operator=(PyObjWrapper* theObjWrapper){
135     Py_XDECREF(myObject);
136     myObject = theObjWrapper->myObject;
137     return *this;
138   }
139   virtual ~PyObjWrapper(){ 
140     Py_XDECREF(myObject);
141   }
142 };
143
144 #endif