aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken6/ApiExtractor/predefined_templates.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'sources/shiboken6/ApiExtractor/predefined_templates.cpp')
-rw-r--r--sources/shiboken6/ApiExtractor/predefined_templates.cpp94
1 files changed, 48 insertions, 46 deletions
diff --git a/sources/shiboken6/ApiExtractor/predefined_templates.cpp b/sources/shiboken6/ApiExtractor/predefined_templates.cpp
index 96610844c..992f735ac 100644
--- a/sources/shiboken6/ApiExtractor/predefined_templates.cpp
+++ b/sources/shiboken6/ApiExtractor/predefined_templates.cpp
@@ -1,30 +1,5 @@
-/****************************************************************************
-**
-** Copyright (C) 2021 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of Qt for Python.
-**
-** $QT_BEGIN_LICENSE:GPL-EXCEPT$
-** 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 General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 3 as published by the Free Software
-** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
-** 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-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2021 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "predefined_templates.h"
@@ -35,7 +10,7 @@ using namespace Qt::StringLiterals;
static QString pySequenceToCppContainer(const QString &insertFunc,
bool reserve)
{
- QString result;
+ QString result = u"(%out).clear();\n"_s;
if (reserve) {
result += uR"(if (PyList_Check(%in)) {
const Py_ssize_t size = PySequence_Size(%in);
@@ -64,20 +39,37 @@ while (true) {
return result;
}
-static const char stlMapKeyAccessor[] = "->first";
-static const char stlMapValueAccessor[] = "->second";
-static const char qtMapKeyAccessor[] = ".key()";
-static const char qtMapValueAccessor[] = ".value()";
+// Convert a sequence to a limited/fixed array
+static QString pySequenceToCppArray()
+{
+ return uR"(Shiboken::AutoDecRef it(PyObject_GetIter(%in));
+for (auto oit = std::begin(%out), oend = std::end(%out); oit != oend; ++oit) {
+ Shiboken::AutoDecRef pyItem(PyIter_Next(it.object()));
+ if (pyItem.isNull()) {
+ if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_StopIteration))
+ PyErr_Clear();
+ break;
+ }
+ %OUTTYPE_0 cppItem = %CONVERTTOCPP[%OUTTYPE_0](pyItem);
+ *oit = cppItem;
+}
+)"_s;
+}
+
+static constexpr auto stlMapKeyAccessor = "->first"_L1;
+static constexpr auto stlMapValueAccessor = "->second"_L1;
+static constexpr auto qtMapKeyAccessor = ".key()"_L1;
+static constexpr auto qtMapValueAccessor = ".value()"_L1;
static QString cppMapToPyDict(bool isQMap)
{
return uR"(PyObject *%out = PyDict_New();
-for (auto it = %in.cbegin(), end = %in.cend(); it != end; ++it) {
+for (auto it = std::cbegin(%in), end = std::cend(%in); it != end; ++it) {
const auto &key = it)"_s
- + QLatin1StringView(isQMap ? qtMapKeyAccessor : stlMapKeyAccessor)
+ + (isQMap ? qtMapKeyAccessor : stlMapKeyAccessor)
+ uR"(;
const auto &value = it)"_s
- + QLatin1StringView(isQMap ? qtMapValueAccessor : stlMapValueAccessor)
+ + (isQMap ? qtMapValueAccessor : stlMapValueAccessor)
+ uR"(;
PyObject *pyKey = %CONVERTTOPYTHON[%INTYPE_0](key);
PyObject *pyValue = %CONVERTTOPYTHON[%INTYPE_1](value);
@@ -93,6 +85,7 @@ static QString pyDictToCppMap(bool isQMap)
{
return uR"(PyObject *key;
PyObject *value;
+%out.clear();
Py_ssize_t pos = 0;
while (PyDict_Next(%in, &pos, &key, &value)) {
%OUTTYPE_0 cppKey = %CONVERTTOCPP[%OUTTYPE_0](key);
@@ -108,9 +101,9 @@ while (PyDict_Next(%in, &pos, &key, &value)) {
static QString cppMultiMapToPyDict(bool isQMultiMap)
{
return uR"(PyObject *%out = PyDict_New();
- for (auto it = %in.cbegin(), end = %in.cend(); it != end; ) {
+ for (auto it = std::cbegin(%in), end = std::cend(%in); it != end; ) {
const auto &key = it)"_s
- + QLatin1StringView(isQMultiMap ? qtMapKeyAccessor : stlMapKeyAccessor)
+ + (isQMultiMap ? qtMapKeyAccessor : stlMapKeyAccessor)
+ uR"(;
PyObject *pyKey = %CONVERTTOPYTHON[%INTYPE_0](key);
auto upper = %in.)"_s
@@ -134,9 +127,9 @@ static QString cppMultiMapToPyDict(bool isQMultiMap)
static QString cppMultiHashToPyDict(bool isQMultiHash)
{
return uR"(PyObject *%out = PyDict_New();
- for (auto it = %in.cbegin(), end = %in.cend(); it != end; ) {
+ for (auto it = std::cbegin(%in), end = std::cend(%in); it != end; ) {
const auto &key = it)"_s
- + QLatin1StringView(isQMultiHash ? qtMapKeyAccessor : stlMapKeyAccessor)
+ + (isQMultiHash ? qtMapKeyAccessor : stlMapKeyAccessor)
+ uR"(;
PyObject *pyKey = %CONVERTTOPYTHON[%INTYPE_0](key);
auto range = %in.equal_range(key);
@@ -159,6 +152,7 @@ static QString pyDictToCppMultiHash(bool isQMultiHash)
{
return uR"(PyObject *key;
PyObject *values;
+ %out.clear();
Py_ssize_t pos = 0;
while (PyDict_Next(%in, &pos, &key, &values)) {
%OUTTYPE_0 cppKey = %CONVERTTOCPP[%OUTTYPE_0](key);
@@ -197,7 +191,7 @@ return %out;
{u"shiboken_conversion_cppsequence_to_pylist"_s,
uR"(PyObject *%out = PyList_New(Py_ssize_t(%in.size()));
Py_ssize_t idx = 0;
-for (auto it = %in.cbegin(), end = %in.cend(); it != end; ++it, ++idx) {
+for (auto it = std::cbegin(%in), end = std::cend(%in); it != end; ++it, ++idx) {
const auto &cppItem = *it;
PyList_SET_ITEM(%out, idx, %CONVERTTOPYTHON[%INTYPE_0](cppItem));
}
@@ -215,6 +209,8 @@ return %out;)"_s},
pySequenceToCppContainer(u"push_back"_s, false)},
{u"shiboken_conversion_pyiterable_to_cppsequentialcontainer_reserve"_s,
pySequenceToCppContainer(u"push_back"_s, true)},
+ {u"shiboken_conversion_pyiterable_to_cpparray"_s,
+ pySequenceToCppArray()},
{u"shiboken_conversion_pyiterable_to_cppsetcontainer"_s,
pySequenceToCppContainer(u"insert"_s, false)},
@@ -256,19 +252,25 @@ QByteArray containerTypeSystemSnippet(const char *name, const char *type,
const char *targetToNativeType,
const char *targetToNative)
{
- return QByteArrayLiteral("<container-type name=\"")
+ QByteArray result = QByteArrayLiteral("<container-type name=\"")
+ name + QByteArrayLiteral("\" type=\"") + type + R"(">
<include file-name=")" + include + R"(" location="global"/>
<conversion-rule>
<native-to-target>
<insert-template name=")" + nativeToTarget + R"("/>
</native-to-target>
- <target-to-native>
- <add-conversion type=")" + targetToNativeType + R"(">
- <insert-template name=")" + targetToNative + R"("/>
+)";
+ if (targetToNativeType != nullptr) {
+ result += QByteArrayLiteral(R"( <target-to-native>
+ <add-conversion type=")") + targetToNativeType
+ + QByteArrayLiteral(R"(">
+ <insert-template name=")") + targetToNative + QByteArrayLiteral(R"("/>
</add-conversion>
</target-to-native>
- </conversion-rule>
+)");
+ }
+result += QByteArrayLiteral(R"( </conversion-rule>
</container-type>
-)";
+)");
+ return result;
}