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
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,
))
)
}
}

View File

@ -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