diff options
| author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2022-07-01 09:53:08 +0200 |
|---|---|---|
| committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2022-07-01 16:19:00 +0200 |
| commit | f0db6d8ccdf77dab462ea299b497bbfdabcff515 (patch) | |
| tree | f392cde30d4ee51c920c5be8608a3aaf45cbd05f | |
| parent | 6d81913ed1189a5c7b1e26d2456f05e8e8684914 (diff) | |
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 <cristian.maureira-fredes@qt.io>
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<Integer> &p) return str; } +std::ostream &operator<<(std::ostream &str, const std::unique_ptr<Smart::Integer2> &p) +{ + str << "unique_ptr<Integer>("; + if (p.get()) + str << p->value(); + else + str << "nullptr"; + str << ')'; + return str; +} + std::ostream &operator<<(std::ostream &str, const std::unique_ptr<int> &p) { str << "unique_ptr<int>("; @@ -115,3 +126,8 @@ int StdUniquePtrVirtualMethodTester::doModifyIntegerByValue(std::unique_ptr<Inte { return p->value() + 1; } + +void StdUniquePtrTestBench::printInteger2(const std::unique_ptr<Smart::Integer2> &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 <memory> class Integer; +namespace Smart { +class Integer2; +} class LIB_SMART_API StdUniquePtrTestBench { @@ -18,6 +21,7 @@ public: static std::unique_ptr<Integer> createInteger(int v = 42); static std::unique_ptr<Integer> createNullInteger(); + static void printInteger2(const std::unique_ptr<Smart::Integer2> &p); static void printInteger(const std::unique_ptr<Integer> &p); static void takeInteger(std::unique_ptr<Integer> 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 @@ <smart-pointer-type name="unique_ptr" type="unique" getter="get" value-check-method="operator bool" reset-method="reset" - instantiations="Integer,int"> + instantiations="Integer,Smart::Integer2,int"> <include file-name="memory" location="global"/> </smart-pointer-type> |
