Salome HOME
Fix pb with persistence of "light" modules: SaveAs operation leads to loss of non...
[modules/gui.git] / src / PyInterp / PyInterp_Interp.h
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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
23 //  File   : PyInterp_Interp.h
24 //  Author : Christian CAREMOLI, Paul RASCLE, EDF
25 //  Module : SALOME
26 //
27 #ifndef PYINTERP_INTERP_H
28 #define PYINTERP_INTERP_H
29
30 #include "PyInterp.h"   // !!! WARNING !!! THIS INCLUDE MUST BE THE VERY FIRST !!!
31
32 #include <list>
33 #include <string>
34
35 class PYINTERP_EXPORT PyLockWrapper
36 {
37   PyThreadState* myThreadState;
38   PyThreadState* mySaveThreadState;
39   PyGILState_STATE _savestate;
40 public:
41   PyLockWrapper(PyThreadState* theThreadState);
42   ~PyLockWrapper();
43 };
44
45 typedef void PyOutChanged(void* data,char * c);
46
47 class PYINTERP_EXPORT PyInterp_Interp
48 {
49 public:
50   static int _argc;
51   static char* _argv[];
52   static PyObject *builtinmodule;
53   static PyThreadState *_gtstate;
54   static PyInterpreterState *_interp;
55   
56   PyInterp_Interp();
57   virtual ~PyInterp_Interp();
58   
59   void initialize();
60
61   virtual int run(const char *command); 
62
63   PyLockWrapper GetLockWrapper();
64
65   std::string getbanner(); 
66   void setverrcb(PyOutChanged*,void*);
67   void setvoutcb(PyOutChanged*,void*);
68
69   const char * getPrevious();
70   const char * getNext();    
71
72 protected:
73   PyThreadState * _tstate;
74   PyObject * _vout;
75   PyObject * _verr;
76   PyObject * _g;
77   PyObject * _codeop;
78   std::list<std::string> _history;
79   std::list<std::string>::iterator _ith;
80
81   virtual int beforeRun() { return 0; }
82   int simpleRun(const char* command, const bool addToHistory = true);
83
84   virtual bool initRun();
85   virtual void initPython();
86   virtual bool initState() = 0;
87   virtual bool initContext() = 0;  
88 };
89
90 class PYINTERP_EXPORT PyObjWrapper
91 {
92   PyObject* myObject;
93 public:
94   PyObjWrapper(PyObject* theObject) : myObject(theObject) {}
95   PyObjWrapper() : myObject(0) {}
96   virtual ~PyObjWrapper() { Py_XDECREF(myObject); }
97
98   operator PyObject*()    { return myObject;  }
99   PyObject* operator->()  { return myObject;  }
100   PyObject* get()         { return myObject;  }
101   bool operator!()        { return !myObject; }
102   bool operator==(PyObject* theObject) { return myObject == theObject; }
103   PyObject** operator&()  { return &myObject; }
104   PyObjWrapper& operator=(PyObjWrapper* theObjWrapper)
105   {
106     Py_XDECREF(myObject);
107     myObject = theObjWrapper->myObject;
108     return *this;
109   }
110 };
111
112 typedef struct {
113   PyObject_HEAD
114   int softspace;
115   PyOutChanged* _cb;
116   void* _data;
117   bool _iscerr;
118 } PyStdOut;
119
120 #endif // PYINTERP_INTERP_H