From 490753a1ff1fc523391914468c5ffc4ce9cab90c Mon Sep 17 00:00:00 2001 From: gucio321 Date: Fri, 20 Dec 2024 10:55:38 +0100 Subject: [PATCH] gencpp: clean code; move more things to preset --- cmd/codegen/cimgui-go-preset.json | 11 +++++++++-- cmd/codegen/gencpp.go | 12 ++---------- cmd/codegen/presets.go | 4 ++++ 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/cmd/codegen/cimgui-go-preset.json b/cmd/codegen/cimgui-go-preset.json index 692685a3..97695c25 100644 --- a/cmd/codegen/cimgui-go-preset.json +++ b/cmd/codegen/cimgui-go-preset.json @@ -76,8 +76,15 @@ "TextEditor_GetText": "TextEditor_GetText_alloc" }, "DefaultArgReplace": { - "FLT_MIN": "igGET_FLT_MIN()", - "FLT_MAX": "igGET_FLT_MAX()" + "FLT_MIN": "igGET_FLT_MIN()", + "FLT_MAX": "igGET_FLT_MAX()", + "nullptr": "0", + "NULL": "0", + "((void*)0)": "0" + }, + "DefaultArgArbitraryValue": { + "text_end": 0, + "text_end_": "0" }, "ExtraCGOPreamble": [ "#include \"../imgui/extra_types.h\"" diff --git a/cmd/codegen/gencpp.go b/cmd/codegen/gencpp.go index 4206a258..1ba4d232 100644 --- a/cmd/codegen/gencpp.go +++ b/cmd/codegen/gencpp.go @@ -239,16 +239,8 @@ extern "C" { v = string(r) } - if v == "((void*)0)" { - v = "NULL" - } - - if v == "nullptr" || v == "NULL" { - v = "0" - } - - if k == "text_end" || k == "text_end_" { - v = "0" + if r, ok := ctx.preset.DefaultArgArbitraryValue[CIdentifier(k)]; ok { + v = string(r) } if strings.Contains(invocationStmt, ","+k) { diff --git a/cmd/codegen/presets.go b/cmd/codegen/presets.go index c93307d2..7a30ea8d 100644 --- a/cmd/codegen/presets.go +++ b/cmd/codegen/presets.go @@ -39,6 +39,10 @@ type Preset struct { // cimgui-go uses this to change FLT_MIN with igGet_FLT_MIN() // NOTE: Iterated randomly! DefaultArgReplace map[CIdentifier]CIdentifier + // DefaultArgArbitraryValue is similar to the above DefaultArgReplace, but + // associates default arg name with any arbitrary value. + // cimgui-go uses this to set text_end to 0 + DefaultArgArbitraryValue map[CIdentifier]CIdentifier // ExtraCGOPreamble allows to specify additional C code elements in Go files. // For example could be used to include extra files. // For ease of use, its in form of []string. These lines will be merged and prefixed (if appliable) with '//'