utils.go 741 B

1234567891011121314151617181920212223242526272829303132333435
  1. package qt
  2. /*
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. typedef void (*CgoCallback)(size_t);
  6. void CgoInvokeCallback(size_t id);
  7. */
  8. import "C"
  9. import "unsafe"
  10. import "kumachan/standalone/qt/cgohelper"
  11. func str_alloc() (func(string) *C.char, func()) {
  12. var alloc, dealloc = cgohelper.CreateStringAllocator()
  13. return func(str string) *C.char {
  14. return (*C.char)(alloc(str))
  15. }, dealloc
  16. }
  17. //export invoke_callback
  18. func invoke_callback(id C.size_t) {
  19. var f = cgohelper.GetCallback(uint(id))
  20. f()
  21. }
  22. func addrlen(buf ([] byte)) (*C.uint8_t, C.size_t) {
  23. if len(buf) == 0 {
  24. return nil, C.size_t(0)
  25. } else {
  26. return (*C.uint8_t)(unsafe.Pointer(&(buf[0]))), C.size_t(uint(len(buf)))
  27. }
  28. }
  29. var cgo_callback = C.CgoCallback(C.CgoInvokeCallback)