Salome HOME
refs #492: implement dump to Python
[modules/hydro.git] / src / HYDROPy / CAS / NCollection_Sequence.sip
index dc4f30d9d9c9e313d7968dd8ab3f0125d4ba2239..91c751cee92944693f405ddb719d85bd7186b8e0 100644 (file)
@@ -112,6 +112,67 @@ template<TYPE>
 %End
 };
 
+// NCollection_Sequence<double> is implemented as a Python list of floats.
+%MappedType NCollection_Sequence<double>
+{
+%TypeHeaderCode
+#include <NCollection_Sequence.hxx>
+%End
+
+%ConvertFromTypeCode
+    // Create the list.
+    PyObject *l;
+
+    if ( ( l = PyList_New( sipCpp->Length() ) ) == NULL )
+      return NULL;
+
+    // Set the list elements.
+    for ( int i = 1, n = sipCpp->Length(); i <= n; ++i )
+    {
+      PyObject *pobj;
+      if ( ( pobj = PyFloat_FromDouble( sipCpp->Value( i ) ) ) == NULL )
+      {
+        Py_DECREF(l);
+
+        return NULL;
+      }
+
+      PyList_SET_ITEM( l, i - 1, pobj );
+    }
+
+    return l;
+%End
+
+%ConvertToTypeCode
+    // Check the type if that is all that is required.
+    if ( sipIsErr == NULL )
+        return ( PySequence_Check( sipPy)  && PySequence_Size( sipPy ) >= 0 );
+
+    NCollection_Sequence<double> *aSeq = new NCollection_Sequence<double>;
+
+    SIP_SSIZE_T len = PySequence_Size(sipPy);
+    for ( SIP_SSIZE_T i = 0; i < len; ++i )
+    {
+      PyObject *itm = PySequence_ITEM( sipPy, i );
+      if ( !itm )
+      {
+        delete aSeq;
+        *sipIsErr = 1;
+
+        return 0;
+      }
+
+      aSeq->Append( PyFloat_AsDouble( itm ) );
+
+      Py_DECREF( itm );
+    }
+    *sipCppPtr = aSeq;
+    return sipGetState( sipTransferObj );
+%End
+};
+
 // NCollection_Sequence<int> is implemented as a Python list of integers.
 %MappedType NCollection_Sequence<int>
 {
@@ -137,7 +198,7 @@ template<TYPE>
         return NULL;
       }
 
-      PyList_SET_ITEM( l, i, pobj );
+      PyList_SET_ITEM( l, i - 1, pobj );
     }
 
     return l;
@@ -198,7 +259,7 @@ template<TYPE>
         return NULL;
       }
 
-      PyList_SET_ITEM( l, i, pobj );
+      PyList_SET_ITEM( l, i - 1, pobj );
     }
 
     return l;