]> SALOME platform Git repositories - tools/py2cpp.git/commitdiff
Salome HOME
Conversion from py tuple to std list and vector. v2.0
authorOvidiu Mircescu <ovidiu.mircescu@edf.fr>
Wed, 9 Oct 2019 14:03:39 +0000 (16:03 +0200)
committerOvidiu Mircescu <ovidiu.mircescu@edf.fr>
Wed, 9 Oct 2019 14:03:39 +0000 (16:03 +0200)
src/Test/ConversionTest.cxx
src/TypeConversions.hxx

index c54e54995e02635feb9423d4fffbfb027653e685..edfa6ee7bbafe05c1c5529b2d27266d9b09c01fd 100644 (file)
@@ -115,6 +115,11 @@ void ConversionTest::vectorTest()
   py2cpp::pyResult(result) = fn(v);
 
   CPPUNIT_ASSERT(std::vector<double>({2.1, 3.2, 4.3, 5.4}) == result);
+
+  auto tup = std::make_tuple(4, 4.2);
+  py2cpp::PyPtr pyTup = py2cpp::toPyPtr(tup);
+  py2cpp::fromPyPtr(pyTup, result);
+  CPPUNIT_ASSERT(std::vector<double>({4, 4.2}) == result);
 }
 
 void ConversionTest::listTest()
@@ -129,6 +134,11 @@ void ConversionTest::listTest()
   py2cpp::pyResult(result) = fn(v);
 
   CPPUNIT_ASSERT(std::list<double>({2.1, 3.2, 4.3, 5.4}) == result);
+
+  auto tup = std::make_tuple(4, 4.2);
+  py2cpp::PyPtr pyTup = py2cpp::toPyPtr(tup);
+  py2cpp::fromPyPtr(pyTup, result);
+  CPPUNIT_ASSERT(std::list<double>({4, 4.2}) == result);
 }
 
 void ConversionTest::pyobjTest()
index 9e04d57959612a95c44d8e25ff04b09123ad868b..48ec9da2b38e6304653d80d7965b428b5a55cb4c 100644 (file)
@@ -210,6 +210,16 @@ ConversionCheck fromPy( PyObject *obj, std::vector<T>& result)
     if(!check)
       check.addError("std::vector", obj);
   }
+  else if(PyTuple_Check(obj))
+  {
+    result.clear();
+    std::size_t size = PyTuple_Size(obj);
+    result.resize(size);
+    for(std::size_t i=0; i < size && check; i++)
+      check.addError(fromPy(PyTuple_GetItem(obj, i), result[i]));
+    if(!check)
+      check.addError("std::vector", obj);
+  }
   else
     check.addError("std::vector", obj);
   return check;
@@ -236,6 +246,23 @@ ConversionCheck fromPy( PyObject *obj, std::list<T>& result)
       ++i;
     }
   }
+  else if(PyTuple_Check(obj))
+  {
+    result.clear();
+    std::size_t size = PyTuple_Size(obj);
+    result.resize(size); //result will have "size" default constructed elements.
+    std::size_t i = 0;
+    for(T& it : result)
+    {
+      check.addError(fromPy(PyTuple_GetItem(obj, i), it));
+      if(!check)
+      {
+        check.addError("std::list", obj);
+        break;
+      }
+      ++i;
+    }
+  }
   else
     check.addError("std::list", obj);
   return check;