Skip to content

Commit 313fe2c

Browse files
committed
input iterator support
[SVN r13960]
1 parent 53c69e7 commit 313fe2c

File tree

5 files changed

+73
-2
lines changed

5 files changed

+73
-2
lines changed

include/boost/python/object/iterator.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ namespace detail
9696
typename mpl::apply1<result_converter,ValueType&>::type cr;
9797
if (!cr.convertible()) return 0;
9898

99+
return cr(x);
100+
}
101+
template <class ValueType>
102+
static PyObject* convert_result(ValueType const& x)
103+
{
104+
typedef typename Policies::result_converter result_converter;
105+
typename mpl::apply1<result_converter,ValueType const&>::type cr;
106+
if (!cr.convertible()) return 0;
107+
99108
return cr(x);
100109
}
101110
};

test/Jamfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ bpl-test bienstman1 ;
6565
bpl-test bienstman2 ;
6666
bpl-test bienstman3 ;
6767
bpl-test multi_arg_constructor ;
68-
bpl-test iterator ;
68+
bpl-test iterator : iterator.py iterator.cpp input_iterator.cpp ;
6969

7070
if $(TEST_BIENSTMAN_NON_BUGS)
7171
{

test/input_iterator.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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"

test/iterator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
#include <iterator>
1515
#include <algorithm>
1616

17-
1817
using namespace boost::python;
1918

2019
typedef std::list<int> list_int;
2120
typedef std::list<list_int> list_list;
2221

22+
2323
void push_back(list_int& x, int y)
2424
{
2525
x.push_back(y);

test/iterator.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'''
22
>>> from iterator_ext import *
3+
>>> from input_iterator import *
34
>>> x = list_int()
45
>>> x.push_back(1)
56
>>> x.back()
@@ -17,6 +18,17 @@
1718
1
1819
3
1920
5
21+
22+
Range2 wraps a transform_iterator which doubles the elements it
23+
traverses. This proves we can wrap input iterators
24+
25+
>>> z2 = range2(x)
26+
>>> for y in z2:
27+
... print y
28+
2
29+
6
30+
10
31+
2032
>>> l2 = two_lists()
2133
>>> for y in l2.primes:
2234
... print y

0 commit comments

Comments
 (0)