diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2022-12-21 16:01:59 +0100 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2023-03-07 13:10:10 +0100 |
commit | 504cf3341c0622c16d4283eef58aed9fe9c35036 (patch) | |
tree | 4b09fad4d74021319dab2adb71e11ab30996d9a8 /sources/shiboken6/tests | |
parent | 21fbc5ec9a973a80199134db77708b7bc48a0707 (diff) |
shiboken6: Add support for std::span
Convert std::span to a Python list. For the conversion from Python to
C++, make it a view on a vector.
A special type is introduced since a different implementation
is needed for opaque containers.
Task-number: PYSIDE-2174
Change-Id: I17a7385282a7d373dc73d5ac63a5d3363d61d130
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Diffstat (limited to 'sources/shiboken6/tests')
7 files changed, 108 insertions, 0 deletions
diff --git a/sources/shiboken6/tests/libminimal/CMakeLists.txt b/sources/shiboken6/tests/libminimal/CMakeLists.txt index 8a5ad4fe2..4a10f96bf 100644 --- a/sources/shiboken6/tests/libminimal/CMakeLists.txt +++ b/sources/shiboken6/tests/libminimal/CMakeLists.txt @@ -9,6 +9,7 @@ libminimalmacros.h listuser.cpp listuser.h minbool.h obj.cpp obj.h +spanuser.cpp spanuser.h typedef.cpp typedef.h val.h ) diff --git a/sources/shiboken6/tests/libminimal/spanuser.cpp b/sources/shiboken6/tests/libminimal/spanuser.cpp new file mode 100644 index 000000000..1fcfe1e97 --- /dev/null +++ b/sources/shiboken6/tests/libminimal/spanuser.cpp @@ -0,0 +1,41 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +#include "spanuser.h" + +#include <numeric> + +SpanUser::SpanUser() = default; + +bool SpanUser::enabled() +{ +#if __cplusplus >= 202002L + return true; +#else + return false; +#endif +} + +#if __cplusplus >= 202002L +IntSpan3 SpanUser::getIntSpan3() +{ + static int iv[] = {1, 2, 3}; + return IntSpan3(iv); +} + +ConstIntSpan3 SpanUser::getConstIntSpan3() +{ + static const int civ[] = {1, 2, 3}; + return ConstIntSpan3(civ); +} + +int SpanUser::sumIntSpan3(IntSpan3 isp3) +{ + return std::accumulate(isp3.begin(), isp3.end(), 0); +} + +int SpanUser::sumConstIntSpan3(ConstIntSpan3 ispc3) +{ + return std::accumulate(ispc3.begin(), ispc3.end(), 0); +} +#endif // C++ 20 diff --git a/sources/shiboken6/tests/libminimal/spanuser.h b/sources/shiboken6/tests/libminimal/spanuser.h new file mode 100644 index 000000000..ad6a0f2df --- /dev/null +++ b/sources/shiboken6/tests/libminimal/spanuser.h @@ -0,0 +1,31 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +#ifndef SPANUSER_H +#define SPANUSER_H + +#include "libminimalmacros.h" + +#if __cplusplus >= 202002L +# include <span> + +using IntSpan3 = std::span<int, 3>; +using ConstIntSpan3 = std::span<const int, 3>; +#endif + +struct LIBMINIMAL_API SpanUser +{ + SpanUser(); + + static bool enabled(); + +#if __cplusplus >= 202002L + static IntSpan3 getIntSpan3(); + static ConstIntSpan3 getConstIntSpan3(); + + static int sumIntSpan3(IntSpan3 isp3); + static int sumConstIntSpan3(ConstIntSpan3 ispc3); +#endif // C++ 20 +}; + +#endif // SPANUSER_H diff --git a/sources/shiboken6/tests/minimalbinding/CMakeLists.txt b/sources/shiboken6/tests/minimalbinding/CMakeLists.txt index ffbf9f111..7f132bd34 100644 --- a/sources/shiboken6/tests/minimalbinding/CMakeLists.txt +++ b/sources/shiboken6/tests/minimalbinding/CMakeLists.txt @@ -13,6 +13,7 @@ ${CMAKE_CURRENT_BINARY_DIR}/minimal/containeruser_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/minimal/obj_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/minimal/val_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/minimal/listuser_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/minimal/spanuser_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/minimal/minbooluser_wrapper.cpp ) diff --git a/sources/shiboken6/tests/minimalbinding/global.h b/sources/shiboken6/tests/minimalbinding/global.h index 4e792705a..fc5c59a26 100644 --- a/sources/shiboken6/tests/minimalbinding/global.h +++ b/sources/shiboken6/tests/minimalbinding/global.h @@ -6,4 +6,5 @@ #include "val.h" #include "minbool.h" #include "listuser.h" +#include "spanuser.h" #include "typedef.h" diff --git a/sources/shiboken6/tests/minimalbinding/spanuser_test.py b/sources/shiboken6/tests/minimalbinding/spanuser_test.py new file mode 100644 index 000000000..ee384eb81 --- /dev/null +++ b/sources/shiboken6/tests/minimalbinding/spanuser_test.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python +# Copyright (C) 2023 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +import os +import sys +import unittest + +from pathlib import Path +sys.path.append(os.fspath(Path(__file__).resolve().parents[1])) +from shiboken_paths import init_paths +init_paths() + +from minimal import SpanUser + + +class IntSpanTest(unittest.TestCase): + + def testCreateIntSpan(self): + if not SpanUser.enabled(): + return + expected = [1, 2, 3] + self.assertEqual(SpanUser.getIntSpan3(), expected) + self.assertEqual(SpanUser.getConstIntSpan3(), expected) + + self.assertEqual(SpanUser.sumIntSpan3(expected), 6) + self.assertEqual(SpanUser.sumConstIntSpan3(expected), 6) + + +if __name__ == '__main__': + unittest.main() diff --git a/sources/shiboken6/tests/minimalbinding/typesystem_minimal.xml b/sources/shiboken6/tests/minimalbinding/typesystem_minimal.xml index e73ddc228..ef317f415 100644 --- a/sources/shiboken6/tests/minimalbinding/typesystem_minimal.xml +++ b/sources/shiboken6/tests/minimalbinding/typesystem_minimal.xml @@ -38,6 +38,8 @@ </modify-argument> </modify-function> </value-type> + <value-type name="SpanUser"/> + <value-type name="MinBoolUser"/> <value-type name="ContainerUser"> |