Skip to content

Commit

Permalink
move the rest of args to the preset
Browse files Browse the repository at this point in the history
  • Loading branch information
gucio321 committed Dec 20, 2024
1 parent ee480bd commit d6dd326
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 47 deletions.
73 changes: 27 additions & 46 deletions cmd/codegen/arguments_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,55 +52,31 @@ func getArgWrapper(
"float": simpleW("float32", "C.float"),
"float*": simplePtrW("float32", "C.float"),

"time_t": simpleW("uint64", "C.xulong"),
"double": simpleW("float64", "C.double"),
"double*": simplePtrW("float64", "C.double"),

"short": simpleW("int16", "C.short"),
"short*": simplePtrW("int16", "C.short"),
"unsigned short": simpleW("uint16", "C.ushort"),
"unsigned short*": simplePtrW("uint16", "C.ushort"),
"int32_t": simpleW("int32", "C.int32_t"),
"void*": simpleW("unsafe.Pointer", "unsafe.Pointer"),
"tm": wrappableW(prefixGoPackage("Tm", "implot", context), "C.struct_tm"),
"const tm": wrappableW(prefixGoPackage("Tm", "implot", context), "C.struct_tm"),
"tm*": wrappablePtrW(prefixGoPackage("*Tm", "imgui", context), "C.struct_tm"),
"const tm*": wrappablePtrW(prefixGoPackage("*Tm", "imgui", context), "C.struct_tm"),
"int": simpleW("int32", "C.int"),
"const int": simpleW("int32", "C.int"),
"int*": simplePtrW("int32", "C.int"),
"unsigned int": simpleW("uint32", "C.uint"),
"unsigned int*": simplePtrW("uint32", "C.uint"),
"double": simpleW("float64", "C.double"),
"double*": simplePtrW("float64", "C.double"),
"const double*": simplePtrW("float64", "C.double"),
"bool": simpleW("bool", "C.bool"),
"const bool": simpleW("bool", "C.bool"),
"bool*": simplePtrW("bool", "C.bool"),
"const bool*": simplePtrW("bool", "C.bool"),

"ImWchar*": simpleW("("+prefixGoPackage("*Wchar", "imgui", context)+")", "(*C.ImWchar)"),
"const ImWchar*": simpleW("("+prefixGoPackage("*Wchar", "imgui", context)+")", "(*C.ImWchar)"),
"uintptr_t": simpleW("uintptr", "C.uintptr_t"),
"const uintptr_t": simpleW("uintptr", "C.uintptr_t"),
"const ImVec2": wrappableW(prefixGoPackage("Vec2", "imgui", context), "C.ImVec2"),
"const ImVec2*": wrappablePtrW(prefixGoPackage("*Vec2", "imgui", context), "C.ImVec2"),
"ImVec2": wrappableW(prefixGoPackage("Vec2", "imgui", context), "C.ImVec2"),
"ImVec2*": wrappablePtrW(prefixGoPackage("*Vec2", "imgui", context), "C.ImVec2"),
"ImVec2[2]": wrappablePtrArrayW(2, "C.ImVec2", prefixGoPackage("Vec2", "imgui", context)),
"const ImVec4": wrappableW(prefixGoPackage("Vec4", "imgui", context), "C.ImVec4"),
"const ImVec4*": wrappablePtrW(prefixGoPackage("*Vec4", "imgui", context), "C.ImVec4"),
"ImVec4": wrappableW(prefixGoPackage("Vec4", "imgui", context), "C.ImVec4"),
"ImVec4*": wrappablePtrW(prefixGoPackage("*Vec4", "imgui", context), "C.ImVec4"),
"ImColor": wrappableW(prefixGoPackage("Color", "imgui", context), "C.ImColor"),
"ImColor*": wrappablePtrW(prefixGoPackage("*Color", "imgui", context), "C.ImColor"),
"ImRect": wrappableW(prefixGoPackage("Rect", "imgui", context), "C.ImRect"),
"const ImRect": wrappableW(prefixGoPackage("Rect", "imgui", context), "C.ImRect"),
"ImRect*": wrappablePtrW(prefixGoPackage("*Rect", "imgui", context), "C.ImRect"),
"const ImRect*": wrappablePtrW(prefixGoPackage("*Rect", "imgui", context), "C.ImRect"),
"ImPlotPoint": wrappableW(prefixGoPackage("PlotPoint", "implot", context), "C.ImPlotPoint"),
"const ImPlotPoint": wrappableW(prefixGoPackage("PlotPoint", "implot", context), "C.ImPlotPoint"),
"ImPlotPoint*": wrappablePtrW(prefixGoPackage("*PlotPoint", "implot", context), "C.ImPlotPoint"),
"ImPlotTime": wrappableW(prefixGoPackage("PlotTime", "implot", context), "C.ImPlotTime"),
"const ImPlotTime": wrappableW(prefixGoPackage("PlotTime", "implot", context), "C.ImPlotTime"),
"ImPlotTime*": wrappablePtrW(prefixGoPackage("*PlotTime", "implot", context), "C.ImPlotTime"),
"const ImPlotTime*": wrappablePtrW(prefixGoPackage("*PlotTime", "implot", context), "C.ImPlotTime"),

"int": simpleW("int32", "C.int"),
"int32_t": simpleW("int32", "C.int32_t"),
"int*": simplePtrW("int32", "C.int"),
"unsigned int": simpleW("uint32", "C.uint"),
"unsigned int*": simplePtrW("uint32", "C.uint"),

"bool": simpleW("bool", "C.bool"),
"bool*": simplePtrW("bool", "C.bool"),

"time_t": simpleW("uint64", "C.xulong"),
"tm": wrappableW(prefixGoPackage("Tm", "implot", context), "C.struct_tm"),
"tm*": wrappablePtrW(prefixGoPackage("*Tm", "imgui", context), "C.struct_tm"),

"void*": simpleW("unsafe.Pointer", "unsafe.Pointer"),
"uintptr_t": simpleW("uintptr", "C.uintptr_t"),

"ImVec2[2]": wrappablePtrArrayW(2, "C.ImVec2", prefixGoPackage("Vec2", "imgui", context)),
}

// introduce content of preset
Expand All @@ -112,6 +88,11 @@ func getArgWrapper(
argWrapperMap[k+"*"] = simplePtrW(prefixGoPackage(v[0], v[2], context), v[1])
}

for k, v := range context.preset.WrappableTypes {
argWrapperMap[k] = wrappableW(prefixGoPackage(v[0], v[2], context), v[1])
argWrapperMap[k+"*"] = wrappablePtrW(prefixGoPackage("*"+v[0], v[2], context), v[1])
}

if a.Name == "type" || a.Name == "range" {
a.Name += "Arg"
}
Expand Down
9 changes: 9 additions & 0 deletions cmd/codegen/cimgui-go-preset.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,14 @@
"ImS32": ["int32", "C.ImS32"],
"ImS64": ["int64", "C.ImS64"]

},
"WrappableTypes": {
"ImVec2": ["Vec2", "C.ImVec2", "imgui"],
"ImVec4": ["Vec4", "C.ImVec4", "imgui"],
"ImColor": ["Color", "C.ImColor", "imgui"],
"ImRect": ["Rect", "C.ImRect", "imgui"],
"ImPlotPoint": ["PlotPoint", "C.ImPlotPoint", "implot"],
"ImPlotTime": ["PlotTime", "C.ImPlotTime", "implot"]

}
}
15 changes: 14 additions & 1 deletion cmd/codegen/presets.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type Preset struct {
// If enerated with -pkg imgui, import path
// is supposed to be "github.com/AllenDang/cimgui-go/imgui"
PackagePath string
// SimpleTypes are used for simple (go-convertable) custom types.
// SimpleTypes are used for simple (go-convertable) custom types (wrappers will be generated by simpleW/simpleR).
// Example:
// ImS16 is defined as short in C code, so Go can easily convert it via int16()
// Expected format is:
Expand All @@ -76,6 +76,19 @@ type Preset struct {
// Example:
// "ImS16": ["int16", "C.ImS16", "pkgname"]
SimplePtrTypes map[CIdentifier][3]GoIdentifier
// WrappableTypes are types that implement a special interface
// github.com/AllenDang/cimgui-go/internal.WrappableType[CTYPE, SELF]
// In short they are supposed to have 2 methods:
// - ToC() CTYPE which returns SELF converted to CTYPE
// - FromC(CTYPE) SELF which restores SELF from CTYPE.
//
// Key is a C type name, value is a list:
// - Go name
// - C name
// - package where the type is defined
// Example syntax:
// "ImVec2": ["Vec2", "C.ImVec2", "imgui"]
WrappableTypes map[CIdentifier][3]GoIdentifier
}

func (p *Preset) MergeCGoPreamble() string {
Expand Down

0 comments on commit d6dd326

Please sign in to comment.