1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : PyInterp_Interp.h
23 // Author : Christian CAREMOLI, Paul RASCLE, EDF
26 #ifndef PYINTERP_INTERP_H
27 #define PYINTERP_INTERP_H
29 #include "PyInterp.h" // !!! WARNING !!! THIS INCLUDE MUST BE THE VERY FIRST !!!
34 class PYINTERP_EXPORT PyLockWrapper
36 PyThreadState* myThreadState;
37 PyThreadState* mySaveThreadState;
38 PyGILState_STATE _savestate;
40 PyLockWrapper(PyThreadState* theThreadState);
44 typedef void PyOutChanged(void* data,char * c);
46 class PYINTERP_EXPORT PyInterp_Interp
51 static PyObject *builtinmodule;
52 static PyThreadState *_gtstate;
53 static PyInterpreterState *_interp;
56 virtual ~PyInterp_Interp();
60 virtual int run(const char *command);
62 PyLockWrapper GetLockWrapper();
64 std::string getbanner();
65 void setverrcb(PyOutChanged*,void*);
66 void setvoutcb(PyOutChanged*,void*);
68 const char * getPrevious();
69 const char * getNext();
72 PyThreadState * _tstate;
77 std::list<std::string> _history;
78 std::list<std::string>::iterator _ith;
80 virtual int beforeRun() { return 0; }
81 int simpleRun(const char* command, const bool addToHistory = true);
83 virtual bool initRun();
84 virtual void initPython();
85 virtual bool initState() = 0;
86 virtual bool initContext() = 0;
89 class PYINTERP_EXPORT PyObjWrapper
93 PyObjWrapper(PyObject* theObject) : myObject(theObject) {}
94 PyObjWrapper() : myObject(0) {}
95 virtual ~PyObjWrapper() { Py_XDECREF(myObject); }
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)
105 Py_XDECREF(myObject);
106 myObject = theObjWrapper->myObject;
119 #endif // PYINTERP_INTERP_H