summaryrefslogtreecommitdiff
path: root/objc_darwin.go
diff options
context:
space:
mode:
authorPietro Gagliardi <[email protected]>2014-03-01 04:17:32 -0500
committerPietro Gagliardi <[email protected]>2014-03-01 04:17:32 -0500
commita3b01892fdd13d37eff64d915319fb337a7bfe27 (patch)
tree4ce513c4cf7b3cad297aa8402ba15d28ed31db6b /objc_darwin.go
parent6b8a8d2d15ef016650ac277f4439b6f0cfb9b371 (diff)
Moved the Objective-C wrappers and helpers out of darwintest.
Diffstat (limited to 'objc_darwin.go')
-rw-r--r--objc_darwin.go84
1 files changed, 84 insertions, 0 deletions
diff --git a/objc_darwin.go b/objc_darwin.go
new file mode 100644
index 0000000..524d9f7
--- /dev/null
+++ b/objc_darwin.go
@@ -0,0 +1,84 @@
+// 28 february 2014
+package main
+
+import (
+ "unsafe"
+)
+
+// #cgo LDFLAGS: -lobjc -framework Foundation
+// #include <stdlib.h>
+// #include "objc_darwin.h"
+import "C"
+
+func objc_getClass(class string) C.id {
+ cclass := C.CString(class)
+ defer C.free(unsafe.Pointer(cclass))
+
+ return C.objc_getClass(cclass)
+}
+
+func sel_getUid(sel string) C.SEL {
+ csel := C.CString(sel)
+ defer C.free(unsafe.Pointer(csel))
+
+ return C.sel_getUid(csel)
+}
+
+// Common Objective-C types and selectors.
+var (
+ _NSObject = objc_getClass("NSObject")
+ _NSString = objc_getClass("NSString")
+
+ _alloc = sel_getUid("alloc")
+ _new = sel_getUid("new")
+ _release = sel_getUid("release")
+ _stringWithUTF8String = sel_getUid("stringWithUTF8String:")
+ _UTF8String = sel_getUid("UTF8String")
+)
+
+// some helper functions
+
+func objc_alloc(class C.id) C.id {
+ return C.objc_msgSend_noargs(class, _alloc)
+}
+
+func objc_new(class C.id) C.id {
+ return C.objc_msgSend_noargs(class, _new)
+}
+
+func objc_release(obj C.id) {
+ C.objc_msgSend_noargs(obj, _release)
+}
+
+func toNSString(str string) C.id {
+ cstr := C.CString(str)
+ defer C.free(unsafe.Pointer(cstr))
+
+ return C.objc_msgSend_str(_NSString,
+ _stringWithUTF8String,
+ cstr)
+}
+
+func fromNSString(str C.id) string {
+ cstr := C.objc_msgSend_noargs(str, _UTF8String)
+ return C.GoString((*C.char)(unsafe.Pointer(cstr)))
+}
+
+/*
+These are wrapper functions for the functions in bleh_darwin.m to wrap around stdint.h type casting.
+*/
+
+func objc_msgSend_rect(obj C.id, sel C.SEL, x int, y int, w int, h int) C.id {
+ return C._objc_msgSend_rect(obj, sel,
+ C.int64_t(x), C.int64_t(y), C.int64_t(w), C.int64_t(h))
+}
+
+func objc_msgSend_uint(obj C.id, sel C.SEL, a uintptr) C.id {
+ return C._objc_msgSend_uint(obj, sel, C.uintptr_t(a))
+}
+
+func objc_msgSend_rect_uint_uint_bool(obj C.id, sel C.SEL, x int, y int, w int, h int, b uintptr, c uintptr, d C.BOOL) C.id {
+ return C._objc_msgSend_rect_uint_uint_bool(obj, sel,
+ C.int64_t(x), C.int64_t(y), C.int64_t(w), C.int64_t(h),
+ C.uintptr_t(b), C.uintptr_t(c), d)
+}