-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.hh
330 lines (323 loc) · 9.33 KB
/
types.hh
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#ifndef CAPY_TYPES_HH
#define CAPY_TYPES_HH
// This header contains wrappers around Python objects
namespace Capy
{
// Wrapper around Python objects, implicitly convertible from and
// to basic C++ types
class Object
{
public:
explicit Object(PyObject *self_)
: self(self_)
{
check_error(self);
}
Object(const Object &other)
: self(other.self)
{
Py_INCREF(self);
}
~Object()
{
Py_DECREF(self);
}
Object(bool value)
: self(PyBool_FromLong(value))
{}
Object(long value)
: self(PyInt_FromLong(value))
{
check_error(self);
}
Object(int value)
: self(PyInt_FromLong(value))
{
check_error(self);
}
Object(double value)
: self(PyFloat_FromDouble(value))
{
check_error(self);
}
Object(const char *value)
: self(PyString_FromString(value))
{
check_error(self);
}
Object &operator=(const Object &other)
{
Py_INCREF(other.self);
Py_DECREF(self);
self = other.self;
return *this;
}
operator bool() const
{
return check_error(PyObject_IsTrue(self));
}
operator long() const
{
return check_error(PyInt_AsLong(self));
}
operator int() const
{
return check_error(PyInt_AsLong(self));
}
operator double() const
{
return check_error(PyFloat_AsDouble(self));
}
operator const char *() const
{
return check_error(PyString_AsString(self));
}
operator PyObject *() const
{
return self;
}
Object &new_reference()
{
Py_INCREF(self);
return *this;
}
Object operator()() const
{
return Object(PyObject_CallObject(self, 0));
}
Object operator()(Object arg1) const
{
return Object(PyObject_CallFunctionObjArgs(self,
(PyObject *)arg1, 0));
}
Object operator()(Object arg1, Object arg2) const
{
return Object(PyObject_CallFunctionObjArgs(self,
(PyObject *)arg1,
(PyObject *)arg2, 0));
}
Object operator()(Object arg1, Object arg2, Object arg3) const
{
return Object(PyObject_CallFunctionObjArgs(self,
(PyObject *)arg1,
(PyObject *)arg2,
(PyObject *)arg3, 0));
}
Object operator()(Object arg1, Object arg2, Object arg3, Object arg4) const
{
return Object(PyObject_CallFunctionObjArgs(self,
(PyObject *)arg1,
(PyObject *)arg2,
(PyObject *)arg3,
(PyObject *)arg4, 0));
}
protected:
PyObject *self;
};
class Sequence : public Object
{
public:
explicit Sequence(PyObject *self_)
: Object(self_)
{
if (!PySequence_Check(self))
throw TypeError("argument must be a sequence");
}
Sequence(const Object &other)
: Object(other)
{
if (!PySequence_Check(self))
throw TypeError("argument must be a sequence");
}
Object get(ssize_t item) const
{
return Object(PySequence_GetItem(self, item));
}
void set(ssize_t item, Object value)
{
check_error(PySequence_SetItem(self, item, value));
}
void del(ssize_t item)
{
check_error(PySequence_DelItem(self, item));
}
};
class List : public Sequence
{
public:
explicit List(PyObject *self_)
: Sequence(self_)
{
if (!PyList_Check(self))
throw TypeError("argument must be a list");
}
List(const Object &other)
: Sequence(other)
{
if (!PyList_Check(self))
throw TypeError("argument must be a list");
}
List()
: Sequence(PyList_New(0))
{}
template <typename T>
List(const std::vector<T> &v)
: Sequence(PyList_New(v.size()))
{
for (size_t i = 0; i < v.size(); ++i)
PyList_SET_ITEM(self, i, Object(v[i]).new_reference());
}
template <typename T>
void as_std_vector(std::vector<T> &v) const
{
v.clear();
v.resize(Py_SIZE(self));
for (size_t i = 0; i < v.size(); ++i) {
Object item(PyList_GET_ITEM(self, i));
item.new_reference();
v[i] = item;
}
}
void insert(ssize_t index, Object value)
{
check_error(PyList_Insert(self, index, value));
}
void append(Object value)
{
check_error(PyList_Append(self, value));
}
void sort()
{
check_error(PyList_Sort(self));
}
void reverse()
{
check_error(PyList_Reverse(self));
}
};
class Mapping : public Object
{
public:
explicit Mapping(PyObject *self_)
: Object(self_)
{
if (!PyMapping_Check(self))
throw TypeError("argument must be a mapping");
}
Mapping(const Object &other)
: Object(other)
{
if (!PyMapping_Check(self))
throw TypeError("argument must be a mapping");
}
bool contains(const char *key) const
{
return PyMapping_HasKeyString(self, const_cast<char *>(key));
}
Object get(const char *key) const
{
return Object(PyMapping_GetItemString(self, const_cast<char *>(key)));
}
template <typename T>
T get(const char *key, T default_value) const
{
if (contains(key))
return get(key);
return default_value;
}
void set(const char *key, Object value)
{
check_error(PyMapping_SetItemString(
self, const_cast<char *>(key), value));
}
template <typename T>
T setdefault(const char *key, T default_value)
{
if (contains(key))
return get(key);
set(key, default_value);
return default_value;
}
void del(const char *key)
{
check_error(PyMapping_DelItemString(self, const_cast<char *>(key)));
}
List keys() const
{
// This should actually be
// return List(PyMapping_Keys(self));
// Since PyMapping_Keys is a macro expanding to code that
// generates a warning, we include the expansion of the
// macro and eliminate the warning.
return List(PyObject_CallMethod(self, (char *)"keys", 0));
}
};
class Dict : public Mapping
{
public:
explicit Dict(PyObject *self_)
: Mapping(self_)
{
if (!PyDict_Check(self))
throw TypeError("argument must be a dictionary");
}
Dict(const Object &other)
: Mapping(other)
{
if (!PyDict_Check(self))
throw TypeError("argument must be a dictionary");
}
Dict()
: Mapping(PyDict_New())
{}
void clear()
{
PyDict_Clear(self);
}
Dict copy() const
{
return Dict(PyDict_Copy(self));
}
bool contains(Object key) const
{
return check_error(PyDict_Contains(self, key));
}
Object get(Object key) const
{
return Object(PyDict_GetItem(self, key)).new_reference();
}
template <typename T>
T get(Object key, T default_value) const
{
if (contains(key))
return get(key);
return default_value;
}
void set(Object key, Object value)
{
check_error(PyDict_SetItem(self, key, value));
}
template <typename T>
T setdefault(Object key, T default_value)
{
if (contains(key))
return get(key);
set(key, default_value);
return default_value;
}
void del(Object key)
{
check_error(PyDict_DelItem(self, key));
}
List keys() const
{
return List(PyDict_Keys(self));
}
void update(Mapping other)
{
PyObject_CallMethod(self, (char *)"update",(char *) "O",
(PyObject *)other);
}
};
}
#endif