b44caf77 |
1 | ;; Common Lisp bindings for GTK+ v2.0 |
2 | ;; Copyright (C) 2000 Espen S. Johnsen <esj@stud.cs.uit.no> |
3 | ;; |
4 | ;; This library is free software; you can redistribute it and/or |
5 | ;; modify it under the terms of the GNU Lesser General Public |
6 | ;; License as published by the Free Software Foundation; either |
7 | ;; version 2 of the License, or (at your option) any later version. |
8 | ;; |
9 | ;; This library is distributed in the hope that it will be useful, |
10 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | ;; Lesser General Public License for more details. |
13 | ;; |
14 | ;; You should have received a copy of the GNU Lesser General Public |
15 | ;; License along with this library; if not, write to the Free Software |
16 | ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 | |
29933e83 |
18 | ;; $Id: proxy.lisp,v 1.12 2004/11/09 10:10:59 espen Exp $ |
b44caf77 |
19 | |
20 | (in-package "GLIB") |
21 | |
b44caf77 |
22 | ;;;; Superclass for all metaclasses implementing some sort of virtual slots |
23 | |
24 | (eval-when (:compile-toplevel :load-toplevel :execute) |
6baf860c |
25 | (defclass virtual-slots-class (standard-class) |
935a783c |
26 | ()) |
b44caf77 |
27 | |
28 | (defclass direct-virtual-slot-definition (standard-direct-slot-definition) |
ba25fa44 |
29 | ((setter :reader slot-definition-setter :initarg :setter) |
935a783c |
30 | (getter :reader slot-definition-getter :initarg :getter) |
31 | (boundp :reader slot-definition-boundp :initarg :boundp))) |
b44caf77 |
32 | |
935a783c |
33 | (defclass effective-virtual-slot-definition (standard-effective-slot-definition) |
34 | ((setter :reader slot-definition-setter :initarg :setter) |
35 | (getter :reader slot-definition-getter :initarg :getter) |
36 | (boundp :reader slot-definition-boundp :initarg :boundp))) |
37 | |
38 | (defun most-specific-slot-value (instances slot &optional default) |
39 | (let ((object (find-if |
40 | #'(lambda (ob) |
41 | (and (slot-exists-p ob slot) (slot-boundp ob slot))) |
42 | instances))) |
43 | (if object |
44 | (slot-value object slot) |
6baf860c |
45 | default)))) |
935a783c |
46 | |
b44caf77 |
47 | |
48 | |
6baf860c |
49 | (defmethod direct-slot-definition-class ((class virtual-slots-class) &rest initargs) |
b44caf77 |
50 | (if (eq (getf initargs :allocation) :virtual) |
51 | (find-class 'direct-virtual-slot-definition) |
52 | (call-next-method))) |
53 | |
6baf860c |
54 | (defmethod effective-slot-definition-class ((class virtual-slots-class) &rest initargs) |
b44caf77 |
55 | (if (eq (getf initargs :allocation) :virtual) |
56 | (find-class 'effective-virtual-slot-definition) |
57 | (call-next-method))) |
58 | |
935a783c |
59 | |
60 | (defmethod initialize-internal-slot-functions ((slotd effective-virtual-slot-definition)) |
61 | (with-slots (getter setter boundp) slotd |
62 | (unless (slot-boundp slotd 'reader-function) |
63 | (setf |
64 | (slot-value slotd 'reader-function) |
65 | (etypecase getter |
66 | (function getter) |
67 | (null #'(lambda (object) |
68 | (declare (ignore object)) |
69 | (error "Can't read slot: ~A" (slot-definition-name slotd)))) |
70 | (symbol #'(lambda (object) |
0466f75e |
71 | (funcall getter object))) |
6baf860c |
72 | (string ;(let ()(reader (mkbinding getter |
73 | ;; (slot-definition-type slotd) 'pointer))) |
0466f75e |
74 | (setf (slot-value slotd 'reader-function) |
75 | #'(lambda (object) |
6baf860c |
76 | (let ((reader |
77 | (mkbinding getter |
78 | (slot-definition-type slotd) 'pointer))) |
0466f75e |
79 | (funcall reader (proxy-location object))))))))) |
935a783c |
80 | |
81 | (unless (slot-boundp slotd 'writer-function) |
82 | (setf |
83 | (slot-value slotd 'writer-function) |
84 | (etypecase setter |
85 | (function setter) |
86 | (null #'(lambda (object) |
87 | (declare (ignore object)) |
88 | (error "Can't set slot: ~A" (slot-definition-name slotd)))) |
89 | ((or symbol cons) #'(lambda (value object) |
0466f75e |
90 | (funcall (fdefinition setter) value object))) |
91 | (string |
6baf860c |
92 | (let ((writer ()));; (mkbinding setter 'nil 'pointer |
93 | ;; (slot-definition-type slotd)))) |
0466f75e |
94 | (setf (slot-value slotd 'writer-function) |
95 | #'(lambda (value object) |
6baf860c |
96 | (unless writer |
97 | (setq writer |
98 | (mkbinding setter 'nil 'pointer |
99 | (slot-definition-type slotd)))) |
0466f75e |
100 | (funcall writer (proxy-location object) value)))))))) |
935a783c |
101 | |
102 | (unless (slot-boundp slotd 'boundp-function) |
103 | (setf |
104 | (slot-value slotd 'boundp-function) |
105 | (etypecase boundp |
106 | (function boundp) |
107 | (null #'(lambda (object) |
108 | (declare (ignore object)) |
109 | t)) |
110 | (symbol #'(lambda (object) |
111 | (funcall boundp object))))))) |
112 | (initialize-internal-slot-gfs (slot-definition-name slotd))) |
113 | |
114 | |
115 | |
116 | (defmethod compute-slot-accessor-info ((slotd effective-virtual-slot-definition) |
117 | type gf) |
118 | nil) |
119 | |
6baf860c |
120 | (defmethod compute-effective-slot-definition-initargs ((class virtual-slots-class) direct-slotds) |
935a783c |
121 | (if (eq (most-specific-slot-value direct-slotds 'allocation) :virtual) |
122 | (nconc |
123 | (list :getter (most-specific-slot-value direct-slotds 'getter) |
124 | :setter (most-specific-slot-value direct-slotds 'setter) |
125 | :boundp (most-specific-slot-value direct-slotds 'boundp)) |
126 | (call-next-method)) |
127 | (call-next-method))) |
128 | |
b44caf77 |
129 | |
b44caf77 |
130 | (defmethod slot-value-using-class |
6baf860c |
131 | ((class virtual-slots-class) (object standard-object) |
b44caf77 |
132 | (slotd effective-virtual-slot-definition)) |
935a783c |
133 | (if (funcall (slot-value slotd 'boundp-function) object) |
134 | (funcall (slot-value slotd 'reader-function) object) |
135 | (slot-unbound class object (slot-definition-name slotd)))) |
b44caf77 |
136 | |
b44caf77 |
137 | (defmethod slot-boundp-using-class |
6baf860c |
138 | ((class virtual-slots-class) (object standard-object) |
b44caf77 |
139 | (slotd effective-virtual-slot-definition)) |
935a783c |
140 | (funcall (slot-value slotd 'boundp-function) object)) |
141 | |
142 | (defmethod (setf slot-value-using-class) |
6baf860c |
143 | (value (class virtual-slots-class) (object standard-object) |
b44caf77 |
144 | (slotd effective-virtual-slot-definition)) |
935a783c |
145 | (funcall (slot-value slotd 'writer-function) value object)) |
146 | |
147 | |
b44caf77 |
148 | (defmethod validate-superclass |
6baf860c |
149 | ((class virtual-slots-class) (super standard-class)) |
b44caf77 |
150 | t) |
151 | |
152 | |
153 | ;;;; Proxy cache |
154 | |
155 | (internal *instance-cache*) |
156 | (defvar *instance-cache* (make-hash-table :test #'eql)) |
157 | |
158 | (defun cache-instance (instance) |
159 | (setf |
160 | (gethash (system:sap-int (proxy-location instance)) *instance-cache*) |
161 | (ext:make-weak-pointer instance))) |
162 | |
163 | (defun find-cached-instance (location) |
164 | (let ((ref (gethash (system:sap-int location) *instance-cache*))) |
165 | (when ref |
166 | (ext:weak-pointer-value ref)))) |
167 | |
a5c3a597 |
168 | (defun instance-cached-p (location) |
169 | (gethash (system:sap-int location) *instance-cache*)) |
170 | |
b44caf77 |
171 | (defun remove-cached-instance (location) |
172 | (remhash (system:sap-int location) *instance-cache*)) |
173 | |
6baf860c |
174 | ;; For debuging |
175 | (defun cached-instances () |
176 | (let ((instances ())) |
177 | (maphash #'(lambda (location ref) |
178 | (declare (ignore location)) |
179 | (push (ext:weak-pointer-value ref) instances)) |
180 | *instance-cache*) |
181 | instances)) |
182 | |
b44caf77 |
183 | |
184 | |
185 | ;;;; Proxy for alien instances |
186 | |
6baf860c |
187 | (defclass proxy () |
188 | ((location :reader proxy-location :type system-area-pointer))) |
b44caf77 |
189 | |
6baf860c |
190 | (defgeneric initialize-proxy (object &rest initargs)) |
191 | (defgeneric instance-finalizer (object)) |
192 | (defgeneric reference-foreign (class location)) |
193 | (defgeneric unreference-foreign (class location)) |
194 | |
195 | (defmethod unreference-foreign :around ((class class) location) |
196 | (unless (null-pointer-p location) |
197 | ;; (format t "Unreferencing ~A at ~A" (class-name class) location) |
198 | ;; (finish-output *standard-output*) |
199 | (call-next-method) |
200 | ;; (write-line " done") |
201 | ;; (finish-output *standard-output*) |
202 | )) |
b44caf77 |
203 | |
a5c3a597 |
204 | (defmethod print-object ((instance proxy) stream) |
205 | (print-unreadable-object (instance stream :type t :identity nil) |
206 | (format stream "at 0x~X" (sap-int (proxy-location instance))))) |
207 | |
6baf860c |
208 | (defmethod print-object ((instance proxy) stream) |
209 | (print-unreadable-object (instance stream :type t :identity nil) |
210 | (format stream "at 0x~X" (sap-int (proxy-location instance))))) |
b44caf77 |
211 | |
b44caf77 |
212 | |
6baf860c |
213 | (defmethod initialize-instance :around ((instance proxy) &key location) |
214 | (if location |
215 | (setf (slot-value instance 'location) location) |
216 | (call-next-method)) |
ba25fa44 |
217 | (cache-instance instance) |
6baf860c |
218 | (ext:finalize instance (instance-finalizer instance)) |
219 | instance) |
b44caf77 |
220 | |
221 | (defmethod instance-finalizer ((instance proxy)) |
6baf860c |
222 | (let ((location (proxy-location instance)) |
223 | (class (class-of instance))) |
224 | ;; (unless (find-method #'unreference-foreign nil (list (class-of class) t) nil) |
225 | ;; (error "No matching method for UNREFERENCE-INSTANCE when called with class ~A" class)) |
226 | #'(lambda () |
29933e83 |
227 | (remove-cached-instance location) |
6baf860c |
228 | (unreference-foreign class location)))) |
ba25fa44 |
229 | |
b44caf77 |
230 | |
231 | ;;;; Metaclass used for subclasses of proxy |
232 | |
233 | (eval-when (:compile-toplevel :load-toplevel :execute) |
6baf860c |
234 | (defclass proxy-class (virtual-slots-class) |
235 | ((size :reader proxy-instance-size))) |
b44caf77 |
236 | |
237 | (defclass direct-alien-slot-definition (direct-virtual-slot-definition) |
ba25fa44 |
238 | ((allocation :initform :alien) |
239 | (offset :reader slot-definition-offset :initarg :offset))) |
b44caf77 |
240 | |
241 | (defclass effective-alien-slot-definition (effective-virtual-slot-definition) |
935a783c |
242 | ((offset :reader slot-definition-offset :initarg :offset))) |
ba25fa44 |
243 | |
b44caf77 |
244 | |
245 | (defmethod most-specific-proxy-superclass ((class proxy-class)) |
246 | (find-if |
247 | #'(lambda (class) |
248 | (subtypep (class-name class) 'proxy)) |
935a783c |
249 | (cdr (compute-class-precedence-list class)))) |
250 | |
ba25fa44 |
251 | (defmethod direct-proxy-superclass ((class proxy-class)) |
252 | (find-if |
253 | #'(lambda (class) |
254 | (subtypep (class-name class) 'proxy)) |
935a783c |
255 | (class-direct-superclasses class))) |
256 | |
b44caf77 |
257 | (defmethod shared-initialize ((class proxy-class) names |
6baf860c |
258 | &rest initargs &key size) |
b44caf77 |
259 | (declare (ignore initargs)) |
260 | (call-next-method) |
ba25fa44 |
261 | (cond |
935a783c |
262 | (size (setf (slot-value class 'size) (first size))) |
6baf860c |
263 | ((slot-boundp class 'size) (slot-makunbound class 'size)))) |
935a783c |
264 | |
265 | (defmethod direct-slot-definition-class ((class proxy-class) &rest initargs) |
b44caf77 |
266 | (case (getf initargs :allocation) |
267 | ((nil :alien) (find-class 'direct-alien-slot-definition)) |
b44caf77 |
268 | (t (call-next-method)))) |
935a783c |
269 | |
270 | (defmethod effective-slot-definition-class ((class proxy-class) &rest initargs) |
b44caf77 |
271 | (case (getf initargs :allocation) |
272 | (:alien (find-class 'effective-alien-slot-definition)) |
b44caf77 |
273 | (t (call-next-method)))) |
274 | |
935a783c |
275 | |
276 | (defmethod compute-effective-slot-definition-initargs ((class proxy-class) direct-slotds) |
277 | (if (eq (most-specific-slot-value direct-slotds 'allocation) :alien) |
278 | (nconc |
279 | (list :offset (most-specific-slot-value direct-slotds 'offset)) |
280 | (call-next-method)) |
281 | (call-next-method))) |
282 | |
283 | |
284 | (defmethod initialize-internal-slot-functions ((slotd effective-alien-slot-definition)) |
285 | (with-slots (offset) slotd |
6baf860c |
286 | (let ((type (slot-definition-type slotd))) |
935a783c |
287 | (unless (slot-boundp slotd 'reader-function) |
6baf860c |
288 | (let ((reader (reader-function type))) |
289 | (setf |
290 | (slot-value slotd 'reader-function) |
291 | #'(lambda (object) |
292 | (funcall reader (proxy-location object) offset))))) |
935a783c |
293 | |
294 | (unless (slot-boundp slotd 'writer-function) |
6baf860c |
295 | (let ((writer (writer-function type)) |
296 | (destroy (destroy-function type))) |
297 | (setf |
298 | (slot-value slotd 'writer-function) |
299 | #'(lambda (value object) |
300 | (let ((location (proxy-location object))) |
301 | (funcall destroy location offset) ; destroy old value |
302 | (funcall writer value location offset)))))) |
935a783c |
303 | |
304 | (unless (slot-boundp slotd 'boundp-function) |
305 | (setf |
306 | (slot-value slotd 'boundp-function) |
307 | #'(lambda (object) |
308 | (declare (ignore object)) |
309 | t))))) |
310 | (call-next-method)) |
311 | |
312 | |
935a783c |
313 | ;; TODO: call some C code to detect this a compile time |
314 | (defconstant +struct-alignmen+ 4) |
b44caf77 |
315 | |
316 | (defmethod compute-slots ((class proxy-class)) |
935a783c |
317 | (loop |
6baf860c |
318 | with offset = (proxy-instance-size (most-specific-proxy-superclass class)) |
935a783c |
319 | with size = offset |
320 | for slotd in (class-direct-slots class) |
321 | when (eq (slot-definition-allocation slotd) :alien) |
322 | do (if (not (slot-boundp slotd 'offset)) |
323 | (setf (slot-value slotd 'offset) offset) |
324 | (setq offset (slot-value slotd 'offset))) |
325 | |
326 | (incf offset (size-of (slot-definition-type slotd))) |
327 | (incf offset (mod offset +struct-alignmen+)) |
328 | (setq size (max size offset)) |
329 | |
330 | finally (unless (slot-boundp class 'size) |
331 | (setf (slot-value class 'size) size))) |
b44caf77 |
332 | (call-next-method)) |
8ae7ddc2 |
333 | |
935a783c |
334 | |
335 | (defmethod validate-superclass ((class proxy-class) (super standard-class)) |
336 | (subtypep (class-name super) 'proxy)) |
337 | |
6baf860c |
338 | (defmethod proxy-instance-size (class) |
ba25fa44 |
339 | (declare (ignore class)) |
340 | 0) |
935a783c |
341 | ) |
342 | |
6baf860c |
343 | (defmethod alien-type ((class proxy-class) &rest args) |
344 | (declare (ignore class args)) |
345 | (alien-type 'pointer)) |
346 | |
347 | (defmethod size-of ((class proxy-class) &rest args) |
348 | (declare (ignore class args)) |
349 | (size-of 'pointer)) |
350 | |
351 | (defmethod from-alien-form (location (class proxy-class) &rest args) |
352 | (declare (ignore args)) |
353 | `(ensure-proxy-instance ',(class-name class) ,location)) |
354 | |
355 | (defmethod from-alien-function ((class proxy-class) &rest args) |
356 | (declare (ignore args)) |
357 | #'(lambda (location) |
358 | (ensure-proxy-instance class location))) |
b44caf77 |
359 | |
6baf860c |
360 | (defmethod to-alien-form (instance (class proxy-class) &rest args) |
361 | (declare (ignore class args)) |
362 | `(proxy-location ,instance)) |
b44caf77 |
363 | |
6baf860c |
364 | (defmethod to-alien-function ((class proxy-class) &rest args) |
365 | (declare (ignore class args)) |
366 | #'proxy-location) |
367 | |
368 | (defmethod writer-function ((class proxy-class) &rest args) |
369 | (declare (ignore args)) |
370 | #'(lambda (instance location &optional (offset 0)) |
371 | (assert (null-pointer-p (sap-ref-sap location offset))) |
372 | (setf |
373 | (sap-ref-sap location offset) |
374 | (reference-foreign class (proxy-location instance))))) |
b44caf77 |
375 | |
6baf860c |
376 | (defmethod reader-function ((class proxy-class) &rest args) |
377 | (declare (ignore args)) |
378 | #'(lambda (location &optional (offset 0)) |
379 | (ensure-proxy-instance class (sap-ref-sap location offset)))) |
b44caf77 |
380 | |
6baf860c |
381 | (defmethod destroy-function ((class proxy-class) &rest args) |
382 | (declare (ignore args)) |
383 | #'(lambda (location &optional (offset 0)) |
384 | (unreference-foreign class (sap-ref-sap location offset)))) |
385 | |
386 | |
387 | (defgeneric ensure-proxy-instance (class location) |
388 | (:documentation "Returns a proxy object representing the foreign object at the give location.")) |
389 | |
390 | (defmethod ensure-proxy-instance :around (class location) |
391 | (unless (null-pointer-p location) |
392 | (or |
393 | (find-cached-instance location) |
394 | (call-next-method)))) |
395 | |
396 | (defmethod ensure-proxy-instance ((class symbol) location) |
397 | (ensure-proxy-instance (find-class class) location)) |
398 | |
399 | (defmethod ensure-proxy-instance ((class proxy-class) location) |
400 | (make-instance class :location location)) |
b44caf77 |
401 | |
ba25fa44 |
402 | |
403 | ;;;; Superclasses for wrapping of C structures |
b44caf77 |
404 | |
6baf860c |
405 | (defclass struct (proxy) |
406 | () |
407 | (:metaclass proxy-class)) |
b44caf77 |
408 | |
6baf860c |
409 | (defmethod initialize-instance ((struct struct) &rest initargs) |
b44caf77 |
410 | (declare (ignore initargs)) |
29933e83 |
411 | (let ((size (proxy-instance-size (class-of struct)))) |
412 | (if (zerop size) |
413 | (error "~A has zero size" (class-of struct)) |
414 | (setf (slot-value struct 'location) (allocate-memory size)))) |
b44caf77 |
415 | (call-next-method)) |
416 | |
417 | |
6baf860c |
418 | ;;;; Metaclasses used for subclasses of struct |
419 | |
420 | (defclass struct-class (proxy-class) |
421 | ()) |
b44caf77 |
422 | |
6baf860c |
423 | (defmethod reference-foreign ((class struct-class) location) |
424 | (copy-memory location (proxy-instance-size class))) |
425 | |
426 | (defmethod unreference-foreign ((class struct-class) location) |
ba25fa44 |
427 | (deallocate-memory location)) |
b44caf77 |
428 | |
6baf860c |
429 | (defmethod reader-function ((class struct-class) &rest args) |
430 | (declare (ignore args)) |
431 | #'(lambda (location &optional (offset 0)) |
432 | (let ((instance (sap-ref-sap location offset))) |
433 | (unless (null-pointer-p instance) |
434 | (ensure-proxy-instance class (reference-foreign class instance)))))) |
435 | |
b44caf77 |
436 | |
6baf860c |
437 | (defclass static-struct-class (struct-class) |
438 | ()) |
b44caf77 |
439 | |
6baf860c |
440 | (defmethod reference-foreign ((class static-struct-class) location) |
441 | (declare (ignore class)) |
ba25fa44 |
442 | location) |
b44caf77 |
443 | |
6baf860c |
444 | (defmethod unreference-foreign ((class static-struct-class) location) |
445 | (declare (ignore class location)) |
ba25fa44 |
446 | nil) |