chiark / gitweb /
Added bindings to message dialog widget
[clg] / glib / proxy.lisp
CommitLineData
94f15c3c 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
3b167652 18;; $Id: proxy.lisp,v 1.16 2004-12-19 23:33:57 espen Exp $
94f15c3c 19
20(in-package "GLIB")
21
94f15c3c 22;;;; Superclass for all metaclasses implementing some sort of virtual slots
23
24(eval-when (:compile-toplevel :load-toplevel :execute)
9adccb27 25 (defclass virtual-slots-class (standard-class)
4d83a8a6 26 ())
94f15c3c 27
28 (defclass direct-virtual-slot-definition (standard-direct-slot-definition)
12d0437e 29 ((setter :reader slot-definition-setter :initarg :setter)
4d83a8a6 30 (getter :reader slot-definition-getter :initarg :getter)
eeda1c2d 31 (unbound :reader slot-definition-unbound :initarg :unbound)
4d83a8a6 32 (boundp :reader slot-definition-boundp :initarg :boundp)))
94f15c3c 33
4d83a8a6 34 (defclass effective-virtual-slot-definition (standard-effective-slot-definition)
35 ((setter :reader slot-definition-setter :initarg :setter)
36 (getter :reader slot-definition-getter :initarg :getter)
eeda1c2d 37 (unbound :reader slot-definition-unbound :initarg :unbound)
4d83a8a6 38 (boundp :reader slot-definition-boundp :initarg :boundp)))
eeda1c2d 39
40 (defvar *unbound-marker* (gensym "UNBOUND-MARKER-"))
4d83a8a6 41
eeda1c2d 42 (defun most-specific-slot-value (instances slot &optional
43 (default *unbound-marker*))
4d83a8a6 44 (let ((object (find-if
45 #'(lambda (ob)
46 (and (slot-exists-p ob slot) (slot-boundp ob slot)))
47 instances)))
48 (if object
49 (slot-value object slot)
9adccb27 50 default))))
4d83a8a6 51
94f15c3c 52
53
9adccb27 54(defmethod direct-slot-definition-class ((class virtual-slots-class) &rest initargs)
94f15c3c 55 (if (eq (getf initargs :allocation) :virtual)
56 (find-class 'direct-virtual-slot-definition)
57 (call-next-method)))
58
9adccb27 59(defmethod effective-slot-definition-class ((class virtual-slots-class) &rest initargs)
94f15c3c 60 (if (eq (getf initargs :allocation) :virtual)
61 (find-class 'effective-virtual-slot-definition)
62 (call-next-method)))
63
4d83a8a6 64
65(defmethod initialize-internal-slot-functions ((slotd effective-virtual-slot-definition))
eeda1c2d 66 (if (not (slot-boundp slotd 'getter))
67 (setf
4d83a8a6 68 (slot-value slotd 'reader-function)
eeda1c2d 69 #'(lambda (object)
70 (declare (ignore object))
71 (error "Can't read slot: ~A" (slot-definition-name slotd)))
72 (slot-value slotd 'boundp-function)
73 #'(lambda (object) (declare (ignore object)) nil))
74
75 (let ((getter-function
76 (let ((getter (slot-value slotd 'getter)))
77 (etypecase getter
78 (function getter)
79 (symbol
80 #'(lambda (object)
81 (funcall getter object)))
82 (string
83 (let ((reader nil))
84 (setf (slot-value slotd 'reader-function)
85 #'(lambda (object)
86 (unless reader
87 (setq reader
88 (mkbinding getter
89 (slot-definition-type slotd) 'pointer)))
90 (funcall reader (proxy-location object))))))))))
91
4d83a8a6 92 (setf
eeda1c2d 93 (slot-value slotd 'boundp-function)
94 (cond
95 ((and
96 (not (slot-boundp slotd 'unbound))
97 (not (slot-boundp slotd 'boundp)))
98 #'(lambda (object) (declare (ignore object)) t))
99 ((slot-boundp slotd 'unbound)
100 (let ((unbound-value (slot-value slotd 'unbound)))
101 (lambda (object)
102 (not (eq (funcall getter-function object) unbound-value)))))
103 ((let ((boundp (slot-value slotd 'boundp)))
104 (etypecase boundp
105 (function boundp)
106 (symbol #'(lambda (object)
107 (funcall boundp object)))
108 (string (let ((reader ()))
109 #'(lambda (object)
110 (unless reader
111 (setq reader
112 (mkbinding boundp
113 (slot-definition-type slotd) 'pointer)))
114 (funcall reader (proxy-location object))))))))))
115
116 (setf
117 (slot-value slotd 'reader-function)
118 (cond
119 ((slot-boundp slotd 'unbound)
120 (let ((unbound (slot-value slotd 'unbound))
121 (slot-name (slot-definition-name slotd)))
122 (lambda (object)
123 (let ((value (funcall getter-function object)))
124 (if (eq value unbound)
125 (slot-unbound (class-of object) object slot-name)
126 value)))))
127 ((slot-boundp slotd 'boundp)
128 (let ((boundp-function (slot-value slotd 'boundp-function)))
129 (lambda (object)
130 (and
131 (funcall boundp-function object)
132 (funcall getter-function object)))))
133 (getter-function)))))
134
135 (setf
136 (slot-value slotd 'writer-function)
137 (if (not (slot-boundp slotd 'setter))
138 #'(lambda (object)
139 (declare (ignore object))
140 (error "Can't set slot: ~A" (slot-definition-name slotd)))
141 (with-slots (setter) slotd
4d83a8a6 142 (etypecase setter
143 (function setter)
eeda1c2d 144 ((or symbol cons)
145 #'(lambda (value object)
146 (funcall (fdefinition setter) value object)))
7d1ddc9e 147 (string
eeda1c2d 148 (let ((writer ()))
149 (setf
150 (slot-value slotd 'writer-function)
151 #'(lambda (value object)
152 (unless writer
153 (setq writer
154 (mkbinding setter 'nil 'pointer
155 (slot-definition-type slotd))))
156 (funcall writer (proxy-location object) value)))))))))
157
4d83a8a6 158 (initialize-internal-slot-gfs (slot-definition-name slotd)))
159
160
161
eeda1c2d 162(defmethod compute-slot-accessor-info ((slotd effective-virtual-slot-definition) type gf)
4d83a8a6 163 nil)
164
9adccb27 165(defmethod compute-effective-slot-definition-initargs ((class virtual-slots-class) direct-slotds)
eeda1c2d 166 (if (typep (first direct-slotds) 'direct-virtual-slot-definition)
167 (let ((initargs ()))
168 (let ((getter (most-specific-slot-value direct-slotds 'getter)))
169 (unless (eq getter *unbound-marker*)
170 (setf (getf initargs :getter) getter)))
171 (let ((setter (most-specific-slot-value direct-slotds 'setter)))
172 (unless (eq setter *unbound-marker*)
173 (setf (getf initargs :setter) setter)))
174 (let ((unbound (most-specific-slot-value direct-slotds 'unbound)))
175 (unless (eq unbound *unbound-marker*)
176 (setf (getf initargs :unbound) unbound)))
177 (let ((boundp (most-specific-slot-value direct-slotds 'boundp)))
178 (unless (eq boundp *unbound-marker*)
179 (setf (getf initargs :boundp) boundp)))
180 (nconc initargs (call-next-method)))
4d83a8a6 181 (call-next-method)))
182
94f15c3c 183
94f15c3c 184(defmethod slot-value-using-class
9adccb27 185 ((class virtual-slots-class) (object standard-object)
94f15c3c 186 (slotd effective-virtual-slot-definition))
4d83a8a6 187 (if (funcall (slot-value slotd 'boundp-function) object)
188 (funcall (slot-value slotd 'reader-function) object)
189 (slot-unbound class object (slot-definition-name slotd))))
94f15c3c 190
94f15c3c 191(defmethod slot-boundp-using-class
9adccb27 192 ((class virtual-slots-class) (object standard-object)
94f15c3c 193 (slotd effective-virtual-slot-definition))
4d83a8a6 194 (funcall (slot-value slotd 'boundp-function) object))
195
196(defmethod (setf slot-value-using-class)
9adccb27 197 (value (class virtual-slots-class) (object standard-object)
94f15c3c 198 (slotd effective-virtual-slot-definition))
4d83a8a6 199 (funcall (slot-value slotd 'writer-function) value object))
200
201
94f15c3c 202(defmethod validate-superclass
9adccb27 203 ((class virtual-slots-class) (super standard-class))
94f15c3c 204 t)
205
206
207;;;; Proxy cache
208
209(internal *instance-cache*)
210(defvar *instance-cache* (make-hash-table :test #'eql))
211
212(defun cache-instance (instance)
213 (setf
214 (gethash (system:sap-int (proxy-location instance)) *instance-cache*)
215 (ext:make-weak-pointer instance)))
216
217(defun find-cached-instance (location)
218 (let ((ref (gethash (system:sap-int location) *instance-cache*)))
219 (when ref
220 (ext:weak-pointer-value ref))))
221
0f134a29 222(defun instance-cached-p (location)
223 (gethash (system:sap-int location) *instance-cache*))
224
94f15c3c 225(defun remove-cached-instance (location)
226 (remhash (system:sap-int location) *instance-cache*))
227
9adccb27 228;; For debuging
229(defun cached-instances ()
230 (let ((instances ()))
231 (maphash #'(lambda (location ref)
232 (declare (ignore location))
233 (push (ext:weak-pointer-value ref) instances))
234 *instance-cache*)
235 instances))
236
94f15c3c 237
238
239;;;; Proxy for alien instances
240
9adccb27 241(defclass proxy ()
242 ((location :reader proxy-location :type system-area-pointer)))
94f15c3c 243
9adccb27 244(defgeneric initialize-proxy (object &rest initargs))
245(defgeneric instance-finalizer (object))
246(defgeneric reference-foreign (class location))
247(defgeneric unreference-foreign (class location))
248
3b167652 249(defmethod reference-foreign ((name symbol) location)
250 (reference-foreign (find-class name) location))
251
252(defmethod unreference-foreign ((name symbol) location)
253 (unreference-foreign (find-class name) location))
254
9adccb27 255(defmethod unreference-foreign :around ((class class) location)
256 (unless (null-pointer-p location)
257;; (format t "Unreferencing ~A at ~A" (class-name class) location)
258;; (finish-output *standard-output*)
259 (call-next-method)
260;; (write-line " done")
261;; (finish-output *standard-output*)
262 ))
94f15c3c 263
0f134a29 264(defmethod print-object ((instance proxy) stream)
265 (print-unreadable-object (instance stream :type t :identity nil)
eeda1c2d 266 (when (slot-boundp instance 'location)
267 (format stream "at 0x~X" (sap-int (proxy-location instance))))))
94f15c3c 268
9adccb27 269(defmethod initialize-instance :around ((instance proxy) &key location)
270 (if location
271 (setf (slot-value instance 'location) location)
272 (call-next-method))
12d0437e 273 (cache-instance instance)
9adccb27 274 (ext:finalize instance (instance-finalizer instance))
275 instance)
94f15c3c 276
277(defmethod instance-finalizer ((instance proxy))
9adccb27 278 (let ((location (proxy-location instance))
279 (class (class-of instance)))
280;; (unless (find-method #'unreference-foreign nil (list (class-of class) t) nil)
281;; (error "No matching method for UNREFERENCE-INSTANCE when called with class ~A" class))
282 #'(lambda ()
1dbf4216 283 (remove-cached-instance location)
9adccb27 284 (unreference-foreign class location))))
12d0437e 285
94f15c3c 286
287;;;; Metaclass used for subclasses of proxy
288
289(eval-when (:compile-toplevel :load-toplevel :execute)
9adccb27 290 (defclass proxy-class (virtual-slots-class)
291 ((size :reader proxy-instance-size)))
94f15c3c 292
293 (defclass direct-alien-slot-definition (direct-virtual-slot-definition)
12d0437e 294 ((allocation :initform :alien)
295 (offset :reader slot-definition-offset :initarg :offset)))
94f15c3c 296
297 (defclass effective-alien-slot-definition (effective-virtual-slot-definition)
4d83a8a6 298 ((offset :reader slot-definition-offset :initarg :offset)))
12d0437e 299
94f15c3c 300
301 (defmethod most-specific-proxy-superclass ((class proxy-class))
302 (find-if
303 #'(lambda (class)
304 (subtypep (class-name class) 'proxy))
4d83a8a6 305 (cdr (compute-class-precedence-list class))))
306
12d0437e 307 (defmethod direct-proxy-superclass ((class proxy-class))
308 (find-if
309 #'(lambda (class)
310 (subtypep (class-name class) 'proxy))
4d83a8a6 311 (class-direct-superclasses class)))
312
eeda1c2d 313 (defmethod shared-initialize ((class proxy-class) names &key size)
94f15c3c 314 (call-next-method)
12d0437e 315 (cond
4d83a8a6 316 (size (setf (slot-value class 'size) (first size)))
9adccb27 317 ((slot-boundp class 'size) (slot-makunbound class 'size))))
4d83a8a6 318
319 (defmethod direct-slot-definition-class ((class proxy-class) &rest initargs)
94f15c3c 320 (case (getf initargs :allocation)
321 ((nil :alien) (find-class 'direct-alien-slot-definition))
94f15c3c 322 (t (call-next-method))))
4d83a8a6 323
324 (defmethod effective-slot-definition-class ((class proxy-class) &rest initargs)
94f15c3c 325 (case (getf initargs :allocation)
326 (:alien (find-class 'effective-alien-slot-definition))
94f15c3c 327 (t (call-next-method))))
328
4d83a8a6 329
330 (defmethod compute-effective-slot-definition-initargs ((class proxy-class) direct-slotds)
331 (if (eq (most-specific-slot-value direct-slotds 'allocation) :alien)
332 (nconc
333 (list :offset (most-specific-slot-value direct-slotds 'offset))
334 (call-next-method))
335 (call-next-method)))
336
337
338 (defmethod initialize-internal-slot-functions ((slotd effective-alien-slot-definition))
339 (with-slots (offset) slotd
9adccb27 340 (let ((type (slot-definition-type slotd)))
eeda1c2d 341 (unless (slot-boundp slotd 'getter)
9adccb27 342 (let ((reader (reader-function type)))
343 (setf
eeda1c2d 344 (slot-value slotd 'getter)
9adccb27 345 #'(lambda (object)
346 (funcall reader (proxy-location object) offset)))))
4d83a8a6 347
eeda1c2d 348 (unless (slot-boundp slotd 'setter)
9adccb27 349 (let ((writer (writer-function type))
350 (destroy (destroy-function type)))
351 (setf
eeda1c2d 352 (slot-value slotd 'setter)
9adccb27 353 #'(lambda (value object)
354 (let ((location (proxy-location object)))
355 (funcall destroy location offset) ; destroy old value
eeda1c2d 356 (funcall writer value location offset))))))))
357
4d83a8a6 358 (call-next-method))
359
360
4d83a8a6 361 ;; TODO: call some C code to detect this a compile time
362 (defconstant +struct-alignmen+ 4)
94f15c3c 363
364 (defmethod compute-slots ((class proxy-class))
4d83a8a6 365 (loop
3b167652 366 with offset = (let ((size-of-super-classes
367 (proxy-instance-size
368 (most-specific-proxy-superclass class))))
369 (+ size-of-super-classes
370 (mod size-of-super-classes +struct-alignmen+)))
4d83a8a6 371 with size = offset
372 for slotd in (class-direct-slots class)
373 when (eq (slot-definition-allocation slotd) :alien)
374 do (if (not (slot-boundp slotd 'offset))
375 (setf (slot-value slotd 'offset) offset)
376 (setq offset (slot-value slotd 'offset)))
377
378 (incf offset (size-of (slot-definition-type slotd)))
379 (incf offset (mod offset +struct-alignmen+))
380 (setq size (max size offset))
381
382 finally (unless (slot-boundp class 'size)
383 (setf (slot-value class 'size) size)))
94f15c3c 384 (call-next-method))
3e15002d 385
4d83a8a6 386
387 (defmethod validate-superclass ((class proxy-class) (super standard-class))
388 (subtypep (class-name super) 'proxy))
389
9adccb27 390 (defmethod proxy-instance-size (class)
12d0437e 391 (declare (ignore class))
392 0)
4d83a8a6 393)
394
9adccb27 395(defmethod alien-type ((class proxy-class) &rest args)
396 (declare (ignore class args))
397 (alien-type 'pointer))
398
399(defmethod size-of ((class proxy-class) &rest args)
400 (declare (ignore class args))
401 (size-of 'pointer))
402
403(defmethod from-alien-form (location (class proxy-class) &rest args)
404 (declare (ignore args))
405 `(ensure-proxy-instance ',(class-name class) ,location))
406
407(defmethod from-alien-function ((class proxy-class) &rest args)
408 (declare (ignore args))
409 #'(lambda (location)
410 (ensure-proxy-instance class location)))
94f15c3c 411
9adccb27 412(defmethod to-alien-form (instance (class proxy-class) &rest args)
413 (declare (ignore class args))
414 `(proxy-location ,instance))
94f15c3c 415
9adccb27 416(defmethod to-alien-function ((class proxy-class) &rest args)
417 (declare (ignore class args))
418 #'proxy-location)
419
9ca5565a 420(defmethod copy-from-alien-form (location (class proxy-class) &rest args)
421 (declare (ignore args))
422 (let ((class-name (class-name class)))
423 `(ensure-proxy-instance ',class-name
424 (reference-foreign ',class-name ,location))))
425
426(defmethod copy-from-alien-function ((class proxy-class) &rest args)
427 (declare (ignore args))
428 #'(lambda (location)
429 (ensure-proxy-instance class (reference-foreign class location))))
430
431(defmethod copy-to-alien-form (instance (class proxy-class) &rest args)
432 (declare (ignore args))
433 `(reference-foreign ',(class-name class) (proxy-location ,instance)))
434
435(defmethod copy-to-alien-function ((class proxy-class) &rest args)
436 (declare (ignore class args))
437 #'(lambda (instance)
438 (reference-foreign class (proxy-location instance))))
439
9adccb27 440(defmethod writer-function ((class proxy-class) &rest args)
441 (declare (ignore args))
442 #'(lambda (instance location &optional (offset 0))
443 (assert (null-pointer-p (sap-ref-sap location offset)))
444 (setf
445 (sap-ref-sap location offset)
446 (reference-foreign class (proxy-location instance)))))
94f15c3c 447
9adccb27 448(defmethod reader-function ((class proxy-class) &rest args)
449 (declare (ignore args))
450 #'(lambda (location &optional (offset 0))
9ca5565a 451 (let ((instance (sap-ref-sap location offset)))
452 (unless (null-pointer-p instance)
453 (ensure-proxy-instance class (reference-foreign class instance))))))
94f15c3c 454
9adccb27 455(defmethod destroy-function ((class proxy-class) &rest args)
456 (declare (ignore args))
457 #'(lambda (location &optional (offset 0))
458 (unreference-foreign class (sap-ref-sap location offset))))
459
460
461(defgeneric ensure-proxy-instance (class location)
462 (:documentation "Returns a proxy object representing the foreign object at the give location."))
463
464(defmethod ensure-proxy-instance :around (class location)
465 (unless (null-pointer-p location)
466 (or
467 (find-cached-instance location)
468 (call-next-method))))
469
470(defmethod ensure-proxy-instance ((class symbol) location)
471 (ensure-proxy-instance (find-class class) location))
472
473(defmethod ensure-proxy-instance ((class proxy-class) location)
474 (make-instance class :location location))
94f15c3c 475
12d0437e 476
477;;;; Superclasses for wrapping of C structures
94f15c3c 478
9adccb27 479(defclass struct (proxy)
480 ()
481 (:metaclass proxy-class))
94f15c3c 482
9adccb27 483(defmethod initialize-instance ((struct struct) &rest initargs)
94f15c3c 484 (declare (ignore initargs))
ec1a4146 485 (unless (slot-boundp struct 'location)
486 (let ((size (proxy-instance-size (class-of struct))))
487 (if (zerop size)
488 (error "~A has zero size" (class-of struct))
489 (setf (slot-value struct 'location) (allocate-memory size)))))
94f15c3c 490 (call-next-method))
491
492
9adccb27 493;;;; Metaclasses used for subclasses of struct
494
495(defclass struct-class (proxy-class)
496 ())
94f15c3c 497
9adccb27 498(defmethod reference-foreign ((class struct-class) location)
499 (copy-memory location (proxy-instance-size class)))
500
501(defmethod unreference-foreign ((class struct-class) location)
12d0437e 502 (deallocate-memory location))
94f15c3c 503
504
9adccb27 505(defclass static-struct-class (struct-class)
506 ())
94f15c3c 507
9adccb27 508(defmethod reference-foreign ((class static-struct-class) location)
509 (declare (ignore class))
12d0437e 510 location)
94f15c3c 511
9adccb27 512(defmethod unreference-foreign ((class static-struct-class) location)
513 (declare (ignore class location))
12d0437e 514 nil)