Salome HOME
ENV: Windows porting
[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 #ifndef WNT
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   static void init_python();
55
56   virtual int run(const char *command); 
57
58   PyLockWrapper GetLockWrapper();
59
60   std::string getbanner(); 
61   std::string getverr();
62   std::string getvout();  
63
64   const char * getPrevious();
65   const char * getNext();    
66
67  protected:
68   PyThreadState * _tstate;
69   PyObject * _vout;
70   PyObject * _verr;
71   PyObject * _g;
72   PyObject * _codeop;
73   std::list<std::string> _history;
74   std::list<std::string>::iterator _ith;
75   bool _atFirst;
76
77   int simpleRun(const char* command);
78   int initRun();
79
80   virtual bool initState() = 0;
81   virtual bool initContext() = 0;  
82 };
83
84
85 class PYINTERP_EXPORT PyObjWrapper{
86   PyObject* myObject;
87 public:
88   PyObjWrapper(PyObject* theObject): myObject(theObject) {}
89   PyObjWrapper(): myObject(0) {}
90   operator PyObject*(){
91     return myObject;
92   }
93   PyObject* operator->(){
94     return myObject;
95   }
96   PyObject* get(){
97     return myObject;
98   }
99   bool operator!(){
100     return !myObject;
101   }
102   bool operator==(PyObject* theObject){
103     return myObject == theObject;
104   }
105   PyObject** operator&(){
106     return &myObject;
107   }
108   PyObjWrapper& operator=(PyObjWrapper* theObjWrapper){
109     Py_XDECREF(myObject);
110     myObject = theObjWrapper->myObject;
111     return *this;
112   }
113   virtual ~PyObjWrapper(){ 
114     Py_XDECREF(myObject);
115   }
116 };
117
118 #endif