genbindings: reorder ctors to put (QWidget* parent) version first

This commit is contained in:
mappu 2024-10-26 13:46:16 +13:00
parent 2a8b59eb76
commit ecab4f1d09
2 changed files with 39 additions and 0 deletions

View File

@ -161,6 +161,7 @@ func generate(packageName string, srcDirs []string, clangBin, cflagsCombined, ou
astTransformChildClasses(parsed) // must be first
astTransformOptional(parsed)
astTransformOverloads(parsed)
astTransformConstructorOrder(parsed)
atr.Process(parsed)
// Update global state tracker (AFTER astTransformChildClasses)

View File

@ -0,0 +1,38 @@
package main
import (
"sort"
)
// astTransformConstructorOrder creates a canonical ordering for constructors
// where the 0th entry is any entry taking solely a QWidget* parameter.
// This is so that UIC can safely assume this for types.
// @ref https://github.com/mappu/miqt/issues/46
func astTransformConstructorOrder(parsed *CppParsedHeader) {
// QFoo(QWidget* parent);
checkIsDefaultCtor := func(candidate CppMethod) bool {
return len(candidate.Parameters) == 1 &&
candidate.Parameters[0].ParameterType == "QWidget" &&
candidate.Parameters[0].Pointer
}
// Iterate all classes
for i, c := range parsed.Classes {
// Sort
sort.SliceStable(c.Ctors, func(i, j int) bool {
ic := checkIsDefaultCtor(c.Ctors[i])
jc := checkIsDefaultCtor(c.Ctors[j])
if ic && !jc {
return true
}
return false
})
// Persist mutation
parsed.Classes[i] = c
}
}