-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTuple.h
More file actions
202 lines (157 loc) · 4.45 KB
/
Tuple.h
File metadata and controls
202 lines (157 loc) · 4.45 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#pragma once
#include <tuple>
#include "./AutoObject.h"
#include "./Object.h"
namespace xpo {
namespace python {
struct Tuple;
template <Py_ssize_t C, typename T, typename... Ts>
struct TupleUnpacker {
TupleUnpacker(Tuple const& tuple)
: m_tuple(tuple)
{
}
bool unpack(T ptr, Ts... rest) {
*ptr = m_tuple.get(m_tuple.size() - C).to_native<std::remove_pointer_t<T>>();
return TupleUnpacker<C - 1, Ts...>(m_tuple).unpack(rest...);
}
std::tuple<T, Ts...> to_tuple() {
return std::tuple_cat(std::make_tuple(m_tuple.get(m_tuple.size() - C).to_native<T>()), TupleUnpacker<C - 1, Ts...>(m_tuple).to_tuple());
}
private:
Tuple const& m_tuple;
};
/// Why do we need to specialize for both 0 and 1?
template <typename T>
struct TupleUnpacker<1, T> {
TupleUnpacker(Tuple const& tuple)
: m_tuple(tuple)
{
}
bool unpack(T ptr) {
*ptr = m_tuple.get(m_tuple.size() - 1).to_native<std::remove_pointer_t<T>>();
return true;
}
std::tuple<T> to_tuple() {
return std::make_tuple(m_tuple.get(m_tuple.size() - 1).to_native<T>());
}
private:
Tuple const& m_tuple;
};
template <typename T>
struct TupleUnpacker<0, T> {
TupleUnpacker(Tuple const& tuple)
{}
bool unpack() {
return true;
}
};
struct Tuple : public Object
{
Tuple(PyObject* pyObject) noexcept
: Object(PyTuple_Check(pyObject) ? pyObject : nullptr)
, m_size(PyTuple_Size(pyObject))
{
}
template <Py_ssize_t Size>
Tuple() noexcept
: Object(PyTuple_New(Size))
, m_size(Size)
{
}
template <std::convertible_to<Object>... Ts>
Tuple(Ts ...args) noexcept
: Object(PyTuple_Pack(static_cast<Py_ssize_t>(sizeof...(Ts)), std::forward<PyObject*>(args)...))
, m_size(sizeof...(Ts))
{
incref_all();
}
template <typename... Ts>
Tuple(Ts ...args) noexcept
: Tuple(AutoObject(args)...)
{
}
~Tuple() noexcept {
decref_all();
}
template <std::bidirectional_iterator... Ts>
requires (sizeof...(Ts) > 0)
bool unpack(Ts ...args) const noexcept {
return TupleUnpacker<sizeof...(Ts), Ts...>(*this).unpack(args...);
}
template <typename... Ts>
requires (sizeof...(Ts) > 0)
std::tuple<Ts...> unpack() const noexcept {
return TupleUnpacker<sizeof...(Ts), Ts...>(*this).to_tuple();
}
template <typename... Ts>
requires (sizeof...(Ts) > 0)
std::tuple<Ts...> to_tuple() const noexcept {
return unpack<Ts...>();
}
template <std::invocable<Object> T>
Tuple const& foreach(T && function) const {
for (int i = 0; i < m_size; ++i) {
function(get(i));
}
return *this;
}
Tuple const& incref_all() const {
return foreach([](Object object) { object.incref(); });
}
Tuple const& decref_all() const {
return foreach([](Object object) { object.decref(); });
}
Py_ssize_t size() const {
return m_size;
}
Tuple slice(Py_ssize_t begin, Py_ssize_t end) const {
return Tuple(PyTuple_GetSlice(m_pyObject, begin, end));
}
Object get(Py_ssize_t index) const {
return Object(PyTuple_GetItem(m_pyObject, index));
}
Object operator[](Py_ssize_t index) {
return Object(PyTuple_GetItem(m_pyObject, index));
}
static Tuple const from_args(PyObject* args) {
return Tuple(Object(args).incref().ptr());
}
template <typename... Ts>
requires (sizeof...(Ts) > 0)
operator std::tuple<Ts...>() {
return unpack<Ts...>();
}
private:
Py_ssize_t m_size;
};
template <typename... Ts>
requires (sizeof...(Ts) > 0)
struct TypedTuple : public Tuple {
TypedTuple(Tuple const& tuple) noexcept : Tuple(tuple) {}
TypedTuple(PyObject* pyObject) noexcept : Tuple(pyObject) {}
std::tuple<Ts...> unpack() const {
return Tuple::unpack<Ts...>();
}
template <size_t I>
typename std::tuple_element<I, TypedTuple<Ts...>>::type get() {
return Tuple::get(I).to_native<typename std::tuple_element<I, TypedTuple<Ts...>>::type>();
}
};
}
}
namespace std {
template <typename... Ts>
struct tuple_size<xpo::python::TypedTuple<Ts...>> {
static constexpr size_t value = sizeof...(Ts);
};
// recursive case
template <std::size_t I, class Head, class... Tail>
struct tuple_element<I, xpo::python::TypedTuple<Head, Tail...>>
: std::tuple_element<I - 1, xpo::python::TypedTuple<Tail...>> { };
// base case
template <class Head, class... Tail>
struct tuple_element<0, xpo::python::TypedTuple<Head, Tail...>> {
using type = Head;
};
}