|
| 1 | +// Copyright David Abrahams 2002. Permission to copy, use, |
| 2 | +// modify, sell and distribute this software is granted provided this |
| 3 | +// copyright notice appears in all copies. This software is provided |
| 4 | +// "as is" without express or implied warranty, and with no claim as |
| 5 | +// to its suitability for any purpose. |
| 6 | +#include <boost/python/module.hpp> |
| 7 | +#include <boost/python/class.hpp> |
| 8 | +#include <boost/python/iterator.hpp> |
| 9 | +#include <boost/iterator_adaptors.hpp> |
| 10 | +#include <list> |
| 11 | +#include <utility> |
| 12 | +#include <iterator> |
| 13 | +#include <functional> |
| 14 | + |
| 15 | +using namespace boost::python; |
| 16 | + |
| 17 | +typedef std::list<int> list_int; |
| 18 | + |
| 19 | +// Prove that we can handle InputIterators which return rvalues. This |
| 20 | +// input iterator example was stolen from the iterator_adaptors |
| 21 | +// documentation |
| 22 | +typedef std::binder1st<std::multiplies<int> > doubler; |
| 23 | +typedef boost::transform_iterator_generator<doubler, list_int::iterator>::type doubling_iterator; |
| 24 | +typedef std::pair<doubling_iterator,doubling_iterator> list_range2; |
| 25 | +list_range2 range2(list_int& x) |
| 26 | +{ |
| 27 | + return list_range2( |
| 28 | + boost::make_transform_iterator<doubler>(x.begin(), std::bind1st(std::multiplies<int>(),2)) |
| 29 | + , boost::make_transform_iterator<doubler>(x.end(), std::bind1st(std::multiplies<int>(),2))); |
| 30 | +} |
| 31 | + |
| 32 | +// We do this in a separate module from iterators_ext (iterators.cpp) |
| 33 | +// to work around an MSVC6 linker bug, which causes it to complain |
| 34 | +// about a "duplicate comdat" if the input iterator is instantiated in |
| 35 | +// the same module with the others. |
| 36 | +BOOST_PYTHON_MODULE_INIT(input_iterator) |
| 37 | +{ |
| 38 | + module("input_iterator") |
| 39 | + .def("range2", &::range2) |
| 40 | + .add( |
| 41 | + class_<list_range2>("list_range2") |
| 42 | + |
| 43 | + // We can wrap InputIterators which return by-value |
| 44 | + .def("__iter__" |
| 45 | + , range(&list_range2::first, &list_range2::second)) |
| 46 | + ) |
| 47 | + ; |
| 48 | +} |
| 49 | + |
| 50 | +#include "module_tail.cpp" |
0 commit comments