diff options
Diffstat (limited to 'sources/shiboken6/libshiboken/sbkconverter.h')
-rw-r--r-- | sources/shiboken6/libshiboken/sbkconverter.h | 146 |
1 files changed, 73 insertions, 73 deletions
diff --git a/sources/shiboken6/libshiboken/sbkconverter.h b/sources/shiboken6/libshiboken/sbkconverter.h index d7803a42a..0d68f3faf 100644 --- a/sources/shiboken6/libshiboken/sbkconverter.h +++ b/sources/shiboken6/libshiboken/sbkconverter.h @@ -1,56 +1,19 @@ -/**************************************************************************** -** -** 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$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #ifndef SBK_CONVERTER_H #define SBK_CONVERTER_H #include "sbkpython.h" +#include "sbkmodule.h" #include "shibokenmacros.h" #include "sbkenum.h" -#include "sbkenum_p.h" #include "basewrapper_p.h" #include <limits> #include <string> struct SbkObject; -struct SbkObjectType; /** * This is a convenience macro identical to Python's PyObject_TypeCheck, @@ -80,7 +43,7 @@ struct SbkArrayConverter; * * C++ -> Python */ -typedef PyObject *(*CppToPythonFunc)(const void *); +using CppToPythonFunc = PyObject *(*)(const void *); /** * This function converts a Python object to a C++ value, it may be @@ -93,7 +56,7 @@ typedef PyObject *(*CppToPythonFunc)(const void *); * * Python -> C++ */ -typedef void (*PythonToCppFunc)(PyObject *,void *); +using PythonToCppFunc = void (*)(PyObject *,void *); /** * Checks if the Python object passed in the argument is convertible to a @@ -104,7 +67,7 @@ typedef void (*PythonToCppFunc)(PyObject *,void *); * * Python -> C++ ? */ -typedef PythonToCppFunc (*IsConvertibleToCppFunc)(PyObject *); +using IsConvertibleToCppFunc = PythonToCppFunc (*)(PyObject *); } // extern "C" @@ -150,7 +113,7 @@ private: * \param copyToPythonFunc Function to convert a C++ object to a Python \p type, copying the object. * \returns The new converter referred by the wrapper \p type. */ -LIBSHIBOKEN_API SbkConverter *createConverter(SbkObjectType *type, +LIBSHIBOKEN_API SbkConverter *createConverter(PyTypeObject *type, PythonToCppFunc toCppPointerConvFunc, IsConvertibleToCppFunc toCppPointerCheckFunc, CppToPythonFunc pointerToPythonFunc, @@ -181,7 +144,10 @@ LIBSHIBOKEN_API void setPythonToCppPointerFunctions(SbkConverter *converter, LIBSHIBOKEN_API void addPythonToCppValueConversion(SbkConverter *converter, PythonToCppFunc pythonToCppFunc, IsConvertibleToCppFunc isConvertibleToCppFunc); -LIBSHIBOKEN_API void addPythonToCppValueConversion(SbkObjectType *type, +LIBSHIBOKEN_API void addPythonToCppValueConversion(PyTypeObject *type, + PythonToCppFunc pythonToCppFunc, + IsConvertibleToCppFunc isConvertibleToCppFunc); +LIBSHIBOKEN_API void addPythonToCppValueConversion(Shiboken::Module::TypeInitStruct typeStruct, PythonToCppFunc pythonToCppFunc, IsConvertibleToCppFunc isConvertibleToCppFunc); @@ -194,7 +160,7 @@ LIBSHIBOKEN_API void addPythonToCppValueConversion(SbkObjectType *type, * TYPE *var; * PyObject *pyVar = pointerToPython(SBKTYPE, &var); */ -LIBSHIBOKEN_API PyObject *pointerToPython(SbkObjectType *type, const void *cppIn); +LIBSHIBOKEN_API PyObject *pointerToPython(PyTypeObject *type, const void *cppIn); LIBSHIBOKEN_API PyObject *pointerToPython(const SbkConverter *converter, const void *cppIn); /** @@ -206,7 +172,7 @@ LIBSHIBOKEN_API PyObject *pointerToPython(const SbkConverter *converter, const v * TYPE &var = SOMETHING; * PyObject *pyVar = referenceToPython(SBKTYPE, &var); */ -LIBSHIBOKEN_API PyObject *referenceToPython(SbkObjectType *type, const void *cppIn); +LIBSHIBOKEN_API PyObject *referenceToPython(PyTypeObject *type, const void *cppIn); LIBSHIBOKEN_API PyObject *referenceToPython(const SbkConverter *converter, const void *cppIn); /** @@ -216,16 +182,32 @@ LIBSHIBOKEN_API PyObject *referenceToPython(const SbkConverter *converter, const * TYPE var; * PyObject *pyVar = copyToPython(SBKTYPE, &var); */ -LIBSHIBOKEN_API PyObject *copyToPython(SbkObjectType *type, const void *cppIn); +LIBSHIBOKEN_API PyObject *copyToPython(PyTypeObject *type, const void *cppIn); LIBSHIBOKEN_API PyObject *copyToPython(const SbkConverter *converter, const void *cppIn); // Python -> C++ --------------------------------------------------------------------------- +struct PythonToCppConversion +{ + enum Type {Invalid, Pointer, Value}; + + operator bool() const { return type != Invalid; } + + void operator()(PyObject *po,void *cpp) const { function(po, cpp); } + + bool isValue() const { return type == Value; } + + PythonToCppFunc function = nullptr; + Type type = Invalid; +}; + /** * Returns a Python to C++ conversion function if the Python object is convertible to a C++ pointer. * It returns NULL if the Python object is not convertible to \p type. */ -LIBSHIBOKEN_API PythonToCppFunc isPythonToCppPointerConvertible(SbkObjectType *type, PyObject *pyIn); +LIBSHIBOKEN_API PythonToCppFunc isPythonToCppPointerConvertible(PyTypeObject *type, PyObject *pyIn); +LIBSHIBOKEN_API PythonToCppConversion pythonToCppPointerConversion(PyTypeObject *type, PyObject *pyIn); +LIBSHIBOKEN_API PythonToCppConversion pythonToCppPointerConversion(Module::TypeInitStruct typeStruct, PyObject *pyIn); /** * Returns a Python to C++ conversion function if the Python object is convertible to a C++ value. @@ -233,7 +215,8 @@ LIBSHIBOKEN_API PythonToCppFunc isPythonToCppPointerConvertible(SbkObjectType *t * convert the object to the expected \p type. * It returns NULL if the Python object is not convertible to \p type. */ -LIBSHIBOKEN_API PythonToCppFunc isPythonToCppValueConvertible(SbkObjectType *type, PyObject *pyIn); +LIBSHIBOKEN_API PythonToCppFunc isPythonToCppValueConvertible(PyTypeObject *type, PyObject *pyIn); +LIBSHIBOKEN_API PythonToCppConversion pythonToCppValueConversion(PyTypeObject *type, PyObject *pyIn); /** * Returns a Python to C++ conversion function if the Python object is convertible to a C++ reference. @@ -241,12 +224,18 @@ LIBSHIBOKEN_API PythonToCppFunc isPythonToCppValueConvertible(SbkObjectType *typ * or a new C++ value if it must be a implicit conversion. * It returns NULL if the Python object is not convertible to \p type. */ -LIBSHIBOKEN_API PythonToCppFunc isPythonToCppReferenceConvertible(SbkObjectType *type, PyObject *pyIn); +LIBSHIBOKEN_API PythonToCppFunc isPythonToCppReferenceConvertible(PyTypeObject *type, PyObject *pyIn); +LIBSHIBOKEN_API PythonToCppConversion pythonToCppReferenceConversion(PyTypeObject *type, PyObject *pyIn); /// This is the same as isPythonToCppValueConvertible function. LIBSHIBOKEN_API PythonToCppFunc isPythonToCppConvertible(const SbkConverter *converter, PyObject *pyIn); +LIBSHIBOKEN_API PythonToCppConversion pythonToCppReferenceConversion(const SbkConverter *converter, PyObject *pyIn); + +LIBSHIBOKEN_API PythonToCppConversion pythonToCppConversion(const SbkConverter *converter, PyObject *pyIn); LIBSHIBOKEN_API PythonToCppFunc isPythonToCppConvertible(const SbkArrayConverter *converter, int dim1, int dim2, PyObject *pyIn); +LIBSHIBOKEN_API PythonToCppConversion pythonToCppConversion(const SbkArrayConverter *converter, + int dim1, int dim2, PyObject *pyIn); /** * Returns the C++ pointer for the \p pyIn object cast to the type passed via \p desiredType. @@ -256,11 +245,11 @@ LIBSHIBOKEN_API PythonToCppFunc isPythonToCppConvertible(const SbkArrayConverter LIBSHIBOKEN_API void *cppPointer(PyTypeObject *desiredType, SbkObject *pyIn); /// Converts a Python object \p pyIn to C++ and stores the result in the C++ pointer passed in \p cppOut. -LIBSHIBOKEN_API void pythonToCppPointer(SbkObjectType *type, PyObject *pyIn, void *cppOut); +LIBSHIBOKEN_API void pythonToCppPointer(PyTypeObject *type, PyObject *pyIn, void *cppOut); LIBSHIBOKEN_API void pythonToCppPointer(const SbkConverter *converter, PyObject *pyIn, void *cppOut); /// Converts a Python object \p pyIn to C++, and copies the result in the C++ variable passed in \p cppOut. -LIBSHIBOKEN_API void pythonToCppCopy(SbkObjectType *type, PyObject *pyIn, void *cppOut); +LIBSHIBOKEN_API void pythonToCppCopy(PyTypeObject *type, PyObject *pyIn, void *cppOut); LIBSHIBOKEN_API void pythonToCppCopy(const SbkConverter *converter, PyObject *pyIn, void *cppOut); /** @@ -274,7 +263,7 @@ LIBSHIBOKEN_API void nonePythonToCppNullPtr(PyObject *, void *cppOut); * It is used when C++ expects a reference argument, so it may be the same object received * from Python, or another created through implicit conversion. */ -LIBSHIBOKEN_API bool isImplicitConversion(SbkObjectType *type, PythonToCppFunc toCpp); +[[deprecated]] LIBSHIBOKEN_API bool isImplicitConversion(PyTypeObject *type, PythonToCppFunc toCpp); /// Registers a converter with a type name that may be used to retrieve the converter. LIBSHIBOKEN_API void registerConverterName(SbkConverter *converter, const char *typeName); @@ -288,11 +277,23 @@ LIBSHIBOKEN_API SbkConverter *primitiveTypeConverter(int index); /// Returns true if a Python sequence is comprised of objects of the given \p type. LIBSHIBOKEN_API bool checkSequenceTypes(PyTypeObject *type, PyObject *pyIn); +/// Returns true if a Python type is iterable and comprised of objects of the +/// given \p type. +LIBSHIBOKEN_API bool checkIterableTypes(PyTypeObject *type, PyObject *pyIn); + /// Returns true if a Python sequence is comprised of objects of a type convertible to the one represented by the given \p converter. LIBSHIBOKEN_API bool convertibleSequenceTypes(const SbkConverter *converter, PyObject *pyIn); /// Returns true if a Python sequence is comprised of objects of a type convertible to \p type. -LIBSHIBOKEN_API bool convertibleSequenceTypes(SbkObjectType *type, PyObject *pyIn); +LIBSHIBOKEN_API bool convertibleSequenceTypes(PyTypeObject *type, PyObject *pyIn); + +/// Returns true if a Python type is iterable and comprised of objects of a +/// type convertible to the one represented by the given \p converter. +LIBSHIBOKEN_API bool convertibleIterableTypes(const SbkConverter *converter, PyObject *pyIn); + +/// Returns true if a Python type is iterable and comprised of objects of a +/// type convertible to \p type. +LIBSHIBOKEN_API bool convertibleIterableTypes(PyTypeObject *type, PyObject *pyIn); /// Returns true if a Python sequence can be converted to a C++ pair. LIBSHIBOKEN_API bool checkPairTypes(PyTypeObject *firstType, PyTypeObject *secondType, PyObject *pyIn); @@ -351,13 +352,14 @@ LIBSHIBOKEN_API bool pythonTypeIsWrapperType(const SbkConverter *converter); #define SBK_SHORT_IDX 8 #define SBK_SIGNEDCHAR_IDX 9 #define SBK_STD_STRING_IDX 10 -#define SBK_UNSIGNEDPY_LONG_LONG_IDX 11 -#define SBK_UNSIGNEDCHAR_IDX 12 -#define SBK_UNSIGNEDINT_IDX 13 -#define SBK_UNSIGNEDLONG_IDX 14 -#define SBK_UNSIGNEDSHORT_IDX 15 -#define SBK_VOIDPTR_IDX 16 -#define SBK_NULLPTR_T_IDX 17 +#define SBK_STD_WSTRING_IDX 11 +#define SBK_UNSIGNEDPY_LONG_LONG_IDX 12 +#define SBK_UNSIGNEDCHAR_IDX 13 +#define SBK_UNSIGNEDINT_IDX 14 +#define SBK_UNSIGNEDLONG_IDX 15 +#define SBK_UNSIGNEDSHORT_IDX 16 +#define SBK_VOIDPTR_IDX 17 +#define SBK_NULLPTR_T_IDX 18 template<typename T> SbkConverter *PrimitiveTypeConverter() { return nullptr; } template<> inline SbkConverter *PrimitiveTypeConverter<PY_LONG_LONG>() { return primitiveTypeConverter(SBK_PY_LONG_LONG_IDX); } @@ -371,6 +373,7 @@ template<> inline SbkConverter *PrimitiveTypeConverter<long>() { return primitiv template<> inline SbkConverter *PrimitiveTypeConverter<short>() { return primitiveTypeConverter(SBK_SHORT_IDX); } template<> inline SbkConverter *PrimitiveTypeConverter<signed char>() { return primitiveTypeConverter(SBK_SIGNEDCHAR_IDX); } template<> inline SbkConverter *PrimitiveTypeConverter<std::string>() { return primitiveTypeConverter(SBK_STD_STRING_IDX); } +template<> inline SbkConverter *PrimitiveTypeConverter<std::wstring>() { return primitiveTypeConverter(SBK_STD_WSTRING_IDX); } template<> inline SbkConverter *PrimitiveTypeConverter<unsigned PY_LONG_LONG>() { return primitiveTypeConverter(SBK_UNSIGNEDPY_LONG_LONG_IDX); } template<> inline SbkConverter *PrimitiveTypeConverter<unsigned char>() { return primitiveTypeConverter(SBK_UNSIGNEDCHAR_IDX); } template<> inline SbkConverter *PrimitiveTypeConverter<unsigned int>() { return primitiveTypeConverter(SBK_UNSIGNEDINT_IDX); } @@ -392,29 +395,26 @@ template<typename T> PyTypeObject *SbkType() { return nullptr; } // Below are the template specializations for C++ primitive types. template<> inline PyTypeObject *SbkType<PY_LONG_LONG>() { return &PyLong_Type; } template<> inline PyTypeObject *SbkType<bool>() { return &PyBool_Type; } -template<> inline PyTypeObject *SbkType<char>() { return &PyInt_Type; } +template<> inline PyTypeObject *SbkType<char>() { return &PyLong_Type; } template<> inline PyTypeObject *SbkType<double>() { return &PyFloat_Type; } template<> inline PyTypeObject *SbkType<float>() { return &PyFloat_Type; } -template<> inline PyTypeObject *SbkType<int>() { return &PyInt_Type; } +template<> inline PyTypeObject *SbkType<int>() { return &PyLong_Type; } template<> inline PyTypeObject *SbkType<long>() { return &PyLong_Type; } -template<> inline PyTypeObject *SbkType<short>() { return &PyInt_Type; } -template<> inline PyTypeObject *SbkType<signed char>() { return &PyInt_Type; } +template<> inline PyTypeObject *SbkType<short>() { return &PyLong_Type; } +template<> inline PyTypeObject *SbkType<signed char>() { return &PyLong_Type; } template<> inline PyTypeObject *SbkType<unsigned PY_LONG_LONG>() { return &PyLong_Type; } -template<> inline PyTypeObject *SbkType<unsigned char>() { return &PyInt_Type; } +template<> inline PyTypeObject *SbkType<unsigned char>() { return &PyLong_Type; } template<> inline PyTypeObject *SbkType<unsigned int>() { return &PyLong_Type; } template<> inline PyTypeObject *SbkType<unsigned long>() { return &PyLong_Type; } -template<> inline PyTypeObject *SbkType<unsigned short>() { return &PyInt_Type; } +template<> inline PyTypeObject *SbkType<unsigned short>() { return &PyLong_Type; } template<> inline PyTypeObject *SbkType<std::nullptr_t>() { return Py_TYPE(&_Py_NoneStruct); } } // namespace Shiboken -// When the user adds a function with an argument unknown for the typesystem, the generator writes type checks as -// TYPENAME_Check, so this macro allows users to add PyObject arguments to their added functions. -#define PyObject_Check(X) true -#define SbkChar_Check(X) (SbkNumber_Check(X) || Shiboken::String::checkChar(X)) +#define SbkChar_Check(X) (PyNumber_Check(X) || Shiboken::String::checkChar(X)) struct PySideQFlagsType; -struct PySideQFlagsTypePrivate +struct SbkQFlagsTypePrivate { SbkConverter *converter; }; |