/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt for Python.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

//workaround to access protected functions
#define protected public

// default includes
#include <shiboken.h>
#ifndef QT_NO_VERSION_TAGGING
#  define QT_NO_VERSION_TAGGING
#endif
#include <QDebug>
#include <pysidesignal.h>
#include <pysideproperty.h>
#include <pyside.h>
#include <pysideqenum.h>
#include <feature_select.h>
QT_WARNING_DISABLE_DEPRECATED

#include <typeinfo>
#include <iterator>

// module include
#include "pyside6_qtcore_python.h"

// main header
#include "qtcorehelper_qmutexlocker_wrapper.h"

// inner classes

// Extra includes
#include <qmutex.h>


#include <cctype>
#include <cstring>

using namespace QtCoreHelper;



template <class T>
static const char *typeNameOf(const T &t)
{
    const char *typeName =  typeid(t).name();
    auto size = std::strlen(typeName);
#if defined(Q_CC_MSVC) // MSVC: "class QPaintDevice * __ptr64"
    if (auto lastStar = strchr(typeName, '*')) {
        // MSVC: "class QPaintDevice * __ptr64"
        while (*--lastStar == ' ') {
        }
        size = lastStar - typeName + 1;
    }
#else // g++, Clang: "QPaintDevice *" -> "P12QPaintDevice"
    if (size > 2 && typeName[0] == 'P' && std::isdigit(typeName[1])) {
        ++typeName;
        --size;
    }
#endif
    char *result = new char[size + 1];
    result[size] = '\0';
    memcpy(result, typeName, size);
    return result;
}


// Target ---------------------------------------------------------

extern "C" {
static int
Sbk_QtCoreHelper_QMutexLocker_Init(PyObject *self, PyObject *args, PyObject *kwds)
{
    SbkObject *sbkSelf = reinterpret_cast<SbkObject *>(self);
    PySide::Feature::Select(self);
    if (Shiboken::Object::isUserType(self) && !Shiboken::ObjectType::canCallConstructor(self->ob_type, Shiboken::SbkType< ::QtCoreHelper::QMutexLocker >()))
        return -1;

    ::QtCoreHelper::QMutexLocker *cptr{};
    Shiboken::AutoDecRef errInfo{};
    static const char *fullName = "PySide6.QtCore.QMutexLocker.__init__";
    SBK_UNUSED(fullName)
    int overloadId = -1;
    Shiboken::Conversions::PythonToCppConversion pythonToCpp[1];
    SBK_UNUSED(pythonToCpp)
    const Py_ssize_t numArgs = PyTuple_GET_SIZE(args);
    SBK_UNUSED(numArgs)
    PyObject *pyArgs[] = {0};

    // invalid argument lengths


    if (!PyArg_UnpackTuple(args, "QMutexLocker", 1, 1, &(pyArgs[0])))
        return -1;


    // Overloaded function decisor
    // 0: QMutexLocker::QMutexLocker(QMutex*)
    // 1: QMutexLocker::QMutexLocker(QRecursiveMutex*)
    if (numArgs == 1
        && (pythonToCpp[0] = Shiboken::Conversions::pythonToCppPointerConversion(SbkPySide6_QtCoreTypes[SBK_QRECURSIVEMUTEX_IDX], (pyArgs[0])))) {
        overloadId = 1; // QMutexLocker(QRecursiveMutex*)
    } else if (numArgs == 1
        && (pythonToCpp[0] = Shiboken::Conversions::pythonToCppPointerConversion(SbkPySide6_QtCoreTypes[SBK_QMUTEX_IDX], (pyArgs[0])))) {
        overloadId = 0; // QMutexLocker(QMutex*)
    }

    // Function signature not found.
    if (overloadId == -1) goto Sbk_QtCoreHelper_QMutexLocker_Init_TypeError;

    // Call function/method
    switch (overloadId) {
        case 0: // QMutexLocker(QMutex * m)
        {
            if (!Shiboken::Object::isValid(pyArgs[0]))
                return -1;
            ::QMutex *cppArg0;
            pythonToCpp[0](pyArgs[0], &cppArg0);

            if (!PyErr_Occurred()) {
                // QMutexLocker(QMutex*)
                PyThreadState *_save = PyEval_SaveThread(); // Py_BEGIN_ALLOW_THREADS
                cptr = new ::QtCoreHelper::QMutexLocker(cppArg0);
                PyEval_RestoreThread(_save); // Py_END_ALLOW_THREADS
                Shiboken::Object::keepReference(reinterpret_cast<SbkObject *>(self), "mutex()const0", pyArgs[0]);
            }
            break;
        }
        case 1: // QMutexLocker(QRecursiveMutex * m)
        {
            if (!Shiboken::Object::isValid(pyArgs[0]))
                return -1;
            ::QRecursiveMutex *cppArg0;
            pythonToCpp[0](pyArgs[0], &cppArg0);

            if (!PyErr_Occurred()) {
                // QMutexLocker(QRecursiveMutex*)
                PyThreadState *_save = PyEval_SaveThread(); // Py_BEGIN_ALLOW_THREADS
                cptr = new ::QtCoreHelper::QMutexLocker(cppArg0);
                PyEval_RestoreThread(_save); // Py_END_ALLOW_THREADS
                Shiboken::Object::keepReference(reinterpret_cast<SbkObject *>(self), "recursiveMutex()const0", pyArgs[0]);
            }
            break;
        }
    }

    if (PyErr_Occurred() || !Shiboken::Object::setCppPointer(sbkSelf, Shiboken::SbkType< ::QtCoreHelper::QMutexLocker >(), cptr)) {
        delete cptr;
        return -1;
    }
    if (!cptr) goto Sbk_QtCoreHelper_QMutexLocker_Init_TypeError;

    Shiboken::Object::setValidCpp(sbkSelf, true);
    if (Shiboken::BindingManager::instance().hasWrapper(cptr)) {
        Shiboken::BindingManager::instance().releaseWrapper(Shiboken::BindingManager::instance().retrieveWrapper(cptr));
    }
    Shiboken::BindingManager::instance().registerWrapper(sbkSelf, cptr);


    return 1;

    Sbk_QtCoreHelper_QMutexLocker_Init_TypeError:
        Shiboken::setErrorAboutWrongArguments(args, fullName, errInfo);
        return -1;
}

static PyObject *Sbk_QtCoreHelper_QMutexLockerFunc___enter__(PyObject *self)
{
    if (!Shiboken::Object::isValid(self))
        return {};
    auto cppSelf = reinterpret_cast< ::QtCoreHelper::QMutexLocker *>(Shiboken::Conversions::cppPointer(SbkPySide6_QtCoreTypes[SBK_QTCOREHELPER_QMUTEXLOCKER_IDX], reinterpret_cast<SbkObject *>(self)));
    SBK_UNUSED(cppSelf)
    Shiboken::AutoDecRef errInfo{};
    static const char *fullName = "PySide6.QtCore.QMutexLocker.__enter__";
    SBK_UNUSED(fullName)

    // Call function/method
    {

        if (!PyErr_Occurred()) {
            // __enter__()
        }
    }

    if (PyErr_Occurred()) {
        return {};
    }
    Py_RETURN_NONE;
}

static PyObject *Sbk_QtCoreHelper_QMutexLockerFunc___exit__(PyObject *self, PyObject *args)
{
    if (!Shiboken::Object::isValid(self))
        return {};
    auto cppSelf = reinterpret_cast< ::QtCoreHelper::QMutexLocker *>(Shiboken::Conversions::cppPointer(SbkPySide6_QtCoreTypes[SBK_QTCOREHELPER_QMUTEXLOCKER_IDX], reinterpret_cast<SbkObject *>(self)));
    SBK_UNUSED(cppSelf)
    Shiboken::AutoDecRef errInfo{};
    static const char *fullName = "PySide6.QtCore.QMutexLocker.__exit__";
    SBK_UNUSED(fullName)
    int overloadId = -1;
    Shiboken::Conversions::PythonToCppConversion pythonToCpp[3];
    SBK_UNUSED(pythonToCpp)
    const Py_ssize_t numArgs = PyTuple_GET_SIZE(args);
    SBK_UNUSED(numArgs)
    PyObject *pyArgs[] = {0, 0, 0};

    // invalid argument lengths


    if (!PyArg_UnpackTuple(args, "__exit__", 3, 3, &(pyArgs[0]), &(pyArgs[1]), &(pyArgs[2])))
        return {};


    // Overloaded function decisor
    // 0: QMutexLocker::__exit__(PyObject*,PyObject*,PyObject*)
    if (numArgs == 3
        && PyObject_Check(pyArgs[0])
        && PyObject_Check(pyArgs[1])
        && PyObject_Check(pyArgs[2])) {
        overloadId = 0; // __exit__(PyObject*,PyObject*,PyObject*)
    }

    // Function signature not found.
    if (overloadId == -1) goto Sbk_QtCoreHelper_QMutexLockerFunc___exit___TypeError;

    // Call function/method
    {

        if (!PyErr_Occurred()) {
            // __exit__(PyObject*,PyObject*,PyObject*)
            // Begin code injection
            // ========================================================================
            // START of custom code block [file: ../glue/qtcore.cpp (unlock)]
            cppSelf->unlock();
            // END of custom code block [file: ../glue/qtcore.cpp (unlock)]
            // ========================================================================
            // End of code injection

        }
    }

    if (PyErr_Occurred()) {
        return {};
    }
    Py_RETURN_NONE;

    Sbk_QtCoreHelper_QMutexLockerFunc___exit___TypeError:
        Shiboken::setErrorAboutWrongArguments(args, fullName, errInfo);
        return {};
}

static PyObject *Sbk_QtCoreHelper_QMutexLockerFunc_mutex(PyObject *self)
{
    if (!Shiboken::Object::isValid(self))
        return {};
    auto cppSelf = reinterpret_cast< ::QtCoreHelper::QMutexLocker *>(Shiboken::Conversions::cppPointer(SbkPySide6_QtCoreTypes[SBK_QTCOREHELPER_QMUTEXLOCKER_IDX], reinterpret_cast<SbkObject *>(self)));
    SBK_UNUSED(cppSelf)
    PyObject *pyResult{};
    Shiboken::AutoDecRef errInfo{};
    static const char *fullName = "PySide6.QtCore.QMutexLocker.mutex";
    SBK_UNUSED(fullName)

    // Call function/method
    {

        if (!PyErr_Occurred()) {
            // mutex()const
            QMutex * cppResult = const_cast<const ::QtCoreHelper::QMutexLocker *>(cppSelf)->mutex();
            pyResult = Shiboken::Conversions::pointerToPython(SbkPySide6_QtCoreTypes[SBK_QMUTEX_IDX], cppResult);
            Shiboken::Object::keepReference(reinterpret_cast<SbkObject *>(self), "mutex()const0", pyResult);
        }
    }

    if (PyErr_Occurred() || !pyResult) {
        Py_XDECREF(pyResult);
        return {};
    }
    return pyResult;
}

static PyObject *Sbk_QtCoreHelper_QMutexLockerFunc_recursiveMutex(PyObject *self)
{
    if (!Shiboken::Object::isValid(self))
        return {};
    auto cppSelf = reinterpret_cast< ::QtCoreHelper::QMutexLocker *>(Shiboken::Conversions::cppPointer(SbkPySide6_QtCoreTypes[SBK_QTCOREHELPER_QMUTEXLOCKER_IDX], reinterpret_cast<SbkObject *>(self)));
    SBK_UNUSED(cppSelf)
    PyObject *pyResult{};
    Shiboken::AutoDecRef errInfo{};
    static const char *fullName = "PySide6.QtCore.QMutexLocker.recursiveMutex";
    SBK_UNUSED(fullName)

    // Call function/method
    {

        if (!PyErr_Occurred()) {
            // recursiveMutex()const
            QRecursiveMutex * cppResult = const_cast<const ::QtCoreHelper::QMutexLocker *>(cppSelf)->recursiveMutex();
            pyResult = Shiboken::Conversions::pointerToPython(SbkPySide6_QtCoreTypes[SBK_QRECURSIVEMUTEX_IDX], cppResult);
            Shiboken::Object::keepReference(reinterpret_cast<SbkObject *>(self), "recursiveMutex()const0", pyResult);
        }
    }

    if (PyErr_Occurred() || !pyResult) {
        Py_XDECREF(pyResult);
        return {};
    }
    return pyResult;
}

static PyObject *Sbk_QtCoreHelper_QMutexLockerFunc_relock(PyObject *self)
{
    if (!Shiboken::Object::isValid(self))
        return {};
    auto cppSelf = reinterpret_cast< ::QtCoreHelper::QMutexLocker *>(Shiboken::Conversions::cppPointer(SbkPySide6_QtCoreTypes[SBK_QTCOREHELPER_QMUTEXLOCKER_IDX], reinterpret_cast<SbkObject *>(self)));
    SBK_UNUSED(cppSelf)
    Shiboken::AutoDecRef errInfo{};
    static const char *fullName = "PySide6.QtCore.QMutexLocker.relock";
    SBK_UNUSED(fullName)

    // Call function/method
    {

        if (!PyErr_Occurred()) {
            // relock()
            PyThreadState *_save = PyEval_SaveThread(); // Py_BEGIN_ALLOW_THREADS
            cppSelf->relock();
            PyEval_RestoreThread(_save); // Py_END_ALLOW_THREADS
        }
    }

    if (PyErr_Occurred()) {
        return {};
    }
    Py_RETURN_NONE;
}

static PyObject *Sbk_QtCoreHelper_QMutexLockerFunc_unlock(PyObject *self)
{
    if (!Shiboken::Object::isValid(self))
        return {};
    auto cppSelf = reinterpret_cast< ::QtCoreHelper::QMutexLocker *>(Shiboken::Conversions::cppPointer(SbkPySide6_QtCoreTypes[SBK_QTCOREHELPER_QMUTEXLOCKER_IDX], reinterpret_cast<SbkObject *>(self)));
    SBK_UNUSED(cppSelf)
    Shiboken::AutoDecRef errInfo{};
    static const char *fullName = "PySide6.QtCore.QMutexLocker.unlock";
    SBK_UNUSED(fullName)

    // Call function/method
    {

        if (!PyErr_Occurred()) {
            // unlock()
            cppSelf->unlock();
        }
    }

    if (PyErr_Occurred()) {
        return {};
    }
    Py_RETURN_NONE;
}


static const char *Sbk_QtCoreHelper_QMutexLocker_PropertyStrings[] = {
    nullptr // Sentinel
};

static PyMethodDef Sbk_QtCoreHelper_QMutexLocker_methods[] = {
    {"__enter__", reinterpret_cast<PyCFunction>(Sbk_QtCoreHelper_QMutexLockerFunc___enter__), METH_NOARGS},
    {"__exit__", reinterpret_cast<PyCFunction>(Sbk_QtCoreHelper_QMutexLockerFunc___exit__), METH_VARARGS},
    {"mutex", reinterpret_cast<PyCFunction>(Sbk_QtCoreHelper_QMutexLockerFunc_mutex), METH_NOARGS},
    {"recursiveMutex", reinterpret_cast<PyCFunction>(Sbk_QtCoreHelper_QMutexLockerFunc_recursiveMutex), METH_NOARGS},
    {"relock", reinterpret_cast<PyCFunction>(Sbk_QtCoreHelper_QMutexLockerFunc_relock), METH_NOARGS},
    {"unlock", reinterpret_cast<PyCFunction>(Sbk_QtCoreHelper_QMutexLockerFunc_unlock), METH_NOARGS},

    {nullptr, nullptr} // Sentinel
};

} // extern "C"

static int Sbk_QtCoreHelper_QMutexLocker_traverse(PyObject *self, visitproc visit, void *arg)
{
    return SbkObject_TypeF()->tp_traverse(self, visit, arg);
}
static int Sbk_QtCoreHelper_QMutexLocker_clear(PyObject *self)
{
    return reinterpret_cast<PyTypeObject *>(SbkObject_TypeF())->tp_clear(self);
}
// Class Definition -----------------------------------------------
extern "C" {
static PyTypeObject *_Sbk_QtCoreHelper_QMutexLocker_Type = nullptr;
static PyTypeObject *Sbk_QtCoreHelper_QMutexLocker_TypeF(void)
{
    return _Sbk_QtCoreHelper_QMutexLocker_Type;
}

static PyType_Slot Sbk_QtCoreHelper_QMutexLocker_slots[] = {
    {Py_tp_base,        nullptr}, // inserted by introduceWrapperType
    {Py_tp_dealloc,     reinterpret_cast<void *>(&SbkDeallocWrapper)},
    {Py_tp_repr,        nullptr},
    {Py_tp_hash,        nullptr},
    {Py_tp_call,        nullptr},
    {Py_tp_str,         nullptr},
    {Py_tp_getattro,    nullptr},
    {Py_tp_setattro,    nullptr},
    {Py_tp_traverse,    reinterpret_cast<void *>(Sbk_QtCoreHelper_QMutexLocker_traverse)},
    {Py_tp_clear,       reinterpret_cast<void *>(Sbk_QtCoreHelper_QMutexLocker_clear)},
    {Py_tp_richcompare, nullptr},
    {Py_tp_iter,        nullptr},
    {Py_tp_iternext,    nullptr},
    {Py_tp_methods,     reinterpret_cast<void *>(Sbk_QtCoreHelper_QMutexLocker_methods)},
    {Py_tp_getset,      nullptr},
    {Py_tp_init,        reinterpret_cast<void *>(Sbk_QtCoreHelper_QMutexLocker_Init)},
    {Py_tp_new,         reinterpret_cast<void *>(SbkObjectTpNew)},
    {0, nullptr}
};
static PyType_Spec Sbk_QtCoreHelper_QMutexLocker_spec = {
    "2:PySide6.QtCore.QMutexLocker",
    sizeof(SbkObject),
    0,
    Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC,
    Sbk_QtCoreHelper_QMutexLocker_slots
};

} //extern "C"


// Type conversion functions.

// Python to C++ pointer conversion - returns the C++ object of the Python wrapper (keeps object identity).
static void QMutexLocker_PythonToCpp_QMutexLocker_PTR(PyObject *pyIn, void *cppOut)
{
    Shiboken::Conversions::pythonToCppPointer(Sbk_QtCoreHelper_QMutexLocker_TypeF(), pyIn, cppOut);
}
static PythonToCppFunc is_QMutexLocker_PythonToCpp_QMutexLocker_PTR_Convertible(PyObject *pyIn)
{
    if (pyIn == Py_None)
        return Shiboken::Conversions::nonePythonToCppNullPtr;
    if (PyObject_TypeCheck(pyIn, reinterpret_cast<PyTypeObject *>(Sbk_QtCoreHelper_QMutexLocker_TypeF())))
        return QMutexLocker_PythonToCpp_QMutexLocker_PTR;
    return {};
}

// C++ to Python pointer conversion - tries to find the Python wrapper for the C++ object (keeps object identity).
static PyObject *QMutexLocker_PTR_CppToPython_QMutexLocker(const void *cppIn)
{
    auto pyOut = reinterpret_cast<PyObject *>(Shiboken::BindingManager::instance().retrieveWrapper(cppIn));
    if (pyOut) {
        Py_INCREF(pyOut);
        return pyOut;
    }
    bool changedTypeName = false;
    auto tCppIn = reinterpret_cast<const ::QtCoreHelper::QMutexLocker *>(cppIn);
    const char *typeName = typeid(*tCppIn).name();
    auto sbkType = Shiboken::ObjectType::typeForTypeName(typeName);
    if (sbkType && Shiboken::ObjectType::hasSpecialCastFunction(sbkType)) {
        typeName = typeNameOf(tCppIn);
        changedTypeName = true;
    }
    PyObject *result = Shiboken::Object::newObject(Sbk_QtCoreHelper_QMutexLocker_TypeF(), const_cast<void *>(cppIn), false, /* exactType */ changedTypeName, typeName);
    if (changedTypeName)
        delete [] typeName;
    return result;
}

// The signatures string for the functions.
// Multiple signatures have their index "n:" in front.
static const char *QtCoreHelper_QMutexLocker_SignatureStrings[] = {
    "1:PySide6.QtCore.QMutexLocker(self,m:PySide6.QtCore.QMutex)",
    "0:PySide6.QtCore.QMutexLocker(self,m:PySide6.QtCore.QRecursiveMutex)",
    "PySide6.QtCore.QMutexLocker.__enter__(self)",
    "PySide6.QtCore.QMutexLocker.__exit__(self,arg__1:PyObject,arg__2:PyObject,arg__3:PyObject)",
    "PySide6.QtCore.QMutexLocker.mutex(self)->PySide6.QtCore.QMutex",
    "PySide6.QtCore.QMutexLocker.recursiveMutex(self)->PySide6.QtCore.QRecursiveMutex",
    "PySide6.QtCore.QMutexLocker.relock(self)",
    "PySide6.QtCore.QMutexLocker.unlock(self)",
    nullptr}; // Sentinel

void init_QtCoreHelper_QMutexLocker(PyObject *module)
{
    _Sbk_QtCoreHelper_QMutexLocker_Type = Shiboken::ObjectType::introduceWrapperType(
        module,
        "QMutexLocker",
        "QtCoreHelper::QMutexLocker*",
        &Sbk_QtCoreHelper_QMutexLocker_spec,
        &Shiboken::callCppDestructor< ::QtCoreHelper::QMutexLocker >,
        0,
        0,
        0);
    auto *pyType = Sbk_QtCoreHelper_QMutexLocker_TypeF(); // references _Sbk_QtCoreHelper_QMutexLocker_Type
    InitSignatureStrings(pyType, QtCoreHelper_QMutexLocker_SignatureStrings);
    SbkObjectType_SetPropertyStrings(reinterpret_cast<PyTypeObject *>(_Sbk_QtCoreHelper_QMutexLocker_Type), Sbk_QtCoreHelper_QMutexLocker_PropertyStrings);
    SbkPySide6_QtCoreTypes[SBK_QTCOREHELPER_QMUTEXLOCKER_IDX]
        = Sbk_QtCoreHelper_QMutexLocker_TypeF();

    // Register Converter
    SbkConverter *converter = Shiboken::Conversions::createConverter(Sbk_QtCoreHelper_QMutexLocker_TypeF(),
        QMutexLocker_PythonToCpp_QMutexLocker_PTR,
        is_QMutexLocker_PythonToCpp_QMutexLocker_PTR_Convertible,
        QMutexLocker_PTR_CppToPython_QMutexLocker);

    Shiboken::Conversions::registerConverterName(converter, "QtCoreHelper::QMutexLocker");
    Shiboken::Conversions::registerConverterName(converter, "QtCoreHelper::QMutexLocker*");
    Shiboken::Conversions::registerConverterName(converter, "QtCoreHelper::QMutexLocker&");
    Shiboken::Conversions::registerConverterName(converter, "QMutexLocker");
    Shiboken::Conversions::registerConverterName(converter, "QMutexLocker*");
    Shiboken::Conversions::registerConverterName(converter, "QMutexLocker&");
    Shiboken::Conversions::registerConverterName(converter, typeid(::QtCoreHelper::QMutexLocker).name());


}
