Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / PyInterp / PyInterp_Interp.h
1
2 #ifndef PYINTERP_INTERP_H
3 #define PYINTERP_INTERP_H
4
5 #include "PyInterp.h"   // !!! WARNING !!! THIS INCLUDE MUST BE THE VERY FIRST !!!
6
7 #include <list>
8 #include <string>
9
10 class PYINTERP_EXPORT PyLockWrapper
11 {
12   PyThreadState* myThreadState;
13   PyThreadState* mySaveThreadState;
14   PyGILState_STATE _savestate;
15 public:
16   PyLockWrapper(PyThreadState* theThreadState);
17   ~PyLockWrapper();
18 };
19
20 typedef void PyOutChanged(void* data,char * c);
21
22 class PYINTERP_EXPORT PyInterp_Interp
23 {
24 public:
25   static int _argc;
26   static char* _argv[];
27   static PyObject *builtinmodule;
28   static PyThreadState *_gtstate;
29   static PyInterpreterState *_interp;
30   
31   PyInterp_Interp();
32   virtual ~PyInterp_Interp();
33   
34   void initialize();
35
36   virtual int run(const char *command); 
37
38   PyLockWrapper GetLockWrapper();
39
40   std::string getbanner(); 
41   void setverrcb(PyOutChanged*,void*);
42   void setvoutcb(PyOutChanged*,void*);
43
44   const char * getPrevious();
45   const char * getNext();    
46
47 protected:
48   PyThreadState * _tstate;
49   PyObject * _vout;
50   PyObject * _verr;
51   PyObject * _g;
52   PyObject * _codeop;
53   std::list<std::string> _history;
54   std::list<std::string>::iterator _ith;
55
56   virtual int beforeRun() { return 0; }
57   int simpleRun(const char* command, const bool addToHistory = true);
58
59   virtual bool initRun();
60   virtual void initPython();
61   virtual bool initState() = 0;
62   virtual bool initContext() = 0;  
63 };
64
65 class PYINTERP_EXPORT PyObjWrapper
66 {
67   PyObject* myObject;
68 public:
69   PyObjWrapper(PyObject* theObject) : myObject(theObject) {}
70   PyObjWrapper() : myObject(0) {}
71   virtual ~PyObjWrapper() { Py_XDECREF(myObject); }
72
73   operator PyObject*()    { return myObject;  }
74   PyObject* operator->()  { return myObject;  }
75   PyObject* get()         { return myObject;  }
76   bool operator!()        { return !myObject; }
77   bool operator==(PyObject* theObject) { return myObject == theObject; }
78   PyObject** operator&()  { return &myObject; }
79   PyObjWrapper& operator=(PyObjWrapper* theObjWrapper)
80   {
81     Py_XDECREF(myObject);
82     myObject = theObjWrapper->myObject;
83     return *this;
84   }
85 };
86
87 typedef struct {
88   PyObject_HEAD
89   int softspace;
90   PyOutChanged* _cb;
91   void* _data;
92   bool _iscerr;
93 } PyStdOut;
94
95 #endif // PYINTERP_INTERP_H