genbindings: delete either subclass or direct class

This commit is contained in:
mappu 2024-11-19 19:28:00 +13:00
parent fb56258334
commit 6fa97722c5
2 changed files with 12 additions and 8 deletions

View File

@ -733,7 +733,7 @@ extern "C" {
// delete // delete
if c.CanDelete { 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") ret.WriteString("\n")
@ -1133,13 +1133,16 @@ func emitBindingCpp(src *CppParsedHeader, filename string) (string, error) {
// Delete // Delete
if c.CanDelete { if c.CanDelete {
ret.WriteString(fmt.Sprintf( ret.WriteString(
"void %s_Delete(%s* self) {\n"+ "void " + methodPrefixName + "_Delete(" + methodPrefixName + "* self, bool isSubclass) {\n" +
"\tdelete self;\n"+ "\tif (isSubclass) {\n" +
"}\n"+ "\t\tdelete dynamic_cast<" + cppClassName + "*>( self );\n" +
"\t} else {\n" +
"\t\tdelete self;\n" +
"\t}\n" +
"}\n" +
"\n", "\n",
methodPrefixName, methodPrefixName, )
))
} }
} }

View File

@ -730,6 +730,7 @@ import "C"
ret.WriteString(` ret.WriteString(`
type ` + goClassName + ` struct { type ` + goClassName + ` struct {
h *C.` + goClassName + ` h *C.` + goClassName + `
isSubclass bool
`) `)
// Embed all inherited types to directly allow calling inherited methods // Embed all inherited types to directly allow calling inherited methods
@ -1028,7 +1029,7 @@ import "C"
ret.WriteString(` ret.WriteString(`
// Delete this object from C++ memory. // Delete this object from C++ memory.
func (this *` + goClassName + `) Delete() { 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 // GoGC adds a Go Finalizer to this pointer, so that it will be deleted