]> SALOME platform Git repositories - modules/gui.git/blob - src/PyInterp/PyInterp_base.h
Salome HOME
b2e0a0b261cdbf0ff8cfb6a2e8e036e09ccb06d3
[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 #if PY_VERSION_HEX < 0x02040000 // python version earlier than 2.4.0
28 extern "C" PyObject * PyEval_EvalCode(PyObject *co, PyObject *g, PyObject *l);
29 #endif
30
31 #define TOP_HISTORY_PY "--- top of history ---"
32 #define BEGIN_HISTORY_PY "--- begin of history ---"
33
34 class PYINTERP_EXPORT PyLockWrapper
35 {
36   PyThreadState* myThreadState;
37   PyThreadState* mySaveThreadState;
38  public:
39   PyLockWrapper(PyThreadState* theThreadState);
40   ~PyLockWrapper();
41 };
42
43
44 class PYINTERP_EXPORT PyInterp_base{
45  public:
46   static int _argc;
47   static char* _argv[];
48   static PyObject *builtinmodule;
49   
50   PyInterp_base();
51   ~PyInterp_base();
52   
53   virtual void initialize();
54   virtual void init_python();
55   // init_python() made virtual to:
56   // 1. Remove dependency on KERNEL in light SALOME configuration
57   // 2. Allow redefinition of this method in SalomeApp_PyInterp class (it should be empty there and rely on KERNEL_PYTHON)
58
59   virtual int run(const char *command); 
60
61   PyLockWrapper GetLockWrapper();
62
63   std::string getbanner(); 
64   std::string getverr();
65   std::string getvout();  
66
67   const char * getPrevious();
68   const char * getNext();    
69
70  protected:
71   PyThreadState * _tstate;
72   PyObject * _vout;
73   PyObject * _verr;
74   PyObject * _g;
75   PyObject * _codeop;
76   std::list<std::string> _history;
77   std::list<std::string>::iterator _ith;
78   bool _atFirst;
79
80   int simpleRun(const char* command);
81   int initRun();
82
83   virtual bool initState() = 0;
84   virtual bool initContext() = 0;  
85 };
86
87
88 class PYINTERP_EXPORT PyObjWrapper{
89   PyObject* myObject;
90 public:
91   PyObjWrapper(PyObject* theObject): myObject(theObject) {}
92   PyObjWrapper(): myObject(0) {}
93   operator PyObject*(){
94     return myObject;
95   }
96   PyObject* operator->(){
97     return myObject;
98   }
99   PyObject* get(){
100     return myObject;
101   }
102   bool operator!(){
103     return !myObject;
104   }
105   bool operator==(PyObject* theObject){
106     return myObject == theObject;
107   }
108   PyObject** operator&(){
109     return &myObject;
110   }
111   PyObjWrapper& operator=(PyObjWrapper* theObjWrapper){
112     Py_XDECREF(myObject);
113     myObject = theObjWrapper->myObject;
114     return *this;
115   }
116   virtual ~PyObjWrapper(){ 
117     Py_XDECREF(myObject);
118   }
119 };
120
121 #endif