diff options
| author | Pietro Gagliardi <[email protected]> | 2014-04-04 21:06:01 -0400 |
|---|---|---|
| committer | Pietro Gagliardi <[email protected]> | 2014-04-04 21:06:01 -0400 |
| commit | dd20d56f257b001fe58783e86c113c11de58ca53 (patch) | |
| tree | 57a09f99f406efe5427ba31cfe18bc02505313cd /objc_darwin.go | |
| parent | 9488824240943af4c88cc0368c87b5d26851f016 (diff) | |
Created a new, simpler, easier to work with, and consistent way to create the necessary Objective-C classes in our Go code, and converted the appDelegate class to use it. Now for goArea...
Diffstat (limited to 'objc_darwin.go')
| -rw-r--r-- | objc_darwin.go | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/objc_darwin.go b/objc_darwin.go index f8d7771..90afd74 100644 --- a/objc_darwin.go +++ b/objc_darwin.go @@ -3,6 +3,7 @@ package ui import ( + "fmt" "unsafe" ) @@ -50,3 +51,46 @@ func fromNSString(str C.id) string { cstr := C.objc_msgSend_noargs(str, _UTF8String) return C.GoString((*C.char)(unsafe.Pointer(cstr))) } + +// These create new classes. + +// selector contains the information for a new selector. +type selector struct { + name string + imp uintptr // not unsafe.Pointer because https://code.google.com/p/go/issues/detail?id=7665 + itype itype + desc string // for error reporting +} + +type itype uint +const ( + sel_void_id itype = iota + sel_bool_id + nitypes +) + +var itypes = [nitypes][]C.char{ + sel_void_id: []C.char{'v', '@', ':', '@', 0}, + sel_bool_id: []C.char{'c', '@', ':', '@', 0}, +} + +func makeClass(name string, super C.id, sels []selector, desc string) (err error) { + cname := C.CString(name) + defer C.free(unsafe.Pointer(cname)) + + // an id that describes a class is itself a Class + // thanks to Psy| in irc.freenode.net/##objc + c := C.objc_allocateClassPair(C.Class(unsafe.Pointer(super)), cname, 0) + if c == C.NilClass { + return fmt.Errorf("unable to create Objective-C class %s for %s; reason unknown", name, desc) + } + C.objc_registerClassPair(c) + for _, v := range sels { + ok := C.class_addMethod(c, sel_getUid(v.name), + C.IMP(unsafe.Pointer(v.imp)), &itypes[v.itype][0]) + if ok == C.BOOL(C.NO) { + return fmt.Errorf("unable to add selector %s to class %s (needed for %s; reason unknown)", v.name, name, v.desc) + } + } + return nil +} |
