This repository has been archived by the owner on Dec 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 101
/
WrapperConverter.go
132 lines (116 loc) · 2.84 KB
/
WrapperConverter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package imgui
// #include "wrapper/Types.h"
// #include <memory.h>
// #include <stdlib.h>
import "C"
import "unsafe"
func castBool(value bool) (cast C.IggBool) {
if value {
cast = 1
}
return
}
func wrapBool(goValue *bool) (wrapped *C.IggBool, finisher func()) {
if goValue != nil {
var cValue C.IggBool
if *goValue {
cValue = 1
}
wrapped = &cValue
finisher = func() {
*goValue = cValue != 0
}
} else {
finisher = func() {}
}
return
}
func wrapInt32(goValue *int32) (wrapped *C.int, finisher func()) {
if goValue != nil {
cValue := C.int(*goValue)
wrapped = &cValue
finisher = func() {
*goValue = int32(cValue)
}
} else {
finisher = func() {}
}
return
}
func wrapFloat(goValue *float32) (wrapped *C.float, finisher func()) {
if goValue != nil {
cValue := C.float(*goValue)
wrapped = &cValue
finisher = func() {
*goValue = float32(cValue)
}
} else {
finisher = func() {}
}
return
}
func wrapString(value string) (wrapped *C.char, finisher func()) {
wrapped = C.CString(value)
finisher = func() { C.free(unsafe.Pointer(wrapped)) } // nolint: gas
return
}
func wrapBytes(value []byte) (wrapped unsafe.Pointer, finisher func()) {
wrapped = C.CBytes(value)
finisher = func() { C.free(wrapped) } // nolint: gas
return
}
type stringBuffer struct {
ptr unsafe.Pointer
size int
}
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)
copy(buf[:zeroOffset], rawText)
buf[zeroOffset] = 0
return &stringBuffer{ptr: newPtr, size: bufSize}
}
func (buf *stringBuffer) free() {
C.free(buf.ptr)
buf.size = 0
}
func (buf *stringBuffer) resizeTo(requestedSize int) {
bufSize := requestedSize
if bufSize < 1 {
bufSize = 1
}
newPtr := C.malloc(C.size_t(bufSize))
copySize := bufSize
if copySize > buf.size {
copySize = buf.size
}
if copySize > 0 {
C.memcpy(newPtr, buf.ptr, C.size_t(copySize))
}
ptrToByteSlice(newPtr)[bufSize-1] = 0
C.free(buf.ptr)
buf.ptr = newPtr
buf.size = bufSize
}
func (buf stringBuffer) toGo() string {
if (buf.ptr == nil) || (buf.size < 1) {
return ""
}
ptrToByteSlice(buf.ptr)[buf.size-1] = 0
return C.GoString((*C.char)(buf.ptr))
}
// unrealisticLargePointer is used to cast an arbitrary native pointer to a slice.
// Its value is chosen to fit into a 32bit architecture, and still be large
// enough to cover "any" data blob. Note that this value is in bytes.
// Should an array of larger primitives be addressed, be sure to divide the value
// by the size of the elements.
const unrealisticLargePointer = 1 << 30
func ptrToByteSlice(p unsafe.Pointer) []byte {
return (*[unrealisticLargePointer]byte)(p)[:]
}
func ptrToUint16Slice(p unsafe.Pointer) []uint16 {
return (*[unrealisticLargePointer / 2]uint16)(p)[:]
}