Add support for iterators with differing end type

This commit is contained in:
Ivan Smirnov 2016-08-24 23:29:04 +01:00
parent c5a1c8a6b9
commit 2b308e01f7
2 changed files with 11 additions and 7 deletions

View File

@ -1117,8 +1117,10 @@ PYBIND11_NOINLINE inline void keep_alive_impl(int Nurse, int Patient, handle arg
keep_alive_impl(nurse, patient);
}
template <typename Iterator, bool KeyIterator = false> struct iterator_state {
Iterator it, end;
template <typename Iterator, typename Sentinel, bool KeyIterator = false>
struct iterator_state {
Iterator it;
Sentinel end;
bool first;
};
@ -1127,10 +1129,11 @@ NAMESPACE_END(detail)
template <typename... Args> detail::init<Args...> init() { return detail::init<Args...>(); }
template <typename Iterator,
typename Sentinel,
typename ValueType = decltype(*std::declval<Iterator>()),
typename... Extra>
iterator make_iterator(Iterator first, Iterator last, Extra &&... extra) {
typedef detail::iterator_state<Iterator> state;
iterator make_iterator(Iterator first, Sentinel last, Extra &&... extra) {
typedef detail::iterator_state<Iterator, Sentinel> state;
if (!detail::get_type_info(typeid(state))) {
class_<state>(handle(), "")
@ -1149,10 +1152,11 @@ iterator make_iterator(Iterator first, Iterator last, Extra &&... extra) {
return (iterator) cast(state { first, last, true });
}
template <typename Iterator,
typename Sentinel,
typename KeyType = decltype((*std::declval<Iterator>()).first),
typename... Extra>
iterator make_key_iterator(Iterator first, Iterator last, Extra &&... extra) {
typedef detail::iterator_state<Iterator, true> state;
iterator make_key_iterator(Iterator first, Sentinel last, Extra &&... extra) {
typedef detail::iterator_state<Iterator, Sentinel, true> state;
if (!detail::get_type_info(typeid(state))) {
class_<state>(handle(), "")

View File

@ -245,7 +245,7 @@ pybind11::class_<std::vector<T, Allocator>, holder_type> bind_vector(pybind11::m
cl.def("__iter__",
[](Vector &v) {
return pybind11::make_iterator<ItType, T>(v.begin(), v.end());
return pybind11::make_iterator<ItType, ItType, T>(v.begin(), v.end());
},
pybind11::keep_alive<0, 1>() /* Essential: keep list alive while iterator exists */
);