mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-22 13:15:12 +00:00
Add support for iterators with differing end type
This commit is contained in:
parent
c5a1c8a6b9
commit
2b308e01f7
@ -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(), "")
|
||||
|
@ -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 */
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user