genbindings: inheritance support

This commit is contained in:
mappu 2024-08-10 12:54:26 +12:00
parent 55481316ed
commit afcde0a69b
3 changed files with 35 additions and 2 deletions

View File

@ -76,6 +76,26 @@ func processClassType(node map[string]interface{}, className string) (CppClass,
} }
} }
// Check if this (publicly) inherits another class
if bases, ok := node["bases"].([]interface{}); ok {
for _, base := range bases {
base, ok := base.(map[string]interface{})
if !ok {
continue
}
access, ok := base["access"].(string)
if !(ok && access == "public") {
continue
}
if typ, ok := base["type"].(map[string]interface{}); ok {
if qualType, ok := typ["qualType"].(string); ok {
ret.Inherits = append(ret.Inherits, qualType)
}
}
}
}
// Parse all methods // Parse all methods

View File

@ -93,6 +93,14 @@ import "C"
ret.WriteString(` ret.WriteString(`
type ` + c.ClassName + ` struct { type ` + c.ClassName + ` struct {
h C.P` + c.ClassName + ` h C.P` + c.ClassName + `
`)
// Embed all inherited types to directly allow calling inherited methods
for _, base := range c.Inherits {
ret.WriteString(base + "\n")
}
ret.WriteString(`
} }
func (this *` + c.ClassName + `) cPointer() C.P` + c.ClassName + ` { func (this *` + c.ClassName + `) cPointer() C.P` + c.ClassName + ` {
@ -104,13 +112,18 @@ import "C"
`) `)
localInit := "h: ret"
for _, base := range c.Inherits {
localInit += ", " + base + ": " + base + "{h: ret}"
}
for i, ctor := range c.Ctors { for i, ctor := range c.Ctors {
preamble, forwarding := emitParametersGo2CABIForwarding(ctor) preamble, forwarding := emitParametersGo2CABIForwarding(ctor)
ret.WriteString(` ret.WriteString(`
// New` + c.ClassName + maybeSuffix(i) + ` constructs a new ` + c.ClassName + ` object. // New` + c.ClassName + maybeSuffix(i) + ` constructs a new ` + c.ClassName + ` object.
func New` + c.ClassName + maybeSuffix(i) + `(` + emitParametersGo(ctor.Parameters) + `) { func New` + c.ClassName + maybeSuffix(i) + `(` + emitParametersGo(ctor.Parameters) + `) {
` + preamble + ` ret := C.` + c.ClassName + `_new` + maybeSuffix(i) + `(` + forwarding + `) ` + preamble + ` ret := C.` + c.ClassName + `_new` + maybeSuffix(i) + `(` + forwarding + `)
return &` + c.ClassName + `{h: ret} return &` + c.ClassName + `{` + localInit + `}
} }
`) `)

View File

@ -71,7 +71,7 @@ type CppClass struct {
ClassName string ClassName string
Abstract bool Abstract bool
Ctors []CppMethod // only use the parameters Ctors []CppMethod // only use the parameters
Extends []string Inherits []string // other class names
Methods []CppMethod Methods []CppMethod
Props []CppProperty Props []CppProperty
} }