chiark / gitweb /
Bug fix
[clg] / glib / proxy.lisp
CommitLineData
55212af1 1;; Common Lisp bindings for GTK+ v2.x
2;; Copyright 2000-2005 Espen S. Johnsen <espen@users.sf.net>
b44caf77 3;;
55212af1 4;; Permission is hereby granted, free of charge, to any person obtaining
5;; a copy of this software and associated documentation files (the
6;; "Software"), to deal in the Software without restriction, including
7;; without limitation the rights to use, copy, modify, merge, publish,
8;; distribute, sublicense, and/or sell copies of the Software, and to
9;; permit persons to whom the Software is furnished to do so, subject to
10;; the following conditions:
b44caf77 11;;
55212af1 12;; The above copyright notice and this permission notice shall be
13;; included in all copies or substantial portions of the Software.
b44caf77 14;;
55212af1 15;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
b44caf77 22
b4edcbf0 23;; $Id: proxy.lisp,v 1.21 2005/04/24 13:26:40 espen Exp $
b44caf77 24
25(in-package "GLIB")
26
b44caf77 27;;;; Superclass for all metaclasses implementing some sort of virtual slots
28
29(eval-when (:compile-toplevel :load-toplevel :execute)
6baf860c 30 (defclass virtual-slots-class (standard-class)
935a783c 31 ())
b44caf77 32
33 (defclass direct-virtual-slot-definition (standard-direct-slot-definition)
ba25fa44 34 ((setter :reader slot-definition-setter :initarg :setter)
935a783c 35 (getter :reader slot-definition-getter :initarg :getter)
64bce834 36 (unbound :reader slot-definition-unbound :initarg :unbound)
935a783c 37 (boundp :reader slot-definition-boundp :initarg :boundp)))
b44caf77 38
935a783c 39 (defclass effective-virtual-slot-definition (standard-effective-slot-definition)
40 ((setter :reader slot-definition-setter :initarg :setter)
41 (getter :reader slot-definition-getter :initarg :getter)
64bce834 42 (unbound :reader slot-definition-unbound :initarg :unbound)
3d36c5d6 43 (boundp :reader slot-definition-boundp :initarg :boundp))))
64bce834 44
45 (defvar *unbound-marker* (gensym "UNBOUND-MARKER-"))
935a783c 46
64bce834 47 (defun most-specific-slot-value (instances slot &optional
48 (default *unbound-marker*))
935a783c 49 (let ((object (find-if
50 #'(lambda (ob)
51 (and (slot-exists-p ob slot) (slot-boundp ob slot)))
52 instances)))
53 (if object
54 (slot-value object slot)
3d36c5d6 55 default)));)
935a783c 56
b44caf77 57
58
6baf860c 59(defmethod direct-slot-definition-class ((class virtual-slots-class) &rest initargs)
b44caf77 60 (if (eq (getf initargs :allocation) :virtual)
61 (find-class 'direct-virtual-slot-definition)
62 (call-next-method)))
63
6baf860c 64(defmethod effective-slot-definition-class ((class virtual-slots-class) &rest initargs)
b44caf77 65 (if (eq (getf initargs :allocation) :virtual)
66 (find-class 'effective-virtual-slot-definition)
67 (call-next-method)))
68
935a783c 69
70(defmethod initialize-internal-slot-functions ((slotd effective-virtual-slot-definition))
64bce834 71 (if (not (slot-boundp slotd 'getter))
72 (setf
935a783c 73 (slot-value slotd 'reader-function)
64bce834 74 #'(lambda (object)
75 (declare (ignore object))
76 (error "Can't read slot: ~A" (slot-definition-name slotd)))
77 (slot-value slotd 'boundp-function)
78 #'(lambda (object) (declare (ignore object)) nil))
79
80 (let ((getter-function
81 (let ((getter (slot-value slotd 'getter)))
82 (etypecase getter
83 (function getter)
84 (symbol
85 #'(lambda (object)
86 (funcall getter object)))
87 (string
88 (let ((reader nil))
89 (setf (slot-value slotd 'reader-function)
90 #'(lambda (object)
91 (unless reader
b6bf802c 92 (setq reader
93 (mkbinding getter
94 (slot-definition-type slotd) 'pointer)))
64bce834 95 (funcall reader (proxy-location object))))))))))
96
935a783c 97 (setf
64bce834 98 (slot-value slotd 'boundp-function)
99 (cond
64bce834 100 ((slot-boundp slotd 'unbound)
101 (let ((unbound-value (slot-value slotd 'unbound)))
b6bf802c 102 #'(lambda (object)
103 (not (eq (funcall getter-function object) unbound-value)))))
104 ((slot-boundp slotd 'boundp)
105 (let ((boundp (slot-value slotd 'boundp)))
64bce834 106 (etypecase boundp
107 (function boundp)
108 (symbol #'(lambda (object)
109 (funcall boundp object)))
110 (string (let ((reader ()))
111 #'(lambda (object)
112 (unless reader
113 (setq reader
114 (mkbinding boundp
115 (slot-definition-type slotd) 'pointer)))
b6bf802c 116 (funcall reader (proxy-location object))))))))
117 ((multiple-value-bind (unbound-p unbound-value)
118 (unbound-value (slot-definition-type slotd))
119 (when unbound-p
120 #'(lambda (object)
121 (not (eq (funcall getter-function object) unbound-value))))))
122 (#'(lambda (object) (declare (ignore object)) t))))
64bce834 123
124 (setf
125 (slot-value slotd 'reader-function)
126 (cond
127 ((slot-boundp slotd 'unbound)
128 (let ((unbound (slot-value slotd 'unbound))
129 (slot-name (slot-definition-name slotd)))
b6bf802c 130 #'(lambda (object)
131 (let ((value (funcall getter-function object)))
132 (if (eq value unbound)
133 (slot-unbound (class-of object) object slot-name)
134 value)))))
64bce834 135 ((slot-boundp slotd 'boundp)
136 (let ((boundp-function (slot-value slotd 'boundp-function)))
b6bf802c 137 #'(lambda (object)
138 (and
139 (funcall boundp-function object)
140 (funcall getter-function object)))))
141 ((multiple-value-bind (unbound-p unbound-value)
142 (unbound-value (slot-definition-type slotd))
143 (let ((slot-name (slot-definition-name slotd)))
144 (when unbound-p
145 #'(lambda (object)
146 (let ((value (funcall getter-function object)))
147 (if (eq value unbound-value)
148 (slot-unbound (class-of object) object slot-name)
149 value)))))))
64bce834 150 (getter-function)))))
151
152 (setf
153 (slot-value slotd 'writer-function)
154 (if (not (slot-boundp slotd 'setter))
155 #'(lambda (object)
156 (declare (ignore object))
157 (error "Can't set slot: ~A" (slot-definition-name slotd)))
158 (with-slots (setter) slotd
935a783c 159 (etypecase setter
160 (function setter)
64bce834 161 ((or symbol cons)
162 #'(lambda (value object)
163 (funcall (fdefinition setter) value object)))
0466f75e 164 (string
64bce834 165 (let ((writer ()))
166 (setf
167 (slot-value slotd 'writer-function)
168 #'(lambda (value object)
169 (unless writer
170 (setq writer
171 (mkbinding setter 'nil 'pointer
172 (slot-definition-type slotd))))
173 (funcall writer (proxy-location object) value)))))))))
174
935a783c 175 (initialize-internal-slot-gfs (slot-definition-name slotd)))
176
177
178
64bce834 179(defmethod compute-slot-accessor-info ((slotd effective-virtual-slot-definition) type gf)
935a783c 180 nil)
181
6baf860c 182(defmethod compute-effective-slot-definition-initargs ((class virtual-slots-class) direct-slotds)
64bce834 183 (if (typep (first direct-slotds) 'direct-virtual-slot-definition)
184 (let ((initargs ()))
185 (let ((getter (most-specific-slot-value direct-slotds 'getter)))
186 (unless (eq getter *unbound-marker*)
187 (setf (getf initargs :getter) getter)))
188 (let ((setter (most-specific-slot-value direct-slotds 'setter)))
189 (unless (eq setter *unbound-marker*)
190 (setf (getf initargs :setter) setter)))
191 (let ((unbound (most-specific-slot-value direct-slotds 'unbound)))
192 (unless (eq unbound *unbound-marker*)
193 (setf (getf initargs :unbound) unbound)))
194 (let ((boundp (most-specific-slot-value direct-slotds 'boundp)))
195 (unless (eq boundp *unbound-marker*)
196 (setf (getf initargs :boundp) boundp)))
197 (nconc initargs (call-next-method)))
935a783c 198 (call-next-method)))
199
b44caf77 200
b44caf77 201(defmethod slot-value-using-class
6baf860c 202 ((class virtual-slots-class) (object standard-object)
b44caf77 203 (slotd effective-virtual-slot-definition))
935a783c 204 (if (funcall (slot-value slotd 'boundp-function) object)
205 (funcall (slot-value slotd 'reader-function) object)
206 (slot-unbound class object (slot-definition-name slotd))))
b44caf77 207
b44caf77 208(defmethod slot-boundp-using-class
6baf860c 209 ((class virtual-slots-class) (object standard-object)
b44caf77 210 (slotd effective-virtual-slot-definition))
935a783c 211 (funcall (slot-value slotd 'boundp-function) object))
212
213(defmethod (setf slot-value-using-class)
6baf860c 214 (value (class virtual-slots-class) (object standard-object)
b44caf77 215 (slotd effective-virtual-slot-definition))
935a783c 216 (funcall (slot-value slotd 'writer-function) value object))
217
218
b44caf77 219(defmethod validate-superclass
6baf860c 220 ((class virtual-slots-class) (super standard-class))
b44caf77 221 t)
222
223
224;;;; Proxy cache
225
226(internal *instance-cache*)
227(defvar *instance-cache* (make-hash-table :test #'eql))
228
229(defun cache-instance (instance)
230 (setf
3d36c5d6 231 (gethash (sap-int (proxy-location instance)) *instance-cache*)
232 (make-weak-pointer instance)))
b44caf77 233
234(defun find-cached-instance (location)
3d36c5d6 235 (let ((ref (gethash (sap-int location) *instance-cache*)))
b44caf77 236 (when ref
3d36c5d6 237 (weak-pointer-value ref))))
b44caf77 238
a5c3a597 239(defun instance-cached-p (location)
3d36c5d6 240 (gethash (sap-int location) *instance-cache*))
a5c3a597 241
b44caf77 242(defun remove-cached-instance (location)
3d36c5d6 243 (remhash (sap-int location) *instance-cache*))
b44caf77 244
6baf860c 245;; For debuging
246(defun cached-instances ()
247 (let ((instances ()))
248 (maphash #'(lambda (location ref)
249 (declare (ignore location))
3d36c5d6 250 (push (weak-pointer-value ref) instances))
6baf860c 251 *instance-cache*)
252 instances))
253
b44caf77 254
255
256;;;; Proxy for alien instances
257
6baf860c 258(defclass proxy ()
259 ((location :reader proxy-location :type system-area-pointer)))
b44caf77 260
6baf860c 261(defgeneric initialize-proxy (object &rest initargs))
262(defgeneric instance-finalizer (object))
263(defgeneric reference-foreign (class location))
264(defgeneric unreference-foreign (class location))
265
c55abd76 266(defmethod reference-foreign ((name symbol) location)
267 (reference-foreign (find-class name) location))
268
269(defmethod unreference-foreign ((name symbol) location)
270 (unreference-foreign (find-class name) location))
271
6baf860c 272(defmethod unreference-foreign :around ((class class) location)
273 (unless (null-pointer-p location)
274;; (format t "Unreferencing ~A at ~A" (class-name class) location)
275;; (finish-output *standard-output*)
276 (call-next-method)
277;; (write-line " done")
278;; (finish-output *standard-output*)
279 ))
b44caf77 280
a5c3a597 281(defmethod print-object ((instance proxy) stream)
282 (print-unreadable-object (instance stream :type t :identity nil)
64bce834 283 (when (slot-boundp instance 'location)
284 (format stream "at 0x~X" (sap-int (proxy-location instance))))))
b44caf77 285
6baf860c 286(defmethod initialize-instance :around ((instance proxy) &key location)
287 (if location
288 (setf (slot-value instance 'location) location)
289 (call-next-method))
ba25fa44 290 (cache-instance instance)
3d36c5d6 291 (finalize instance (instance-finalizer instance))
6baf860c 292 instance)
b44caf77 293
294(defmethod instance-finalizer ((instance proxy))
6baf860c 295 (let ((location (proxy-location instance))
296 (class (class-of instance)))
297;; (unless (find-method #'unreference-foreign nil (list (class-of class) t) nil)
298;; (error "No matching method for UNREFERENCE-INSTANCE when called with class ~A" class))
299 #'(lambda ()
29933e83 300 (remove-cached-instance location)
6baf860c 301 (unreference-foreign class location))))
ba25fa44 302
b44caf77 303
304;;;; Metaclass used for subclasses of proxy
305
3d36c5d6 306(defgeneric most-specific-proxy-superclass (class))
307(defgeneric direct-proxy-superclass (class))
308
309
b44caf77 310(eval-when (:compile-toplevel :load-toplevel :execute)
6baf860c 311 (defclass proxy-class (virtual-slots-class)
312 ((size :reader proxy-instance-size)))
b44caf77 313
314 (defclass direct-alien-slot-definition (direct-virtual-slot-definition)
ba25fa44 315 ((allocation :initform :alien)
316 (offset :reader slot-definition-offset :initarg :offset)))
b44caf77 317
318 (defclass effective-alien-slot-definition (effective-virtual-slot-definition)
935a783c 319 ((offset :reader slot-definition-offset :initarg :offset)))
ba25fa44 320
b44caf77 321 (defmethod most-specific-proxy-superclass ((class proxy-class))
322 (find-if
323 #'(lambda (class)
324 (subtypep (class-name class) 'proxy))
935a783c 325 (cdr (compute-class-precedence-list class))))
3d36c5d6 326
ba25fa44 327 (defmethod direct-proxy-superclass ((class proxy-class))
328 (find-if
329 #'(lambda (class)
330 (subtypep (class-name class) 'proxy))
935a783c 331 (class-direct-superclasses class)))
332
64bce834 333 (defmethod shared-initialize ((class proxy-class) names &key size)
b44caf77 334 (call-next-method)
ba25fa44 335 (cond
935a783c 336 (size (setf (slot-value class 'size) (first size)))
6baf860c 337 ((slot-boundp class 'size) (slot-makunbound class 'size))))
935a783c 338
339 (defmethod direct-slot-definition-class ((class proxy-class) &rest initargs)
b44caf77 340 (case (getf initargs :allocation)
341 ((nil :alien) (find-class 'direct-alien-slot-definition))
b44caf77 342 (t (call-next-method))))
935a783c 343
344 (defmethod effective-slot-definition-class ((class proxy-class) &rest initargs)
b44caf77 345 (case (getf initargs :allocation)
346 (:alien (find-class 'effective-alien-slot-definition))
b44caf77 347 (t (call-next-method))))
348
935a783c 349
350 (defmethod compute-effective-slot-definition-initargs ((class proxy-class) direct-slotds)
351 (if (eq (most-specific-slot-value direct-slotds 'allocation) :alien)
352 (nconc
353 (list :offset (most-specific-slot-value direct-slotds 'offset))
354 (call-next-method))
355 (call-next-method)))
356
357
358 (defmethod initialize-internal-slot-functions ((slotd effective-alien-slot-definition))
359 (with-slots (offset) slotd
6baf860c 360 (let ((type (slot-definition-type slotd)))
64bce834 361 (unless (slot-boundp slotd 'getter)
6baf860c 362 (let ((reader (reader-function type)))
363 (setf
64bce834 364 (slot-value slotd 'getter)
6baf860c 365 #'(lambda (object)
366 (funcall reader (proxy-location object) offset)))))
935a783c 367
64bce834 368 (unless (slot-boundp slotd 'setter)
6baf860c 369 (let ((writer (writer-function type))
370 (destroy (destroy-function type)))
371 (setf
64bce834 372 (slot-value slotd 'setter)
6baf860c 373 #'(lambda (value object)
374 (let ((location (proxy-location object)))
375 (funcall destroy location offset) ; destroy old value
64bce834 376 (funcall writer value location offset))))))))
377
935a783c 378 (call-next-method))
379
380
935a783c 381 ;; TODO: call some C code to detect this a compile time
382 (defconstant +struct-alignmen+ 4)
b44caf77 383
384 (defmethod compute-slots ((class proxy-class))
935a783c 385 (loop
c55abd76 386 with offset = (let ((size-of-super-classes
387 (proxy-instance-size
388 (most-specific-proxy-superclass class))))
389 (+ size-of-super-classes
390 (mod size-of-super-classes +struct-alignmen+)))
935a783c 391 with size = offset
392 for slotd in (class-direct-slots class)
393 when (eq (slot-definition-allocation slotd) :alien)
394 do (if (not (slot-boundp slotd 'offset))
395 (setf (slot-value slotd 'offset) offset)
396 (setq offset (slot-value slotd 'offset)))
397
398 (incf offset (size-of (slot-definition-type slotd)))
399 (incf offset (mod offset +struct-alignmen+))
400 (setq size (max size offset))
401
402 finally (unless (slot-boundp class 'size)
403 (setf (slot-value class 'size) size)))
b44caf77 404 (call-next-method))
8ae7ddc2 405
935a783c 406
407 (defmethod validate-superclass ((class proxy-class) (super standard-class))
408 (subtypep (class-name super) 'proxy))
409
6baf860c 410 (defmethod proxy-instance-size (class)
ba25fa44 411 (declare (ignore class))
412 0)
556b4a05 413
414 (defmethod proxy-instance-size ((class-name symbol))
415 (proxy-instance-size (find-class class-name)))
935a783c 416)
417
6baf860c 418(defmethod alien-type ((class proxy-class) &rest args)
419 (declare (ignore class args))
420 (alien-type 'pointer))
421
422(defmethod size-of ((class proxy-class) &rest args)
423 (declare (ignore class args))
424 (size-of 'pointer))
425
426(defmethod from-alien-form (location (class proxy-class) &rest args)
427 (declare (ignore args))
428 `(ensure-proxy-instance ',(class-name class) ,location))
429
430(defmethod from-alien-function ((class proxy-class) &rest args)
431 (declare (ignore args))
432 #'(lambda (location)
433 (ensure-proxy-instance class location)))
b44caf77 434
6baf860c 435(defmethod to-alien-form (instance (class proxy-class) &rest args)
436 (declare (ignore class args))
437 `(proxy-location ,instance))
b44caf77 438
6baf860c 439(defmethod to-alien-function ((class proxy-class) &rest args)
440 (declare (ignore class args))
441 #'proxy-location)
442
508d13a7 443(defmethod copy-from-alien-form (location (class proxy-class) &rest args)
444 (declare (ignore args))
445 (let ((class-name (class-name class)))
446 `(ensure-proxy-instance ',class-name
447 (reference-foreign ',class-name ,location))))
448
449(defmethod copy-from-alien-function ((class proxy-class) &rest args)
450 (declare (ignore args))
451 #'(lambda (location)
452 (ensure-proxy-instance class (reference-foreign class location))))
453
454(defmethod copy-to-alien-form (instance (class proxy-class) &rest args)
455 (declare (ignore args))
456 `(reference-foreign ',(class-name class) (proxy-location ,instance)))
457
458(defmethod copy-to-alien-function ((class proxy-class) &rest args)
3d36c5d6 459 (declare (ignore args))
508d13a7 460 #'(lambda (instance)
461 (reference-foreign class (proxy-location instance))))
462
6baf860c 463(defmethod writer-function ((class proxy-class) &rest args)
464 (declare (ignore args))
465 #'(lambda (instance location &optional (offset 0))
466 (assert (null-pointer-p (sap-ref-sap location offset)))
467 (setf
468 (sap-ref-sap location offset)
469 (reference-foreign class (proxy-location instance)))))
b44caf77 470
6baf860c 471(defmethod reader-function ((class proxy-class) &rest args)
472 (declare (ignore args))
473 #'(lambda (location &optional (offset 0))
508d13a7 474 (let ((instance (sap-ref-sap location offset)))
475 (unless (null-pointer-p instance)
476 (ensure-proxy-instance class (reference-foreign class instance))))))
b44caf77 477
6baf860c 478(defmethod destroy-function ((class proxy-class) &rest args)
479 (declare (ignore args))
480 #'(lambda (location &optional (offset 0))
481 (unreference-foreign class (sap-ref-sap location offset))))
482
b6bf802c 483(defmethod unbound-value ((class proxy-class) &rest args)
556b4a05 484 (declare (ignore args))
b6bf802c 485 (values t nil))
6baf860c 486
487(defgeneric ensure-proxy-instance (class location)
488 (:documentation "Returns a proxy object representing the foreign object at the give location."))
489
490(defmethod ensure-proxy-instance :around (class location)
491 (unless (null-pointer-p location)
492 (or
493 (find-cached-instance location)
494 (call-next-method))))
495
496(defmethod ensure-proxy-instance ((class symbol) location)
497 (ensure-proxy-instance (find-class class) location))
498
499(defmethod ensure-proxy-instance ((class proxy-class) location)
500 (make-instance class :location location))
b44caf77 501
ba25fa44 502
503;;;; Superclasses for wrapping of C structures
b44caf77 504
6baf860c 505(defclass struct (proxy)
506 ()
507 (:metaclass proxy-class))
b44caf77 508
6baf860c 509(defmethod initialize-instance ((struct struct) &rest initargs)
b44caf77 510 (declare (ignore initargs))
9da5a342 511 (unless (slot-boundp struct 'location)
512 (let ((size (proxy-instance-size (class-of struct))))
513 (if (zerop size)
514 (error "~A has zero size" (class-of struct))
556b4a05 515 (setf (slot-value struct 'location) (allocate-memory size)))))
b44caf77 516 (call-next-method))
517
518
6baf860c 519;;;; Metaclasses used for subclasses of struct
520
521(defclass struct-class (proxy-class)
522 ())
b44caf77 523
6baf860c 524(defmethod reference-foreign ((class struct-class) location)
525 (copy-memory location (proxy-instance-size class)))
526
527(defmethod unreference-foreign ((class struct-class) location)
ba25fa44 528 (deallocate-memory location))
b44caf77 529
530
6baf860c 531(defclass static-struct-class (struct-class)
532 ())
b44caf77 533
6baf860c 534(defmethod reference-foreign ((class static-struct-class) location)
535 (declare (ignore class))
ba25fa44 536 location)
b44caf77 537
6baf860c 538(defmethod unreference-foreign ((class static-struct-class) location)
539 (declare (ignore class location))
ba25fa44 540 nil)
b4edcbf0 541
542
543;;; Pseudo type for structs which are inlined in other objects
544
545(defmethod size-of ((type (eql 'inlined)) &rest args)
546 (declare (ignore type))
547 (proxy-instance-size (first args)))
548
549(defmethod reader-function ((type (eql 'inlined)) &rest args)
550 (declare (ignore type))
551 (destructuring-bind (class) args
552 #'(lambda (location &optional (offset 0))
553 (ensure-proxy-instance class
554 (reference-foreign class (sap+ location offset))))))
555
556(defmethod destroy-function ((type (eql 'inlined)) &rest args)
557 (declare (ignore args))
558 #'(lambda (location &optional (offset 0))
559 (declare (ignore location offset))))
560
561(export 'inlined)