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 
12 using namespace boost::python;
13 
14 typedef std::list<int> list_int;
15 
16 // Prove that we can handle InputIterators which return rvalues.
17 struct doubler
18 {
19     typedef int result_type;
operator ()doubler20     int operator()(int x) const { return x * 2; }
21 };
22 
23 typedef boost::transform_iterator<doubler, list_int::iterator> doubling_iterator;
24 typedef std::pair<doubling_iterator,doubling_iterator> list_range2;
25 
range2(list_int & x)26 list_range2 range2(list_int& x)
27 {
28     return list_range2(
29         boost::make_transform_iterator<doubler>(x.begin(), doubler())
30       , boost::make_transform_iterator<doubler>(x.end(), doubler()));
31 }
32 
33 // We do this in a separate module from iterators_ext (iterators.cpp)
34 // to work around an MSVC6 linker bug, which causes it to complain
35 // about a "duplicate comdat" if the input iterator is instantiated in
36 // the same module with the others.
BOOST_PYTHON_MODULE(input_iterator)37 BOOST_PYTHON_MODULE(input_iterator)
38 {
39     def("range2", &::range2);
40 
41     class_<list_range2>("list_range2")
42         // We can wrap InputIterators which return by-value
43         .def("__iter__"
44              , range(&list_range2::first, &list_range2::second))
45         ;
46 }
47 
48 #include "module_tail.cpp"
49