Salome HOME
copy tag mergefrom_BR_V0_1_CC_Salome_04oct07
[modules/yacs.git] / src / runtime / PythonCORBAConv.cxx
1
2 //#define REFCNT
3 #ifdef REFCNT
4 #define private public
5 #define protected public
6 #include <omniORB4/CORBA.h>
7 #include <omniORB4/internal/typecode.h>
8 #endif
9
10 #include "PythonCORBAConv.hxx"
11 #include "TypeConversions.hxx"
12 #include "RuntimeSALOME.hxx"
13
14 #include <iostream>
15
16 //#define _DEVDEBUG_
17 #include "YacsTrace.hxx"
18
19 using namespace YACS::ENGINE;
20 using namespace std;
21
22 /*!Convert a PyObject (integer) to CORBA::Any (integer)
23  * It's only a wrapper around put(PyObject *data)
24  */
25 void PyCorbaInt::put(const void *data)  throw(ConversionException)
26 {
27   put((PyObject *)data);
28 }
29
30 //!Convert a PyObject (integer) to CORBA::Any (integer)
31 /*!
32  *   \param data : python object
33  */
34 void PyCorbaInt::put(PyObject *data)  throw(ConversionException)
35 {
36   CORBA::Long l= 0;
37   if (PyInt_Check(data))l=PyInt_AS_LONG(data);
38   else if(PyLong_Check(data))l=PyLong_AsLong(data);
39   else throw ConversionException("Not an int");
40
41   CORBA::Any a;
42   a <<= l;
43   _port->put(&a);
44 }
45
46 //!Convert a PyObject (boolean) to CORBA::Any (boolean)
47 /*!
48  * It's only a wrapper around PyCorbaBool::put(PyObject *data)
49  *
50  *   \param data : python object
51  */
52 void PyCorbaBool::put(const void *data)  throw(ConversionException)
53 {
54   put((PyObject *)data);
55 }
56
57 //!Convert a PyObject (boolean) to CORBA::Any (boolean)
58 /*!
59  * Convert it and push it to proxy port
60  *
61  *   \param data : python object
62  */
63 void PyCorbaBool::put(PyObject *data)  throw(ConversionException)
64 {
65   CORBA::Any *a= convertPyObjectCorba(_port->edGetType(),data);
66   _port->put(a);
67   //delete Any that has been allocated by convertPyObjectCorba
68   delete a;
69 }
70
71 void PyCorbaString::put(const void *data)  throw(ConversionException)
72 {
73   put((PyObject *)data);
74 }
75
76 //!Convert a PyObject (string) to CORBA::Any (string)
77 /*!
78  *   \param data : python object
79  */
80
81 void PyCorbaString::put(PyObject *data)  throw(ConversionException)
82 {
83   char * s;
84   if(PyString_Check(data))s=PyString_AsString(data);
85   else throw ConversionException("Not a string");
86
87   CORBA::Any a;
88   a <<= s;
89   _port->put(&a);
90 }
91
92
93 void PyCorbaDouble::put(const void *data)  throw(ConversionException)
94 {
95   put((PyObject *)data);
96 }
97
98 //!Convert a PyObject (double) to CORBA::Any (double)
99 /*!
100  *   \param data : python object
101  */
102
103 void PyCorbaDouble::put(PyObject *data)  throw(ConversionException)
104 {
105   CORBA::Double d = 0;
106   if (PyFloat_Check(data)) d = (CORBA::Double)PyFloat_AS_DOUBLE(data);
107   else if (PyInt_Check(data)) d = (CORBA::Double)PyInt_AS_LONG(data);
108   else if (PyLong_Check(data)) d = (CORBA::Double)PyLong_AsDouble(data);
109   else throw ConversionException("Not a double");
110
111   CORBA::Any a;
112   a <<= d;
113   _port->put(&a);
114 }
115
116 //!Class PyCorbaSequence is a proxy port that converts a PyObject object (of type sequence) to a CORBA::Any object (of type sequence)
117 /*!
118  *   \param p : the input CORBA port to adapt to Python output port
119  */
120 PyCorbaSequence::PyCorbaSequence(InputCorbaPort* p)
121   : ProxyPort(p), DataPort(p->getName(), p->getNode(), p->edGetType()), Port(p->getNode())
122 {
123 }
124
125 //!Convert a PyObject (sequence) to CORBA::Any (sequence)
126 /*!
127  *   \param data : python object
128  */
129
130 void PyCorbaSequence::put(const void *data)  throw(ConversionException)
131 {
132   put((PyObject *)data);
133 }
134
135 void PyCorbaSequence::put(PyObject *data)  throw(ConversionException)
136 {
137   DEBTRACE("data refcnt: " << data->ob_refcnt);
138 #ifdef _DEVDEBUG_
139   PyObject_Print(data,stderr,Py_PRINT_RAW);
140   std::cerr << std::endl;
141 #endif
142   CORBA::Any *a= convertPyObjectCorba(_port->edGetType(),data);
143   _port->put(a);
144 #ifdef REFCNT
145   DEBTRACE("refcount CORBA seqTC: " << ((omni::TypeCode_base*)a->pd_tc.in())->pd_ref_count);
146 #endif
147   //delete Any that has been allocated by convertPyObjectCorba
148   delete a;
149 #ifdef REFCNT
150   DEBTRACE("refcount CORBA seqTC: " << ((omni::TypeCode_base*)((InputCorbaPort*)_port)->getAny()->pd_tc.in())->pd_ref_count);
151 #endif
152 }
153
154 //!Class PyCorbaObjref is a proxy port that converts a PyObject object (of type objref) to a CORBA::Any object (of type objref)
155 /*!
156  *   \param p : the input CORBA port to adapt to Python output port
157  */
158 PyCorbaObjref::PyCorbaObjref(InputCorbaPort* p)
159   : ProxyPort(p), DataPort(p->getName(), p->getNode(), p->edGetType()), Port(p->getNode())
160 {
161   _pyorb = getSALOMERuntime()->getPyOrb();
162   _orb = getSALOMERuntime()->getOrb();
163   //  _dynFactory = getSALOMERuntime()->getDynFactory();
164 }
165
166 //!Convert a PyObject (Objref) to CORBA::Any (Objref)
167 /*!
168  *   \param data : python object
169  */
170
171 void PyCorbaObjref::put(const void *data)  throw(ConversionException)
172 {
173   put((PyObject *)data);
174 }
175
176 void PyCorbaObjref::put(PyObject *data)  throw(ConversionException)
177 {
178   DEBTRACE("data refcnt: " << data->ob_refcnt);
179 #ifdef _DEVDEBUG_
180   PyObject_Print(data,stderr,Py_PRINT_RAW);
181   std::cerr << std::endl;
182 #endif
183
184   //does not work : replace by a call to object_to_string - string_to_object
185   //hold_lock is true: caller is supposed to hold the GIL. 
186   //omniorb will not take the GIL
187   //CORBA::Object_ptr ob=api->pyObjRefToCxxObjRef(data,(CORBA::Boolean)1);
188
189   PyObject *pystring = PyObject_CallMethod(_pyorb, "object_to_string", "O", data);
190   if(pystring == NULL)
191     {
192       PyErr_Print();
193       throw ConversionException("can't get objref");
194     }
195   CORBA::Object_var ob= _orb->string_to_object(PyString_AsString(pystring));
196   Py_DECREF(pystring);
197
198   CORBA::Any a;
199   a <<= ob;
200   _port->put(&a);
201 }
202
203 //!Class PyCorbaStruct is a proxy port that converts a PyObject object (of type struct) to a CORBA::Any object (of type struct)
204 /*!
205  *   \param p : the input CORBA port to adapt to Python output port
206  */
207 PyCorbaStruct::PyCorbaStruct(InputCorbaPort* p)
208   : ProxyPort(p), DataPort(p->getName(), p->getNode(), p->edGetType()), Port(p->getNode())
209 {
210 }
211
212 void PyCorbaStruct::put(const void *data)  throw(ConversionException)
213 {
214   put((PyObject *)data);
215 }
216
217 //!Convert a PyObject (struct) to CORBA::Any (struct)
218 /*!
219  *   \param data : python object
220  */
221 void PyCorbaStruct::put(PyObject *data)  throw(ConversionException)
222 {
223   DEBTRACE("data refcnt: " << data->ob_refcnt);
224 #ifdef _DEVDEBUG_
225   PyObject_Print(data,stderr,Py_PRINT_RAW);
226   std::cerr << std::endl;
227 #endif
228   CORBA::Any *a= convertPyObjectCorba(_port->edGetType(),data);
229   _port->put(a);
230 #ifdef REFCNT
231   DEBTRACE("refcount CORBA structTC: " << ((omni::TypeCode_base*)a->pd_tc.in())->pd_ref_count);
232 #endif
233   //delete Any that has been allocated by convertPyObjectCorba
234   delete a;
235 #ifdef REFCNT
236   DEBTRACE("refcount CORBA structTC: " << ((omni::TypeCode_base*)((InputCorbaPort*)_port)->getAny()->pd_tc.in())->pd_ref_count);
237 #endif
238 }
239