]> SALOME platform Git repositories - modules/gui.git/blob - src/PyInterp/PyInterp_Interp.h
Salome HOME
32fadb60f3b12e2c8a46c9f64f044597b1963f49
[modules/gui.git] / src / PyInterp / PyInterp_Interp.h
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  File   : PyInterp_Interp.h
23 //  Author : Christian CAREMOLI, Paul RASCLE, EDF
24 //  Module : SALOME
25 //
26 #ifndef PYINTERP_INTERP_H
27 #define PYINTERP_INTERP_H
28
29 #include "PyInterp.h"   // !!! WARNING !!! THIS INCLUDE MUST BE THE VERY FIRST !!!
30
31 #include <list>
32 #include <string>
33
34 class PYINTERP_EXPORT PyLockWrapper
35 {
36   PyThreadState* myThreadState;
37   PyThreadState* mySaveThreadState;
38   PyGILState_STATE _savestate;
39 public:
40   PyLockWrapper(PyThreadState* theThreadState);
41   ~PyLockWrapper();
42 };
43
44 typedef void PyOutChanged(void* data,char * c);
45
46 class PYINTERP_EXPORT PyInterp_Interp
47 {
48 public:
49   static int _argc;
50   static char* _argv[];
51   static PyObject *builtinmodule;
52   static PyThreadState *_gtstate;
53   static PyInterpreterState *_interp;
54   
55   PyInterp_Interp();
56   virtual ~PyInterp_Interp();
57   
58   void initialize();
59
60   virtual int run(const char *command); 
61
62   PyLockWrapper GetLockWrapper();
63
64   std::string getbanner(); 
65   void setverrcb(PyOutChanged*,void*);
66   void setvoutcb(PyOutChanged*,void*);
67
68   const char * getPrevious();
69   const char * getNext();    
70
71 protected:
72   PyThreadState * _tstate;
73   PyObject * _vout;
74   PyObject * _verr;
75   PyObject * _g;
76   PyObject * _codeop;
77   std::list<std::string> _history;
78   std::list<std::string>::iterator _ith;
79
80   virtual int beforeRun() { return 0; }
81   int simpleRun(const char* command, const bool addToHistory = true);
82
83   virtual bool initRun();
84   virtual void initPython();
85   virtual bool initState() = 0;
86   virtual bool initContext() = 0;  
87 };
88
89 class PYINTERP_EXPORT PyObjWrapper
90 {
91   PyObject* myObject;
92 public:
93   PyObjWrapper(PyObject* theObject) : myObject(theObject) {}
94   PyObjWrapper() : myObject(0) {}
95   virtual ~PyObjWrapper() { Py_XDECREF(myObject); }
96
97   operator PyObject*()    { return myObject;  }
98   PyObject* operator->()  { return myObject;  }
99   PyObject* get()         { return myObject;  }
100   bool operator!()        { return !myObject; }
101   bool operator==(PyObject* theObject) { return myObject == theObject; }
102   PyObject** operator&()  { return &myObject; }
103   PyObjWrapper& operator=(PyObjWrapper* theObjWrapper)
104   {
105     Py_XDECREF(myObject);
106     myObject = theObjWrapper->myObject;
107     return *this;
108   }
109 };
110
111 typedef struct {
112   PyObject_HEAD
113   int softspace;
114   PyOutChanged* _cb;
115   void* _data;
116   bool _iscerr;
117 } PyStdOut;
118
119 #endif // PYINTERP_INTERP_H