From 0689949bc2065f26ba4678e3da9f12c071102a9c Mon Sep 17 00:00:00 2001 From: mappu Date: Wed, 11 Sep 2024 17:40:52 +1200 Subject: [PATCH] genbindings: replace const_cast<>(self) with const self* --- cmd/genbindings/emitcabi.go | 8 +++----- cmd/genbindings/util.go | 7 +++++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/cmd/genbindings/emitcabi.go b/cmd/genbindings/emitcabi.go index 07c6f8e6..0c778b5f 100644 --- a/cmd/genbindings/emitcabi.go +++ b/cmd/genbindings/emitcabi.go @@ -460,7 +460,7 @@ extern "C" { } for _, m := range c.Methods { - ret.WriteString(fmt.Sprintf("%s %s_%s(%s);\n", emitReturnTypeCabi(m.ReturnType), cClassName, m.SafeMethodName(), emitParametersCabi(m, cClassName+"*"))) + ret.WriteString(fmt.Sprintf("%s %s_%s(%s);\n", emitReturnTypeCabi(m.ReturnType), cClassName, m.SafeMethodName(), emitParametersCabi(m, ifv(m.IsConst, "const ", "")+cClassName+"*"))) if m.IsSignal { ret.WriteString(fmt.Sprintf("%s %s_connect_%s(%s* self, void* slot);\n", emitReturnTypeCabi(m.ReturnType), cClassName, m.SafeMethodName(), cClassName)) @@ -687,8 +687,6 @@ extern "C" { if m.IsStatic { callTarget = c.ClassName + "::" - } else if m.IsConst { - callTarget = "const_cast(self)->" } if m.LinuxOnly { @@ -704,7 +702,7 @@ extern "C" { "#endif\n"+ "}\n"+ "\n", - emitReturnTypeCabi(m.ReturnType), cClassName, m.SafeMethodName(), emitParametersCabi(m, cClassName+"*"), + emitReturnTypeCabi(m.ReturnType), cClassName, m.SafeMethodName(), emitParametersCabi(m, ifv(m.IsConst, "const ", "")+cClassName+"*"), preamble, shouldReturn, callTarget, nativeMethodName, forwarding, afterCall, @@ -720,7 +718,7 @@ extern "C" { "%s"+ "}\n"+ "\n", - emitReturnTypeCabi(m.ReturnType), cClassName, m.SafeMethodName(), emitParametersCabi(m, cClassName+"*"), + emitReturnTypeCabi(m.ReturnType), cClassName, m.SafeMethodName(), emitParametersCabi(m, ifv(m.IsConst, "const ", "")+cClassName+"*"), preamble, shouldReturn, callTarget, nativeMethodName, forwarding, afterCall, diff --git a/cmd/genbindings/util.go b/cmd/genbindings/util.go index 661e1358..20a7931d 100644 --- a/cmd/genbindings/util.go +++ b/cmd/genbindings/util.go @@ -27,3 +27,10 @@ func prettyPrint(obj interface{}) { log.Println(string(jb)) } + +func ifv[T any](condition bool, trueval T, falseval T) T { + if condition { + return trueval + } + return falseval +} \ No newline at end of file