input_iterator.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright David Abrahams 2002.
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #include <boost/python/module.hpp>
  6. #include <boost/python/def.hpp>
  7. #include <boost/python/class.hpp>
  8. #include <boost/python/iterator.hpp>
  9. #include <boost/iterator/transform_iterator.hpp>
  10. #include <list>
  11. using namespace boost::python;
  12. typedef std::list<int> list_int;
  13. // Prove that we can handle InputIterators which return rvalues.
  14. struct doubler
  15. {
  16. typedef int result_type;
  17. int operator()(int x) const { return x * 2; }
  18. };
  19. typedef boost::transform_iterator<doubler, list_int::iterator> doubling_iterator;
  20. typedef std::pair<doubling_iterator,doubling_iterator> list_range2;
  21. list_range2 range2(list_int& x)
  22. {
  23. return list_range2(
  24. boost::make_transform_iterator<doubler>(x.begin(), doubler())
  25. , boost::make_transform_iterator<doubler>(x.end(), doubler()));
  26. }
  27. // We do this in a separate module from iterators_ext (iterators.cpp)
  28. // to work around an MSVC6 linker bug, which causes it to complain
  29. // about a "duplicate comdat" if the input iterator is instantiated in
  30. // the same module with the others.
  31. BOOST_PYTHON_MODULE(input_iterator)
  32. {
  33. def("range2", &::range2);
  34. class_<list_range2>("list_range2")
  35. // We can wrap InputIterators which return by-value
  36. .def("__iter__"
  37. , range(&list_range2::first, &list_range2::second))
  38. ;
  39. }
  40. #include "module_tail.cpp"