Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 115 lines (87 sloc) 2.847 kB
107999c @mkrautz objc: add LICENSE.
authored
1 // Copyright (c) 2012 The 'objc' Package Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
d9fa1ac @mkrautz objc: split objc.go into object.go and class.go.
authored
5 // Package objc implements access to the Objective-C runtime from Go
6 package objc
7
209cac7 @mkrautz Floating point return values.
authored
8 import (
9 "C"
fca50aa @mkrautz object: clean up Object interface.
authored
10 "unsafe"
209cac7 @mkrautz Floating point return values.
authored
11 )
d9fa1ac @mkrautz objc: split objc.go into object.go and class.go.
authored
12
13 // An Object represents an Objective-C object, along with
14 // some convenience methods only found on NSObjects.
15 type Object interface {
16 // SendMsg sends an arbitrary message to the method on the
17 // object that is idenfieid by selectorName.
18 SendMsg(selectorName string, args ...interface{}) Object
19
9d48418 @mkrautz objc: clean up Go-backed ObjC class initialization.
authored
20 // SendSuperMsg is like SendMsg, but sends to the object's
21 // super class instead.
22 SendSuperMsg(selectorName string, args ...interface{}) Object
23
d9fa1ac @mkrautz objc: split objc.go into object.go and class.go.
authored
24 // Alloc sends the "alloc" message to the object.
25 Alloc() Object
26
27 // Init sends the "init" message to the object.
28 Init() Object
29
30 // Retain sends the "retain" message to the object.
31 Retain() Object
32
33 // Release sends the "release" message to the object.
34 Release() Object
35
36 // AutoRelease sends the "autorelease" message to the object.
37 AutoRelease() Object
38
39 // Copy sends the "copy" message to the object.
40 Copy() Object
41
42 // String returns a string-representation of the object.
43 // This is equivalent to sending the "description"
44 // message to the object, except that this method
45 // returns a Go string.
46 String() string
7ba4ec1 @mkrautz Handle return values of ObjC exported Go methods.
authored
47
fca50aa @mkrautz object: clean up Object interface.
authored
48 // Pointer returns the in-memory address of the object.
49 Pointer() uintptr
50
7ba4ec1 @mkrautz Handle return values of ObjC exported Go methods.
authored
51 // Uint returns the value of the object as an uint64.
52 Uint() uint64
53
54 // Int returns the value of the object as an int64.
55 Int() int64
56
57 // Float returns the value of the object as a float64.
58 Float() float64
59
60 // Bool returns the value of the object as a bool.
61 Bool() bool
d9fa1ac @mkrautz objc: split objc.go into object.go and class.go.
authored
62 }
63
64 // Type object is the package's internal representation of an Object.
65 // Besides implementing the Objct interface, object also implements
66 // the Class interface.
84affa2 @mkrautz Allow ObjC class implementations in pure Go.
authored
67 type object struct {
038365a @mkrautz Correctly handle float arguments.
authored
68 ptr uintptr
84affa2 @mkrautz Allow ObjC class implementations in pure Go.
authored
69 }
d9fa1ac @mkrautz objc: split objc.go into object.go and class.go.
authored
70
71 // Return the Object as a uintptr.
72 //
73 // Using package unsafe, this uintptr can further
74 // be converted to an unsafe.Pointer.
75 func (obj object) Pointer() uintptr {
84affa2 @mkrautz Allow ObjC class implementations in pure Go.
authored
76 return obj.ptr
d9fa1ac @mkrautz objc: split objc.go into object.go and class.go.
authored
77 }
78
79 func (obj object) Alloc() Object {
80 return obj.SendMsg("alloc")
81 }
82
83 func (obj object) Init() Object {
84 return obj.SendMsg("init")
85 }
86
87 func (obj object) Retain() Object {
88 return obj.SendMsg("retain")
89 }
90
91 func (obj object) Release() Object {
92 return obj.SendMsg("release")
93 }
94
95 func (obj object) AutoRelease() Object {
96 return obj.SendMsg("autorelease")
97 }
98
99 func (obj object) Copy() Object {
100 return obj.SendMsg("copy")
101 }
102
103 func (obj object) String() string {
104 pool := GetClass("NSAutoreleasePool").Alloc().Init()
105 defer pool.Release()
106
107 descString := obj.SendMsg("description")
108 utf8Bytes := descString.SendMsg("UTF8String")
df69361 @mkrautz object: fix NULL check.
authored
109 if utf8Bytes.Pointer() != 0 {
d9fa1ac @mkrautz objc: split objc.go into object.go and class.go.
authored
110 return C.GoString((*C.char)(unsafe.Pointer(utf8Bytes.Pointer())))
111 }
112
113 return "(nil)"
84affa2 @mkrautz Allow ObjC class implementations in pure Go.
authored
114 }
Something went wrong with that request. Please try again.