1// This is the SIP interface definition for the QHash based mapped types
2// specific to the QtNetwork module.
3//
4// Copyright (c) 2021 Riverbank Computing Limited <info@riverbankcomputing.com>
5//
6// This file is part of PyQt5.
7//
8// This file may be used under the terms of the GNU General Public License
9// version 3.0 as published by the Free Software Foundation and appearing in
10// the file LICENSE included in the packaging of this file.  Please review the
11// following information to ensure the GNU General Public License version 3.0
12// requirements will be met: http://www.gnu.org/copyleft/gpl.html.
13//
14// If you do not wish to use this file under the terms of the GPL version 3.0
15// then you may purchase a commercial license.  For more information contact
16// info@riverbankcomputing.com.
17//
18// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
19// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20
21
22%MappedType QHash<QNetworkRequest::Attribute, QVariant>
23        /TypeHint="Dict[QNetworkRequest.Attribute, QVariant]",
24        TypeHintValue="{}"/
25{
26%TypeHeaderCode
27#include <qhash.h>
28#include <qnetworkrequest.h>
29#include <qvariant.h>
30%End
31
32%ConvertFromTypeCode
33    PyObject *d = PyDict_New();
34
35    if (!d)
36        return 0;
37
38    QHash<QNetworkRequest::Attribute, QVariant>::const_iterator it = sipCpp->constBegin();
39    QHash<QNetworkRequest::Attribute, QVariant>::const_iterator end = sipCpp->constEnd();
40
41    while (it != end)
42    {
43        PyObject *kobj = sipConvertFromEnum(it.key(),
44                sipType_QNetworkRequest_Attribute);
45
46        if (!kobj)
47        {
48            Py_DECREF(d);
49
50            return 0;
51        }
52
53        QVariant *v = new QVariant(it.value());
54        PyObject *vobj = sipConvertFromNewType(v, sipType_QVariant,
55                sipTransferObj);
56
57        if (!vobj)
58        {
59            delete v;
60            Py_DECREF(kobj);
61            Py_DECREF(d);
62
63            return 0;
64        }
65
66        int rc = PyDict_SetItem(d, kobj, vobj);
67
68        Py_DECREF(vobj);
69        Py_DECREF(kobj);
70
71        if (rc < 0)
72        {
73            Py_DECREF(d);
74
75            return 0;
76        }
77
78        ++it;
79    }
80
81    return d;
82%End
83
84%ConvertToTypeCode
85    if (!sipIsErr)
86        return PyDict_Check(sipPy);
87
88    QHash<QNetworkRequest::Attribute, QVariant> *qh = new QHash<QNetworkRequest::Attribute, QVariant>;
89
90    Py_ssize_t pos = 0;
91    PyObject *kobj, *vobj;
92
93    while (PyDict_Next(sipPy, &pos, &kobj, &vobj))
94    {
95        int k = sipConvertToEnum(kobj, sipType_QNetworkRequest_Attribute);
96
97        if (PyErr_Occurred())
98        {
99            PyErr_Format(PyExc_TypeError,
100                    "a key has type '%s' but 'QNetworkRequest.Attribute' is expected",
101                    sipPyTypeName(Py_TYPE(kobj)));
102
103            delete qh;
104            *sipIsErr = 1;
105
106            return 0;
107        }
108
109        int vstate;
110        QVariant *v = reinterpret_cast<QVariant *>(
111                sipForceConvertToType(vobj, sipType_QVariant, sipTransferObj,
112                        SIP_NOT_NONE, &vstate, sipIsErr));
113
114        if (*sipIsErr)
115        {
116            // Any error must be internal, so leave the exception as it is.
117
118            delete qh;
119
120            return 0;
121        }
122
123        qh->insert(static_cast<QNetworkRequest::Attribute>(k), *v);
124
125        sipReleaseType(v, sipType_QVariant, vstate);
126    }
127
128    *sipCppPtr = qh;
129
130    return sipGetState(sipTransferObj);
131%End
132};
133