├── imgui ├── cimgui.cpp ├── imgui.cpp ├── cgo_helpers.h ├── imgui.h ├── cimgui.h ├── imgui.go ├── types.go └── cgo_helpers.go ├── cmd └── main.go ├── Makefile ├── .gitignore └── imgui.yml /imgui/cimgui.cpp: -------------------------------------------------------------------------------- 1 | #include "imgui.h" 2 | #include "cimgui.h" 3 | 4 | CIMGUI_API void foo(ImVec2 *out) { 5 | *out = ImGui::foo(); 6 | } 7 | -------------------------------------------------------------------------------- /imgui/imgui.cpp: -------------------------------------------------------------------------------- 1 | #include "imgui.h" 2 | 3 | namespace ImGui 4 | { 5 | 6 | IMGUI_API ImVec2 foo() { 7 | return ImVec2(1337,1337); 8 | } 9 | 10 | } -------------------------------------------------------------------------------- /cmd/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "github.com/Xzya/go-bindings-test/imgui" 6 | ) 7 | 8 | func main() { 9 | v := new(imgui.ImVec2) 10 | imgui.Foo(v) 11 | fmt.Printf("%#v\n", v) 12 | } 13 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | c-for-go imgui.yml 3 | 4 | clean: 5 | rm -f imgui/cgo_helpers.go imgui/cgo_helpers.h imgui/cgo_helpers.c 6 | rm -f imgui/const.go imgui/doc.go imgui/types.go 7 | rm -f imgui/imgui.go 8 | 9 | test: 10 | cd foobar && go build -------------------------------------------------------------------------------- /imgui/cgo_helpers.h: -------------------------------------------------------------------------------- 1 | // WARNING: This file has automatically been generated on Thu, 15 Feb 2018 23:59:40 EET. 2 | // By https://git.io/c-for-go. DO NOT EDIT. 3 | 4 | #include "cimgui.h" 5 | #include 6 | #pragma once 7 | 8 | #define __CGOGEN 1 9 | 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, build with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | 14 | .idea -------------------------------------------------------------------------------- /imgui/imgui.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #ifndef IMGUI_API 4 | #define IMGUI_API 5 | #endif 6 | 7 | struct ImVec2 8 | { 9 | float x, y; 10 | ImVec2() { x = y = 0.0f; } 11 | ImVec2(float _x, float _y) { x = _x; y = _y; } 12 | }; 13 | 14 | namespace ImGui 15 | { 16 | IMGUI_API ImVec2 foo(); 17 | } -------------------------------------------------------------------------------- /imgui/cimgui.h: -------------------------------------------------------------------------------- 1 | #define API 2 | 3 | #if defined __cplusplus 4 | #define EXTERN extern "C" 5 | #else 6 | #define EXTERN extern 7 | #endif 8 | 9 | #define CIMGUI_API EXTERN API 10 | 11 | struct ImVec2; 12 | 13 | #ifdef CIMGUI_DEFINE_ENUMS_AND_STRUCTS 14 | struct ImVec2 15 | { 16 | float x, y; 17 | }; 18 | #endif 19 | 20 | CIMGUI_API void foo(struct ImVec2 *out); -------------------------------------------------------------------------------- /imgui/imgui.go: -------------------------------------------------------------------------------- 1 | // WARNING: This file has automatically been generated on Thu, 15 Feb 2018 23:59:40 EET. 2 | // By https://git.io/c-for-go. DO NOT EDIT. 3 | 4 | package imgui 5 | 6 | /* 7 | #include "cimgui.h" 8 | #include 9 | #include "cgo_helpers.h" 10 | */ 11 | import "C" 12 | 13 | // Foo function as declared in imgui/cimgui.h:20 14 | func Foo(out *ImVec2) { 15 | cout, _ := out.PassRef() 16 | C.foo(cout) 17 | } 18 | -------------------------------------------------------------------------------- /imgui/types.go: -------------------------------------------------------------------------------- 1 | // WARNING: This file has automatically been generated on Thu, 15 Feb 2018 23:59:40 EET. 2 | // By https://git.io/c-for-go. DO NOT EDIT. 3 | 4 | package imgui 5 | 6 | /* 7 | #include "cimgui.h" 8 | #include 9 | #include "cgo_helpers.h" 10 | */ 11 | import "C" 12 | 13 | // ImVec2 as declared in imgui/cimgui.h:11 14 | type ImVec2 struct { 15 | X float32 16 | Y float32 17 | ref74e98a33 *C.struct_ImVec2 18 | allocs74e98a33 interface{} 19 | } 20 | -------------------------------------------------------------------------------- /imgui.yml: -------------------------------------------------------------------------------- 1 | --- 2 | GENERATOR: 3 | PackageName: imgui 4 | PackageDescription: 5 | PackageLicense: 6 | Includes: ["cimgui.h"] 7 | Options: 8 | SafeStrings: true 9 | 10 | PARSER: 11 | IncludePaths: ["/usr/local/include", "/usr/include", "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/8.0.0/include/"] 12 | SourcesPaths: ["imgui/cimgui.h"] 13 | Defines: 14 | CIMGUI_DEFINE_ENUMS_AND_STRUCTS: 1 15 | 16 | TRANSLATOR: 17 | ConstRules: 18 | defines: expand 19 | enum: expand 20 | PtrTips: 21 | function: 22 | - {target: foo, tips: [ref]} 23 | Rules: 24 | global: 25 | - {action: accept, from: 'ImVec2'} 26 | - {action: accept, from: 'foo'} 27 | - {transform: export} 28 | private: 29 | - {transform: unexport} 30 | post-global: 31 | - {action: replace, from: _$} 32 | - {load: snakecase} 33 | -------------------------------------------------------------------------------- /imgui/cgo_helpers.go: -------------------------------------------------------------------------------- 1 | // WARNING: This file has automatically been generated on Thu, 15 Feb 2018 23:59:40 EET. 2 | // By https://git.io/c-for-go. DO NOT EDIT. 3 | 4 | package imgui 5 | 6 | /* 7 | #include "cimgui.h" 8 | #include 9 | #include "cgo_helpers.h" 10 | */ 11 | import "C" 12 | import ( 13 | "sync" 14 | "unsafe" 15 | ) 16 | 17 | // cgoAllocMap stores pointers to C allocated memory for future reference. 18 | type cgoAllocMap struct { 19 | mux sync.RWMutex 20 | m map[unsafe.Pointer]struct{} 21 | } 22 | 23 | var cgoAllocsUnknown = new(cgoAllocMap) 24 | 25 | func (a *cgoAllocMap) Add(ptr unsafe.Pointer) { 26 | a.mux.Lock() 27 | if a.m == nil { 28 | a.m = make(map[unsafe.Pointer]struct{}) 29 | } 30 | a.m[ptr] = struct{}{} 31 | a.mux.Unlock() 32 | } 33 | 34 | func (a *cgoAllocMap) IsEmpty() bool { 35 | a.mux.RLock() 36 | isEmpty := len(a.m) == 0 37 | a.mux.RUnlock() 38 | return isEmpty 39 | } 40 | 41 | func (a *cgoAllocMap) Borrow(b *cgoAllocMap) { 42 | if b == nil || b.IsEmpty() { 43 | return 44 | } 45 | b.mux.Lock() 46 | a.mux.Lock() 47 | for ptr := range b.m { 48 | if a.m == nil { 49 | a.m = make(map[unsafe.Pointer]struct{}) 50 | } 51 | a.m[ptr] = struct{}{} 52 | delete(b.m, ptr) 53 | } 54 | a.mux.Unlock() 55 | b.mux.Unlock() 56 | } 57 | 58 | func (a *cgoAllocMap) Free() { 59 | a.mux.Lock() 60 | for ptr := range a.m { 61 | C.free(ptr) 62 | delete(a.m, ptr) 63 | } 64 | a.mux.Unlock() 65 | } 66 | 67 | // allocStructImVec2Memory allocates memory for type C.struct_ImVec2 in C. 68 | // The caller is responsible for freeing the this memory via C.free. 69 | func allocStructImVec2Memory(n int) unsafe.Pointer { 70 | mem, err := C.calloc(C.size_t(n), (C.size_t)(sizeOfStructImVec2Value)) 71 | if err != nil { 72 | panic("memory alloc error: " + err.Error()) 73 | } 74 | return mem 75 | } 76 | 77 | const sizeOfStructImVec2Value = unsafe.Sizeof([1]C.struct_ImVec2{}) 78 | 79 | // Ref returns the underlying reference to C object or nil if struct is nil. 80 | func (x *ImVec2) Ref() *C.struct_ImVec2 { 81 | if x == nil { 82 | return nil 83 | } 84 | return x.ref74e98a33 85 | } 86 | 87 | // Free invokes alloc map's free mechanism that cleanups any allocated memory using C free. 88 | // Does nothing if struct is nil or has no allocation map. 89 | func (x *ImVec2) Free() { 90 | if x != nil && x.allocs74e98a33 != nil { 91 | x.allocs74e98a33.(*cgoAllocMap).Free() 92 | x.ref74e98a33 = nil 93 | } 94 | } 95 | 96 | // NewImVec2Ref creates a new wrapper struct with underlying reference set to the original C object. 97 | // Returns nil if the provided pointer to C object is nil too. 98 | func NewImVec2Ref(ref unsafe.Pointer) *ImVec2 { 99 | if ref == nil { 100 | return nil 101 | } 102 | obj := new(ImVec2) 103 | obj.ref74e98a33 = (*C.struct_ImVec2)(unsafe.Pointer(ref)) 104 | return obj 105 | } 106 | 107 | // PassRef returns the underlying C object, otherwise it will allocate one and set its values 108 | // from this wrapping struct, counting allocations into an allocation map. 109 | func (x *ImVec2) PassRef() (*C.struct_ImVec2, *cgoAllocMap) { 110 | if x == nil { 111 | return nil, nil 112 | } else if x.ref74e98a33 != nil { 113 | return x.ref74e98a33, nil 114 | } 115 | mem74e98a33 := allocStructImVec2Memory(1) 116 | ref74e98a33 := (*C.struct_ImVec2)(mem74e98a33) 117 | allocs74e98a33 := new(cgoAllocMap) 118 | allocs74e98a33.Add(mem74e98a33) 119 | 120 | var cx_allocs *cgoAllocMap 121 | ref74e98a33.x, cx_allocs = (C.float)(x.X), cgoAllocsUnknown 122 | allocs74e98a33.Borrow(cx_allocs) 123 | 124 | var cy_allocs *cgoAllocMap 125 | ref74e98a33.y, cy_allocs = (C.float)(x.Y), cgoAllocsUnknown 126 | allocs74e98a33.Borrow(cy_allocs) 127 | 128 | x.ref74e98a33 = ref74e98a33 129 | x.allocs74e98a33 = allocs74e98a33 130 | return ref74e98a33, allocs74e98a33 131 | 132 | } 133 | 134 | // PassValue does the same as PassRef except that it will try to dereference the returned pointer. 135 | func (x ImVec2) PassValue() (C.struct_ImVec2, *cgoAllocMap) { 136 | if x.ref74e98a33 != nil { 137 | return *x.ref74e98a33, nil 138 | } 139 | ref, allocs := x.PassRef() 140 | return *ref, allocs 141 | } 142 | 143 | // Deref uses the underlying reference to C object and fills the wrapping struct with values. 144 | // Do not forget to call this method whether you get a struct for C object and want to read its values. 145 | func (x *ImVec2) Deref() { 146 | if x.ref74e98a33 == nil { 147 | return 148 | } 149 | x.X = (float32)(x.ref74e98a33.x) 150 | x.Y = (float32)(x.ref74e98a33.y) 151 | } 152 | --------------------------------------------------------------------------------