Salome HOME
Moved some functionality to VTKViewer_Utilities.h
[modules/kernel.git] / src / SALOMEGUI / PyInterp_base.h
index 9fe277d6a3a46e24df6cf9137370412bd8aee629..157d5c3f7ae70be4330df10f1e8940c13b81c8a2 100644 (file)
 #ifndef _PYINTERP_BASE_H_
 #define _PYINTERP_BASE_H_
 
-#include <iostream>
-#include <Python.h>
 #include <list>
 #include <string>
-using namespace std;
+#include <iostream>
+
+#include <Python.h>
+
+class QSemaphore;
+class QMutex;
+
+extern "C" PyObject * PyEval_EvalCode(PyObject *co, PyObject *g, PyObject *l);
 
 #define TOP_HISTORY_PY "--- top of history ---"
 #define BEGIN_HISTORY_PY "--- begin of history ---"
 
-/*!
- * We have our own routines which are identical to the SIP routines
- * to not depend from SIP software evolutions
- */
 
-extern "C" void salomeAcquireLock();
-extern "C" void salomeReleaseLock();
-extern "C" int salomeCondAcquireLock();
-extern "C" void salomeCondReleaseLock(int rellock);
+class SemaphoreLock{
+  QSemaphore* mySemaphore;
+  std::string myComment;
+ public:
+  SemaphoreLock(QSemaphore* theSemaphore, const char* theComment = "");
+  ~SemaphoreLock();
+};
 
-/*! this class can only be used with derivation : 
- *  2 pure virtual methods, initstate() & initcontext()
- */
 
-class PyInterp_base
-{
+class PyLockWrapper{
+  PyThreadState* myThreadState;
+  PyThreadState* mySaveThreadState;
+ public:
+  PyLockWrapper(PyThreadState* theThreadState);
+  ~PyLockWrapper();
+};
+
+
+class ThreadLock{
+  QMutex* myMutex;
+  std::string myComment;
+ public:
+  ThreadLock(QMutex* theMutex, const char* theComment = "");
+  ~ThreadLock();
+};
+
+
+bool IsPyLocked();
+
+ThreadLock GetPyThreadLock(const char* theComment = "");
+
+
+class PyInterp_base{
  public:
   static PyThreadState *_gtstate;
   static int _argc;
   static char* _argv[];
   static PyObject *builtinmodule;
   static PyObject *salome_shared_modules_module;
-
+  
   PyInterp_base();
   ~PyInterp_base();
-  void initialize();
+  
+  virtual void initialize();
+
   int run(const char *command); 
-  char * getverr();
-  char * getvout();  
-  string getbanner(); 
+
+  PyLockWrapper GetLockWrapper();
+
+  std::string getbanner(); 
+  std::string getverr();
+  std::string getvout();  
+
   const char * getPrevious();
   const char * getNext();    
-  void enter();
-  void quit();
-  void basicRun(const char *command);
 
  protected:
   PyThreadState * _tstate;
-  PyObject      * _vout;
-  PyObject      * _verr;
-  PyObject      * _g;
-  list <string> _history;
-  list <string>::iterator _ith;
+  PyObject * _vout;
+  PyObject * _verr;
+  PyObject * _g;
+  std::list<std::string> _history;
+  std::list<std::string>::iterator _ith;
   bool _atFirst;
 
   int simpleRun(const char* command);
   int initRun();
 
   virtual void initState() = 0;
-  virtual void initContext() =0;  
+  virtual void initContext() = 0;  
 };
 
+
+class PyObjWrapper{
+  PyObject* myObject;
+public:
+  PyObjWrapper(PyObject* theObject): myObject(theObject) {}
+  PyObjWrapper(): myObject(0) {}
+  operator PyObject*(){
+    return myObject;
+  }
+  PyObject* operator->(){
+    return myObject;
+  }
+  PyObject* get(){
+    return myObject;
+  }
+  bool operator!(){
+    return !myObject;
+  }
+  bool operator==(PyObject* theObject){
+    return myObject == theObject;
+  }
+  PyObject** operator&(){
+    return &myObject;
+  }
+  PyObjWrapper& operator=(PyObjWrapper* theObjWrapper){
+    Py_XDECREF(myObject);
+    myObject = theObjWrapper->myObject;
+    return *this;
+  }
+  virtual ~PyObjWrapper(){ 
+    Py_XDECREF(myObject);
+  }
+};
+
+
 #endif