/*
 * Interface wrapper code.
 *
 * Generated by SIP 6.7.0
 *
 * Copyright (c) 2022 Riverbank Computing Limited <info@riverbankcomputing.com>
 * 
 * This file is part of PyQt6-NetworkAuth.
 * 
 * This file may be used under the terms of the GNU General Public License
 * version 3.0 as published by the Free Software Foundation and appearing in
 * the file LICENSE included in the packaging of this file.  Please review the
 * following information to ensure the GNU General Public License version 3.0
 * requirements will be met: http://www.gnu.org/copyleft/gpl.html.
 * 
 * If you do not wish to use this file under the terms of the GPL version 3.0
 * then you may purchase a commercial license.  For more information contact
 * info@riverbankcomputing.com.
 * 
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#include "sipAPIQtNetworkAuth.h"

#line 259 "/usr/lib/python3.10/site-packages/PyQt6/bindings/QtCore/qpycore_qmap.sip"
#include <qmap.h>
#line 29 "/build/pyqt6-networkauth/src/PyQt6_NetworkAuth-6.4.0/build/QtNetworkAuth/sipQtNetworkAuthQMultiMap0100QString0100QVariant.cpp"

#line 26 "/usr/lib/python3.10/site-packages/PyQt6/bindings/QtCore/qvariant.sip"
#include <qvariant.h>
#line 33 "/build/pyqt6-networkauth/src/PyQt6_NetworkAuth-6.4.0/build/QtNetworkAuth/sipQtNetworkAuthQMultiMap0100QString0100QVariant.cpp"
#line 24 "/usr/lib/python3.10/site-packages/PyQt6/bindings/QtCore/qstring.sip"
#include <qstring.h>
#line 36 "/build/pyqt6-networkauth/src/PyQt6_NetworkAuth-6.4.0/build/QtNetworkAuth/sipQtNetworkAuthQMultiMap0100QString0100QVariant.cpp"


extern "C" {static void assign_QMultiMap_0100QString_0100QVariant(void *, Py_ssize_t, void *);}
static void assign_QMultiMap_0100QString_0100QVariant(void *sipDst, Py_ssize_t sipDstIdx, void *sipSrc)
{
    reinterpret_cast<QMultiMap< ::QString, ::QVariant> *>(sipDst)[sipDstIdx] = *reinterpret_cast<QMultiMap< ::QString, ::QVariant> *>(sipSrc);
}


extern "C" {static void *array_QMultiMap_0100QString_0100QVariant(Py_ssize_t);}
static void *array_QMultiMap_0100QString_0100QVariant(Py_ssize_t sipNrElem)
{
    return new QMultiMap< ::QString, ::QVariant>[sipNrElem];
}


extern "C" {static void *copy_QMultiMap_0100QString_0100QVariant(const void *, Py_ssize_t);}
static void *copy_QMultiMap_0100QString_0100QVariant(const void *sipSrc, Py_ssize_t sipSrcIdx)
{
    return new QMultiMap< ::QString, ::QVariant>(reinterpret_cast<const QMultiMap< ::QString, ::QVariant> *>(sipSrc)[sipSrcIdx]);
}


/* Call the mapped type's destructor. */
extern "C" {static void release_QMultiMap_0100QString_0100QVariant(void *, int, void *);}
static void release_QMultiMap_0100QString_0100QVariant(void *sipCppV, int, void *)
{
    QMultiMap< ::QString, ::QVariant> *sipCpp = reinterpret_cast<QMultiMap< ::QString, ::QVariant> *>(sipCppV);
    delete sipCpp;
}



extern "C" {static int convertTo_QMultiMap_0100QString_0100QVariant(PyObject *, void **, int *, PyObject *, void **);}
static int convertTo_QMultiMap_0100QString_0100QVariant(PyObject *sipPy, void **sipCppPtrV, int *sipIsErr, PyObject *sipTransferObj, void **)
{
    QMultiMap< ::QString, ::QVariant> **sipCppPtr = reinterpret_cast<QMultiMap< ::QString, ::QVariant> **>(sipCppPtrV);

#line 341 "/usr/lib/python3.10/site-packages/PyQt6/bindings/QtCore/qpycore_qmap.sip"
    if (sipIsErr == NULL)
        return PyDict_Check(sipPy);

    QMultiMap<QString, QVariant> *qm = new QMultiMap<QString, QVariant>;

    Py_ssize_t i = 0;
    PyObject *kobj, *vobj;

    while (PyDict_Next(sipPy, &i, &kobj, &vobj))
    {
        int kstate;

        QString *k = reinterpret_cast<QString *>(sipConvertToType(kobj, sipType_QString, sipTransferObj, SIP_NOT_NONE, &kstate, sipIsErr));

        if (*sipIsErr)
        {
            PyErr_Format(PyExc_TypeError,
                    "a dict key has type '%s' but 'QString' is expected",
                    sipPyTypeName(Py_TYPE(kobj)));

            delete qm;

            return 0;
        }

        Py_ssize_t vi = PySequence_Size(vobj);

        if (vi < 0)
        {
            PyErr_Format(PyExc_TypeError,
                    "a dict value has type '%s' but a sequence is expected",
                    sipPyTypeName(Py_TYPE(vobj)));

            *sipIsErr = 1;
            sipReleaseType(k, sipType_QString, kstate);
            delete qm;

            return 0;
        }

        // We go through the sequence backwards to maintain the Qt semantics of
        // later items appearing first.
        while (vi--)
        {
            PyObject *itm_obj = PySequence_GetItem(vobj, vi);

            if (!itm_obj)
            {
                *sipIsErr = 1;
                sipReleaseType(k, sipType_QString, kstate);
                delete qm;

                return 0;
            }

            int itm_state;

            QVariant *itm = reinterpret_cast<QVariant *>(sipConvertToType(itm_obj, sipType_QVariant, sipTransferObj, SIP_NOT_NONE, &itm_state, sipIsErr));

            Py_DECREF(itm_obj);

            if (*sipIsErr)
            {
                PyErr_Format(PyExc_TypeError,
                        "a sequence element has type '%s' but 'QVariant' is expected",
                        sipPyTypeName(Py_TYPE(itm_obj)));

                sipReleaseType(k, sipType_QString, kstate);
                delete qm;

                return 0;
            }

            qm->insert(*k, *itm);
            sipReleaseType(itm, sipType_QVariant, itm_state);
        }

        sipReleaseType(k, sipType_QString, kstate);
    }

    *sipCppPtr = qm;

    return sipGetState(sipTransferObj);
#line 159 "/build/pyqt6-networkauth/src/PyQt6_NetworkAuth-6.4.0/build/QtNetworkAuth/sipQtNetworkAuthQMultiMap0100QString0100QVariant.cpp"
}


extern "C" {static PyObject *convertFrom_QMultiMap_0100QString_0100QVariant(void *, PyObject *);}
static PyObject *convertFrom_QMultiMap_0100QString_0100QVariant(void *sipCppV, PyObject *sipTransferObj)
{
   QMultiMap< ::QString, ::QVariant> *sipCpp = reinterpret_cast<QMultiMap< ::QString, ::QVariant> *>(sipCppV);

#line 263 "/usr/lib/python3.10/site-packages/PyQt6/bindings/QtCore/qpycore_qmap.sip"
    PyObject *d = PyDict_New();

    if (!d)
        return 0;

    QList<QString> keys = sipCpp->keys();
    QList<QString>::const_iterator kit = keys.constBegin();
    QList<QString>::const_iterator kit_end = keys.constEnd();

    while (kit != kit_end)
    {
        QString *k = new QString(*kit);
        PyObject *kobj = sipConvertFromNewType(k, sipType_QString,
                sipTransferObj);

        if (!kobj)
        {
            delete k;
            Py_DECREF(d);

            return 0;
        }

        // Create a Python list as the dictionary value.
        QList<QVariant> values = sipCpp->values(*kit);
        PyObject *vobj = PyList_New(values.count());

        if (!vobj)
        {
            Py_DECREF(kobj);
            Py_DECREF(d);

            return 0;
        }

        QList<QVariant>::const_iterator vit = values.constBegin();
        QList<QVariant>::const_iterator vit_end = values.constEnd();

        for (int i = 0; vit != vit_end; ++i)
        {
            QVariant *sv = new QVariant(*vit);
            PyObject *svobj = sipConvertFromNewType(sv, sipType_QVariant,
                    sipTransferObj);

            if (!svobj)
            {
                delete sv;
                Py_DECREF(vobj);
                Py_DECREF(kobj);
                Py_DECREF(d);

                return 0;
            }

            PyList_SetItem(vobj, i, svobj);

            ++vit;
        }

        int rc = PyDict_SetItem(d, kobj, vobj);

        Py_DECREF(vobj);
        Py_DECREF(kobj);

        if (rc < 0)
        {
            Py_DECREF(d);

            return 0;
        }

        ++kit;
    }

    return d;
#line 244 "/build/pyqt6-networkauth/src/PyQt6_NetworkAuth-6.4.0/build/QtNetworkAuth/sipQtNetworkAuthQMultiMap0100QString0100QVariant.cpp"
}


sipMappedTypeDef sipTypeDef_QtNetworkAuth_QMultiMap_0100QString_0100QVariant = {
    {
        SIP_NULLPTR,
        SIP_TYPE_MAPPED,
        sipNameNr_364,
        SIP_NULLPTR,
        SIP_NULLPTR
    },
    {
        -1,
        {0, 0, 1},
        0, 0,
        0, 0,
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
    },
    assign_QMultiMap_0100QString_0100QVariant,
    array_QMultiMap_0100QString_0100QVariant,
    copy_QMultiMap_0100QString_0100QVariant,
    release_QMultiMap_0100QString_0100QVariant,
    convertTo_QMultiMap_0100QString_0100QVariant,
    convertFrom_QMultiMap_0100QString_0100QVariant
};
