Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/yacs.git] / src / runtime / PythonPorts.hxx
1 // Copyright (C) 2006-2012  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef _PYTHONPORTS_HXX_
21 #define _PYTHONPORTS_HXX_
22
23 #include <Python.h>
24
25 #include "YACSRuntimeSALOMEExport.hxx"
26 #include "InputPort.hxx"
27 #include "OutputPort.hxx"
28
29 namespace YACS
30 {
31   namespace ENGINE
32   {
33     class InterpreterUnlocker 
34     {
35     public:
36       InterpreterUnlocker() {
37           gstate_ = PyGILState_Ensure();
38       }
39       ~InterpreterUnlocker() {
40           PyGILState_Release(gstate_);
41       }
42     private:
43       PyGILState_STATE gstate_;
44     };
45
46     class InterpreterSaveThread {
47     public:
48       inline InterpreterSaveThread() {
49         tstate_ = PyEval_SaveThread();
50       }
51       inline ~InterpreterSaveThread() {
52         PyEval_RestoreThread(tstate_);
53       }
54       inline void lock() {
55         PyEval_RestoreThread(tstate_);
56       }
57       inline void unlock() {
58         tstate_ = PyEval_SaveThread();
59       }
60     private:
61       PyThreadState* tstate_;
62     };
63
64
65     typedef PyObject PyObj;
66
67 /*! \brief Class for Python Ports
68  *
69  * \ingroup Ports
70  *
71  * \see PythonNode
72  */
73     class YACSRUNTIMESALOME_EXPORT InputPyPort : public InputPort
74     {
75     public:
76       InputPyPort(const std::string& name, Node * node, TypeCode * type);
77       InputPyPort(const InputPyPort& other, Node *newHelder);
78       ~InputPyPort();
79       bool edIsManuallyInitialized() const;
80       void edRemoveManInit();
81       virtual void put(const void *data) throw(ConversionException);
82       void put(PyObject *data) throw(ConversionException);
83       InputPort *clone(Node *newHelder) const;
84       //special typedef PyObj used in SWIG to increment ref count on output
85       virtual PyObj * getPyObj() const;
86       virtual std::string getAsString();
87       void *get() const throw(Exception);
88       virtual bool isEmpty();
89       virtual void exSaveInit();
90       virtual void exRestoreInit();
91       virtual std::string dump();
92       virtual std::string typeName() {return "YACS__ENGINE__InputPyPort";}
93       virtual std::string valToStr();
94       virtual void valFromStr(std::string valstr);
95
96     protected:
97       PyObject* _data;
98       PyObject* _initData;
99     };
100
101     class YACSRUNTIMESALOME_EXPORT OutputPyPort : public OutputPort
102     {
103     public:
104       OutputPyPort(const std::string& name, Node * node, TypeCode * type);
105       OutputPyPort(const OutputPyPort& other, Node *newHelder);
106       ~OutputPyPort();
107       virtual void put(const void *data) throw(ConversionException);
108       void put(PyObject *data) throw(ConversionException);
109       OutputPort *clone(Node *newHelder) const;
110       virtual PyObject * get() const;
111       //special typedef PyObj used in SWIG to increment ref count on output
112       virtual PyObj * getPyObj() const;
113       virtual std::string getAsString();
114       virtual std::string dump();
115       virtual std::string typeName() {return "YACS__ENGINE__OutputPyPort";}
116       virtual std::string valToStr();
117       virtual void valFromStr(std::string valstr);
118     protected:
119       PyObject* _data;
120     };
121
122
123
124   }
125 }
126
127 #endif