From 228df93605979ed79cdd3ac0e660c9ad7947e255 Mon Sep 17 00:00:00 2001 From: Ovidiu Mircescu Date: Fri, 28 Jun 2019 16:25:31 +0200 Subject: [PATCH] Add copy methods to PyPtr. --- src/PyPtr.cxx | 49 +++++++++++++++++++++++++++++++++++++ src/PyPtr.hxx | 10 +++++++- src/Test/ConversionTest.cxx | 11 +++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/PyPtr.cxx b/src/PyPtr.cxx index 658c3d6..9e0a6f6 100644 --- a/src/PyPtr.cxx +++ b/src/PyPtr.cxx @@ -22,6 +22,55 @@ 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; diff --git a/src/PyPtr.hxx b/src/PyPtr.hxx index 9c794f6..d616e69 100644 --- a/src/PyPtr.hxx +++ b/src/PyPtr.hxx @@ -35,7 +35,15 @@ typedef std::unique_ptr _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; diff --git a/src/Test/ConversionTest.cxx b/src/Test/ConversionTest.cxx index e4e704b..c54e549 100644 --- a/src/Test/ConversionTest.cxx +++ b/src/Test/ConversionTest.cxx @@ -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() -- 2.30.2