diff --git a/cmd/codegen/testdata/Makefile b/cmd/codegen/testdata/Makefile new file mode 100644 index 00000000..5506544b --- /dev/null +++ b/cmd/codegen/testdata/Makefile @@ -0,0 +1,4 @@ +all: + mkdir -p output + cd output; \ + go run ../.. --verbose --package mypkg --include header.h --preset ..//preset.json -lng -lg -d ../deffinitions.json -e ../structs_and_enums.json -t ../typedefs_dict.json diff --git a/cmd/codegen/testdata/deffinitions.json b/cmd/codegen/testdata/deffinitions.json new file mode 100644 index 00000000..9f418590 --- /dev/null +++ b/cmd/codegen/testdata/deffinitions.json @@ -0,0 +1,21 @@ +{ + "FuncName": { + "ov_cimguiname": "Foo", + "original_func_name": "OrgFoo", + "args": "(A a)", + "argsT": [ + { + "name": "a", + "type": "A", + "custom_type": "" + } + ], + "defaults": { + "a": "1" + }, + "nonUDT": 0, + "comment": "function comment (optional)", + "location": 15 + } +} + diff --git a/cmd/codegen/testdata/output/callbacks.go b/cmd/codegen/testdata/output/callbacks.go new file mode 100644 index 00000000..21031669 --- /dev/null +++ b/cmd/codegen/testdata/output/callbacks.go @@ -0,0 +1,11 @@ +// Code generated by cmd/codegen from https://github.com/AllenDang/cimgui-go. +// DO NOT EDIT. + +package mypkg + +// #include +// #include +// #include "wrapper.h" +// #include "typedefs.h" + +import "C" diff --git a/cmd/codegen/testdata/output/enums.go b/cmd/codegen/testdata/output/enums.go new file mode 100644 index 00000000..0b88b123 --- /dev/null +++ b/cmd/codegen/testdata/output/enums.go @@ -0,0 +1,14 @@ +// Code generated by cmd/codegen from https://github.com/AllenDang/cimgui-go. +// DO NOT EDIT. + +package mypkg + +// this is an example enum +// original name: A_ +type A int32 + +const ( + // A default value of A + ADefault A = 0 + AOne A = 1 +) diff --git a/cmd/codegen/testdata/output/funcs.go b/cmd/codegen/testdata/output/funcs.go new file mode 100644 index 00000000..fdadb4b2 --- /dev/null +++ b/cmd/codegen/testdata/output/funcs.go @@ -0,0 +1,39 @@ +// Code generated by cmd/codegen from https://github.com/AllenDang/cimgui-go. +// DO NOT EDIT. + +package mypkg + +// #include "structs_accessor.h" +// #include "wrapper.h" + +import "C" + +func (self B) SetField1(v int32) { + selfArg, selfFin := self.Handle() + defer selfFin() + C.wrap_B_SetField1(selfArg, C.int(v)) +} + +func (self *B) Field1() int32 { + selfArg, selfFin := self.Handle() + + defer func() { + selfFin() + }() + return int32(C.wrap_B_GetField1(internal.ReinterpretCast[*C.B](selfArg))) +} + +func (self B) SetField2(v A) { + selfArg, selfFin := self.Handle() + defer selfFin() + C.wrap_B_SetField2(selfArg, C.A(v)) +} + +func (self *B) Field2() A { + selfArg, selfFin := self.Handle() + + defer func() { + selfFin() + }() + return A(C.wrap_B_GetField2(internal.ReinterpretCast[*C.B](selfArg))) +} diff --git a/cmd/codegen/testdata/output/structs_accessor.cpp b/cmd/codegen/testdata/output/structs_accessor.cpp new file mode 100644 index 00000000..9c7d16f4 --- /dev/null +++ b/cmd/codegen/testdata/output/structs_accessor.cpp @@ -0,0 +1,12 @@ +// Code generated by cmd/codegen from https://github.com/AllenDang/cimgui-go. +// DO NOT EDIT. + + +#include +#include "wrapper.h" +#include "structs_accessor.h" + +void wrap_B_SetField1(B *BPtr, int v) { BPtr->Field1 = v; } +int wrap_B_GetField1(B *self) { return self->Field1; } +void wrap_B_SetField2(B *BPtr, A v) { BPtr->Field2 = v; } +A wrap_B_GetField2(B *self) { return self->Field2; } diff --git a/cmd/codegen/testdata/output/structs_accessor.h b/cmd/codegen/testdata/output/structs_accessor.h new file mode 100644 index 00000000..3f263c0c --- /dev/null +++ b/cmd/codegen/testdata/output/structs_accessor.h @@ -0,0 +1,19 @@ +// Code generated by cmd/codegen from https://github.com/AllenDang/cimgui-go. +// DO NOT EDIT. + +#pragma once + +#include "wrapper.h" + +#ifdef __cplusplus +extern "C" { +#endif + +extern void wrap_B_SetField1(B *BPtr, int v); +extern int wrap_B_GetField1(B *self); +extern void wrap_B_SetField2(B *BPtr, A v); +extern A wrap_B_GetField2(B *self); + +#ifdef __cplusplus +} +#endif diff --git a/cmd/codegen/testdata/output/typedefs.cpp b/cmd/codegen/testdata/output/typedefs.cpp new file mode 100644 index 00000000..c84473a3 --- /dev/null +++ b/cmd/codegen/testdata/output/typedefs.cpp @@ -0,0 +1,6 @@ +// Code generated by cmd/codegen from https://github.com/AllenDang/cimgui-go. +// DO NOT EDIT. + + +#include "typedefs.h" +#include "header.h" diff --git a/cmd/codegen/testdata/output/typedefs.go b/cmd/codegen/testdata/output/typedefs.go new file mode 100644 index 00000000..1170dd9b --- /dev/null +++ b/cmd/codegen/testdata/output/typedefs.go @@ -0,0 +1,37 @@ +// Code generated by cmd/codegen from https://github.com/AllenDang/cimgui-go. +// DO NOT EDIT. + +package mypkg + +// #include +// #include +// #include "wrapper.h" +// #include "typedefs.h" + +import "C" + +type B struct { + CData *C.B +} + +// Handle returns C version of B and its finalizer func. +func (self *B) Handle() (result *C.B, fin func()) { + return self.CData, func() {} +} + +// C is like Handle but returns plain type instead of pointer. +func (self B) C() (C.B, func()) { + result, fn := self.Handle() + return *result, fn +} + +// NewEmptyB creates B with its 0 value. +func NewEmptyB() *B { + return &B{CData: new(C.B)} +} + +// NewBFromC creates B from its C pointer. +// SRC ~= *C.B +func NewBFromC[SRC any](cvalue SRC) *B { + return &B{CData: internal.ReinterpretCast[*C.B](cvalue)} +} diff --git a/cmd/codegen/testdata/output/typedefs.h b/cmd/codegen/testdata/output/typedefs.h new file mode 100644 index 00000000..24b6730a --- /dev/null +++ b/cmd/codegen/testdata/output/typedefs.h @@ -0,0 +1,15 @@ +// Code generated by cmd/codegen from https://github.com/AllenDang/cimgui-go. +// DO NOT EDIT. + + +#pragma once + +#include "header.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/cmd/codegen/testdata/output/wrapper.cpp b/cmd/codegen/testdata/output/wrapper.cpp new file mode 100644 index 00000000..836615cf --- /dev/null +++ b/cmd/codegen/testdata/output/wrapper.cpp @@ -0,0 +1,6 @@ +// Code generated by cmd/codegen from https://github.com/AllenDang/cimgui-go. +// DO NOT EDIT. + +#include "wrapper.h" +#include "header.h" + diff --git a/cmd/codegen/testdata/output/wrapper.h b/cmd/codegen/testdata/output/wrapper.h new file mode 100644 index 00000000..f3f1a260 --- /dev/null +++ b/cmd/codegen/testdata/output/wrapper.h @@ -0,0 +1,15 @@ +// Code generated by cmd/codegen from https://github.com/AllenDang/cimgui-go. +// DO NOT EDIT. + +#pragma once + +#include "header.h" + +#ifdef __cplusplus +extern "C" { +#endif + + +#ifdef __cplusplus +} +#endif diff --git a/cmd/codegen/testdata/preset.json b/cmd/codegen/testdata/preset.json new file mode 100644 index 00000000..db57a49c --- /dev/null +++ b/cmd/codegen/testdata/preset.json @@ -0,0 +1,13 @@ +{ + "PackagePath": "example.com/mypkg", + "TypedefsPoolSize": 32, + "DefaultArgReplace": { + "nullptr": "0", + "NULL": "0", + "((void*)0)": "0" + }, + "DefaultArgArbitraryValue": { + "text_end": "0", + "text_end_": "0" + } +} diff --git a/cmd/codegen/testdata/structs_and_enums.json b/cmd/codegen/testdata/structs_and_enums.json new file mode 100644 index 00000000..46b025ad --- /dev/null +++ b/cmd/codegen/testdata/structs_and_enums.json @@ -0,0 +1,39 @@ +{ + "structs":{ + "B":[ + { + "name": "Field1", + "type": "int" + }, + { + "name": "Field2", + "type": "A" + } + ] + }, + "struct_comments": { + "B": { + "above": "// this is an example struct" + } + }, + "enums": { + "A_": [ + { + "calc_value": 0, + "comment": "// A default value of A", + "name": "A_Default", + "value": "0" + }, + { + "calc_value": 1, + "name": "A_One", + "value": "1 << 0" + } + ] + }, + "enum_comments": { + "A_": { + "above": "// this is an example enum" + } + } +} diff --git a/cmd/codegen/testdata/typedefs_dict.json b/cmd/codegen/testdata/typedefs_dict.json new file mode 100644 index 00000000..01008955 --- /dev/null +++ b/cmd/codegen/testdata/typedefs_dict.json @@ -0,0 +1,4 @@ +{ + "A": "int", + "B": "struct B" +}