Salome HOME
Avoid using import CORBA
[modules/kernel.git] / src / Communication_SWIG / libSALOME_Comm.i
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 %module libSALOME_Comm
24
25 %feature("autodoc", "1");
26
27 %{
28   #include "ReceiverFactory.hxx"
29   #include "MatrixClient.hxx"
30   #include "SALOME_Comm_i.hxx"
31   #include "SALOMEMultiComm.hxx"
32   #include "SenderFactory.hxx"
33 %}
34
35 %typemap(in) SALOME::SenderDouble_ptr
36 {
37   PyObject* pdict = PyDict_New();
38   PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
39   PyRun_String("from omniORB import CORBA", Py_single_input, pdict, pdict);
40  
41   PyRun_String("o = CORBA.ORB_init([''], CORBA.ORB_ID);", Py_single_input,
42                    pdict, pdict);
43  
44   PyObject* orb = PyDict_GetItemString(pdict, "o");
45
46   // Ask omniORBpy to transform SUPPORT (python Corba) ptr to IOR string
47
48   PyObject* iorSupport = PyObject_CallMethod(orb, (char*)"object_to_string", (char*)"O", $input);
49  
50   if (iorSupport == Py_None)
51     return NULL;
52   char * s = PyString_AsString(PyObject_Str(iorSupport));
53  
54   // Ask omniORB to convert IOR string to SALOME::SenderDouble_ptr
55
56   int argc = 0;
57   char *xargv = (char*)"";
58   char **argv = &xargv;
59   CORBA::ORB_var ORB = CORBA::ORB_init(argc, argv);
60   CORBA::Object_var O =  ORB->string_to_object(s);
61   SALOME::SenderDouble_ptr t = SALOME::SenderDouble::_narrow(O);
62   $1 = t;
63 }
64
65 %typemap(in) SALOME::SenderInt_ptr
66 {
67   PyObject* pdict = PyDict_New();
68   PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
69   PyRun_String("from omniORB import CORBA", Py_single_input, pdict, pdict);
70  
71   PyRun_String("o = CORBA.ORB_init([''], CORBA.ORB_ID);", Py_single_input,
72                    pdict, pdict);
73  
74   PyObject* orb = PyDict_GetItemString(pdict, "o");
75
76   // Ask omniORBpy to transform SUPPORT (python Corba) ptr to IOR string
77
78   PyObject* iorSupport = PyObject_CallMethod(orb, (char*)"object_to_string", (char*)"O", $input);
79  
80   if (iorSupport == Py_None)
81     return NULL;
82   char * s = PyString_AsString(PyObject_Str(iorSupport));
83  
84   // Ask omniORB to convert IOR string to SALOME::SenderInt_ptr
85
86   int argc = 0;
87   char *xargv = (char*)"";
88   char **argv = &xargv;
89   CORBA::ORB_var ORB = CORBA::ORB_init(argc, argv);
90   CORBA::Object_var O =  ORB->string_to_object(s);
91   SALOME::SenderInt_ptr t = SALOME::SenderInt::_narrow(O);
92   $1 = t;
93 }
94
95 %typemap(out) SALOME::SenderDouble_ptr
96 {  
97    PyObject* pdict = PyDict_New();
98    PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
99    PyRun_String("from omniORB import CORBA", Py_single_input, pdict, pdict);
100    PyRun_String("o = CORBA.ORB_init([''], CORBA.ORB_ID);", Py_single_input,
101                    pdict, pdict);
102    PyObject* orb = PyDict_GetItemString(pdict, "o");
103    // Get the orb Corba C++
104    int argc = 0;
105    char *xargv = (char*)"";
106    char **argv = &xargv;
107    CORBA::ORB_var ORB = CORBA::ORB_init(argc, argv);
108    std::string s =  ORB->object_to_string($1);
109    PyObject * tmp = PyString_FromString(s.c_str());
110    $result = PyObject_CallMethod(orb, (char*)"string_to_object", (char*)"O", tmp);
111 }
112
113 %typemap(out) SALOME::SenderInt_ptr
114 {  
115    PyObject* pdict = PyDict_New();
116    PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
117    PyRun_String("from omniORB import CORBA", Py_single_input, pdict, pdict);
118    PyRun_String("o = CORBA.ORB_init([''], CORBA.ORB_ID);", Py_single_input,
119                    pdict, pdict);
120    PyObject* orb = PyDict_GetItemString(pdict, "o");
121    // Get the orb Corba C++
122    int argc = 0;
123    char *xargv = (char*)"";
124    char **argv = &xargv;
125    CORBA::ORB_var ORB = CORBA::ORB_init(argc, argv);
126    std::string s =  ORB->object_to_string($1);
127    PyObject * tmp = PyString_FromString(s.c_str());
128    $result = PyObject_CallMethod(orb, (char*)"string_to_object", (char*)"O", tmp);
129 }
130
131 PyObject * getValueForSenderDouble(SALOME::SenderDouble_ptr senderDouble);
132
133 %{
134 PyObject * getValueForSenderDouble(SALOME::SenderDouble_ptr senderDouble)
135 {
136   PyObject *py_list;
137   long size;
138   double *ret=ReceiverFactory::getValue(senderDouble,size);
139   py_list = PyList_New(size);
140   for (int i=0; i < size; i++)
141     {
142             int err = PyList_SetItem(py_list, i, Py_BuildValue("d", (double) ret[i]));
143             if(err)
144               {
145                 const char * message = "Error in SUPPORT::getTypes";
146                 PyErr_SetString(PyExc_RuntimeError, message);
147                 return NULL;
148               }
149     }
150   PyObject * result = Py_BuildValue("O", py_list);
151   delete [] ret;
152   Py_DECREF(py_list);
153   return result;
154 }
155 %}
156
157
158 PyObject * getValueForSenderInt(SALOME::SenderInt_ptr senderInt);
159
160 %{
161 PyObject * getValueForSenderInt(SALOME::SenderInt_ptr senderInt)
162 {
163   PyObject *py_list;
164   long size;
165   int *ret=ReceiverFactory::getValue(senderInt,size);
166   py_list = PyList_New(size);
167   for (int i=0; i < size; i++)
168     {
169             int err = PyList_SetItem(py_list, i, Py_BuildValue("i", (int) ret[i]));
170             if(err)
171               {
172                 const char * message = "Error in SUPPORT::getTypes";
173                 PyErr_SetString(PyExc_RuntimeError, message);
174                 return NULL;
175               }
176     }
177   PyObject * result = Py_BuildValue("O", py_list);
178   delete [] ret;
179   Py_DECREF(py_list);
180   return result;
181 }
182 %}
183
184 PyObject * getValueForMatrix(SALOME::Matrix_ptr matrix);
185 %{
186 PyObject * getValueForMatrix(SALOME::Matrix_ptr matrix)
187 {
188   PyObject *py_list;
189   int column,row;
190   double *ret=MatrixClient::getValue(matrix,column,row);
191   py_list = PyList_New(row);
192   for(int i=0;i<row;i++)
193     {
194        PyObject *tmpRow=PyList_New(column);
195        for(int j=0;j<column;j++)
196          {
197            int err = PyList_SetItem(tmpRow, j, Py_BuildValue("d", (double) ret[i*column+j]));
198             if(err)
199               {
200                 const char * message = "PyList_SetItem matrix sent may be invalid";
201                 PyErr_SetString(PyExc_RuntimeError, message);
202                 return NULL;
203               }
204          }
205        PyList_SetItem(py_list,i,tmpRow);
206        Py_DECREF(tmpRow);
207     }
208   delete [] ret;
209   Py_DECREF(py_list);
210   return py_list;
211 }
212 %}
213
214 SALOME::SenderDouble_ptr buildSenderDoubleFromList(PyObject *pylist);
215 %{
216 SALOME::SenderDouble_ptr buildSenderDoubleFromList(PyObject *pylist)
217 {
218   if (PyList_Check(pylist)) 
219   {
220     int listLgth = PyList_Size(pylist);
221     double *tab=new double[listLgth];
222     for (int i=0;i<listLgth;i++)
223         {
224           tab[i]=PyFloat_AsDouble(PyList_GetItem(pylist,i));
225         }
226     SALOMEMultiComm communicator;
227     return SenderFactory::buildSender(communicator,tab,listLgth,true);
228   }
229   else
230   { 
231     PyErr_SetString(PyExc_TypeError,"not a list");
232     return SALOME::SenderDouble::_nil();
233   }
234 }
235 %}
236
237 SALOME::SenderInt_ptr buildSenderIntFromList(PyObject *pylist);
238 %{
239 SALOME::SenderInt_ptr buildSenderIntFromList(PyObject *pylist)
240 {
241   if (PyList_Check(pylist)) 
242   {
243     int listLgth = PyList_Size(pylist);
244     int *tab=new int[listLgth];
245     for (int i=0;i<listLgth;i++)
246         {
247           tab[i]=PyInt_AsLong(PyList_GetItem(pylist,i));
248         }
249     SALOMEMultiComm communicator;
250     return SenderFactory::buildSender(communicator,tab,listLgth,true);
251   }
252   else
253   { 
254     PyErr_SetString(PyExc_TypeError,"not a list");
255     return SALOME::SenderInt::_nil();
256   }
257 }
258 %}