Skip to content

Commit

Permalink
Merge pull request #143 from gucio321/wrapper-extract
Browse files Browse the repository at this point in the history
Wrapper export
  • Loading branch information
gucio321 authored Aug 15, 2023
2 parents efad4ba + 81fa482 commit b12f5a4
Show file tree
Hide file tree
Showing 8 changed files with 1,157 additions and 1,157 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ thirdparty/glfw/build
tmp

# make setup codegen build
./codegen
/codegen
1,030 changes: 515 additions & 515 deletions cimgui_funcs.go

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions cimgui_structs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1,192 changes: 596 additions & 596 deletions cimplot_funcs.go

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions cmd/codegen/arguments_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func constCharW(arg ArgDef) ArgumentWrapperData {
return ArgumentWrapperData{
ArgType: "string",
VarName: fmt.Sprintf("%sArg", arg.Name),
ArgDef: fmt.Sprintf("%[1]sArg, %[1]sFin := wrapString(%[1]s)", arg.Name),
ArgDef: fmt.Sprintf("%[1]sArg, %[1]sFin := WrapString(%[1]s)", arg.Name),
Finalizer: fmt.Sprintf("%sFin()", arg.Name),
}
}
Expand All @@ -127,7 +127,7 @@ func charPtrPtrW(arg ArgDef) ArgumentWrapperData {
return ArgumentWrapperData{
ArgType: "[]string",
VarName: fmt.Sprintf("%sArg", arg.Name),
ArgDef: fmt.Sprintf("%[1]sArg, %[1]sFin := wrapStringList(%[1]s)", arg.Name),
ArgDef: fmt.Sprintf("%[1]sArg, %[1]sFin := WrapStringList(%[1]s)", arg.Name),
Finalizer: fmt.Sprintf("%sFin()", arg.Name),
}
}
Expand Down Expand Up @@ -161,7 +161,7 @@ func floatArrayW(arg ArgDef) ArgumentWrapperData {
func boolPtrW(arg ArgDef) ArgumentWrapperData {
return ArgumentWrapperData{
ArgType: "*bool",
ArgDef: fmt.Sprintf("%[1]sArg, %[1]sFin := wrapBool(%[1]s)", arg.Name),
ArgDef: fmt.Sprintf("%[1]sArg, %[1]sFin := WrapBool(%[1]s)", arg.Name),
Finalizer: fmt.Sprintf("%[1]sFin()", arg.Name),
VarName: fmt.Sprintf("%sArg", arg.Name),
}
Expand Down Expand Up @@ -206,7 +206,7 @@ func simplePtrW(goType, cType string) argumentWrapper {
return func(arg ArgDef) ArgumentWrapperData {
return ArgumentWrapperData{
ArgType: fmt.Sprintf("*%s", goType),
ArgDef: fmt.Sprintf("%[1]sArg, %[1]sFin := wrapNumberPtr[%[2]s, %[3]s](%[1]s)", arg.Name, cType, goType),
ArgDef: fmt.Sprintf("%[1]sArg, %[1]sFin := WrapNumberPtr[%[2]s, %[3]s](%[1]s)", arg.Name, cType, goType),
Finalizer: fmt.Sprintf("%[1]sFin()", arg.Name, cType, goType),
VarName: fmt.Sprintf("%sArg", arg.Name),
}
Expand Down
14 changes: 7 additions & 7 deletions glfw_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ func (b *GLFWBackend) SetWindowPos(x, y int) {
}

func (b *GLFWBackend) GetWindowPos() (x, y int32) {
xArg, xFin := wrapNumberPtr[C.int, int32](&x)
xArg, xFin := WrapNumberPtr[C.int, int32](&x)
defer xFin()

yArg, yFin := wrapNumberPtr[C.int, int32](&y)
yArg, yFin := WrapNumberPtr[C.int, int32](&y)
defer yFin()

C.igGLFWWindow_GetWindowPos(b.handle(), xArg, yArg)
Expand All @@ -131,10 +131,10 @@ func (b *GLFWBackend) SetWindowSize(width, height int) {
}

func (b GLFWBackend) DisplaySize() (width int32, height int32) {
widthArg, widthFin := wrapNumberPtr[C.int, int32](&width)
widthArg, widthFin := WrapNumberPtr[C.int, int32](&width)
defer widthFin()

heightArg, heightFin := wrapNumberPtr[C.int, int32](&height)
heightArg, heightFin := WrapNumberPtr[C.int, int32](&height)
defer heightFin()

C.igGLFWWindow_GetDisplaySize(b.handle(), widthArg, heightArg)
Expand All @@ -143,7 +143,7 @@ func (b GLFWBackend) DisplaySize() (width int32, height int32) {
}

func (b *GLFWBackend) SetWindowTitle(title string) {
titleArg, titleFin := wrapString(title)
titleArg, titleFin := WrapString(title)
defer titleFin()

C.igGLFWWindow_SetTitle(b.handle(), titleArg)
Expand All @@ -157,11 +157,11 @@ func (b *GLFWBackend) SetWindowSizeLimits(minWidth, minHeight, maxWidth, maxHeig
}

func (b GLFWBackend) SetShouldClose(value bool) {
C.igGLFWWindow_SetShouldClose(b.handle(), C.int(castBool(value)))
C.igGLFWWindow_SetShouldClose(b.handle(), C.int(CastBool(value)))
}

func (b *GLFWBackend) CreateWindow(title string, width, height int, flags GLFWWindowFlags) {
titleArg, titleFin := wrapString(title)
titleArg, titleFin := WrapString(title)
defer titleFin()

// b.window = uintptr(unsafe.Pointer(C.igCreateGLFWWindow(titleArg, C.int(width), C.int(height), C.GLFWWindowFlags(flags), C.VoidCallback(C.afterCreateContext))))
Expand Down
20 changes: 10 additions & 10 deletions input_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import (
type InputTextCallback func(data InputTextCallbackData) int

type inputTextInternalState struct {
buf *stringBuffer
buf *StringBuffer
callback InputTextCallback
}

func (state *inputTextInternalState) release() {
state.buf.free()
state.buf.Free()
}

//export generalInputTextCallback
Expand All @@ -32,7 +32,7 @@ func generalInputTextCallback(cbData *C.ImGuiInputTextCallbackData) C.int {
statePtr := bufHandle.Value().(*inputTextInternalState)

if data.EventFlag() == InputTextFlagsCallbackResize {
statePtr.buf.resizeTo(int(data.BufSize()))
statePtr.buf.ResizeTo(int(data.BufSize()))
C.wrap_ImGuiInputTextCallbackData_SetBuf(cbData, (*C.char)(statePtr.buf.ptr))
data.SetBufSize(int32(statePtr.buf.size))
data.SetBufTextLen(int32(data.BufTextLen()))
Expand All @@ -47,19 +47,19 @@ func generalInputTextCallback(cbData *C.ImGuiInputTextCallbackData) C.int {
}

func InputTextWithHint(label, hint string, buf *string, flags InputTextFlags, callback InputTextCallback) bool {
labelArg, labelFin := wrapString(label)
labelArg, labelFin := WrapString(label)
defer labelFin()

hintArg, hintFin := wrapString(hint)
hintArg, hintFin := WrapString(hint)
defer hintFin()

state := &inputTextInternalState{
buf: newStringBuffer(*buf),
buf: NewStringBuffer(*buf),
callback: callback,
}

defer func() {
*buf = state.buf.toGo()
*buf = state.buf.ToGo()
state.release()
}()

Expand All @@ -80,16 +80,16 @@ func InputTextWithHint(label, hint string, buf *string, flags InputTextFlags, ca
}

func InputTextMultiline(label string, buf *string, size Vec2, flags InputTextFlags, callback InputTextCallback) bool {
labelArg, labelFin := wrapString(label)
labelArg, labelFin := WrapString(label)
defer labelFin()

state := &inputTextInternalState{
buf: newStringBuffer(*buf),
buf: NewStringBuffer(*buf),
callback: callback,
}

defer func() {
*buf = state.buf.toGo()
*buf = state.buf.ToGo()
state.release()
}()

Expand Down
36 changes: 18 additions & 18 deletions type_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ package imgui

// #include <memory.h>
// #include <stdlib.h>
// #include "cimgui_wrapper.h"
// #include <stdbool.h>
import "C"
import "unsafe"

func castBool(value bool) (cast int) {
func CastBool(value bool) (cast int) {
if value {
cast = 1
}
return
}

func wrapBool(goValue *bool) (wrapped *C.bool, finisher func()) {
func WrapBool(goValue *bool) (wrapped *C.bool, finisher func()) {
if goValue != nil {
var cValue C.bool
if *goValue {
Expand All @@ -38,8 +38,8 @@ type Number interface {
~float32 | ~float64
}

// wrapPtrCType is a generic method to convert GOTYPE (int32/float32 e.t.c.) into CTYPE (c_int/c_float e.t.c.)
func wrapNumberPtr[CTYPE Number, GOTYPE Number](goValue *GOTYPE) (wrapped *CTYPE, finisher func()) {
// WrapNumberPtr is a generic method to convert GOTYPE (int32/float32 e.t.c.) into CTYPE (c_int/c_float e.t.c.)
func WrapNumberPtr[CTYPE Number, GOTYPE Number](goValue *GOTYPE) (wrapped *CTYPE, finisher func()) {
if goValue != nil {
cValue := CTYPE(*goValue)
wrapped = &cValue
Expand All @@ -53,13 +53,13 @@ func wrapNumberPtr[CTYPE Number, GOTYPE Number](goValue *GOTYPE) (wrapped *CTYPE
return
}

func wrapString(value string) (wrapped *C.char, finisher func()) {
func WrapString(value string) (wrapped *C.char, finisher func()) {
wrapped = C.CString(value)
finisher = func() { C.free(unsafe.Pointer(wrapped)) } // nolint: gas
return
}

func wrapStringList(value []string) (wrapped **C.char, finisher func()) {
func WrapStringList(value []string) (wrapped **C.char, finisher func()) {
if len(value) == 0 {
return nil, func() {}
}
Expand Down Expand Up @@ -87,37 +87,37 @@ func wrapStringList(value []string) (wrapped **C.char, finisher func()) {
// by the size of the elements.
const unrealisticLargePointer = 1 << 30

func ptrToByteSlice(p unsafe.Pointer) []byte {
func PtrToByteSlice(p unsafe.Pointer) []byte {
return (*[unrealisticLargePointer]byte)(p)[:]
}

func ptrToUint16Slice(p unsafe.Pointer) []uint16 {
func PtrToUint16Slice(p unsafe.Pointer) []uint16 {
return (*[unrealisticLargePointer / 2]uint16)(p)[:]
}

type stringBuffer struct {
type StringBuffer struct {
ptr unsafe.Pointer
size int
}

func newStringBuffer(initialValue string) *stringBuffer {
func NewStringBuffer(initialValue string) *StringBuffer {
rawText := []byte(initialValue)
bufSize := len(rawText) + 1
newPtr := C.malloc(C.size_t(bufSize))
zeroOffset := bufSize - 1
buf := ptrToByteSlice(newPtr)
buf := PtrToByteSlice(newPtr)
copy(buf[:zeroOffset], rawText)
buf[zeroOffset] = 0

return &stringBuffer{ptr: newPtr, size: bufSize}
return &StringBuffer{ptr: newPtr, size: bufSize}
}

func (buf *stringBuffer) free() {
func (buf *StringBuffer) Free() {
C.free(buf.ptr)
buf.size = 0
}

func (buf *stringBuffer) resizeTo(requestedSize int) {
func (buf *StringBuffer) ResizeTo(requestedSize int) {
bufSize := requestedSize
if bufSize < 1 {
bufSize = 1
Expand All @@ -130,16 +130,16 @@ func (buf *stringBuffer) resizeTo(requestedSize int) {
if copySize > 0 {
C.memcpy(newPtr, buf.ptr, C.size_t(copySize))
}
ptrToByteSlice(newPtr)[bufSize-1] = 0
PtrToByteSlice(newPtr)[bufSize-1] = 0
C.free(buf.ptr)
buf.ptr = newPtr
buf.size = bufSize
}

func (buf stringBuffer) toGo() string {
func (buf *StringBuffer) ToGo() string {
if (buf.ptr == nil) || (buf.size < 1) {
return ""
}
ptrToByteSlice(buf.ptr)[buf.size-1] = 0
PtrToByteSlice(buf.ptr)[buf.size-1] = 0
return C.GoString((*C.char)(buf.ptr))
}

0 comments on commit b12f5a4

Please sign in to comment.