From f0db6d8ccdf77dab462ea299b497bbfdabcff515 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Fri, 1 Jul 2022 09:53:08 +0200 Subject: Fix compilation of unique pointer converters for derived classes Add a std::move() to the converter. Also add a test, which currently still fails. The pointer needs to be moved back after the call. Task-number: PYSIDE-454 Change-Id: I173d1becdbac53739923ddbce8a8cdc4f203ccea Reviewed-by: Cristian Maureira-Fredes --- sources/shiboken6/generator/shiboken/cppgenerator.cpp | 4 +++- .../shiboken6/tests/libsmart/stduniqueptrtestbench.cpp | 16 ++++++++++++++++ sources/shiboken6/tests/libsmart/stduniqueptrtestbench.h | 4 ++++ sources/shiboken6/tests/smartbinding/CMakeLists.txt | 1 + .../shiboken6/tests/smartbinding/std_unique_ptr_test.py | 13 ++++++++++++- .../shiboken6/tests/smartbinding/typesystem_smart.xml | 2 +- 6 files changed, 37 insertions(+), 3 deletions(-) diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.cpp b/sources/shiboken6/generator/shiboken/cppgenerator.cpp index 884a531f6..c23c24590 100644 --- a/sources/shiboken6/generator/shiboken/cppgenerator.cpp +++ b/sources/shiboken6/generator/shiboken/cppgenerator.cpp @@ -3422,7 +3422,9 @@ void CppGenerator::writePythonToCppConversionFunctions(TextStream &s, ? targetType.cppSignature() : getFullTypeName(targetType.typeEntry()); c << "*reinterpret_cast<" << fullTypeName << " *>(cppOut) = " - << fullTypeName << '(' << conversion << ");"; + << fullTypeName << '(' + << (sourceType.isUniquePointer() ? stdMove(conversion) : conversion) + << ");"; QString sourceTypeName = fixedCppTypeName(sourceType); QString targetTypeName = fixedCppTypeName(targetType); writePythonToCppFunction(s, c.toString(), sourceTypeName, targetTypeName); diff --git a/sources/shiboken6/tests/libsmart/stduniqueptrtestbench.cpp b/sources/shiboken6/tests/libsmart/stduniqueptrtestbench.cpp index e1056412c..df4b566fa 100644 --- a/sources/shiboken6/tests/libsmart/stduniqueptrtestbench.cpp +++ b/sources/shiboken6/tests/libsmart/stduniqueptrtestbench.cpp @@ -17,6 +17,17 @@ std::ostream &operator<<(std::ostream &str, const std::unique_ptr &p) return str; } +std::ostream &operator<<(std::ostream &str, const std::unique_ptr &p) +{ + str << "unique_ptr("; + if (p.get()) + str << p->value(); + else + str << "nullptr"; + str << ')'; + return str; +} + std::ostream &operator<<(std::ostream &str, const std::unique_ptr &p) { str << "unique_ptr("; @@ -115,3 +126,8 @@ int StdUniquePtrVirtualMethodTester::doModifyIntegerByValue(std::unique_ptrvalue() + 1; } + +void StdUniquePtrTestBench::printInteger2(const std::unique_ptr &p) +{ + std::cerr << __FUNCTION__ << ' ' << p << '\n'; +} diff --git a/sources/shiboken6/tests/libsmart/stduniqueptrtestbench.h b/sources/shiboken6/tests/libsmart/stduniqueptrtestbench.h index 8d3db740a..868c6d08c 100644 --- a/sources/shiboken6/tests/libsmart/stduniqueptrtestbench.h +++ b/sources/shiboken6/tests/libsmart/stduniqueptrtestbench.h @@ -9,6 +9,9 @@ #include class Integer; +namespace Smart { +class Integer2; +} class LIB_SMART_API StdUniquePtrTestBench { @@ -18,6 +21,7 @@ public: static std::unique_ptr createInteger(int v = 42); static std::unique_ptr createNullInteger(); + static void printInteger2(const std::unique_ptr &p); static void printInteger(const std::unique_ptr &p); static void takeInteger(std::unique_ptr p); // Call with std::move() diff --git a/sources/shiboken6/tests/smartbinding/CMakeLists.txt b/sources/shiboken6/tests/smartbinding/CMakeLists.txt index b4b86d733..7c5d0808f 100644 --- a/sources/shiboken6/tests/smartbinding/CMakeLists.txt +++ b/sources/shiboken6/tests/smartbinding/CMakeLists.txt @@ -21,6 +21,7 @@ ${CMAKE_CURRENT_BINARY_DIR}/smart/std_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/smart/std_optional_int_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/smart/std_optional_integer_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/smart/std_unique_ptr_integer_wrapper.cpp +${CMAKE_CURRENT_BINARY_DIR}/smart/std_unique_ptr_integer2_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/smart/std_unique_ptr_int_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/smart/stdoptionaltestbench_wrapper.cpp ${CMAKE_CURRENT_BINARY_DIR}/smart/stduniqueptrtestbench_wrapper.cpp diff --git a/sources/shiboken6/tests/smartbinding/std_unique_ptr_test.py b/sources/shiboken6/tests/smartbinding/std_unique_ptr_test.py index f21466302..0f4729413 100644 --- a/sources/shiboken6/tests/smartbinding/std_unique_ptr_test.py +++ b/sources/shiboken6/tests/smartbinding/std_unique_ptr_test.py @@ -11,7 +11,7 @@ from pathlib import Path sys.path.append(os.fspath(Path(__file__).resolve().parents[1])) from shiboken_paths import init_paths init_paths() -from smart import Integer, StdUniquePtrTestBench, StdUniquePtrVirtualMethodTester, std +from smart import Integer, Integer2, StdUniquePtrTestBench, StdUniquePtrVirtualMethodTester, std def call_func_on_ptr(ptr): @@ -54,6 +54,17 @@ class StdUniquePtrTests(unittest.TestCase): np = std.unique_ptr_Integer(iv) self.assertEqual(np.value(), 42) + def test_derived(self): + iv2 = Integer2() # Construct from pointee + iv2.setValue(42) + p = std.unique_ptr_Smart_Integer2(iv2) + self.assertEqual(p.value(), 42) + StdUniquePtrTestBench.printInteger2(p) # unique_ptr by ref + self.assertTrue(p) + StdUniquePtrTestBench.printInteger(p) # conversion + # FIXME: This fails, pointer is moved in value conversion + # self.assertTrue(p) + def testInt(self): p = StdUniquePtrTestBench.createInt() # unique_ptr by ref StdUniquePtrTestBench.printInt(p) diff --git a/sources/shiboken6/tests/smartbinding/typesystem_smart.xml b/sources/shiboken6/tests/smartbinding/typesystem_smart.xml index 748e5a0d3..d8cea0e3d 100644 --- a/sources/shiboken6/tests/smartbinding/typesystem_smart.xml +++ b/sources/shiboken6/tests/smartbinding/typesystem_smart.xml @@ -57,7 +57,7 @@ + instantiations="Integer,Smart::Integer2,int"> -- cgit v1.2.3