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

View File

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

View File

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

View File

@ -562,6 +562,9 @@ func (w *Wrapper) processType(tp *types.Type) {
if gt == "SEL" { if gt == "SEL" {
w.SelectorHelpers() w.SelectorHelpers()
} }
if gt == "NSAutoreleasePool" {
w.AutoreleaseHelpers()
}
if bt.IsFunction() || bt.IsFunctionPtr() { if bt.IsFunction() || bt.IsFunctionPtr() {
return return
} }
@ -576,6 +579,9 @@ func (w *Wrapper) processType(tp *types.Type) {
func (w *Wrapper) ObjectHelpers() { func (w *Wrapper) ObjectHelpers() {
w.goHelpers.WriteString(` w.goHelpers.WriteString(`
func (o *Id) Retain() {
C.retain(unsafe.Pointer(o))
}
func (o *Id) Release() { func (o *Id) Release() {
C.release(unsafe.Pointer(o)) C.release(unsafe.Pointer(o))
} }
@ -585,6 +591,10 @@ func (o *Id) Autorelease() {
`) `)
w.cCode.WriteString(` w.cCode.WriteString(`
void void
retain(void* obj) {
[(NSObject*)obj retain];
}
void
release(void* obj) { release(void* obj) {
[(NSObject*)obj release]; [(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() { func (w *Wrapper) SelectorHelpers() {
w.cCode.WriteString(` w.cCode.WriteString(`
void* void*