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/return_internal_reference.hpp>
9 #include <boost/python/copy_non_const_reference.hpp>
10 #include <boost/python/return_value_policy.hpp>
11 #include <boost/python/iterator.hpp>
17 using namespace boost::python
;
19 typedef std::list
<int> list_int
;
20 typedef std::list
<list_int
> list_list
;
23 void push_back(list_int
& x
, int y
)
28 void push_list_back(list_list
& x
, list_int
const& y
)
38 typedef std::pair
<list_int::iterator
,list_int::iterator
> list_range
;
40 struct list_range2
: list_range
42 list_int::iterator
& begin() { return this->first
; }
43 list_int::iterator
& end() { return this->second
; }
46 list_range
range(list_int
& x
)
48 return list_range(x
.begin(), x
.end());
55 int primes
[] = { 2, 3, 5, 7, 11, 13 };
56 std::copy(primes
, primes
+ sizeof(primes
)/sizeof(*primes
), std::back_inserter(one
));
57 int evens
[] = { 2, 4, 6, 8, 10, 12 };
58 std::copy(evens
, evens
+ sizeof(evens
)/sizeof(*evens
), std::back_inserter(two
));
63 typedef list_int::iterator result_type
;
64 result_type
operator()(two_lists
& ll
) const { return ll
.two
.begin(); }
66 friend struct two_start
;
68 list_int::iterator
one_begin() { return one
.begin(); }
69 list_int::iterator
two_begin() { return two
.begin(); }
71 list_int::iterator
one_end() { return one
.end(); }
72 list_int::iterator
two_end() { return two
.end(); }
79 BOOST_PYTHON_MODULE(iterator_ext
)
81 using boost::python::iterator
; // gcc 2.96 bug workaround
82 def("range", &::range
);
84 class_
<list_int
>("list_int")
85 .def("push_back", push_back
)
87 .def("__iter__", iterator
<list_int
>())
90 class_
<list_range
>("list_range")
92 // We can specify data members
94 , range(&list_range::first
, &list_range::second
))
97 // No runtime tests for this one yet
98 class_
<list_range2
>("list_range2")
100 // We can specify member functions returning a non-const reference
101 .def("__iter__", range(&list_range2::begin
, &list_range2::end
))
104 class_
<two_lists
>("two_lists")
106 // We can spcify member functions
109 , range(&two_lists::one_begin
, &two_lists::one_end
))
111 // Prove that we can explicitly specify call policies
114 , range
<return_value_policy
<copy_non_const_reference
> >(
115 &two_lists::two_begin
, &two_lists::two_end
))
117 // Prove that we can specify call policies and target
120 , range
<return_value_policy
<copy_non_const_reference
>, two_lists
>(
121 // And we can use adaptable function objects when
122 // partial specialization is available.
123 # ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
124 two_lists::two_start()
126 &two_lists::two_begin
128 , &two_lists::two_end
))
131 class_
<list_list
>("list_list")
132 .def("push_back", push_list_back
)
133 .def("__iter__", iterator
<list_list
,return_internal_reference
<> >())
137 #include "module_tail.cpp"