-
Notifications
You must be signed in to change notification settings - Fork 27
/
qmap_double.sip
99 lines (77 loc) · 2.3 KB
/
qmap_double.sip
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
// QMap<double, T> is implemented as a Python dictionary.
template<int, _TYPE_>
%MappedType QMap<double, _TYPE_> /TypeHint="Dict[float, _TYPE_]", TypeHintValue="{}"/
{
%TypeHeaderCode
#include <qmap.h>
%End
%ConvertFromTypeCode
// Create the dictionary.
PyObject *d = PyDict_New();
if (!d)
return NULL;
// Set the dictionary elements.
QMap<double, _TYPE_>::const_iterator it = sipCpp->constBegin();
while (it != sipCpp->constEnd())
{
PyObject *kobj = PyFloat_FromDouble(it.key());
if (!kobj) {
Py_DECREF(d);
return NULL;
}
_TYPE_ *v = new _TYPE_(it.value());
PyObject *vobj = sipConvertFromNewType(v, sipType__TYPE_, sipTransferObj);
if (!vobj) {
delete v;
Py_DECREF(kobj);
Py_DECREF(d);
return NULL;
}
int rc = PyDict_SetItem(d, kobj, vobj);
Py_DECREF(vobj);
Py_DECREF(kobj);
if (rc < 0) {
Py_DECREF(d);
return NULL;
}
++it;
}
return d;
%End
%ConvertToTypeCode
if (!sipIsErr)
return PyDict_Check(sipPy);
QMap<double, _TYPE_> *qm = new QMap<double, _TYPE_>;
Py_ssize_t pos = 0;
PyObject *kobj, *vobj;
while (PyDict_Next(sipPy, &pos, &kobj, &vobj)) {
double k {PyFloat_AS_DOUBLE(kobj)};
if (PyErr_Occurred())
{
if (PyErr_ExceptionMatches(PyExc_TypeError))
PyErr_Format(PyExc_TypeError,
"a dict key has type '%s' but 'double' is expected",
sipPyTypeName(Py_TYPE(kobj)));
delete qm;
*sipIsErr = 1;
return 0;
}
int vstate;
_TYPE_ *v = reinterpret_cast<_TYPE_ *>(
sipForceConvertToType(vobj, sipType__TYPE_, sipTransferObj,
SIP_NOT_NONE, &vstate, sipIsErr));
if (*sipIsErr)
{
PyErr_Format(PyExc_TypeError,
"a dict value has type '%s' but '_TYPE_' is expected",
sipPyTypeName(Py_TYPE(vobj)));
delete qm;
return 0;
}
qm->insert(k, *v);
sipReleaseType(v, sipType__TYPE_, vstate);
}
*sipCppPtr = qm;
return sipGetState(sipTransferObj);
%End
};