Salome HOME
Fix for Bug IPAL9053( 3.0.0: "Check Geometry" and "Load script" functionalities from...
[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 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 class PYINTERP_EXPORT PyLockWrapper
33 {
34   PyThreadState* myThreadState;
35   PyThreadState* mySaveThreadState;
36  public:
37   PyLockWrapper(PyThreadState* theThreadState);
38   ~PyLockWrapper();
39 };
40
41
42 class PYINTERP_EXPORT PyInterp_base{
43  public:
44   static int _argc;
45   static char* _argv[];
46   static PyObject *builtinmodule;
47   
48   PyInterp_base();
49   ~PyInterp_base();
50   
51   virtual void initialize();
52   static void init_python();
53
54   virtual int run(const char *command); 
55
56   PyLockWrapper GetLockWrapper();
57
58   std::string getbanner(); 
59   std::string getverr();
60   std::string getvout();  
61
62   const char * getPrevious();
63   const char * getNext();    
64
65  protected:
66   PyThreadState * _tstate;
67   PyObject * _vout;
68   PyObject * _verr;
69   PyObject * _g;
70   PyObject * _codeop;
71   std::list<std::string> _history;
72   std::list<std::string>::iterator _ith;
73   bool _atFirst;
74
75   int simpleRun(const char* command);
76   int initRun();
77
78   virtual bool initState() = 0;
79   virtual bool initContext() = 0;  
80 };
81
82
83 class PYINTERP_EXPORT PyObjWrapper{
84   PyObject* myObject;
85 public:
86   PyObjWrapper(PyObject* theObject): myObject(theObject) {}
87   PyObjWrapper(): myObject(0) {}
88   operator PyObject*(){
89     return myObject;
90   }
91   PyObject* operator->(){
92     return myObject;
93   }
94   PyObject* get(){
95     return myObject;
96   }
97   bool operator!(){
98     return !myObject;
99   }
100   bool operator==(PyObject* theObject){
101     return myObject == theObject;
102   }
103   PyObject** operator&(){
104     return &myObject;
105   }
106   PyObjWrapper& operator=(PyObjWrapper* theObjWrapper){
107     Py_XDECREF(myObject);
108     myObject = theObjWrapper->myObject;
109     return *this;
110   }
111   virtual ~PyObjWrapper(){ 
112     Py_XDECREF(myObject);
113   }
114 };
115
116 #endif