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