Salome HOME
Moved some functionality to VTKViewer_Utilities.h
[modules/kernel.git] / src / SALOMEGUI / PyInterp_base.h
1 //  SALOME SALOMEGUI : implementation of desktop and GUI kernel
2 //
3 //  Copyright (C) 2003  CEA/DEN, EDF R&D
4 //
5 //
6 //
7 //  File   : PyInterp_base.h
8 //  Author : Christian CAREMOLI, Paul RASCLE, EDF
9 //  Module : SALOME
10 //  $Header$
11
12 #ifndef _PYINTERP_BASE_H_
13 #define _PYINTERP_BASE_H_
14
15 #include <list>
16 #include <string>
17 #include <iostream>
18
19 #include <Python.h>
20
21 class QSemaphore;
22 class QMutex;
23
24 extern "C" PyObject * PyEval_EvalCode(PyObject *co, PyObject *g, PyObject *l);
25
26 #define TOP_HISTORY_PY "--- top of history ---"
27 #define BEGIN_HISTORY_PY "--- begin of history ---"
28
29
30 class SemaphoreLock{
31   QSemaphore* mySemaphore;
32   std::string myComment;
33  public:
34   SemaphoreLock(QSemaphore* theSemaphore, const char* theComment = "");
35   ~SemaphoreLock();
36 };
37
38
39 class PyLockWrapper{
40   PyThreadState* myThreadState;
41   PyThreadState* mySaveThreadState;
42  public:
43   PyLockWrapper(PyThreadState* theThreadState);
44   ~PyLockWrapper();
45 };
46
47
48 class ThreadLock{
49   QMutex* myMutex;
50   std::string myComment;
51  public:
52   ThreadLock(QMutex* theMutex, const char* theComment = "");
53   ~ThreadLock();
54 };
55
56
57 bool IsPyLocked();
58
59 ThreadLock GetPyThreadLock(const char* theComment = "");
60
61
62 class PyInterp_base{
63  public:
64   static PyThreadState *_gtstate;
65   static int _argc;
66   static char* _argv[];
67   static PyObject *builtinmodule;
68   static PyObject *salome_shared_modules_module;
69   
70   PyInterp_base();
71   ~PyInterp_base();
72   
73   virtual void initialize();
74
75   int run(const char *command); 
76
77   PyLockWrapper GetLockWrapper();
78
79   std::string getbanner(); 
80   std::string getverr();
81   std::string getvout();  
82
83   const char * getPrevious();
84   const char * getNext();    
85
86  protected:
87   PyThreadState * _tstate;
88   PyObject * _vout;
89   PyObject * _verr;
90   PyObject * _g;
91   std::list<std::string> _history;
92   std::list<std::string>::iterator _ith;
93   bool _atFirst;
94
95   int simpleRun(const char* command);
96   int initRun();
97
98   virtual void initState() = 0;
99   virtual void initContext() = 0;  
100 };
101
102
103 class PyObjWrapper{
104   PyObject* myObject;
105 public:
106   PyObjWrapper(PyObject* theObject): myObject(theObject) {}
107   PyObjWrapper(): myObject(0) {}
108   operator PyObject*(){
109     return myObject;
110   }
111   PyObject* operator->(){
112     return myObject;
113   }
114   PyObject* get(){
115     return myObject;
116   }
117   bool operator!(){
118     return !myObject;
119   }
120   bool operator==(PyObject* theObject){
121     return myObject == theObject;
122   }
123   PyObject** operator&(){
124     return &myObject;
125   }
126   PyObjWrapper& operator=(PyObjWrapper* theObjWrapper){
127     Py_XDECREF(myObject);
128     myObject = theObjWrapper->myObject;
129     return *this;
130   }
131   virtual ~PyObjWrapper(){ 
132     Py_XDECREF(myObject);
133   }
134 };
135
136
137 #endif