]> SALOME platform Git repositories - modules/gui.git/blobdiff - src/PyInterp/PyInterp_Interp.h
Salome HOME
0023270: [CEA 1822] PARAVIS GUI widgets should be instanciated only when needed:...
[modules/gui.git] / src / PyInterp / PyInterp_Interp.h
index 72125dbd68b5fb0cec814de795d1b4fef04bb889..88e5519166450fe10e93ed39061f5a36636343bb 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
 //
 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
-
 //  File   : PyInterp_Interp.h
-//  Author : Christian CAREMOLI, Paul RASCLE, EDF
-//  Module : SALOME
-//
+//  Author : Christian CAREMOLI, Paul RASCLE, Adrien BRUNETON
+
 #ifndef PYINTERP_INTERP_H
 #define PYINTERP_INTERP_H
 
 #include "PyInterp.h"   // !!! WARNING !!! THIS INCLUDE MUST BE THE VERY FIRST !!!
+#include "PyInterp_Utils.h"
 
 #include <list>
 #include <string>
 
-/**
- * Utility class wrappin the Python GIL acquisition. This makes use of the high level
- * API (PyGILState_Ensure and PyGILState_Release), and is hence compatible with only
- * one running Python interpreter (no call to Py_NewInterpreter()).
- * When the class is instanciated the lock is acquired. It is released at destruction time.
- * Copy construction (and hence assignation) is forbidden.
- */
-class PYINTERP_EXPORT PyLockWrapper
-{
-  PyGILState_STATE _gil_state;
-public:
-  PyLockWrapper();
-  ~PyLockWrapper();
-
-private:
-  // "Rule of 3" - Forbid usage of copy operator and copy-constructor
-  PyLockWrapper(const PyLockWrapper & another);
-  const PyLockWrapper & operator=(const PyLockWrapper & another);
-};
-
 typedef void PyOutChanged(void* data,char * c);
 
+typedef struct {
+  PyObject_HEAD
+  int softspace;
+  PyOutChanged* _cb;
+  void* _data;
+  bool _iscerr;
+} PyStdOut;
+
 /**
  * Main class representing a *virtual* Python interpreter. There is really only one true
  * Python interpreter in the whole application (no call to Py_NewInterpreter),
@@ -73,72 +60,44 @@ public:
   virtual ~PyInterp_Interp();
   
   void initialize();
+  void destroy();
 
   virtual int run(const char *command); 
+  virtual void initStudy() {};
 
-  // [ABN] - the PyLockWrapper is no more attached to the interpreter
-  // PyLockWrapper GetLockWrapper() const;
-
-  std::string getbanner() const;
-  void setverrcb(PyOutChanged*,void*);
-  void setvoutcb(PyOutChanged*,void*);
+  std::string getBanner() const;
+  void setverrcb(PyOutChanged*, void*);
+  void setvoutcb(PyOutChanged*, void*);
 
-  const char * getPrevious();
-  const char * getNext();
+  const char* getPrevious();
+  const char* getNext();
 
 protected:
   /** Redirection of stdout and stderr */
-  PyObject * _vout;
-  PyObject * _verr;
+  PyObject* _vout;
+  PyObject* _verr;
   /** Execution context (local and global variables) */
-  PyObject * _context;
-  PyObject * _codeop;
+  PyObject* _global_context;
+  PyObject* _local_context;
+
   std::list<std::string> _history;
   std::list<std::string>::iterator _ith;
 
   virtual int beforeRun();
+  virtual int afterRun();
   int simpleRun(const char* command, const bool addToHistory = true);
 
   virtual void initPython();
-  /** OBSOLETE - should'nt be called anymore */
-  //virtual bool initState() = 0;
 
-  /** Initialize execution context. Must set the member _context, and return True on success. */
-  virtual bool initContext() = 0;
+  /** Initialize execution context. */
+  virtual bool initContext();
   virtual bool initRun();
-};
+  virtual void closeContext();
 
-/**
- * Utility class to properly handle the reference counting required on Python objects.
- */
-class PYINTERP_EXPORT PyObjWrapper
-{
-  PyObject* myObject;
-public:
-  PyObjWrapper(PyObject* theObject) : myObject(theObject) {}
-  PyObjWrapper() : myObject(0) {}
-  virtual ~PyObjWrapper() { Py_XDECREF(myObject); }
-
-  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;
-  }
-};
+  bool initialized() const;
 
-typedef struct {
-  PyObject_HEAD
-  int softspace;
-  PyOutChanged* _cb;
-  void* _data;
-  bool _iscerr;
-} PyStdOut;
+private:
+  bool _initialized;
+};
 
 #endif // PYINTERP_INTERP_H