chiark / gitweb /
Slots with instance allocation can now safely be added to gobject classes
[clg] / glib / proxy.lisp
CommitLineData
112ac1d3 1;; Common Lisp bindings for GTK+ v2.x
2;; Copyright 2000-2005 Espen S. Johnsen <espen@users.sf.net>
94f15c3c 3;;
112ac1d3 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:
94f15c3c 11;;
112ac1d3 12;; The above copyright notice and this permission notice shall be
13;; included in all copies or substantial portions of the Software.
94f15c3c 14;;
112ac1d3 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.
94f15c3c 22
982a215a 23;; $Id: proxy.lisp,v 1.22 2006-02-02 18:37:46 espen Exp $
94f15c3c 24
25(in-package "GLIB")
26
94f15c3c 27;;;; Superclass for all metaclasses implementing some sort of virtual slots
28
29(eval-when (:compile-toplevel :load-toplevel :execute)
9adccb27 30 (defclass virtual-slots-class (standard-class)
4d83a8a6 31 ())
94f15c3c 32
33 (defclass direct-virtual-slot-definition (standard-direct-slot-definition)
12d0437e 34 ((setter :reader slot-definition-setter :initarg :setter)
4d83a8a6 35 (getter :reader slot-definition-getter :initarg :getter)
eeda1c2d 36 (unbound :reader slot-definition-unbound :initarg :unbound)
4d83a8a6 37 (boundp :reader slot-definition-boundp :initarg :boundp)))
94f15c3c 38
4d83a8a6 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)
eeda1c2d 42 (unbound :reader slot-definition-unbound :initarg :unbound)
73572c12 43 (boundp :reader slot-definition-boundp :initarg :boundp))))
eeda1c2d 44
45 (defvar *unbound-marker* (gensym "UNBOUND-MARKER-"))
4d83a8a6 46
eeda1c2d 47 (defun most-specific-slot-value (instances slot &optional
48 (default *unbound-marker*))
4d83a8a6 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)
73572c12 55 default)));)
4d83a8a6 56
94f15c3c 57
58
9adccb27 59(defmethod direct-slot-definition-class ((class virtual-slots-class) &rest initargs)
94f15c3c 60 (if (eq (getf initargs :allocation) :virtual)
61 (find-class 'direct-virtual-slot-definition)
62 (call-next-method)))
63
9adccb27 64(defmethod effective-slot-definition-class ((class virtual-slots-class) &rest initargs)
94f15c3c 65 (if (eq (getf initargs :allocation) :virtual)
66 (find-class 'effective-virtual-slot-definition)
67 (call-next-method)))
68
4d83a8a6 69
70(defmethod initialize-internal-slot-functions ((slotd effective-virtual-slot-definition))
eeda1c2d 71 (if (not (slot-boundp slotd 'getter))
72 (setf
4d83a8a6 73 (slot-value slotd 'reader-function)
eeda1c2d 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
12b7df04 92 (setq reader
93 (mkbinding getter
94 (slot-definition-type slotd) 'pointer)))
eeda1c2d 95 (funcall reader (proxy-location object))))))))))
96
4d83a8a6 97 (setf
eeda1c2d 98 (slot-value slotd 'boundp-function)
99 (cond
eeda1c2d 100 ((slot-boundp slotd 'unbound)
101 (let ((unbound-value (slot-value slotd 'unbound)))
12b7df04 102 #'(lambda (object)
103 (not (eq (funcall getter-function object) unbound-value)))))
104 ((slot-boundp slotd 'boundp)
105 (let ((boundp (slot-value slotd 'boundp)))
eeda1c2d 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)))
12b7df04 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))))
eeda1c2d 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)))
12b7df04 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)))))
eeda1c2d 135 ((slot-boundp slotd 'boundp)
136 (let ((boundp-function (slot-value slotd 'boundp-function)))
12b7df04 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)))))))
eeda1c2d 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
4d83a8a6 159 (etypecase setter
160 (function setter)
eeda1c2d 161 ((or symbol cons)
162 #'(lambda (value object)
163 (funcall (fdefinition setter) value object)))
7d1ddc9e 164 (string
eeda1c2d 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
4d83a8a6 175 (initialize-internal-slot-gfs (slot-definition-name slotd)))
176
177
178
eeda1c2d 179(defmethod compute-slot-accessor-info ((slotd effective-virtual-slot-definition) type gf)
4d83a8a6 180 nil)
181
9adccb27 182(defmethod compute-effective-slot-definition-initargs ((class virtual-slots-class) direct-slotds)
eeda1c2d 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)))
4d83a8a6 198 (call-next-method)))
199
94f15c3c 200
94f15c3c 201(defmethod slot-value-using-class
9adccb27 202 ((class virtual-slots-class) (object standard-object)
94f15c3c 203 (slotd effective-virtual-slot-definition))
4d83a8a6 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))))
94f15c3c 207
94f15c3c 208(defmethod slot-boundp-using-class
9adccb27 209 ((class virtual-slots-class) (object standard-object)
94f15c3c 210 (slotd effective-virtual-slot-definition))
4d83a8a6 211 (funcall (slot-value slotd 'boundp-function) object))
212
213(defmethod (setf slot-value-using-class)
9adccb27 214 (value (class virtual-slots-class) (object standard-object)
94f15c3c 215 (slotd effective-virtual-slot-definition))
4d83a8a6 216 (funcall (slot-value slotd 'writer-function) value object))
217
218
94f15c3c 219(defmethod validate-superclass
9adccb27 220 ((class virtual-slots-class) (super standard-class))
94f15c3c 221 t)
222
223
224;;;; Proxy cache
225
226(internal *instance-cache*)
227(defvar *instance-cache* (make-hash-table :test #'eql))
228
982a215a 229(defun cache-instance (instance &optional (weak-ref t))
94f15c3c 230 (setf
73572c12 231 (gethash (sap-int (proxy-location instance)) *instance-cache*)
982a215a 232 (if weak-ref
233 (make-weak-pointer instance)
234 instance)))
94f15c3c 235
236(defun find-cached-instance (location)
73572c12 237 (let ((ref (gethash (sap-int location) *instance-cache*)))
94f15c3c 238 (when ref
982a215a 239 (if (weak-pointer-p ref)
240 (weak-pointer-value ref)
241 ref))))
94f15c3c 242
0f134a29 243(defun instance-cached-p (location)
73572c12 244 (gethash (sap-int location) *instance-cache*))
0f134a29 245
94f15c3c 246(defun remove-cached-instance (location)
73572c12 247 (remhash (sap-int location) *instance-cache*))
94f15c3c 248
9adccb27 249;; For debuging
982a215a 250(defun list-cached-instances ()
9adccb27 251 (let ((instances ()))
252 (maphash #'(lambda (location ref)
253 (declare (ignore location))
982a215a 254 (push ref instances))
9adccb27 255 *instance-cache*)
256 instances))
257
94f15c3c 258
259
260;;;; Proxy for alien instances
261
9adccb27 262(defclass proxy ()
263 ((location :reader proxy-location :type system-area-pointer)))
94f15c3c 264
9adccb27 265(defgeneric instance-finalizer (object))
266(defgeneric reference-foreign (class location))
267(defgeneric unreference-foreign (class location))
268
3b167652 269(defmethod reference-foreign ((name symbol) location)
270 (reference-foreign (find-class name) location))
271
272(defmethod unreference-foreign ((name symbol) location)
273 (unreference-foreign (find-class name) location))
274
9adccb27 275(defmethod unreference-foreign :around ((class class) location)
276 (unless (null-pointer-p location)
277;; (format t "Unreferencing ~A at ~A" (class-name class) location)
278;; (finish-output *standard-output*)
279 (call-next-method)
280;; (write-line " done")
281;; (finish-output *standard-output*)
282 ))
94f15c3c 283
0f134a29 284(defmethod print-object ((instance proxy) stream)
285 (print-unreadable-object (instance stream :type t :identity nil)
eeda1c2d 286 (when (slot-boundp instance 'location)
287 (format stream "at 0x~X" (sap-int (proxy-location instance))))))
94f15c3c 288
9adccb27 289(defmethod initialize-instance :around ((instance proxy) &key location)
290 (if location
291 (setf (slot-value instance 'location) location)
292 (call-next-method))
12d0437e 293 (cache-instance instance)
73572c12 294 (finalize instance (instance-finalizer instance))
9adccb27 295 instance)
94f15c3c 296
297(defmethod instance-finalizer ((instance proxy))
9adccb27 298 (let ((location (proxy-location instance))
299 (class (class-of instance)))
300;; (unless (find-method #'unreference-foreign nil (list (class-of class) t) nil)
301;; (error "No matching method for UNREFERENCE-INSTANCE when called with class ~A" class))
302 #'(lambda ()
1dbf4216 303 (remove-cached-instance location)
9adccb27 304 (unreference-foreign class location))))
12d0437e 305
94f15c3c 306
307;;;; Metaclass used for subclasses of proxy
308
73572c12 309(defgeneric most-specific-proxy-superclass (class))
310(defgeneric direct-proxy-superclass (class))
311
312
94f15c3c 313(eval-when (:compile-toplevel :load-toplevel :execute)
9adccb27 314 (defclass proxy-class (virtual-slots-class)
315 ((size :reader proxy-instance-size)))
94f15c3c 316
317 (defclass direct-alien-slot-definition (direct-virtual-slot-definition)
12d0437e 318 ((allocation :initform :alien)
319 (offset :reader slot-definition-offset :initarg :offset)))
94f15c3c 320
321 (defclass effective-alien-slot-definition (effective-virtual-slot-definition)
4d83a8a6 322 ((offset :reader slot-definition-offset :initarg :offset)))
12d0437e 323
94f15c3c 324 (defmethod most-specific-proxy-superclass ((class proxy-class))
325 (find-if
326 #'(lambda (class)
327 (subtypep (class-name class) 'proxy))
4d83a8a6 328 (cdr (compute-class-precedence-list class))))
73572c12 329
12d0437e 330 (defmethod direct-proxy-superclass ((class proxy-class))
331 (find-if
332 #'(lambda (class)
333 (subtypep (class-name class) 'proxy))
4d83a8a6 334 (class-direct-superclasses class)))
335
eeda1c2d 336 (defmethod shared-initialize ((class proxy-class) names &key size)
94f15c3c 337 (call-next-method)
12d0437e 338 (cond
4d83a8a6 339 (size (setf (slot-value class 'size) (first size)))
9adccb27 340 ((slot-boundp class 'size) (slot-makunbound class 'size))))
4d83a8a6 341
342 (defmethod direct-slot-definition-class ((class proxy-class) &rest initargs)
94f15c3c 343 (case (getf initargs :allocation)
344 ((nil :alien) (find-class 'direct-alien-slot-definition))
94f15c3c 345 (t (call-next-method))))
4d83a8a6 346
347 (defmethod effective-slot-definition-class ((class proxy-class) &rest initargs)
94f15c3c 348 (case (getf initargs :allocation)
349 (:alien (find-class 'effective-alien-slot-definition))
94f15c3c 350 (t (call-next-method))))
351
4d83a8a6 352
353 (defmethod compute-effective-slot-definition-initargs ((class proxy-class) direct-slotds)
354 (if (eq (most-specific-slot-value direct-slotds 'allocation) :alien)
355 (nconc
356 (list :offset (most-specific-slot-value direct-slotds 'offset))
357 (call-next-method))
358 (call-next-method)))
359
360
361 (defmethod initialize-internal-slot-functions ((slotd effective-alien-slot-definition))
362 (with-slots (offset) slotd
9adccb27 363 (let ((type (slot-definition-type slotd)))
eeda1c2d 364 (unless (slot-boundp slotd 'getter)
9adccb27 365 (let ((reader (reader-function type)))
366 (setf
eeda1c2d 367 (slot-value slotd 'getter)
9adccb27 368 #'(lambda (object)
369 (funcall reader (proxy-location object) offset)))))
4d83a8a6 370
eeda1c2d 371 (unless (slot-boundp slotd 'setter)
9adccb27 372 (let ((writer (writer-function type))
373 (destroy (destroy-function type)))
374 (setf
eeda1c2d 375 (slot-value slotd 'setter)
9adccb27 376 #'(lambda (value object)
377 (let ((location (proxy-location object)))
378 (funcall destroy location offset) ; destroy old value
eeda1c2d 379 (funcall writer value location offset))))))))
380
4d83a8a6 381 (call-next-method))
382
383
4d83a8a6 384 ;; TODO: call some C code to detect this a compile time
385 (defconstant +struct-alignmen+ 4)
94f15c3c 386
387 (defmethod compute-slots ((class proxy-class))
4d83a8a6 388 (loop
3b167652 389 with offset = (let ((size-of-super-classes
390 (proxy-instance-size
391 (most-specific-proxy-superclass class))))
392 (+ size-of-super-classes
393 (mod size-of-super-classes +struct-alignmen+)))
4d83a8a6 394 with size = offset
395 for slotd in (class-direct-slots class)
396 when (eq (slot-definition-allocation slotd) :alien)
397 do (if (not (slot-boundp slotd 'offset))
398 (setf (slot-value slotd 'offset) offset)
399 (setq offset (slot-value slotd 'offset)))
400
401 (incf offset (size-of (slot-definition-type slotd)))
402 (incf offset (mod offset +struct-alignmen+))
403 (setq size (max size offset))
404
405 finally (unless (slot-boundp class 'size)
406 (setf (slot-value class 'size) size)))
94f15c3c 407 (call-next-method))
3e15002d 408
4d83a8a6 409
410 (defmethod validate-superclass ((class proxy-class) (super standard-class))
411 (subtypep (class-name super) 'proxy))
412
9adccb27 413 (defmethod proxy-instance-size (class)
12d0437e 414 (declare (ignore class))
415 0)
47a11c16 416
417 (defmethod proxy-instance-size ((class-name symbol))
418 (proxy-instance-size (find-class class-name)))
4d83a8a6 419)
420
9adccb27 421(defmethod alien-type ((class proxy-class) &rest args)
422 (declare (ignore class args))
423 (alien-type 'pointer))
424
425(defmethod size-of ((class proxy-class) &rest args)
426 (declare (ignore class args))
427 (size-of 'pointer))
428
429(defmethod from-alien-form (location (class proxy-class) &rest args)
430 (declare (ignore args))
431 `(ensure-proxy-instance ',(class-name class) ,location))
432
433(defmethod from-alien-function ((class proxy-class) &rest args)
434 (declare (ignore args))
435 #'(lambda (location)
436 (ensure-proxy-instance class location)))
94f15c3c 437
9adccb27 438(defmethod to-alien-form (instance (class proxy-class) &rest args)
439 (declare (ignore class args))
440 `(proxy-location ,instance))
94f15c3c 441
9adccb27 442(defmethod to-alien-function ((class proxy-class) &rest args)
443 (declare (ignore class args))
444 #'proxy-location)
445
9ca5565a 446(defmethod copy-from-alien-form (location (class proxy-class) &rest args)
447 (declare (ignore args))
448 (let ((class-name (class-name class)))
449 `(ensure-proxy-instance ',class-name
450 (reference-foreign ',class-name ,location))))
451
452(defmethod copy-from-alien-function ((class proxy-class) &rest args)
453 (declare (ignore args))
454 #'(lambda (location)
455 (ensure-proxy-instance class (reference-foreign class location))))
456
457(defmethod copy-to-alien-form (instance (class proxy-class) &rest args)
458 (declare (ignore args))
459 `(reference-foreign ',(class-name class) (proxy-location ,instance)))
460
461(defmethod copy-to-alien-function ((class proxy-class) &rest args)
73572c12 462 (declare (ignore args))
9ca5565a 463 #'(lambda (instance)
464 (reference-foreign class (proxy-location instance))))
465
9adccb27 466(defmethod writer-function ((class proxy-class) &rest args)
467 (declare (ignore args))
468 #'(lambda (instance location &optional (offset 0))
469 (assert (null-pointer-p (sap-ref-sap location offset)))
470 (setf
471 (sap-ref-sap location offset)
472 (reference-foreign class (proxy-location instance)))))
94f15c3c 473
9adccb27 474(defmethod reader-function ((class proxy-class) &rest args)
475 (declare (ignore args))
476 #'(lambda (location &optional (offset 0))
9ca5565a 477 (let ((instance (sap-ref-sap location offset)))
478 (unless (null-pointer-p instance)
479 (ensure-proxy-instance class (reference-foreign class instance))))))
94f15c3c 480
9adccb27 481(defmethod destroy-function ((class proxy-class) &rest args)
482 (declare (ignore args))
483 #'(lambda (location &optional (offset 0))
484 (unreference-foreign class (sap-ref-sap location offset))))
485
12b7df04 486(defmethod unbound-value ((class proxy-class) &rest args)
47a11c16 487 (declare (ignore args))
12b7df04 488 (values t nil))
9adccb27 489
490(defgeneric ensure-proxy-instance (class location)
491 (:documentation "Returns a proxy object representing the foreign object at the give location."))
492
493(defmethod ensure-proxy-instance :around (class location)
494 (unless (null-pointer-p location)
495 (or
496 (find-cached-instance location)
497 (call-next-method))))
498
499(defmethod ensure-proxy-instance ((class symbol) location)
500 (ensure-proxy-instance (find-class class) location))
501
502(defmethod ensure-proxy-instance ((class proxy-class) location)
503 (make-instance class :location location))
94f15c3c 504
12d0437e 505
506;;;; Superclasses for wrapping of C structures
94f15c3c 507
9adccb27 508(defclass struct (proxy)
509 ()
510 (:metaclass proxy-class))
94f15c3c 511
9adccb27 512(defmethod initialize-instance ((struct struct) &rest initargs)
94f15c3c 513 (declare (ignore initargs))
ec1a4146 514 (unless (slot-boundp struct 'location)
515 (let ((size (proxy-instance-size (class-of struct))))
516 (if (zerop size)
517 (error "~A has zero size" (class-of struct))
47a11c16 518 (setf (slot-value struct 'location) (allocate-memory size)))))
94f15c3c 519 (call-next-method))
520
521
9adccb27 522;;;; Metaclasses used for subclasses of struct
523
524(defclass struct-class (proxy-class)
525 ())
94f15c3c 526
9adccb27 527(defmethod reference-foreign ((class struct-class) location)
528 (copy-memory location (proxy-instance-size class)))
529
530(defmethod unreference-foreign ((class struct-class) location)
12d0437e 531 (deallocate-memory location))
94f15c3c 532
533
9adccb27 534(defclass static-struct-class (struct-class)
535 ())
94f15c3c 536
9adccb27 537(defmethod reference-foreign ((class static-struct-class) location)
538 (declare (ignore class))
12d0437e 539 location)
94f15c3c 540
9adccb27 541(defmethod unreference-foreign ((class static-struct-class) location)
542 (declare (ignore class location))
12d0437e 543 nil)
bde0b906 544
545
546;;; Pseudo type for structs which are inlined in other objects
547
548(defmethod size-of ((type (eql 'inlined)) &rest args)
549 (declare (ignore type))
550 (proxy-instance-size (first args)))
551
552(defmethod reader-function ((type (eql 'inlined)) &rest args)
553 (declare (ignore type))
554 (destructuring-bind (class) args
555 #'(lambda (location &optional (offset 0))
556 (ensure-proxy-instance class
557 (reference-foreign class (sap+ location offset))))))
558
559(defmethod destroy-function ((type (eql 'inlined)) &rest args)
560 (declare (ignore args))
561 #'(lambda (location &optional (offset 0))
562 (declare (ignore location offset))))
563
564(export 'inlined)