2014-02-27 22:07:45 -06:00
|
|
|
// 27 february 2014
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
// #cgo LDFLAGS: -lobjc -framework Foundation
|
|
|
|
// #include <stdlib.h>
|
2014-02-28 17:30:06 -06:00
|
|
|
// #include "objc_darwin.h"
|
2014-02-28 19:53:14 -06:00
|
|
|
// /* because cgo doesn't like Nil */
|
2014-02-28 20:30:32 -06:00
|
|
|
// Class NilClass = Nil;
|
2014-02-27 22:07:45 -06:00
|
|
|
import "C"
|
|
|
|
|
2014-02-28 19:53:14 -06:00
|
|
|
var (
|
|
|
|
_NSObject_Class = C.object_getClass(_NSObject)
|
|
|
|
)
|
2014-02-27 22:07:45 -06:00
|
|
|
|
|
|
|
func newClass(name string) C.Class {
|
|
|
|
cname := C.CString(name)
|
|
|
|
defer C.free(unsafe.Pointer(cname))
|
|
|
|
|
2014-02-28 19:53:14 -06:00
|
|
|
c := C.objc_allocateClassPair(_NSObject_Class, cname, 0)
|
2014-02-27 22:07:45 -06:00
|
|
|
if c == C.NilClass {
|
|
|
|
panic("unable to create Objective-C class " + name)
|
|
|
|
}
|
2014-02-28 20:30:32 -06:00
|
|
|
C.objc_registerClassPair(c)
|
2014-02-27 22:07:45 -06:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2014-02-28 20:30:32 -06:00
|
|
|
// according to errors spit out by cgo, C function pointers are unsafe.Pointer
|
|
|
|
func addDelegateMethod(class C.Class, sel C.SEL, imp unsafe.Pointer) {
|
|
|
|
// maps to void (*)(id, SEL, id)
|
2014-02-28 10:06:20 -06:00
|
|
|
ty := []C.char{'v', '@', ':', '@', 0}
|
2014-02-27 22:07:45 -06:00
|
|
|
|
|
|
|
// clas methods get stored in the metaclass; the objc_allocateClassPair() docs say this will work
|
2014-02-28 20:30:32 -06:00
|
|
|
// metaclass := C.object_getClass(C.id(unsafe.Pointer(class)))
|
|
|
|
// we're adding instance methods, so just class will do
|
2014-02-28 10:06:20 -06:00
|
|
|
ok := C.class_addMethod(class,
|
2014-02-27 22:07:45 -06:00
|
|
|
sel,
|
2014-02-28 20:30:32 -06:00
|
|
|
C.IMP(imp),
|
2014-02-27 22:07:45 -06:00
|
|
|
&ty[0])
|
|
|
|
if ok == C.BOOL(C.NO) {
|
|
|
|
panic("unable to add ourMethod")
|
|
|
|
}
|
|
|
|
}
|