Salome HOME
PR: add matrix type
authorprascle <prascle>
Tue, 18 Apr 2006 14:25:50 +0000 (14:25 +0000)
committerprascle <prascle>
Tue, 18 Apr 2006 14:25:50 +0000 (14:25 +0000)
idl/SALOME_Comm.idl
src/Communication/Makefile.am
src/Communication/MatrixClient.cxx [new file with mode: 0644]
src/Communication/MatrixClient.hxx [new file with mode: 0644]
src/Communication/SALOME_Matrix_i.cxx [new file with mode: 0644]
src/Communication/SALOME_Matrix_i.hxx [new file with mode: 0644]
src/Communication_SWIG/libSALOME_Comm.i

index b4e1f9de59a871cff6f09acf65abab37cef24b16..0ce2dc7c36cf20a7e59148c03627d416608d79d2 100644 (file)
@@ -118,6 +118,12 @@ module SALOME {
 
   interface SocketSenderInt : SenderInt,SocketSender {
   };
+
+  interface Matrix {
+    SenderDouble getData();
+    long getSizeOfColumn();
+    void release();
+  };
 };
 
 #endif
index fd20d963a040dbd7da7833a31e0e55d20a6683e5..5131e7b75630fe2a3430c506408dd6efe6af302f 100644 (file)
@@ -11,7 +11,9 @@ salomeinclude_HEADERS = \
        SenderFactory.hxx \
        SALOMEMultiComm.hxx \
        MultiCommException.hxx \
-       SALOME_Comm_i.hxx
+       SALOME_Comm_i.hxx \
+       MatrixClient.hxx \
+       SALOME_Matrix_i.hxx
 
 # Scripts to be installed
 
@@ -50,15 +52,19 @@ OPT_LDFLAGS  = -Xlinker -export-dynamic
 lib_LTLIBRARIES = libSalomeCommunication.la 
 libSalomeCommunication_la_SOURCES =\
        SALOME_Comm_i.cxx \
+       SALOME_Matrix_i.cxx \
        SenderFactory.cxx \
        MultiCommException.cxx \
        SALOMEMultiComm.cxx \
        ReceiverFactory.cxx \
+       MatrixClient.cxx \
        \
        MultiCommException.hxx \
        SALOME_Comm_i.hxx \
+       SALOME_Matrix_i.hxx \
        SenderFactory.hxx \
        ReceiverFactory.hxx \
+        MatrixClient.hxx \
        SALOMEMultiComm.hxx \
        Receivers.hxx \
        Receiver.hxx
diff --git a/src/Communication/MatrixClient.cxx b/src/Communication/MatrixClient.cxx
new file mode 100644 (file)
index 0000000..a7eeca2
--- /dev/null
@@ -0,0 +1,12 @@
+#include "MatrixClient.hxx"
+#include "ReceiverFactory.hxx"
+
+double *MatrixClient::getValue(SALOME::Matrix_ptr distMat, int& columnSize, int& rowSize)
+{
+  long totalSize;
+  double *ret=ReceiverFactory::getValue(distMat->getData(),totalSize);
+  columnSize=distMat->getSizeOfColumn();
+  distMat->release();
+  rowSize=totalSize/columnSize;
+  return ret;
+}
diff --git a/src/Communication/MatrixClient.hxx b/src/Communication/MatrixClient.hxx
new file mode 100644 (file)
index 0000000..489340e
--- /dev/null
@@ -0,0 +1,13 @@
+#ifndef __MATRIXCLIENT_HXX__
+#define __MATRIXCLIENT_HXX__
+
+#include <SALOMEconfig.h>
+#include CORBA_SERVER_HEADER(SALOME_Comm)
+
+class MatrixClient
+{
+public:
+  static double *getValue(SALOME::Matrix_ptr distMat, int& columnSize, int& rowSize);
+};
+
+#endif
diff --git a/src/Communication/SALOME_Matrix_i.cxx b/src/Communication/SALOME_Matrix_i.cxx
new file mode 100644 (file)
index 0000000..72521d6
--- /dev/null
@@ -0,0 +1,33 @@
+#include "SALOME_Matrix_i.hxx"
+#include "SenderFactory.hxx"
+
+SALOME_Matrix_i::SALOME_Matrix_i(const SALOMEMultiComm& multiCommunicator,const double *tabToSend,int nbOfRow,int nbOfColumn,bool ownTabToSend):_tabToSend(tabToSend),
+                                                                                                                                               _nbOfRow(nbOfRow),
+                                                                                                                                               _nbOfColumn(nbOfColumn),
+                                                                                                                                               _ownTabToSend(ownTabToSend),
+                                                                                                                                               _type(multiCommunicator)
+{
+}
+
+SALOME_Matrix_i::~SALOME_Matrix_i()
+{
+  if(_ownTabToSend)
+    delete [] _tabToSend;
+}
+
+SALOME::SenderDouble_ptr SALOME_Matrix_i::getData()
+{
+  return SenderFactory::buildSender(_type,_tabToSend,_nbOfRow*_nbOfColumn,_ownTabToSend);
+}
+
+CORBA::Long SALOME_Matrix_i::getSizeOfColumn()
+{
+  return _nbOfColumn;
+}
+
+void SALOME_Matrix_i::release()
+{
+  PortableServer::ObjectId_var oid = _default_POA()->servant_to_id(this);
+  _default_POA()->deactivate_object(oid);
+  _remove_ref();
+}
diff --git a/src/Communication/SALOME_Matrix_i.hxx b/src/Communication/SALOME_Matrix_i.hxx
new file mode 100644 (file)
index 0000000..24a5597
--- /dev/null
@@ -0,0 +1,26 @@
+#ifndef __SALOME_MATRIX_I_HXX__
+#define __SALOME_MATRIX_I_HXX__
+
+#include <string>
+#include <SALOMEconfig.h>
+#include CORBA_SERVER_HEADER(SALOME_Comm)
+#include "SALOMEMultiComm.hxx"
+
+class SALOME_Matrix_i : public virtual POA_SALOME::Matrix,
+                       public PortableServer::RefCountServantBase {
+private:
+  const double *_tabToSend;
+  int _nbOfRow;
+  int _nbOfColumn;
+  bool _ownTabToSend;
+  SALOMEMultiComm _type;
+protected:
+  ~SALOME_Matrix_i();
+public:
+  SALOME_Matrix_i(const SALOMEMultiComm& multiCommunicator,const double *tabToSend,int nbOfRow,int nbOfColumn,bool ownTabToSend=false);
+  SALOME::SenderDouble_ptr getData();
+  CORBA::Long getSizeOfColumn();
+  void release();
+};
+
+#endif
index 9a1b8dc0ca8af5b3cadc0a9d54d013d1690b6f0b..7fce88a2193c110d32f09e18d131f3f6ab45c434 100644 (file)
 
 %{
   #include "ReceiverFactory.hxx"
+  #include "MatrixClient.hxx"
   #undef SEEK_SET
   #undef SEEK_CUR
   #undef SEEK_END
   #include "SALOME_Comm_i.hxx"
+  #include "SALOMEMultiComm.hxx"
+  #include "SenderFactory.hxx"
 %}
 
 %typemap(python,in) SALOME::SenderDouble_ptr
   $1 = t;
 }
 
+%typemap(python,out) SALOME::SenderDouble_ptr
+{  
+   PyObject* pdict = PyDict_New();
+   PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
+   PyRun_String("import CORBA", Py_single_input, pdict, pdict);
+   PyRun_String("o = CORBA.ORB_init([''], CORBA.ORB_ID);", Py_single_input,
+                   pdict, pdict);
+   PyObject* orb = PyDict_GetItemString(pdict, "o");
+   // Get the orb Corba C++
+   int argc = 0;
+   char *xargv = "";
+   char **argv = &xargv;
+   CORBA::ORB_var ORB = CORBA::ORB_init(argc, argv);
+   std::string s =  ORB->object_to_string($1);
+   PyObject * tmp = PyString_FromString(s.c_str());
+   $result = PyObject_CallMethod(orb, "string_to_object", "O", tmp);
+}
+
+%typemap(python,out) SALOME::SenderInt_ptr
+{  
+   PyObject* pdict = PyDict_New();
+   PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
+   PyRun_String("import CORBA", Py_single_input, pdict, pdict);
+   PyRun_String("o = CORBA.ORB_init([''], CORBA.ORB_ID);", Py_single_input,
+                   pdict, pdict);
+   PyObject* orb = PyDict_GetItemString(pdict, "o");
+   // Get the orb Corba C++
+   int argc = 0;
+   char *xargv = "";
+   char **argv = &xargv;
+   CORBA::ORB_var ORB = CORBA::ORB_init(argc, argv);
+   std::string s =  ORB->object_to_string($1);
+   PyObject * tmp = PyString_FromString(s.c_str());
+   $result = PyObject_CallMethod(orb, "string_to_object", "O", tmp);
+}
+
 PyObject * getValueForSenderDouble(SALOME::SenderDouble_ptr senderDouble);
 
 %{
@@ -139,3 +178,79 @@ PyObject * getValueForSenderInt(SALOME::SenderInt_ptr senderInt)
   return result;
 }
 %}
+
+PyObject * getValueForMatrix(SALOME::Matrix_ptr matrix);
+%{
+PyObject * getValueForMatrix(SALOME::Matrix_ptr matrix)
+{
+  PyObject *py_list;
+  int column,row;
+  double *ret=MatrixClient::getValue(matrix,column,row);
+  py_list = PyList_New(row);
+  for(int i=0;i<row;i++)
+    {
+       PyObject *tmpRow=PyList_New(column);
+       for(int j=0;j<column;j++)
+         {
+           int err = PyList_SetItem(tmpRow, j, Py_BuildValue("d", (double) ret[i*column+j]));
+            if(err)
+              {
+                char * message = "PyList_SetItem matrix sent may be invalid";
+                PyErr_SetString(PyExc_RuntimeError, message);
+                return NULL;
+              }
+         }
+       PyList_SetItem(py_list,i,tmpRow);
+       Py_DECREF(tmpRow);
+    }
+  delete [] ret;
+  Py_DECREF(py_list);
+  return py_list;
+}
+%}
+
+SALOME::SenderDouble_ptr buildSenderDoubleFromList(PyObject *pylist);
+%{
+SALOME::SenderDouble_ptr buildSenderDoubleFromList(PyObject *pylist)
+{
+  if (PyList_Check(pylist)) 
+  {
+    int listLgth = PyList_Size(pylist);
+    double *tab=new double[listLgth];
+    for (int i=0;i<listLgth;i++)
+       {
+         tab[i]=PyFloat_AsDouble(PyList_GetItem(pylist,i));
+       }
+    SALOMEMultiComm communicator;
+    return SenderFactory::buildSender(communicator,tab,listLgth,true);
+  }
+  else
+  { 
+    PyErr_SetString(PyExc_TypeError,"not a list");
+    return SALOME::SenderDouble::_nil();
+  }
+}
+%}
+
+SALOME::SenderInt_ptr buildSenderIntFromList(PyObject *pylist);
+%{
+SALOME::SenderInt_ptr buildSenderIntFromList(PyObject *pylist)
+{
+  if (PyList_Check(pylist)) 
+  {
+    int listLgth = PyList_Size(pylist);
+    int *tab=new int[listLgth];
+    for (int i=0;i<listLgth;i++)
+       {
+         tab[i]=PyInt_AsLong(PyList_GetItem(pylist,i));
+       }
+    SALOMEMultiComm communicator;
+    return SenderFactory::buildSender(communicator,tab,listLgth,true);
+  }
+  else
+  { 
+    PyErr_SetString(PyExc_TypeError,"not a list");
+    return SALOME::SenderInt::_nil();
+  }
+}
+%}