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