From 6fa97722c558c43bd84c30199d6f2936e851b6cf Mon Sep 17 00:00:00 2001 From: mappu Date: Tue, 19 Nov 2024 19:28:00 +1300 Subject: [PATCH] genbindings: delete either subclass or direct class --- cmd/genbindings/emitcabi.go | 17 ++++++++++------- cmd/genbindings/emitgo.go | 3 ++- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/cmd/genbindings/emitcabi.go b/cmd/genbindings/emitcabi.go index 3f996f7a..f3a433ba 100644 --- a/cmd/genbindings/emitcabi.go +++ b/cmd/genbindings/emitcabi.go @@ -733,7 +733,7 @@ extern "C" { // delete if c.CanDelete { - ret.WriteString(fmt.Sprintf("void %s_Delete(%s* self);\n", methodPrefixName, methodPrefixName)) + ret.WriteString(fmt.Sprintf("void %s_Delete(%s* self, bool isSubclass);\n", methodPrefixName, methodPrefixName)) } ret.WriteString("\n") @@ -1133,13 +1133,16 @@ func emitBindingCpp(src *CppParsedHeader, filename string) (string, error) { // Delete if c.CanDelete { - ret.WriteString(fmt.Sprintf( - "void %s_Delete(%s* self) {\n"+ - "\tdelete self;\n"+ - "}\n"+ + ret.WriteString( + "void " + methodPrefixName + "_Delete(" + methodPrefixName + "* self, bool isSubclass) {\n" + + "\tif (isSubclass) {\n" + + "\t\tdelete dynamic_cast<" + cppClassName + "*>( self );\n" + + "\t} else {\n" + + "\t\tdelete self;\n" + + "\t}\n" + + "}\n" + "\n", - methodPrefixName, methodPrefixName, - )) + ) } } diff --git a/cmd/genbindings/emitgo.go b/cmd/genbindings/emitgo.go index 905971c2..70606eaa 100644 --- a/cmd/genbindings/emitgo.go +++ b/cmd/genbindings/emitgo.go @@ -730,6 +730,7 @@ import "C" ret.WriteString(` type ` + goClassName + ` struct { h *C.` + goClassName + ` + isSubclass bool `) // Embed all inherited types to directly allow calling inherited methods @@ -1028,7 +1029,7 @@ import "C" ret.WriteString(` // Delete this object from C++ memory. func (this *` + goClassName + `) Delete() { - C.` + goClassName + `_Delete(this.h) + C.` + goClassName + `_Delete(this.h, C.bool(this.isSubclass)) } // GoGC adds a Go Finalizer to this pointer, so that it will be deleted