-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIterator.h
54 lines (40 loc) · 1.12 KB
/
Iterator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#pragma once
#include <Python.h>
#include "./Object.h"
#include <concepts>
namespace xpo::python {
template <std::convertible_to<Object> T>
struct Iterator : public Object {
Iterator(PyObject* pyObject)
: Object(PyIter_Check(pyObject) ? pyObject : nullptr)
, m_current{ nullptr }
{
next();
}
void next() {
m_current = PyIter_Next(m_pyObject);
}
Iterator& operator++() { next(); return *this; }
Iterator operator++(int) { Iterator retval = *this; ++(*this); return retval; }
bool operator==(Iterator other) const { return m_current == other.m_current; }
bool operator!=(Iterator other) const { return !(*this == other); }
T operator*() { return static_cast<T>(Object(m_current)); }
// iterator traits
using difference_type = long;
using value_type = T;
using pointer = T const*;
using reference = T const&;
using iterator_category = std::forward_iterator_tag;
static Iterator<T> end() {
return Iterator<T>();
}
private:
Iterator()
: Object(nullptr)
, m_current{ nullptr }
{
}
private:
PyObject* m_current;
};
}