genbindings: generate signal connection handlers for overloads

This commit is contained in:
mappu 2024-09-07 15:25:25 +12:00
parent 9d8153c4d3
commit d6d4afb4f2
4 changed files with 24 additions and 25 deletions

View File

@ -119,11 +119,16 @@ func emitParametersCpp(m CppMethod) string {
return strings.Join(tmp, `, `) return strings.Join(tmp, `, `)
} }
func emitParameterTypesCpp(m CppMethod) string { func emitParameterTypesCpp(m CppMethod, includeHidden bool) string {
tmp := make([]string, 0, len(m.Parameters)) tmp := make([]string, 0, len(m.Parameters))
for _, p := range m.Parameters { for _, p := range m.Parameters {
tmp = append(tmp, p.RenderTypeQtCpp()) tmp = append(tmp, p.RenderTypeQtCpp())
} }
if includeHidden {
for _, p := range m.HiddenParams {
tmp = append(tmp, p.RenderTypeQtCpp())
}
}
return strings.Join(tmp, `, `) return strings.Join(tmp, `, `)
} }
@ -457,7 +462,7 @@ extern "C" {
for _, m := range c.Methods { for _, m := range c.Methods {
ret.WriteString(fmt.Sprintf("%s %s_%s(%s);\n", emitReturnTypeCabi(m.ReturnType), cClassName, m.SafeMethodName(), emitParametersCabi(m, cClassName+"*"))) ret.WriteString(fmt.Sprintf("%s %s_%s(%s);\n", emitReturnTypeCabi(m.ReturnType), cClassName, m.SafeMethodName(), emitParametersCabi(m, cClassName+"*")))
if m.IsSignal && !m.HasHiddenParams { if m.IsSignal {
ret.WriteString(fmt.Sprintf("%s %s_connect_%s(%s* self, void* slot);\n", emitReturnTypeCabi(m.ReturnType), cClassName, m.SafeMethodName(), cClassName)) ret.WriteString(fmt.Sprintf("%s %s_connect_%s(%s* self, void* slot);\n", emitReturnTypeCabi(m.ReturnType), cClassName, m.SafeMethodName(), cClassName))
} }
} }
@ -723,8 +728,10 @@ extern "C" {
} }
if m.IsSignal && !m.HasHiddenParams { if m.IsSignal {
exactSignal := `static_cast<void (` + c.ClassName + `::*)(` + emitParameterTypesCpp(m) + `)>(&` + c.ClassName + `::` + nativeMethodName + `)` // If there are hidden parameters, the type of the signal itself
// needs to include them
exactSignal := `static_cast<void (` + c.ClassName + `::*)(` + emitParameterTypesCpp(m, true) + `)>(&` + c.ClassName + `::` + nativeMethodName + `)`
ret.WriteString( ret.WriteString(
`void ` + cClassName + `_connect_` + m.SafeMethodName() + `(` + cClassName + `* self, void* slot) {` + "\n" + `void ` + cClassName + `_connect_` + m.SafeMethodName() + `(` + cClassName + `* self, void* slot) {` + "\n" +

View File

@ -540,7 +540,7 @@ import "C"
} }
// Add Connect() wrappers for signal functions // Add Connect() wrappers for signal functions
if m.IsSignal && !m.HasHiddenParams { if m.IsSignal {
gfs.imports["unsafe"] = struct{}{} gfs.imports["unsafe"] = struct{}{}
gfs.imports["runtime/cgo"] = struct{}{} gfs.imports["runtime/cgo"] = struct{}{}
ret.WriteString(`func (this *` + goClassName + `) On` + m.SafeMethodName() + `(slot func()) { ret.WriteString(`func (this *` + goClassName + `) On` + m.SafeMethodName() + `(slot func()) {

View File

@ -187,7 +187,7 @@ type CppMethod struct {
IsStatic bool IsStatic bool
IsSignal bool IsSignal bool
IsConst bool IsConst bool
HasHiddenParams bool // Set to true if there is an overload with more parameters HiddenParams []CppParameter // Populated if there is an overload with more parameters
LinuxOnly bool LinuxOnly bool
} }

View File

@ -32,23 +32,19 @@ func astTransformOptional(parsed *CppParsedHeader) {
// Add method copies // Add method copies
for x := optionalStart; x < len(m.Parameters); x++ { for x := optionalStart; x < len(m.Parameters); x++ {
dupMethod := CppMethod{ dupMethod := m // copy
MethodName: m.MethodName + fmt.Sprintf("%d", x+1), dupMethod.MethodName = m.MethodName + fmt.Sprintf("%d", x+1)
OverrideMethodName: m.MethodName, dupMethod.OverrideMethodName = m.MethodName
ReturnType: m.ReturnType, dupMethod.Parameters = m.Parameters[0 : x+1]
Parameters: nil, dupMethod.HiddenParams = m.Parameters[x+1:]
IsSignal: m.IsSignal,
IsStatic: m.IsStatic,
HasHiddenParams: (x != len(m.Parameters)-1),
}
dupMethod.Parameters = append(dupMethod.Parameters, m.Parameters[0:x+1]...)
c.Methods = append(c.Methods, dupMethod) // TODO can we insert them next, instead of at the end? c.Methods = append(c.Methods, dupMethod) // TODO can we insert them next, instead of at the end?
} }
// Truncate the original method's parameters to only the // Truncate the original method's parameters to only the
// mandatory ones // mandatory ones
m.HiddenParams = m.Parameters[optionalStart:]
m.Parameters = m.Parameters[0:optionalStart] m.Parameters = m.Parameters[0:optionalStart]
m.HasHiddenParams = true
c.Methods[j] = m c.Methods[j] = m
} }
@ -73,20 +69,16 @@ func astTransformOptional(parsed *CppParsedHeader) {
// Add ctor copies // Add ctor copies
for x := optionalStart; x < len(m.Parameters); x++ { for x := optionalStart; x < len(m.Parameters); x++ {
dupCtor := CppMethod{ dupCtor := m // copy
ReturnType: m.ReturnType, dupCtor.Parameters = m.Parameters[0 : x+1]
Parameters: nil, dupCtor.HiddenParams = m.Parameters[x+1:]
IsStatic: m.IsStatic,
HasHiddenParams: (x != len(m.Parameters)-1),
}
dupCtor.Parameters = append(dupCtor.Parameters, m.Parameters[0:x+1]...)
c.Ctors = append(c.Ctors, dupCtor) c.Ctors = append(c.Ctors, dupCtor)
} }
// Truncate the original ctor's parameters to only the // Truncate the original ctor's parameters to only the
// mandatory ones // mandatory ones
m.HiddenParams = m.Parameters[optionalStart:]
m.Parameters = m.Parameters[0:optionalStart] m.Parameters = m.Parameters[0:optionalStart]
m.HasHiddenParams = true
c.Ctors[j] = m c.Ctors[j] = m
} }