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, `, `)
}
func emitParameterTypesCpp(m CppMethod) string {
func emitParameterTypesCpp(m CppMethod, includeHidden bool) string {
tmp := make([]string, 0, len(m.Parameters))
for _, p := range m.Parameters {
tmp = append(tmp, p.RenderTypeQtCpp())
}
if includeHidden {
for _, p := range m.HiddenParams {
tmp = append(tmp, p.RenderTypeQtCpp())
}
}
return strings.Join(tmp, `, `)
}
@ -457,7 +462,7 @@ extern "C" {
for _, m := range c.Methods {
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))
}
}
@ -723,8 +728,10 @@ extern "C" {
}
if m.IsSignal && !m.HasHiddenParams {
exactSignal := `static_cast<void (` + c.ClassName + `::*)(` + emitParameterTypesCpp(m) + `)>(&` + c.ClassName + `::` + nativeMethodName + `)`
if m.IsSignal {
// 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(
`void ` + cClassName + `_connect_` + m.SafeMethodName() + `(` + cClassName + `* self, void* slot) {` + "\n" +

View File

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

View File

@ -187,7 +187,7 @@ type CppMethod struct {
IsStatic bool
IsSignal 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
}

View File

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