From a79d9e796b82a9277ab4ac8de19ae596b5bfa266 Mon Sep 17 00:00:00 2001 From: Hugo Parente Lima Date: Thu, 11 Nov 2010 17:58:40 -0200 Subject: Add operator= to AutoDecRef. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reviewer: Renato Araújo Luciano Wolf --- libshiboken/autodecref.h | 47 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 13 deletions(-) (limited to 'libshiboken') diff --git a/libshiboken/autodecref.h b/libshiboken/autodecref.h index 20ed168d8..85445951f 100644 --- a/libshiboken/autodecref.h +++ b/libshiboken/autodecref.h @@ -37,34 +37,55 @@ class LIBSHIBOKEN_API AutoDecRef public: /** * AutoDecRef constructor. - * /param pyobj A borrowed reference to a Python object + * \param pyobj A borrowed reference to a Python object */ - explicit AutoDecRef(PyObject* pyobj) : m_pyobj(pyobj) {} + explicit AutoDecRef(PyObject* pyObj) : m_pyObj(pyObj) {} - ~AutoDecRef() { - Py_XDECREF(m_pyobj); + /// Decref the borrowed python reference + ~AutoDecRef() + { + Py_XDECREF(m_pyObj); } - inline bool isNull() const { return m_pyobj == 0; } + inline bool isNull() const { return m_pyObj == 0; } /// Returns the pointer of the Python object being held. - inline PyObject* object() { return m_pyobj; } - inline operator PyObject*() { return m_pyobj; } - inline operator PyTupleObject*() { return reinterpret_cast(m_pyobj); } - inline operator bool() const { return m_pyobj; } - inline PyObject* operator->() { return m_pyobj; } + inline PyObject* object() { return m_pyObj; } + inline operator PyObject*() { return m_pyObj; } + inline operator PyTupleObject*() { return reinterpret_cast(m_pyObj); } + inline operator bool() const { return m_pyObj; } + inline PyObject* operator->() { return m_pyObj; } template T cast() { - return reinterpret_cast(m_pyobj); + return reinterpret_cast(m_pyObj); + } + + /** + * Decref the current borrowed python reference and take the reference + * borrowed by \p other, so other.isNull() will return true. + */ + void operator=(AutoDecRef& other) + { + Py_XDECREF(m_pyObj); + m_pyObj = other.m_pyObj; + other.m_pyObj = 0; + } + + /** + * Decref the current borrowed python reference and borrow \p other. + */ + void operator=(PyObject* other) + { + Py_XDECREF(m_pyObj); + m_pyObj = other; } private: - PyObject* m_pyobj; + PyObject* m_pyObj; AutoDecRef(const AutoDecRef&); AutoDecRef& operator=(const AutoDecRef&); }; - } // namespace Shiboken #endif // AUTODECREF_H -- cgit v1.2.3