Salome HOME
Copyrights update 2015.
[modules/yacs.git] / src / runtime / PythonCORBAConv.cxx
1 // Copyright (C) 2006-2015  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, or (at your option) any later version.
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 //#define REFCNT
21 //
22 #ifdef REFCNT
23 #define private public
24 #define protected public
25 #include <omniORB4/CORBA.h>
26 #include <omniORB4/internal/typecode.h>
27 #endif
28
29 #include "PythonCORBAConv.hxx"
30 #include "TypeConversions.hxx"
31 #include "RuntimeSALOME.hxx"
32 #include "PythonPorts.hxx"
33
34 #include <iostream>
35
36 //#define _DEVDEBUG_
37 #include "YacsTrace.hxx"
38
39 using namespace YACS::ENGINE;
40 using namespace std;
41
42 /*!Convert a PyObject (integer) to CORBA::Any (integer)
43  * It's only a wrapper around put(PyObject *data)
44  */
45 void PyCorbaInt::put(const void *data)  throw(ConversionException)
46 {
47   put((PyObject *)data);
48 }
49
50 //!Convert a PyObject (integer) to CORBA::Any (integer)
51 /*!
52  *   \param data : python object
53  */
54 void PyCorbaInt::put(PyObject *data)  throw(ConversionException)
55 {
56   CORBA::Any *a= convertPyObjectCorba(_port->edGetType(),data);
57   YACS::ENGINE::InterpreterSaveThread _loc;
58   _port->put(a);
59   delete a;
60 }
61
62 //!Convert a PyObject (boolean) to CORBA::Any (boolean)
63 /*!
64  * It's only a wrapper around PyCorbaBool::put(PyObject *data)
65  *
66  *   \param data : python object
67  */
68 void PyCorbaBool::put(const void *data)  throw(ConversionException)
69 {
70   put((PyObject *)data);
71 }
72
73 //!Convert a PyObject (boolean) to CORBA::Any (boolean)
74 /*!
75  * Convert it and push it to proxy port
76  *
77  *   \param data : python object
78  */
79 void PyCorbaBool::put(PyObject *data)  throw(ConversionException)
80 {
81   CORBA::Any *a= convertPyObjectCorba(_port->edGetType(),data);
82   YACS::ENGINE::InterpreterSaveThread _loc;
83   _port->put(a);
84   //delete Any that has been allocated by convertPyObjectCorba
85   delete a;
86 }
87
88 void PyCorbaString::put(const void *data)  throw(ConversionException)
89 {
90   put((PyObject *)data);
91 }
92
93 //!Convert a PyObject (string) to CORBA::Any (string)
94 /*!
95  *   \param data : python object
96  */
97
98 void PyCorbaString::put(PyObject *data)  throw(ConversionException)
99 {
100   CORBA::Any *a= convertPyObjectCorba(_port->edGetType(),data);
101   YACS::ENGINE::InterpreterSaveThread _loc;
102   _port->put(a);
103   delete a;
104 }
105
106
107 void PyCorbaDouble::put(const void *data)  throw(ConversionException)
108 {
109   put((PyObject *)data);
110 }
111
112 //!Convert a PyObject (double) to CORBA::Any (double)
113 /*!
114  *   \param data : python object
115  */
116
117 void PyCorbaDouble::put(PyObject *data)  throw(ConversionException)
118 {
119   CORBA::Any *a= convertPyObjectCorba(_port->edGetType(),data);
120   YACS::ENGINE::InterpreterSaveThread _loc;
121   _port->put(a);
122   delete a;
123 }
124
125 //!Class PyCorbaSequence is a proxy port that converts a PyObject object (of type sequence) to a CORBA::Any object (of type sequence)
126 /*!
127  *   \param p : the input CORBA port to adapt to Python output port
128  */
129 PyCorbaSequence::PyCorbaSequence(InputCorbaPort* p)
130   : ProxyPort(p), DataPort(p->getName(), p->getNode(), p->edGetType()), Port(p->getNode())
131 {
132 }
133
134 //!Convert a PyObject (sequence) to CORBA::Any (sequence)
135 /*!
136  *   \param data : python object
137  */
138
139 void PyCorbaSequence::put(const void *data)  throw(ConversionException)
140 {
141   put((PyObject *)data);
142 }
143
144 void PyCorbaSequence::put(PyObject *data)  throw(ConversionException)
145 {
146   DEBTRACE("data refcnt: " << data->ob_refcnt);
147 #ifdef _DEVDEBUG_
148   PyObject_Print(data,stderr,Py_PRINT_RAW);
149   std::cerr << std::endl;
150 #endif
151   CORBA::Any *a= convertPyObjectCorba(_port->edGetType(),data);
152   YACS::ENGINE::InterpreterSaveThread _loc;
153   _port->put(a);
154 #ifdef REFCNT
155   DEBTRACE("refcount CORBA seqTC: " << ((omni::TypeCode_base*)a->pd_tc.in())->pd_ref_count);
156 #endif
157   //delete Any that has been allocated by convertPyObjectCorba
158   delete a;
159 #ifdef REFCNT
160   DEBTRACE("refcount CORBA seqTC: " << ((omni::TypeCode_base*)((InputCorbaPort*)_port)->getAny()->pd_tc.in())->pd_ref_count);
161 #endif
162 }
163
164 //!Class PyCorbaObjref is a proxy port that converts a PyObject object (of type objref) to a CORBA::Any object (of type objref)
165 /*!
166  *   \param p : the input CORBA port to adapt to Python output port
167  */
168 PyCorbaObjref::PyCorbaObjref(InputCorbaPort* p)
169   : ProxyPort(p), DataPort(p->getName(), p->getNode(), p->edGetType()), Port(p->getNode())
170 {
171   _pyorb = getSALOMERuntime()->getPyOrb();
172   _orb = getSALOMERuntime()->getOrb();
173   //  _dynFactory = getSALOMERuntime()->getDynFactory();
174 }
175
176 //!Convert a PyObject (Objref) to CORBA::Any (Objref)
177 /*!
178  *   \param data : python object
179  */
180
181 void PyCorbaObjref::put(const void *data)  throw(ConversionException)
182 {
183   put((PyObject *)data);
184 }
185
186 void PyCorbaObjref::put(PyObject *data)  throw(ConversionException)
187 {
188   DEBTRACE("data refcnt: " << data->ob_refcnt);
189 #ifdef _DEVDEBUG_
190   PyObject_Print(data,stderr,Py_PRINT_RAW);
191   std::cerr << std::endl;
192 #endif
193   CORBA::Any *a= convertPyObjectCorba(_port->edGetType(),data);
194   YACS::ENGINE::InterpreterSaveThread _loc;
195   _port->put(a);
196   delete a;
197 }
198
199 //!Class PyCorbaStruct is a proxy port that converts a PyObject object (of type struct) to a CORBA::Any object (of type struct)
200 /*!
201  *   \param p : the input CORBA port to adapt to Python output port
202  */
203 PyCorbaStruct::PyCorbaStruct(InputCorbaPort* p)
204   : ProxyPort(p), DataPort(p->getName(), p->getNode(), p->edGetType()), Port(p->getNode())
205 {
206 }
207
208 void PyCorbaStruct::put(const void *data)  throw(ConversionException)
209 {
210   put((PyObject *)data);
211 }
212
213 //!Convert a PyObject (struct) to CORBA::Any (struct)
214 /*!
215  *   \param data : python object
216  */
217 void PyCorbaStruct::put(PyObject *data)  throw(ConversionException)
218 {
219   DEBTRACE("data refcnt: " << data->ob_refcnt);
220 #ifdef _DEVDEBUG_
221   PyObject_Print(data,stderr,Py_PRINT_RAW);
222   std::cerr << std::endl;
223 #endif
224   CORBA::Any *a= convertPyObjectCorba(_port->edGetType(),data);
225   YACS::ENGINE::InterpreterSaveThread _loc;
226   _port->put(a);
227 #ifdef REFCNT
228   DEBTRACE("refcount CORBA structTC: " << ((omni::TypeCode_base*)a->pd_tc.in())->pd_ref_count);
229 #endif
230   //delete Any that has been allocated by convertPyObjectCorba
231   delete a;
232 #ifdef REFCNT
233   DEBTRACE("refcount CORBA structTC: " << ((omni::TypeCode_base*)((InputCorbaPort*)_port)->getAny()->pd_tc.in())->pd_ref_count);
234 #endif
235 }
236