Salome HOME
CCAR: replace the old way to take the python global interpreter lock
[modules/kernel.git] / src / Communication_SWIG / libSALOME_Comm.i
1 //  Copyright (C) 2007-2008  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.
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 %module libSALOME_Comm
23
24 %feature("autodoc", "1");
25
26 %{
27   #include "ReceiverFactory.hxx"
28   #include "MatrixClient.hxx"
29   #undef SEEK_SET
30   #undef SEEK_CUR
31   #undef SEEK_END
32   #include "SALOME_Comm_i.hxx"
33   #include "SALOMEMultiComm.hxx"
34   #include "SenderFactory.hxx"
35 %}
36
37 %typemap(python,in) SALOME::SenderDouble_ptr
38 {
39   PyObject* pdict = PyDict_New();
40   PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
41   PyRun_String("import CORBA", Py_single_input, pdict, pdict);
42  
43   PyRun_String("o = CORBA.ORB_init([''], CORBA.ORB_ID);", Py_single_input,
44                    pdict, pdict);
45  
46   PyObject* orb = PyDict_GetItemString(pdict, "o");
47
48   // Ask omniORBpy to transform SUPPORT (python Corba) ptr to IOR string
49
50   PyObject* iorSupport = PyObject_CallMethod(orb, (char*)"object_to_string", (char*)"O", $input);
51  
52   if (iorSupport == Py_None)
53     return NULL;
54   char * s = PyString_AsString(PyObject_Str(iorSupport));
55  
56   // Ask omniORB to convert IOR string to SALOME::SenderDouble_ptr
57
58   int argc = 0;
59   char *xargv = (char*)"";
60   char **argv = &xargv;
61   CORBA::ORB_var ORB = CORBA::ORB_init(argc, argv);
62   CORBA::Object_var O =  ORB->string_to_object(s);
63   SALOME::SenderDouble_ptr t = SALOME::SenderDouble::_narrow(O);
64   $1 = t;
65 }
66
67 %typemap(python,in) SALOME::SenderInt_ptr
68 {
69   PyObject* pdict = PyDict_New();
70   PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
71   PyRun_String("import CORBA", Py_single_input, pdict, pdict);
72  
73   PyRun_String("o = CORBA.ORB_init([''], CORBA.ORB_ID);", Py_single_input,
74                    pdict, pdict);
75  
76   PyObject* orb = PyDict_GetItemString(pdict, "o");
77
78   // Ask omniORBpy to transform SUPPORT (python Corba) ptr to IOR string
79
80   PyObject* iorSupport = PyObject_CallMethod(orb, (char*)"object_to_string", (char*)"O", $input);
81  
82   if (iorSupport == Py_None)
83     return NULL;
84   char * s = PyString_AsString(PyObject_Str(iorSupport));
85  
86   // Ask omniORB to convert IOR string to SALOME::SenderInt_ptr
87
88   int argc = 0;
89   char *xargv = (char*)"";
90   char **argv = &xargv;
91   CORBA::ORB_var ORB = CORBA::ORB_init(argc, argv);
92   CORBA::Object_var O =  ORB->string_to_object(s);
93   SALOME::SenderInt_ptr t = SALOME::SenderInt::_narrow(O);
94   $1 = t;
95 }
96
97 %typemap(python,out) SALOME::SenderDouble_ptr
98 {  
99    PyObject* pdict = PyDict_New();
100    PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
101    PyRun_String("import CORBA", Py_single_input, pdict, pdict);
102    PyRun_String("o = CORBA.ORB_init([''], CORBA.ORB_ID);", Py_single_input,
103                    pdict, pdict);
104    PyObject* orb = PyDict_GetItemString(pdict, "o");
105    // Get the orb Corba C++
106    int argc = 0;
107    char *xargv = (char*)"";
108    char **argv = &xargv;
109    CORBA::ORB_var ORB = CORBA::ORB_init(argc, argv);
110    std::string s =  ORB->object_to_string($1);
111    PyObject * tmp = PyString_FromString(s.c_str());
112    $result = PyObject_CallMethod(orb, (char*)"string_to_object", (char*)"O", tmp);
113 }
114
115 %typemap(python,out) SALOME::SenderInt_ptr
116 {  
117    PyObject* pdict = PyDict_New();
118    PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
119    PyRun_String("import CORBA", Py_single_input, pdict, pdict);
120    PyRun_String("o = CORBA.ORB_init([''], CORBA.ORB_ID);", Py_single_input,
121                    pdict, pdict);
122    PyObject* orb = PyDict_GetItemString(pdict, "o");
123    // Get the orb Corba C++
124    int argc = 0;
125    char *xargv = (char*)"";
126    char **argv = &xargv;
127    CORBA::ORB_var ORB = CORBA::ORB_init(argc, argv);
128    std::string s =  ORB->object_to_string($1);
129    PyObject * tmp = PyString_FromString(s.c_str());
130    $result = PyObject_CallMethod(orb, (char*)"string_to_object", (char*)"O", tmp);
131 }
132
133 PyObject * getValueForSenderDouble(SALOME::SenderDouble_ptr senderDouble);
134
135 %{
136 PyObject * getValueForSenderDouble(SALOME::SenderDouble_ptr senderDouble)
137 {
138   PyObject *py_list;
139   long size;
140   double *ret=ReceiverFactory::getValue(senderDouble,size);
141   py_list = PyList_New(size);
142   for (int i=0; i < size; i++)
143     {
144             int err = PyList_SetItem(py_list, i, Py_BuildValue("d", (double) ret[i]));
145             if(err)
146               {
147                 const char * message = "Error in SUPPORT::getTypes";
148                 PyErr_SetString(PyExc_RuntimeError, message);
149                 return NULL;
150               }
151     }
152   PyObject * result = Py_BuildValue("O", py_list);
153   delete [] ret;
154   Py_DECREF(py_list);
155   return result;
156 }
157 %}
158
159
160 PyObject * getValueForSenderInt(SALOME::SenderInt_ptr senderInt);
161
162 %{
163 PyObject * getValueForSenderInt(SALOME::SenderInt_ptr senderInt)
164 {
165   PyObject *py_list;
166   long size;
167   int *ret=ReceiverFactory::getValue(senderInt,size);
168   py_list = PyList_New(size);
169   for (int i=0; i < size; i++)
170     {
171             int err = PyList_SetItem(py_list, i, Py_BuildValue("i", (int) ret[i]));
172             if(err)
173               {
174                 const char * message = "Error in SUPPORT::getTypes";
175                 PyErr_SetString(PyExc_RuntimeError, message);
176                 return NULL;
177               }
178     }
179   PyObject * result = Py_BuildValue("O", py_list);
180   delete [] ret;
181   Py_DECREF(py_list);
182   return result;
183 }
184 %}
185
186 PyObject * getValueForMatrix(SALOME::Matrix_ptr matrix);
187 %{
188 PyObject * getValueForMatrix(SALOME::Matrix_ptr matrix)
189 {
190   PyObject *py_list;
191   int column,row;
192   double *ret=MatrixClient::getValue(matrix,column,row);
193   py_list = PyList_New(row);
194   for(int i=0;i<row;i++)
195     {
196        PyObject *tmpRow=PyList_New(column);
197        for(int j=0;j<column;j++)
198          {
199            int err = PyList_SetItem(tmpRow, j, Py_BuildValue("d", (double) ret[i*column+j]));
200             if(err)
201               {
202                 const char * message = "PyList_SetItem matrix sent may be invalid";
203                 PyErr_SetString(PyExc_RuntimeError, message);
204                 return NULL;
205               }
206          }
207        PyList_SetItem(py_list,i,tmpRow);
208        Py_DECREF(tmpRow);
209     }
210   delete [] ret;
211   Py_DECREF(py_list);
212   return py_list;
213 }
214 %}
215
216 SALOME::SenderDouble_ptr buildSenderDoubleFromList(PyObject *pylist);
217 %{
218 SALOME::SenderDouble_ptr buildSenderDoubleFromList(PyObject *pylist)
219 {
220   if (PyList_Check(pylist)) 
221   {
222     int listLgth = PyList_Size(pylist);
223     double *tab=new double[listLgth];
224     for (int i=0;i<listLgth;i++)
225         {
226           tab[i]=PyFloat_AsDouble(PyList_GetItem(pylist,i));
227         }
228     SALOMEMultiComm communicator;
229     return SenderFactory::buildSender(communicator,tab,listLgth,true);
230   }
231   else
232   { 
233     PyErr_SetString(PyExc_TypeError,"not a list");
234     return SALOME::SenderDouble::_nil();
235   }
236 }
237 %}
238
239 SALOME::SenderInt_ptr buildSenderIntFromList(PyObject *pylist);
240 %{
241 SALOME::SenderInt_ptr buildSenderIntFromList(PyObject *pylist)
242 {
243   if (PyList_Check(pylist)) 
244   {
245     int listLgth = PyList_Size(pylist);
246     int *tab=new int[listLgth];
247     for (int i=0;i<listLgth;i++)
248         {
249           tab[i]=PyInt_AsLong(PyList_GetItem(pylist,i));
250         }
251     SALOMEMultiComm communicator;
252     return SenderFactory::buildSender(communicator,tab,listLgth,true);
253   }
254   else
255   { 
256     PyErr_SetString(PyExc_TypeError,"not a list");
257     return SALOME::SenderInt::_nil();
258   }
259 }
260 %}