/****************************************************************************
**
** 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_qtscxml_python.h"

// main header
#include "qscxmlcompiler_wrapper.h"

// inner classes
#include "qscxmlcompiler_loader_wrapper.h"

// Extra includes
#include <QList>
#include <QtScxml/qscxmlcompiler.h>
#include <QtScxml/qscxmlerror.h>
#include <QtScxml/qscxmlstatemachine.h>
#include <qxmlstream.h>


#include <cctype>
#include <cstring>



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_QScxmlCompiler_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< ::QScxmlCompiler >()))
        return -1;

    ::QScxmlCompiler *cptr{};
    Shiboken::AutoDecRef errInfo{};
    static const char *fullName = "PySide6.QtScxml.QScxmlCompiler.__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, "QScxmlCompiler", 1, 1, &(pyArgs[0])))
        return -1;


    // Overloaded function decisor
    // 0: QScxmlCompiler::QScxmlCompiler(QXmlStreamReader*)
    if (numArgs == 1
        && (pythonToCpp[0] = Shiboken::Conversions::pythonToCppPointerConversion(SbkPySide6_QtCoreTypes[SBK_QXMLSTREAMREADER_IDX], (pyArgs[0])))) {
        overloadId = 0; // QScxmlCompiler(QXmlStreamReader*)
    }

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

    // Call function/method
    {
        if (!Shiboken::Object::isValid(pyArgs[0]))
            return -1;
        ::QXmlStreamReader *cppArg0;
        pythonToCpp[0](pyArgs[0], &cppArg0);

        if (!PyErr_Occurred()) {
            // QScxmlCompiler(QXmlStreamReader*)
            cptr = new ::QScxmlCompiler(cppArg0);
        }
    }

    if (PyErr_Occurred() || !Shiboken::Object::setCppPointer(sbkSelf, Shiboken::SbkType< ::QScxmlCompiler >(), cptr)) {
        delete cptr;
        return -1;
    }
    if (!cptr) goto Sbk_QScxmlCompiler_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_QScxmlCompiler_Init_TypeError:
        Shiboken::setErrorAboutWrongArguments(args, fullName, errInfo);
        return -1;
}

static PyObject *Sbk_QScxmlCompilerFunc_compile(PyObject *self)
{
    if (!Shiboken::Object::isValid(self))
        return {};
    auto cppSelf = reinterpret_cast< ::QScxmlCompiler *>(Shiboken::Conversions::cppPointer(SbkPySide6_QtScxmlTypes[SBK_QSCXMLCOMPILER_IDX], reinterpret_cast<SbkObject *>(self)));
    SBK_UNUSED(cppSelf)
    PyObject *pyResult{};
    Shiboken::AutoDecRef errInfo{};
    static const char *fullName = "PySide6.QtScxml.QScxmlCompiler.compile";
    SBK_UNUSED(fullName)

    // Call function/method
    {

        if (!PyErr_Occurred()) {
            // compile()
            QScxmlStateMachine * cppResult = cppSelf->compile();
            pyResult = Shiboken::Conversions::pointerToPython(SbkPySide6_QtScxmlTypes[SBK_QSCXMLSTATEMACHINE_IDX], cppResult);
            Shiboken::Object::setParent(self, pyResult);
        }
    }

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

static PyObject *Sbk_QScxmlCompilerFunc_errors(PyObject *self)
{
    if (!Shiboken::Object::isValid(self))
        return {};
    auto cppSelf = reinterpret_cast< ::QScxmlCompiler *>(Shiboken::Conversions::cppPointer(SbkPySide6_QtScxmlTypes[SBK_QSCXMLCOMPILER_IDX], reinterpret_cast<SbkObject *>(self)));
    SBK_UNUSED(cppSelf)
    PyObject *pyResult{};
    Shiboken::AutoDecRef errInfo{};
    static const char *fullName = "PySide6.QtScxml.QScxmlCompiler.errors";
    SBK_UNUSED(fullName)

    // Call function/method
    {

        if (!PyErr_Occurred()) {
            // errors()const
            QList<QScxmlError > cppResult = const_cast<const ::QScxmlCompiler *>(cppSelf)->errors();
            pyResult = Shiboken::Conversions::copyToPython(SbkPySide6_QtScxmlTypeConverters[SBK_QTSCXML_QLIST_QSCXMLERROR_IDX], &cppResult);
        }
    }

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

static PyObject *Sbk_QScxmlCompilerFunc_fileName(PyObject *self)
{
    if (!Shiboken::Object::isValid(self))
        return {};
    auto cppSelf = reinterpret_cast< ::QScxmlCompiler *>(Shiboken::Conversions::cppPointer(SbkPySide6_QtScxmlTypes[SBK_QSCXMLCOMPILER_IDX], reinterpret_cast<SbkObject *>(self)));
    SBK_UNUSED(cppSelf)
    PyObject *pyResult{};
    Shiboken::AutoDecRef errInfo{};
    static const char *fullName = "PySide6.QtScxml.QScxmlCompiler.fileName";
    SBK_UNUSED(fullName)

    // Call function/method
    {

        if (!PyErr_Occurred()) {
            // fileName()const
            QString cppResult = const_cast<const ::QScxmlCompiler *>(cppSelf)->fileName();
            pyResult = Shiboken::Conversions::copyToPython(SbkPySide6_QtCoreTypeConverters[SBK_QSTRING_IDX], &cppResult);
        }
    }

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

static PyObject *Sbk_QScxmlCompilerFunc_loader(PyObject *self)
{
    if (!Shiboken::Object::isValid(self))
        return {};
    auto cppSelf = reinterpret_cast< ::QScxmlCompiler *>(Shiboken::Conversions::cppPointer(SbkPySide6_QtScxmlTypes[SBK_QSCXMLCOMPILER_IDX], reinterpret_cast<SbkObject *>(self)));
    SBK_UNUSED(cppSelf)
    PyObject *pyResult{};
    Shiboken::AutoDecRef errInfo{};
    static const char *fullName = "PySide6.QtScxml.QScxmlCompiler.loader";
    SBK_UNUSED(fullName)

    // Call function/method
    {

        if (!PyErr_Occurred()) {
            // loader()const
            QScxmlCompiler::Loader * cppResult = const_cast<const ::QScxmlCompiler *>(cppSelf)->loader();
            pyResult = Shiboken::Conversions::pointerToPython(SbkPySide6_QtScxmlTypes[SBK_QSCXMLCOMPILER_LOADER_IDX], cppResult);
            Shiboken::Object::setParent(self, pyResult);
        }
    }

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

static PyObject *Sbk_QScxmlCompilerFunc_setFileName(PyObject *self, PyObject *pyArg)
{
    if (!Shiboken::Object::isValid(self))
        return {};
    auto cppSelf = reinterpret_cast< ::QScxmlCompiler *>(Shiboken::Conversions::cppPointer(SbkPySide6_QtScxmlTypes[SBK_QSCXMLCOMPILER_IDX], reinterpret_cast<SbkObject *>(self)));
    SBK_UNUSED(cppSelf)
    Shiboken::AutoDecRef errInfo{};
    static const char *fullName = "PySide6.QtScxml.QScxmlCompiler.setFileName";
    SBK_UNUSED(fullName)
    int overloadId = -1;
    Shiboken::Conversions::PythonToCppConversion pythonToCpp;
    SBK_UNUSED(pythonToCpp)

    // Overloaded function decisor
    // 0: QScxmlCompiler::setFileName(QString)
    if ((pythonToCpp = Shiboken::Conversions::pythonToCppConversion(SbkPySide6_QtCoreTypeConverters[SBK_QSTRING_IDX], (pyArg)))) {
        overloadId = 0; // setFileName(QString)
    }

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

    // Call function/method
    {
        ::QString cppArg0;
        pythonToCpp(pyArg, &cppArg0);

        if (!PyErr_Occurred()) {
            // setFileName(QString)
            cppSelf->setFileName(cppArg0);
        }
    }

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

    Sbk_QScxmlCompilerFunc_setFileName_TypeError:
        Shiboken::setErrorAboutWrongArguments(pyArg, fullName, errInfo);
        return {};
}

static PyObject *Sbk_QScxmlCompilerFunc_setLoader(PyObject *self, PyObject *pyArg)
{
    if (!Shiboken::Object::isValid(self))
        return {};
    auto cppSelf = reinterpret_cast< ::QScxmlCompiler *>(Shiboken::Conversions::cppPointer(SbkPySide6_QtScxmlTypes[SBK_QSCXMLCOMPILER_IDX], reinterpret_cast<SbkObject *>(self)));
    SBK_UNUSED(cppSelf)
    Shiboken::AutoDecRef errInfo{};
    static const char *fullName = "PySide6.QtScxml.QScxmlCompiler.setLoader";
    SBK_UNUSED(fullName)
    int overloadId = -1;
    Shiboken::Conversions::PythonToCppConversion pythonToCpp;
    SBK_UNUSED(pythonToCpp)

    // Overloaded function decisor
    // 0: QScxmlCompiler::setLoader(QScxmlCompiler::Loader*)
    if ((pythonToCpp = Shiboken::Conversions::pythonToCppPointerConversion(SbkPySide6_QtScxmlTypes[SBK_QSCXMLCOMPILER_LOADER_IDX], (pyArg)))) {
        overloadId = 0; // setLoader(QScxmlCompiler::Loader*)
    }

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

    // Call function/method
    {
        if (!Shiboken::Object::isValid(pyArg))
            return {};
        ::QScxmlCompiler::Loader *cppArg0;
        pythonToCpp(pyArg, &cppArg0);

        if (!PyErr_Occurred()) {
            // setLoader(QScxmlCompiler::Loader*)
            cppSelf->setLoader(cppArg0);
        }
    }

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

    Sbk_QScxmlCompilerFunc_setLoader_TypeError:
        Shiboken::setErrorAboutWrongArguments(pyArg, fullName, errInfo);
        return {};
}


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

static PyMethodDef Sbk_QScxmlCompiler_methods[] = {
    {"compile", reinterpret_cast<PyCFunction>(Sbk_QScxmlCompilerFunc_compile), METH_NOARGS},
    {"errors", reinterpret_cast<PyCFunction>(Sbk_QScxmlCompilerFunc_errors), METH_NOARGS},
    {"fileName", reinterpret_cast<PyCFunction>(Sbk_QScxmlCompilerFunc_fileName), METH_NOARGS},
    {"loader", reinterpret_cast<PyCFunction>(Sbk_QScxmlCompilerFunc_loader), METH_NOARGS},
    {"setFileName", reinterpret_cast<PyCFunction>(Sbk_QScxmlCompilerFunc_setFileName), METH_O},
    {"setLoader", reinterpret_cast<PyCFunction>(Sbk_QScxmlCompilerFunc_setLoader), METH_O},

    {nullptr, nullptr} // Sentinel
};

} // extern "C"

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

static PyType_Slot Sbk_QScxmlCompiler_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_QScxmlCompiler_traverse)},
    {Py_tp_clear,       reinterpret_cast<void *>(Sbk_QScxmlCompiler_clear)},
    {Py_tp_richcompare, nullptr},
    {Py_tp_iter,        nullptr},
    {Py_tp_iternext,    nullptr},
    {Py_tp_methods,     reinterpret_cast<void *>(Sbk_QScxmlCompiler_methods)},
    {Py_tp_getset,      nullptr},
    {Py_tp_init,        reinterpret_cast<void *>(Sbk_QScxmlCompiler_Init)},
    {Py_tp_new,         reinterpret_cast<void *>(SbkObjectTpNew)},
    {0, nullptr}
};
static PyType_Spec Sbk_QScxmlCompiler_spec = {
    "2:PySide6.QtScxml.QScxmlCompiler",
    sizeof(SbkObject),
    0,
    Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC,
    Sbk_QScxmlCompiler_slots
};

} //extern "C"


// Type conversion functions.

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

// C++ to Python pointer conversion - tries to find the Python wrapper for the C++ object (keeps object identity).
static PyObject *QScxmlCompiler_PTR_CppToPython_QScxmlCompiler(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 ::QScxmlCompiler *>(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_QScxmlCompiler_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 *QScxmlCompiler_SignatureStrings[] = {
    "PySide6.QtScxml.QScxmlCompiler(self,xmlReader:PySide6.QtCore.QXmlStreamReader)",
    "PySide6.QtScxml.QScxmlCompiler.compile(self)->PySide6.QtScxml.QScxmlStateMachine",
    "PySide6.QtScxml.QScxmlCompiler.errors(self)->QList[PySide6.QtScxml.QScxmlError]",
    "PySide6.QtScxml.QScxmlCompiler.fileName(self)->QString",
    "PySide6.QtScxml.QScxmlCompiler.loader(self)->PySide6.QtScxml.QScxmlCompiler.Loader",
    "PySide6.QtScxml.QScxmlCompiler.setFileName(self,fileName:QString)",
    "PySide6.QtScxml.QScxmlCompiler.setLoader(self,newLoader:PySide6.QtScxml.QScxmlCompiler.Loader)",
    nullptr}; // Sentinel

void init_QScxmlCompiler(PyObject *module)
{
    _Sbk_QScxmlCompiler_Type = Shiboken::ObjectType::introduceWrapperType(
        module,
        "QScxmlCompiler",
        "QScxmlCompiler*",
        &Sbk_QScxmlCompiler_spec,
        &Shiboken::callCppDestructor< ::QScxmlCompiler >,
        0,
        0,
        0);
    auto *pyType = Sbk_QScxmlCompiler_TypeF(); // references _Sbk_QScxmlCompiler_Type
    InitSignatureStrings(pyType, QScxmlCompiler_SignatureStrings);
    SbkObjectType_SetPropertyStrings(reinterpret_cast<PyTypeObject *>(_Sbk_QScxmlCompiler_Type), Sbk_QScxmlCompiler_PropertyStrings);
    SbkPySide6_QtScxmlTypes[SBK_QSCXMLCOMPILER_IDX]
        = Sbk_QScxmlCompiler_TypeF();

    // Register Converter
    SbkConverter *converter = Shiboken::Conversions::createConverter(Sbk_QScxmlCompiler_TypeF(),
        QScxmlCompiler_PythonToCpp_QScxmlCompiler_PTR,
        is_QScxmlCompiler_PythonToCpp_QScxmlCompiler_PTR_Convertible,
        QScxmlCompiler_PTR_CppToPython_QScxmlCompiler);

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


}
