/
callback_inherit.go
327 lines (301 loc) · 9.46 KB
/
callback_inherit.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package qtrt
/*
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
extern uint64_t callbackAllInherits(void* cbobj, char* iname, int*handled, int argc, uint64_t p0, uint64_t p1, uint64_t p2, uint64_t p3, uint64_t p4, uint64_t p5, uint64_t p6, uint64_t p7, uint64_t p8, uint64_t p9);
*/
import "C"
import (
"fmt"
"log"
"os"
"reflect"
"regexp"
"strings"
"unsafe"
qt "github.com/kitech/qt.go/qtqt"
)
/*
*/
// depcreated
func SetInheritCallback2c(name string, fnptr unsafe.Pointer) {
symname := fmt.Sprintf("set_callback%s", name)
InvokeQtFunc6(symname, FFI_TYPE_VOID, fnptr)
}
func SetAllInheritCallback(cbobj CObjectITF, name string, f interface{}) {
if cbobj.GetCthis() != nil {
} else { // take as global function call, just no this field
if debugDynSlot {
log.Println("obj is nil, try as global event", name)
}
}
if signal := qt.LendSignal(cbobj.GetCthis(), name); signal != nil {
log.Println("already exists:", name, reflect.TypeOf(cbobj))
qt.ConnectSignal(cbobj.GetCthis(), name, func(args ...uint64) {
signal.(func(...uint64))(args...)
callbackInheritInvokeGo(f, args...)
})
} else {
qt.ConnectSignal(cbobj.GetCthis(), name, f)
}
}
func UnsetAllInheritCallback(cbobj CObjectITF, name string) {
if signal := qt.LendSignal(cbobj.GetCthis(), name); signal != nil {
qt.DisconnectSignal(cbobj.GetCthis(), name)
} else {
log.Println("not exists:", cbobj, name)
}
}
func UnsetAllInheritCallbackAll(cbobj CObjectITF) {
qt.DisconnectObject(cbobj.GetCthis())
}
// depcreated
// 在go中的projected继承代理方法调用
func CallbackAllInherits(cbobj unsafe.Pointer, name string, args ...interface{}) interface{} {
if debugDynSlot {
log.Println(name, cbobj, len(args), args)
}
if signal := qt.GetSignal(cbobj, name); signal != nil {
return signal.(func(...interface{}) interface{})(args...)
} else {
// NilPrint(nil, "not found sigobj:", cbobj, name, args)
}
return nil
}
// depcreated
func callbackInheritInvoke(f interface{}, args ...interface{}) interface{} {
fv := reflect.ValueOf(f)
// signal函数的参数类型一般还是比较简单的,像整数,布尔,QString
in := []reflect.Value{} // 构造这个call in
for i := 0; i < fv.Type().NumIn(); i++ {
argty := fv.Type().In(i)
if debugDynSlot {
log.Println(i, argty.String(), argty.Kind())
log.Println(i, args[i], reflect.TypeOf(args[i]).String())
}
switch argty.Kind() {
case reflect.Int:
in = append(in, reflect.ValueOf(args[i]))
case reflect.Bool:
in = append(in, reflect.ValueOf(args[i]))
case reflect.Ptr:
argd1ty := argty.Elem()
switch argd1ty.Kind() {
case reflect.Int:
case reflect.Ptr:
default:
reg := regexp.MustCompile(`^(qt.*\.)?Q[A-Z](.+)`)
if reg.MatchString(argd1ty.String()) {
if debugDynSlot {
log.Println("qt class pointer:")
}
prmval := reflect.New(argd1ty)
prmval.MethodByName("SetCthis").Call([]reflect.Value{reflect.ValueOf(args[i])})
in = append(in, prmval)
}
}
default:
log.Println("Unsupported:", argty.Kind().String(), argty.String())
}
}
if debugDynSlot {
log.Println(len(in), in)
}
if len(in) != fv.Type().NumIn() {
log.Println("can not fill enough parameters,", len(in), fv.Type().NumIn())
}
Assert(len(in) == fv.Type().NumIn(), "no engouth parameters")
if true {
out := fv.Call(in)
if debugDynSlot {
log.Println(len(out), out)
}
if len(out) > 0 {
return out[0].Interface()
}
}
return nil
}
// depcreated
func callbackInheritInvokeDefault(f interface{}, args ...interface{}) interface{} {
return nil
}
func setAllInheritCallback2c(name string, fnptr unsafe.Pointer) {
symname := fmt.Sprintf("set_callback%s", name)
InvokeQtFunc6(symname, FFI_TYPE_VOID, fnptr)
}
func init_callack_inherit() { setAllInheritCallback2c("AllInherits", C.callbackAllInherits) }
// 在C绑定中的projected继承代理方法直接调用
//export callbackAllInherits
func callbackAllInherits(cbobj unsafe.Pointer, iname *C.char, handled *C.int, argc C.int, p0, p1, p2, p3, p4, p5, p6, p7, p8, p9 C.uint64_t) C.uint64_t {
name_ := C.GoString(iname)
var handled_ int
rv := C.uint64_t(callbackAllInheritsGo(cbobj, name_, &handled_, int(argc),
uint64(p0), uint64(p1), uint64(p2), uint64(p3), uint64(p4),
uint64(p5), uint64(p6), uint64(p7), uint64(p8), uint64(p9)))
*handled = C.int(handled_)
return C.uint64_t(rv)
}
// return true to show inherit debug info
/*
Example:
debugAllInheritsFilterFunc = func(name string) bool {
return name != "event"
}
*/
var debugAllInheritsFilterFunc func(name string) bool
var debugIneritCall bool = false // call from C++ to Go
func init() {
dbgval := os.Getenv("QTGO_DEBUG_INHERIT_CALL")
if strings.ToLower(dbgval) == "true" || dbgval == "1" {
debugIneritCall = true
}
}
func SetDebugAllinheritsFilterFunc(f func(name string) bool) {
debugAllInheritsFilterFunc = f
}
func callbackAllInheritsGo(cbobj unsafe.Pointer, name string, handled *int, argc int, pargs ...uint64) uint64 {
needDebug := false
if debugAllInheritsFilterFunc != nil && debugAllInheritsFilterFunc(name) {
if debugIneritCall {
needDebug = true
}
} else if debugIneritCall {
needDebug = true
}
if needDebug {
clsname := getClassNameByCObject(cbobj)
log.Printf("call: %s*(%v)::%s(%d:%d:%v)\n", clsname, cbobj, name, *handled, argc, pargs)
}
if signal := qt.GetSignal(cbobj, name); signal != nil {
*handled = 1
rv := callbackInheritInvokeGo(signal, pargs[:argc]...)
if rv == nil {
return 0
}
retv := reflect.ValueOf(rv)
switch retv.Kind() { // TODO
case reflect.Uint64, reflect.Int, reflect.Uint, reflect.Uint16, reflect.Int16,
reflect.Int32, reflect.Uint32, reflect.Int8, reflect.Uint8:
return retv.Convert(reflect.TypeOf(uint64(0))).Interface().(uint64)
case reflect.Bool:
return uint64(IfElseInt(rv.(bool), 1, 0))
case reflect.Float64: // NOTE: need caller copy out
return Float64AsInt(rv.(float64))
case reflect.Float32:
return Float32AsInt(rv.(float32))
case reflect.UnsafePointer:
// impled
var co CObjectITF
if retv.Type().Implements(reflect.TypeOf(co)) {
crvpx := retv.MethodByName("GetCthis").Call([]reflect.Value{})
return uint64(uintptr(crvpx[0].Interface().(unsafe.Pointer)))
} else {
return uint64(uintptr(rv.(unsafe.Pointer)))
}
default:
return rv.(uint64)
}
} else {
// 这个函数还是比较可靠的,没有找到那一定是没有了
// NilPrint(nil, "not found sigobj:", cbobj, name, pargs)
}
return 0
}
func callbackInheritInvokeGo(f interface{}, args ...uint64) interface{} {
fv := reflect.ValueOf(f)
// signal函数的参数类型一般还是比较简单的,像整数,布尔,QString
in := []reflect.Value{} // 构造这个call in
for i := 0; i < fv.Type().NumIn(); i++ {
argty := fv.Type().In(i)
if debugIneritCall {
log.Printf("arg%d: type: %s, kind: %v, val: %d\n", i, argty.String(), argty.Kind(), args[i])
}
switch argty.Kind() {
case reflect.Int:
in = append(in, reflect.ValueOf(int(args[i])))
case reflect.Uint32:
in = append(in, reflect.ValueOf(uint32(args[i])))
case reflect.Int32:
in = append(in, reflect.ValueOf(int32(args[i])))
case reflect.Uint16:
in = append(in, reflect.ValueOf(uint16(args[i])))
case reflect.Int16:
in = append(in, reflect.ValueOf(int16(args[i])))
case reflect.Uint64:
in = append(in, reflect.ValueOf(uint64(args[i])))
case reflect.Int64:
in = append(in, reflect.ValueOf(int64(args[i])))
case reflect.Bool:
in = append(in, reflect.ValueOf(args[i] == 1))
case reflect.Float64:
in = append(in, reflect.ValueOf((float64)(*(*C.double)(unsafe.Pointer(uintptr(args[i]))))))
case reflect.Float32:
in = append(in, reflect.ValueOf((float32)(*(*C.float)(unsafe.Pointer(uintptr(args[i]))))))
case reflect.String:
in = append(in, reflect.ValueOf(GoStringI(args[i])))
case reflect.Ptr:
argd1ty := argty.Elem()
switch argd1ty.Kind() {
case reflect.Int:
case reflect.Ptr:
default:
// TODO see also IsQtclass
reg := regexp.MustCompile(`^(qt.*\.)?Q[A-Z](.+)`)
if reg.MatchString(argd1ty.String()) {
prmval := reflect.New(argd1ty)
prmval.MethodByName("SetCthis").Call([]reflect.Value{
reflect.ValueOf(unsafe.Pointer(uintptr(args[i])))})
in = append(in, prmval)
}
}
default:
log.Println("Unsupported:", argty.Kind().String(), argty.String())
}
}
if len(in) != fv.Type().NumIn() {
log.Println("can not fill enough parameters,", len(in), fv.Type().NumIn())
}
Assert(len(in) == fv.Type().NumIn(), "no engouth parameters", len(in), fv.Type().NumIn())
if true {
out := fv.Call(in)
if debugIneritCall {
log.Println(len(out), out)
}
if len(out) > 0 {
return out[0].Interface()
}
}
return nil
}
// 把浮点数存储在uint64中。注意这不是强制类型转换,是原样存储,不丢失精度
func Float64AsInt(n float64) (rv uint64) {
C.memcpy((unsafe.Pointer(&rv)), (unsafe.Pointer(&n)), 8)
return
}
func Float32AsInt(n float32) (rv uint64) {
C.memcpy((unsafe.Pointer(&rv)), (unsafe.Pointer(&n)), 4)
return
}
func IntAsFloat64(v uint64) (n float64) {
C.memcpy((unsafe.Pointer(&n)), (unsafe.Pointer(&v)), 8)
return
}
func IntAsFloat32(v uint64) (n float32) {
C.memcpy((unsafe.Pointer(&n)), (unsafe.Pointer(&v)), 4)
return
}
func IsQtclass(tystr string) bool {
reg := regexp.MustCompile(`^(qt.*\.)?Q[A-Z](.+)`)
return reg.MatchString(tystr)
}
func GetQtclassName (tystr string) string {
reg := regexp.MustCompile(`^(qt[a-z]+\.)?(Q[A-Z](.+))`)
if reg.MatchString(tystr) {
// qt classes
mats := reg.FindAllStringSubmatch(tystr, -1)
return mats[0][2]
}
return ""
}