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