From cd16ed9028c7ba6a355131b75bfea6a9e00dcca4 Mon Sep 17 00:00:00 2001 From: mappu Date: Tue, 20 Aug 2024 20:20:26 +1200 Subject: [PATCH] genbindings: avoid pointer copy when returning qt type by reference --- cmd/genbindings/emitcabi.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/cmd/genbindings/emitcabi.go b/cmd/genbindings/emitcabi.go index 35ef9e6..90f47a6 100644 --- a/cmd/genbindings/emitcabi.go +++ b/cmd/genbindings/emitcabi.go @@ -507,6 +507,20 @@ extern "C" { } + } else if m.ReturnType.QtClassType() && m.ReturnType.ByRef { + // It's a pointer in disguise, just needs one cast + shouldReturn = m.ReturnType.RenderTypeQtCpp() + " ret = " + afterCall += "\t// Cast returned reference into pointer\n" + if m.ReturnType.Const { + nonConst := m.ReturnType // copy + nonConst.Const = false + nonConst.ByRef = false + nonConst.Pointer = true + afterCall += "\treturn const_cast<" + nonConst.RenderTypeQtCpp() + ">(&ret);\n" + } else { + afterCall += "\treturn &ret;\n" + } + } else if m.ReturnType.QtClassType() && !m.ReturnType.Pointer { shouldReturn = m.ReturnType.ParameterType + " ret = " afterCall = "\t// Copy-construct value returned type into heap-allocated copy\n"