Back to index

salome-gui  6.5.0
PyInterp_Interp.h
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  File   : PyInterp_Interp.h
00024 //  Author : Christian CAREMOLI, Paul RASCLE, EDF
00025 //  Module : SALOME
00026 //
00027 #ifndef PYINTERP_INTERP_H
00028 #define PYINTERP_INTERP_H
00029 
00030 #include "PyInterp.h"   // !!! WARNING !!! THIS INCLUDE MUST BE THE VERY FIRST !!!
00031 
00032 #include <list>
00033 #include <string>
00034 
00035 class PYINTERP_EXPORT PyLockWrapper
00036 {
00037   PyThreadState* myThreadState;
00038   PyThreadState* mySaveThreadState;
00039   PyGILState_STATE _savestate;
00040 public:
00041   PyLockWrapper(PyThreadState* theThreadState);
00042   ~PyLockWrapper();
00043 };
00044 
00045 typedef void PyOutChanged(void* data,char * c);
00046 
00047 class PYINTERP_EXPORT PyInterp_Interp
00048 {
00049 public:
00050   static int _argc;
00051   static char* _argv[];
00052   static PyObject *builtinmodule;
00053   static PyThreadState *_gtstate;
00054   static PyInterpreterState *_interp;
00055   
00056   PyInterp_Interp();
00057   virtual ~PyInterp_Interp();
00058   
00059   void initialize();
00060 
00061   virtual int run(const char *command); 
00062 
00063   PyLockWrapper GetLockWrapper();
00064 
00065   std::string getbanner(); 
00066   void setverrcb(PyOutChanged*,void*);
00067   void setvoutcb(PyOutChanged*,void*);
00068 
00069   const char * getPrevious();
00070   const char * getNext();    
00071 
00072 protected:
00073   PyThreadState * _tstate;
00074   PyObject * _vout;
00075   PyObject * _verr;
00076   PyObject * _g;
00077   PyObject * _codeop;
00078   std::list<std::string> _history;
00079   std::list<std::string>::iterator _ith;
00080 
00081   virtual int beforeRun() { return 0; }
00082   int simpleRun(const char* command, const bool addToHistory = true);
00083 
00084   virtual bool initRun();
00085   virtual void initPython();
00086   virtual bool initState() = 0;
00087   virtual bool initContext() = 0;  
00088 };
00089 
00090 class PYINTERP_EXPORT PyObjWrapper
00091 {
00092   PyObject* myObject;
00093 public:
00094   PyObjWrapper(PyObject* theObject) : myObject(theObject) {}
00095   PyObjWrapper() : myObject(0) {}
00096   virtual ~PyObjWrapper() { Py_XDECREF(myObject); }
00097 
00098   operator PyObject*()    { return myObject;  }
00099   PyObject* operator->()  { return myObject;  }
00100   PyObject* get()         { return myObject;  }
00101   bool operator!()        { return !myObject; }
00102   bool operator==(PyObject* theObject) { return myObject == theObject; }
00103   PyObject** operator&()  { return &myObject; }
00104   PyObjWrapper& operator=(PyObjWrapper* theObjWrapper)
00105   {
00106     Py_XDECREF(myObject);
00107     myObject = theObjWrapper->myObject;
00108     return *this;
00109   }
00110 };
00111 
00112 typedef struct {
00113   PyObject_HEAD
00114   int softspace;
00115   PyOutChanged* _cb;
00116   void* _data;
00117   bool _iscerr;
00118 } PyStdOut;
00119 
00120 #endif // PYINTERP_INTERP_H