Skip to content

Commit

Permalink
Merge pull request #138 from mappu/miqt-subclass
Browse files Browse the repository at this point in the history
Remove isSubclass
  • Loading branch information
mappu authored Jan 18, 2025
2 parents 676012f + 328e80e commit 28fd54f
Show file tree
Hide file tree
Showing 3,205 changed files with 158,678 additions and 97,607 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
28 changes: 16 additions & 12 deletions cmd/genbindings/emitcabi.go
Original file line number Diff line number Diff line change
Expand Up @@ -749,14 +749,14 @@ extern "C" {
}

for _, m := range c.VirtualMethods() {
ret.WriteString(fmt.Sprintf("void %s_override_virtual_%s(%s* self, intptr_t slot);\n", methodPrefixName, m.SafeMethodName(), "void" /*methodPrefixName*/))
ret.WriteString(fmt.Sprintf("bool %s_override_virtual_%s(%s* self, intptr_t slot);\n", methodPrefixName, m.SafeMethodName(), "void" /*methodPrefixName*/))

ret.WriteString(fmt.Sprintf("%s %s_virtualbase_%s(%s);\n", m.ReturnType.RenderTypeCabi(), methodPrefixName, m.SafeMethodName(), emitParametersCabi(m, ifv(m.IsConst, "const ", "")+"void" /*methodPrefixName*/ +"*")))
}

// delete
if c.CanDelete {
ret.WriteString(fmt.Sprintf("void %s_Delete(%s* self, bool isSubclass);\n", methodPrefixName, methodPrefixName))
ret.WriteString(fmt.Sprintf("void %s_Delete(%s* self);\n", methodPrefixName, methodPrefixName))
}

ret.WriteString("\n")
Expand Down Expand Up @@ -865,7 +865,7 @@ extern "C" {

overriddenClassName := "MiqtVirtual" + strings.Replace(cppClassName, `::`, ``, -1)

ret.WriteString("class " + overriddenClassName + " : public virtual " + cppClassName + " {\n" +
ret.WriteString("class " + overriddenClassName + " final : public " + cppClassName + " {\n" +
"public:\n" +
"\n",
)
Expand All @@ -885,7 +885,7 @@ extern "C" {
)
} else {
ret.WriteString(
"\tvirtual ~" + overriddenClassName + "() = default;\n" +
"\tvirtual ~" + overriddenClassName + "() override = default;\n" +
"\n",
)
}
Expand Down Expand Up @@ -1178,8 +1178,14 @@ extern "C" {
// upclass it

ret.WriteString(
`void ` + methodPrefixName + `_override_virtual_` + m.SafeMethodName() + `(void* self, intptr_t slot) {` + "\n" +
"\tdynamic_cast<" + cppClassName + "*>( (" + cabiClassName(c.ClassName) + "*)(self) )->handle__" + m.SafeMethodName() + " = slot;\n" +
`bool ` + methodPrefixName + `_override_virtual_` + m.SafeMethodName() + `(void* self, intptr_t slot) {` + "\n" +
"\t" + cppClassName + "* self_cast = dynamic_cast<" + cppClassName + "*>( (" + cabiClassName(c.ClassName) + "*)(self) );\n" +
"\tif (self_cast == nullptr) {\n" +
"\t\treturn false;\n" +
"\t}\n" +
"\t\n" +
"\tself_cast->handle__" + m.SafeMethodName() + " = slot;\n" +
"\treturn true;\n" +
"}\n" +
"\n",
)
Expand Down Expand Up @@ -1217,14 +1223,12 @@ extern "C" {
}

// Delete
// If we subclassed, our class destructor is always virtual. Therefore
// we can delete from the self ptr without any dynamic_cast<>
if c.CanDelete {
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" +
"void " + methodPrefixName + "_Delete(" + methodPrefixName + "* self) {\n" +
"\tdelete self;\n" +
"}\n" +
"\n",
)
Expand Down
11 changes: 4 additions & 7 deletions cmd/genbindings/emitgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,6 @@ import "C"
ret.WriteString(`
type ` + goClassName + ` struct {
h *C.` + goClassName + `
isSubclass bool
`)

// Embed all inherited types to directly allow calling inherited methods
Expand Down Expand Up @@ -886,9 +885,7 @@ import "C"
// Call Cgo constructor

ret.WriteString(`
ret := new` + goClassName + `(C.` + goClassName + `_new` + maybeSuffix(i) + `(` + forwarding + `))
ret.isSubclass = true
return ret
return new` + goClassName + `(C.` + goClassName + `_new` + maybeSuffix(i) + `(` + forwarding + `))
}
`)
Expand Down Expand Up @@ -1035,10 +1032,10 @@ import "C"
goCbType += `) ` + m.ReturnType.renderReturnTypeGo(&gfs)
callbackName := cabiCallbackName(c, m)
ret.WriteString(`func (this *` + goClassName + `) On` + m.SafeMethodName() + `(slot ` + goCbType + `) {
if ! this.isSubclass {
ok := C.` + goClassName + `_override_virtual_` + m.SafeMethodName() + `(unsafe.Pointer(this.h), C.intptr_t(cgo.NewHandle(slot)) )
if !ok {
panic("miqt: can only override virtual methods for directly constructed types")
}
C.` + goClassName + `_override_virtual_` + m.SafeMethodName() + `(unsafe.Pointer(this.h), C.intptr_t(cgo.NewHandle(slot)) )
}
//export ` + callbackName + `
Expand Down Expand Up @@ -1074,7 +1071,7 @@ import "C"
ret.WriteString(`
// Delete this object from C++ memory.
func (this *` + goClassName + `) Delete() {
C.` + goClassName + `_Delete(this.h, C.bool(this.isSubclass))
C.` + goClassName + `_Delete(this.h)
}
// GoGC adds a Go Finalizer to this pointer, so that it will be deleted
Expand Down
Loading

0 comments on commit 28fd54f

Please sign in to comment.