-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.hh
286 lines (272 loc) · 10 KB
/
class.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
#ifndef CAPY_CLASS_HH
#define CAPY_CLASS_HH
namespace Capy
{
template <typename Cls>
class Class
{
public:
Extension &extension;
const char *const type_name;
struct ClsObject
{
PyObject_HEAD
Cls *instance;
};
Class(Extension &ext, const char *type_name_, const char *doc = 0)
: extension(ext),
type_name(type_name_),
type(new PyTypeObject),
methods(new std::vector<PyMethodDef>),
members(new std::vector<int Cls::*>),
py_members(new std::vector<Object Cls::*>),
getset(new std::vector<PyGetSetDef>)
{
memset(type, 0, sizeof(*type));
Py_INCREF(type);
char *qname =
new char[strlen(extension.mod_name) + strlen(type_name) + 2];
sprintf(qname, "%s.%s", extension.mod_name, type_name);
type->tp_name = qname;
type->tp_basicsize = sizeof(ClsObject);
type->tp_dealloc = (destructor)dealloc;
type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
Py_TPFLAGS_HAVE_GC;
type->tp_doc = doc;
type->tp_traverse = (traverseproc)traverse;
type->tp_base = 0; // XXX
type->tp_new = new_;
PyMethodDef meth = {0};
methods->push_back(meth);
PyGetSetDef gs = {0};
getset->push_back(gs);
}
~Class()
{
if (PyErr_Occurred())
return;
type->tp_methods = &methods->front();
type->tp_getset = &getset->front();
if (PyType_Ready(type) == -1)
return;
PyObject *py_members_cobj = PyCObject_FromVoidPtr(py_members, 0);
if (!py_members_cobj)
return;
if (PyDict_SetItemString(type->tp_dict, "_capy_py_members",
py_members_cobj) == -1) {
Py_DECREF(py_members_cobj);
return;
}
Py_DECREF(py_members_cobj);
extension.add_object(type_name, Object((PyObject *)type));
}
template <typename RT, RT (Cls::*method)()>
void add_method(const char *name, const char *doc = 0)
{
add_method_def(name, check_call<Class::call_method<RT, method> >, doc);
}
template <void (Cls::*method)()>
void add_method(const char *name, const char *doc = 0)
{
add_method_def(name, check_call<Class::call_method<method> >, doc);
}
template <typename RT, typename T, RT (Cls::*method)(T)>
void add_method(const char *name, const char *doc = 0)
{
add_method_def(name, check_call<Class::call_method<RT, T, method> >, doc);
}
template <typename T, void (Cls::*method)(T)>
void add_method(const char *name, const char *doc = 0)
{
add_method_def(name, check_call<Class::call_method<T, method> >, doc);
}
template <typename RT, typename T1, typename T2, RT (Cls::*method)(T1, T2)>
void add_method(const char *name, const char *doc = 0)
{
add_method_def(name, check_call<Class::call_method<RT, T1, T2, method> >, doc);
}
template <typename T1, typename T2, void (Cls::*method)(T1, T2)>
void add_method(const char *name, const char *doc = 0)
{
add_method_def(name, check_call<Class::call_method<T1, T2, method> >, doc);
}
template <typename T>
void add_member(const char *name, T Cls::*memb, const char *doc = 0)
{
members->push_back((int Cls::*)memb);
PyGetSetDef gs =
{const_cast<char *>(name),
(getter)(PyObject *(*)(ClsObject *, T Cls::**))get_member<T>,
0, const_cast<char *>(doc), &members->back()};
getset->insert(getset->end() - 1, gs);
}
template <typename T>
void add_py_member(const char *name, T Cls::*memb,
const char *doc = 0, bool visible = true)
{
py_members->push_back((Object Cls::*)memb);
if (!visible)
return;
PyGetSetDef gs =
{const_cast<char *>(name),
(getter)(PyObject *(*)(ClsObject *, T Cls::**))get_member<T>,
0, const_cast<char *>(doc), &py_members->back()};
getset->insert(getset->end() - 1, gs);
}
private:
void add_method_def(const char *name, PyCFunction meth,
const char *doc)
{
PyMethodDef def = {name, meth, METH_VARARGS, doc};
methods->insert(methods->end() - 1, def);
}
template <typename RT, RT (Cls::*method)()>
static PyObject *
call_method(PyObject *self_obj, PyObject *args)
{
ClsObject *self = (ClsObject *)self_obj;
if (!PyArg_ParseTuple(args, ""))
return 0;
return Object((self->instance->*method)()).new_reference();
}
template <void (Cls::*method)()>
static PyObject *
call_method(PyObject *self_obj, PyObject *args)
{
ClsObject *self = (ClsObject *)self_obj;
if (!PyArg_ParseTuple(args, ""))
return 0;
(self->instance->*method)();
Py_RETURN_NONE;
}
template <typename RT, typename T, RT (Cls::*method)(T)>
static PyObject *
call_method(PyObject *self_obj, PyObject *args)
{
ClsObject *self = (ClsObject *)self_obj;
PyObject *py_arg1;
if (!PyArg_ParseTuple(args, "O", &py_arg1))
return 0;
return Object((self->instance->*method)
(Object(py_arg1).new_reference())).new_reference();
}
template <typename T, void (Cls::*method)(T)>
static PyObject *
call_method(PyObject *self_obj, PyObject *args)
{
ClsObject *self = (ClsObject *)self_obj;
PyObject *py_arg1;
if (!PyArg_ParseTuple(args, "O", &py_arg1))
return 0;
(self->instance->*method)(Object(py_arg1).new_reference());
Py_RETURN_NONE;
}
template <typename RT, typename T1, typename T2, RT (Cls::*method)(T1, T2)>
static PyObject *
call_method(PyObject *self_obj, PyObject *args)
{
ClsObject *self = (ClsObject *)self_obj;
PyObject *py_arg1;
PyObject *py_arg2;
if (!PyArg_ParseTuple(args, "OO", &py_arg1, &py_arg2))
return 0;
return Object((self->instance->*method)
(Object(py_arg1).new_reference(),
Object(py_arg2).new_reference())).new_reference();
}
template <typename T1, typename T2, void (Cls::*method)(T1, T2)>
static PyObject *
call_method(PyObject *self_obj, PyObject *args)
{
ClsObject *self = (ClsObject *)self_obj;
PyObject *py_arg1;
PyObject *py_arg2;
if (!PyArg_ParseTuple(args, "OO", &py_arg1, &py_arg2))
return 0;
(self->instance->*method)(Object(py_arg1).new_reference(),
Object(py_arg2).new_reference());
Py_RETURN_NONE;
}
static PyObject *
new_helper(PyObject *self, PyObject *map)
{
Mapping config(map);
try {
((ClsObject *)self)->instance = new Cls(config);
}
catch (...) {
Py_DECREF(self);
throw;
}
return self;
}
static PyObject *
new_(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *map = 0;
if (!PyArg_ParseTuple(args, "|O", &map))
return 0;
if (!map && !kwargs) {
map = PyDict_New();
if (!map)
return 0;
}
else {
if (map && kwargs)
if (!PyObject_CallMethod(map, (char *)"update",
(char *)"O", kwargs))
return 0;
if (!map)
map = kwargs;
Py_INCREF(map);
}
PyObject *instance = type->tp_alloc(type, 0);
if (!instance) {
Py_DECREF(map);
return 0;
}
return check_call<Class::new_helper>(instance, map);
}
template <typename T>
static PyObject *get_member(ClsObject *self, T Cls::**memb)
{
try {
return Object(self->instance->**memb).new_reference();
}
catch (ExceptionInPythonAPI&) {
return 0;
}
}
static void
dealloc(ClsObject *self)
{
delete self->instance;
PyObject_GC_UnTrack(self);
self->ob_type->tp_free((PyObject *)self);
}
static int
traverse(ClsObject *self, visitproc visit, void *arg)
{
PyObject *py_members_cobj = PyObject_GetAttrString(
(PyObject *)Py_TYPE(self), "_capy_py_members");
if (!py_members_cobj) {
PyErr_Clear();
return 0;
}
typedef std::vector<Object Cls::*> PyMembers;
PyMembers py_members =
*(PyMembers *)PyCObject_AsVoidPtr(py_members_cobj);
for (unsigned i = 0; i < py_members.size(); ++i) {
PyObject *ob = self->instance->*py_members[i];
Py_VISIT(ob);
}
return 0;
}
PyTypeObject *type;
std::vector<PyMethodDef> *methods;
std::vector<int Cls::*> *members;
std::vector<Object Cls::*> *py_members;
std::vector<PyGetSetDef> *getset;
};
}
#endif