Salome HOME
Add copy methods to PyPtr.
authorOvidiu Mircescu <ovidiu.mircescu@edf.fr>
Fri, 28 Jun 2019 14:25:31 +0000 (16:25 +0200)
committerOvidiu Mircescu <ovidiu.mircescu@edf.fr>
Fri, 28 Jun 2019 14:25:31 +0000 (16:25 +0200)
src/PyPtr.cxx
src/PyPtr.hxx
src/Test/ConversionTest.cxx

index 658c3d648b00f45080026ab977aab1f90a26a4b4..9e0a6f637e0318668f35afca112ddacc7baa11f4 100644 (file)
 namespace py2cpp
 {
 
+PyPtr::PyPtr()
+: _PyPtr()
+{
+}
+
+PyPtr::PyPtr(std::nullptr_t copy)
+: _PyPtr(copy)
+{
+}
+
+PyPtr::PyPtr(PyObject* pyObj)
+: _PyPtr(pyObj)
+{
+}
+
+PyPtr::PyPtr(const PyPtr& copy)
+: _PyPtr(copy.get())
+{
+  Py_XINCREF(copy.get());
+}
+
+PyPtr::PyPtr(PyPtr&& move)
+: _PyPtr(std::move(move))
+{
+}
+
+PyPtr& PyPtr::operator=(std::nullptr_t copy)
+{
+  _PyPtr::operator=(copy);
+  return *this;
+}
+
+PyPtr& PyPtr::operator=(const PyPtr& copy)
+{
+  PyObject* pyCopy = copy.get();
+  if(get() != pyCopy)
+  {
+    Py_XINCREF(pyCopy);
+    reset(pyCopy);
+  }
+  return *this;
+}
+
+PyPtr& PyPtr::operator=(PyPtr&& move)
+{
+  _PyPtr::operator=(std::move(move));
+  return *this;
+}
+
 PyPtr PyPtr::getAttr(const std::string& attribute)const
 {
   PyObject* result = nullptr;
index 9c794f6229cf1d51558bcb8ab2aa37cf5e507c12..d616e694f9e87e5eab009ddfeaf8cadbe9cbb425 100644 (file)
@@ -35,7 +35,15 @@ typedef std::unique_ptr<PyObject, PyPtrDeleter> _PyPtr;
 class PyPtr: public _PyPtr
 {
 public:
-  using _PyPtr::_PyPtr;
+  PyPtr();
+  PyPtr(std::nullptr_t copy);
+  PyPtr(PyObject* pyObj);
+  PyPtr(const PyPtr& copy);
+  PyPtr(PyPtr&& move);
+  PyPtr& operator=(const PyPtr& copy);
+  PyPtr& operator=(PyPtr&& move);
+  PyPtr& operator=(std::nullptr_t copy);
+
   PyPtr getAttr(const std::string& attribute)const;
   void setAttr(const std::string& attribute, const PyPtr& value)const;
   std::string repr()const;
index e4e704b657e509b3377dc8a35768263fa37f7637..c54e54995e02635feb9423d4fffbfb027653e685 100644 (file)
@@ -58,6 +58,17 @@ void ConversionTest::basicTest()
   CPPUNIT_ASSERT(4.2==d2);
   py2cpp::pyResult(i2) = py2cpp::toPyPtr(42);
   CPPUNIT_ASSERT(42 == i2);
+  py2cpp::PyPtr copy(obj);
+  i2=0; d2=0.;
+  py2cpp::pyResult(i2, d2) = copy;
+  CPPUNIT_ASSERT(4==i2);
+  CPPUNIT_ASSERT(4.2==d2);
+  py2cpp::PyPtr copy2;
+  copy2 = obj;
+  i2=0; d2=0.;
+  py2cpp::pyResult(i2, d2) = copy2;
+  CPPUNIT_ASSERT(4==i2);
+  CPPUNIT_ASSERT(4.2==d2);
 }
 
 void ConversionTest::functionTest()