Add autorelease helpers.

This commit is contained in:
Greg 2019-05-09 13:33:28 -04:00
parent b00ecd4ac0
commit 0d2321d516
4 changed files with 36 additions and 3 deletions

View File

@ -6,7 +6,7 @@ import (
"gitlab.wow.st/gmp/nswrap/examples/app/ns"
)
func nsmgr() {
func app() {
//Lock OS thread because Cocoa uses thread-local storage
runtime.LockOSThread()
a := ns.NSApplicationSharedApplication()
@ -50,7 +50,7 @@ func nsmgr() {
}
func main() {
go nsmgr()
go ns.Autorelease(app)
select { }
}

View File

@ -3,7 +3,8 @@ inputfiles:
- /System/Library/Frameworks/AppKit.framework/Headers/AppKit.h
classes:
- NSArray"
- NSAutoreleasePool
- NSArray
- NSMutableArray
- NSDictionary
- NSEnumerator

View File

@ -1,6 +1,7 @@
inputfiles:
- /System/Library/Frameworks/Foundation.framework/Headers/Foundation.h
classes:
- NSAutoreleasePool
- NSArray
- NSMutableArray
- NSDictionary

View File

@ -562,6 +562,9 @@ func (w *Wrapper) processType(tp *types.Type) {
if gt == "SEL" {
w.SelectorHelpers()
}
if gt == "NSAutoreleasePool" {
w.AutoreleaseHelpers()
}
if bt.IsFunction() || bt.IsFunctionPtr() {
return
}
@ -576,6 +579,9 @@ func (w *Wrapper) processType(tp *types.Type) {
func (w *Wrapper) ObjectHelpers() {
w.goHelpers.WriteString(`
func (o *Id) Retain() {
C.retain(unsafe.Pointer(o))
}
func (o *Id) Release() {
C.release(unsafe.Pointer(o))
}
@ -585,6 +591,10 @@ func (o *Id) Autorelease() {
`)
w.cCode.WriteString(`
void
retain(void* obj) {
[(NSObject*)obj retain];
}
void
release(void* obj) {
[(NSObject*)obj release];
}
@ -621,6 +631,27 @@ func (e *NSEnumerator) ForIn(f func(*Id) bool) {
`)
}
func (w *Wrapper) AutoreleaseHelpers() {
//not sure why this is not coming up automatically...
w.cCode.WriteString(`
void* _Nonnull
NSAutoreleasePool_init(void* o) {
return [(NSAutoreleasePool*)o init];
}
`)
w.goHelpers.WriteString(`
func (o *NSAutoreleasePool) Init() *NSAutoreleasePool {
return (*NSAutoreleasePool)(unsafe.Pointer(C.NSAutoreleasePool_init(o.Ptr())))
}
func Autorelease(f func()) {
pool := NSAutoreleasePoolAlloc().Init()
f()
pool.Drain()
}
`)
}
func (w *Wrapper) SelectorHelpers() {
w.cCode.WriteString(`
void*