From ca60dcebc6cb4f96134fd91d0606ec661feae94c Mon Sep 17 00:00:00 2001 From: Marcelo Matus Date: Thu, 3 Nov 2005 15:00:38 +0000 Subject: [PATCH] clean PyItem and use it in OutputIterator git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@7789 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- Lib/python/pycontainer.swg | 103 ++++++++++++++++++------------------- 1 file changed, 51 insertions(+), 52 deletions(-) diff --git a/Lib/python/pycontainer.swg b/Lib/python/pycontainer.swg index d7b505002..5cf7530d6 100644 --- a/Lib/python/pycontainer.swg +++ b/Lib/python/pycontainer.swg @@ -33,6 +33,54 @@ namespace swig { %apply PyObject * const& {PyItem const&}; } +%{ +namespace swig { + class PyItem { + PyObject *_obj; + public: + PyItem(const PyItem& item) : _obj(item._obj) + { + Py_XINCREF(_obj); + } + + PyItem(PyObject *obj = 0) :_obj(obj) + { + Py_XINCREF(_obj); + } + + PyItem & operator=(PyObject *obj) { + Py_XINCREF(obj); + Py_XDECREF(_obj); + _obj = obj; + return *this; + } + + PyItem & operator=(const PyItem& item) { + this->operator=(static_cast(item)); + return *this; + } + + ~PyItem() + { + Py_XDECREF(_obj); + } + + operator PyObject *() const + { + return _obj; + } + + PyObject *operator->() const + { + return _obj; + } + + }; +} +%} + + + %fragment(SWIG_Traits_frag(swig::PyItem),"header",fragment="StdTraits") { namespace swig { template <> struct traits { @@ -49,7 +97,7 @@ namespace swig { template <> struct traits_check { - static bool check(PyObject *obj) { + static bool check(PyObject *) { return true; } }; @@ -64,10 +112,6 @@ namespace swig { } } -%fragment(SWIG_Traits_frag(PyItem),"header") -{ -} - %fragment("PySequence_Base","header") { @@ -102,48 +146,6 @@ namespace swig { } }; - class PyItem { - PyObject *_obj; - public: - PyItem(const PyItem& item) : _obj(item._obj) - { - Py_XINCREF(_obj); - } - - PyItem(PyObject *obj = 0) :_obj(obj) - { - Py_XINCREF(obj); - } - - PyItem & operator=(PyObject *obj) { - Py_XINCREF(obj); - Py_XDECREF(_obj); - _obj = obj; - return *this; - } - - PyItem & operator=(const PyItem& item) { - this->operator=(static_cast(item)); - return *this; - } - - ~PyItem() - { - Py_XDECREF(_obj); - } - - operator PyObject *() const - { - return _obj; - } - - PyObject *operator->() const - { - return _obj; - } - - }; - } namespace std { @@ -281,18 +283,15 @@ namespace swig { struct PySequence_OutputIterator { private: - PyObject *_seq; + PyItem _seq; protected: PySequence_OutputIterator(PyObject *seq) : _seq(seq) { - if (_seq) Py_INCREF(_seq); } public: - virtual ~PySequence_OutputIterator() { - if (_seq) Py_DECREF(_seq); - } + virtual ~PySequence_OutputIterator() {} virtual PyObject *value() const throw (stop_iteration) = 0; virtual PySequence_OutputIterator *incr(size_t n = 1) throw (stop_iteration) = 0;