From 5f3579bc040c003ddffba2c0a09a888872bd3136 Mon Sep 17 00:00:00 2001 From: Raoul Gough Date: Wed, 10 Sep 2003 11:25:24 +0000 Subject: [PATCH] Add call policies support to get_slice [SVN r1521] --- .../python/suite/indexing/slice_handler.hpp | 52 ++++++++++++++++--- 1 file changed, 45 insertions(+), 7 deletions(-) diff --git a/include/boost/python/suite/indexing/slice_handler.hpp b/include/boost/python/suite/indexing/slice_handler.hpp index c9d5b4a0..38237f99 100755 --- a/include/boost/python/suite/indexing/slice_handler.hpp +++ b/include/boost/python/suite/indexing/slice_handler.hpp @@ -27,6 +27,7 @@ #include #include #include +#include #include // #include @@ -99,9 +100,46 @@ namespace indexing typedef typename Algorithms::value_type value_type; typedef typename Algorithms::reference reference; - static PyObject *get_slice (container &c, slice sl) + class postcall_override { - boost::python::list temp; + // This class overrides our Policy's postcall function and + // result_conveter to handle the list returned from get_slice. + // The Policy's result_converter is removed, since it gets + // applied within get_slice. Our postcall override applies the + // original postcall to each element of the Python list returned + // from get_slice. + + Policy mBase; + + public: + postcall_override (Policy const &p) : mBase (p) { + } + + bool precall (PyObject *args) { + return mBase.precall (args); + } + + PyObject* postcall (PyObject *args, PyObject *result) { + int size = PyList_Size (result); + + for (int count = 0; count < size; ++count) + { + mBase.postcall (args, PyList_GetItem (result, count)); + } + + return result; + } + + typedef boost::python::default_result_converter result_converter; + }; + + static boost::python::list get_slice (container &c, slice sl) + { + typedef typename Policy::result_converter converter_type; + typedef typename Algorithms::reference reference; + typename boost::mpl::apply1::type converter; + + boost::python::list result; sl.setLength (Algorithms::size(c)); @@ -111,18 +149,18 @@ namespace indexing ; ((sl.stop() - index) * direction) > 0 ; index += sl.step()) { - // *FIXME* handle return policies for each element? - temp.append (Algorithms::get (c, index)); + result.append + (boost::python::handle<> + (converter (Algorithms::get (c, index)))); } - PyObject *result = temp.ptr(); - Py_INCREF (result); // ???? return result; } static boost::python::object make_getitem (Policy const &policy) { - return boost::python::make_function (get_slice, policy); + return boost::python::make_function + (get_slice, postcall_override (policy)); } }; }