Hello list, I'm having trouble creating a boost python forward iterator to get a numpy array as input of a C++ function that takes an stl begin and end iterator as a template and modifies it, here's the code for the C++ function:
*template<typename _InputIt>* *void fill_increasing(_InputIt begin, _InputIt end, typename std::iterator_traits<_InputIt>::value_type start, typename std::iterator_traits<_InputIt>::value_type increment)* *{* * typedef typename std::iterator_traits<_InputIt>::value_type _Type;* * if (std::is_floating_point<_Type>::value)* * {* * std::size_t count = 0;* * while (begin != end)* * {* * *begin = start + ((_Type)count*increment);* * ++begin;* * ++count;* * }* * }* * else {* * while (begin != end)* * {* * *begin = start;* * start += increment;* * ++begin;* * }* * }* *} * There is already an implementation of an stl input iterator in Boost Python (i.e. stl_iterator.hpp) but it's not mutable, so that won't help me here, so I tried to create my own stl_forward_iterator based on the input iterator. *namespace boost {* * namespace python* * {* * // An STL forward iterator over a python sequence* * template<typename ValueT>* * struct stl_forward_iterator* * : boost::iterator_facade<* * stl_forward_iterator<ValueT>* * , ValueT* * , std::forward_iterator_tag* * >* * {* * stl_forward_iterator()* * : impl_()* * {* * }* * // ob is the python sequence* * stl_forward_iterator(boost::python::object const &ob)* * : impl_(ob)* * {* * }* * private:* * friend class boost::iterator_core_access;* * void increment()* * {* * this->impl_.increment();* * }* * ValueT& dereference() const* * {* * return extract<ValueT&>(this->impl_.current().get())();* * }* * bool equal(stl_forward_iterator<ValueT> const &that) const* * {* * return this->impl_.equal(that.impl_);* * }* * objects::stl_input_iterator_impl impl_;* * };* *}* *}* The only change I really made was changing the return of the *deference* function to a reference instead of just a value. And I use this in my main application as follows: *template<typename T>* *void wrap_fill_increasing(boost::python::numeric::array& array, T start, T increment) {* * boost::python::stl_forward_iterator<T> begin(array);* * boost::python::stl_forward_iterator<T> end;* * vec_fill_increasing(begin, end, start, increment);* *}* *BOOST_PYTHON_MODULE(example) {* * import_array();* * p::numeric::array::set_module_and_type("numpy", "ndarray");* * p::def("fill_increasing", &wrap_fill_increasing<int>);* *}* *int main(int argc, char **argv){ PyImport_AppendInittab("example", &PyInit_example); Py_Initialize(); PyRun_SimpleString( "import example\n" "import numpy\n" "z3 = numpy.zeros((1024,), dtype=numpy.int <http://numpy.int>)\n" "example.fill_increasing(z3, 0, 1)\n" "print(z3)\n" ); Py_Finalize();}* But I get the error: *TypeError: No registered converter was able to extract a C++ reference to type int from this Python object of type numpy.int32* And it occurs on the *dereference* function in my stl_forward_iterator and I don't know how to resolve this. If something that can help he in this scenario doesn't exist, I really think there should be one as a suggestion. Any help would be greatly appreciated, thank you.
_______________________________________________ Cplusplus-sig mailing list Cplusplus-sig@python.org https://mail.python.org/mailman/listinfo/cplusplus-sig