1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
(in-package #:mulk.objective-cl)
(defgeneric objc-eql (x y))
(defgeneric objc-equal (x y))
(defun truep (b)
(or (eq b t)
(and (numberp b)
(not (eql b +no+)))))
(defun id-eql (x y)
(pointer-eq (pointer-to x) (pointer-to y)))
(defun id-equal (x y)
(truep (if (typep x '(or id objc-class exception))
(primitive-invoke x :is-equal :char y)
(progn
(assert (typep y '(or id objc-class exception)))
(primitive-invoke y :is-equal :char x)))))
(defun objc-typep (x class-designator)
(objc-eql (object-get-class x)
(etypecase x
(class x)
(id (object-get-class x))
((or string symbol) (find-objc-class class-designator t)))))
(defmethod objc-eql (x y)
(cl:eql x y))
(defmethod objc-eql ((x id) y)
(id-eql x y))
(defmethod objc-eql (x (y id))
(id-eql x y))
(defmethod objc-eql ((x objc-class) y)
(id-eql x y))
(defmethod objc-eql (x (y objc-class))
(id-eql x y))
(defmethod objc-eql ((x exception) y)
(id-eql x y))
(defmethod objc-eql (x (y exception))
(id-eql x y))
(defmethod objc-eql ((x selector) (y selector))
(eql (selector-name x) (selector-name y)))
(defmethod objc-eql ((x selector) (y string))
(eql (selector-name x) y))
(defmethod objc-eql ((x string) (y selector))
(eql x (selector-name y)))
(defmethod objc-equal (x y)
(cl:equal x y))
(defmethod objc-equal ((x id) y)
(id-equal x y))
(defmethod objc-equal (x (y id))
(id-equal x y))
(defmethod objc-equal ((x objc-class) y)
(id-equal x y))
(defmethod objc-equal (x (y objc-class))
(id-equal x y))
(defmethod objc-equal ((x exception) y)
(id-equal x y))
(defmethod objc-equal (x (y exception))
(id-equal x y))
(defmethod objc-equal ((x selector) (y selector))
(equal (selector-name x) (selector-name y)))
(defmethod objc-equal ((x selector) (y string))
(equal (selector-name x) y))
(defmethod objc-equal ((x string) (y selector))
(equal x (selector-name y)))
;;; (@* "Object Representation")
(defmethod print-object ((object id) stream)
(with-slots (pointer) object
(if (cffi:pointer-eq pointer (pointer-to +nil+))
(format stream "#.~S" '+nil+)
(print-unreadable-object (object stream)
(format stream "~A `~A' {~X}"
(objcl-class-name (primitive-invoke object "class" 'id))
(primitive-invoke (primitive-invoke object "description" 'id)
"UTF8String" :string)
(cffi:pointer-address pointer))))))
(defmethod print-object ((class objc-class) stream)
(print-unreadable-object (class stream)
(with-slots (pointer) class
(format stream "~S ~A {~X}"
(type-of class)
(objcl-class-name class)
(cffi:pointer-address pointer)))))
(defmethod print-object ((meta-class objc-meta-class) stream)
(print-unreadable-object (meta-class stream)
(with-slots (meta-class-for-class pointer) meta-class
(format stream "~S ~A {~X}"
(type-of meta-class)
(objcl-class-name meta-class-for-class)
(cffi:pointer-address pointer)))))
(defmethod print-object ((selector selector) stream)
(print-unreadable-object (selector stream)
(format stream "~S `~A'"
(type-of selector)
(selector-name selector))))
(defmethod print-object ((exception exception) stream)
(print-unreadable-object (exception stream)
(format stream "~S ~A {~X}"
(type-of exception)
(primitive-invoke (primitive-invoke exception "name" 'id)
"UTF8String" :string)
(primitive-invoke exception "hash" :unsigned-int))))
;;; (@* "Structure and Union Definition")
(defun make-objc-struct/union-definer (type name-and-options c-names
doc-and-slots)
(let ((struct-name (ctypecase name-and-options
(list (first name-and-options))
(symbol name-and-options)))
(slots (typecase (first doc-and-slots)
(string (rest doc-and-slots))
(t doc-and-slots))))
`(progn
,(if (eq type :struct)
`(defcstruct ,name-and-options ,@doc-and-slots)
`(defcunion ,name-and-options ,@doc-and-slots))
,@(mapcar #'(lambda (slot)
(let ((slot-name (first slot)))
`(defun ,(intern (concatenate 'string
(symbol-name struct-name)
"-"
(symbol-name slot-name))
(symbol-package slot-name))
(struct)
(check-type struct ,(if (eq type :struct)
`(or c-pointer
opaque-struct
tagged-struct)
`(or c-pointer
opaque-union
tagged-union)))
(when (typep struct ,(if (eq type :struct)
`'tagged-struct
`'tagged-union))
(assert (member (struct-name struct)
',c-names
:test #'string=)))
(cffi:foreign-slot-value struct
',struct-name
',slot-name))))
slots))))
(defmacro define-objc-struct (name-and-options c-names &rest doc-and-slots)
"Like CFFI:DEFCSTRUCT except that it provides accessors that check
their arguments according to their struct names."
(make-objc-struct/union-definer :struct name-and-options c-names doc-and-slots))
(defmacro define-objc-union (name-and-options c-names &rest doc-and-slots)
"Like CFFI:DEFCUNION except that it provides accessors that check
their arguments according to their struct names."
(make-objc-struct/union-definer :union name-and-options c-names doc-and-slots))
|