chiark / gitweb /
Bug fix: boxed should have struct as super class
[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
9adccb27 18;; $Id: proxy.lisp,v 1.11 2004-11-06 21:39:58 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)
31 (boundp :reader slot-definition-boundp :initarg :boundp)))
94f15c3c 32
4d83a8a6 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)
9adccb27 45 default))))
4d83a8a6 46
94f15c3c 47
48
9adccb27 49(defmethod direct-slot-definition-class ((class virtual-slots-class) &rest initargs)
94f15c3c 50 (if (eq (getf initargs :allocation) :virtual)
51 (find-class 'direct-virtual-slot-definition)
52 (call-next-method)))
53
9adccb27 54(defmethod effective-slot-definition-class ((class virtual-slots-class) &rest initargs)
94f15c3c 55 (if (eq (getf initargs :allocation) :virtual)
56 (find-class 'effective-virtual-slot-definition)
57 (call-next-method)))
58
4d83a8a6 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)
7d1ddc9e 71 (funcall getter object)))
9adccb27 72 (string ;(let ()(reader (mkbinding getter
73;; (slot-definition-type slotd) 'pointer)))
7d1ddc9e 74 (setf (slot-value slotd 'reader-function)
75 #'(lambda (object)
9adccb27 76 (let ((reader
77 (mkbinding getter
78 (slot-definition-type slotd) 'pointer)))
7d1ddc9e 79 (funcall reader (proxy-location object)))))))))
4d83a8a6 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)
7d1ddc9e 90 (funcall (fdefinition setter) value object)))
91 (string
9adccb27 92 (let ((writer ()));; (mkbinding setter 'nil 'pointer
93;; (slot-definition-type slotd))))
7d1ddc9e 94 (setf (slot-value slotd 'writer-function)
95 #'(lambda (value object)
9adccb27 96 (unless writer
97 (setq writer
98 (mkbinding setter 'nil 'pointer
99 (slot-definition-type slotd))))
7d1ddc9e 100 (funcall writer (proxy-location object) value))))))))
4d83a8a6 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
9adccb27 120(defmethod compute-effective-slot-definition-initargs ((class virtual-slots-class) direct-slotds)
4d83a8a6 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
94f15c3c 129
94f15c3c 130(defmethod slot-value-using-class
9adccb27 131 ((class virtual-slots-class) (object standard-object)
94f15c3c 132 (slotd effective-virtual-slot-definition))
4d83a8a6 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))))
94f15c3c 136
94f15c3c 137(defmethod slot-boundp-using-class
9adccb27 138 ((class virtual-slots-class) (object standard-object)
94f15c3c 139 (slotd effective-virtual-slot-definition))
4d83a8a6 140 (funcall (slot-value slotd 'boundp-function) object))
141
142(defmethod (setf slot-value-using-class)
9adccb27 143 (value (class virtual-slots-class) (object standard-object)
94f15c3c 144 (slotd effective-virtual-slot-definition))
4d83a8a6 145 (funcall (slot-value slotd 'writer-function) value object))
146
147
94f15c3c 148(defmethod validate-superclass
9adccb27 149 ((class virtual-slots-class) (super standard-class))
94f15c3c 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
0f134a29 168(defun instance-cached-p (location)
169 (gethash (system:sap-int location) *instance-cache*))
170
94f15c3c 171(defun remove-cached-instance (location)
172 (remhash (system:sap-int location) *instance-cache*))
173
9adccb27 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
94f15c3c 183
184
185;;;; Proxy for alien instances
186
9adccb27 187(defclass proxy ()
188 ((location :reader proxy-location :type system-area-pointer)))
94f15c3c 189
9adccb27 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 ))
94f15c3c 203
0f134a29 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
9adccb27 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)))))
94f15c3c 211
94f15c3c 212
9adccb27 213(defmethod initialize-instance :around ((instance proxy) &key location)
214 (if location
215 (setf (slot-value instance 'location) location)
216 (call-next-method))
12d0437e 217 (cache-instance instance)
9adccb27 218 (ext:finalize instance (instance-finalizer instance))
219 instance)
94f15c3c 220
221(defmethod instance-finalizer ((instance proxy))
9adccb27 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 ()
227 (when (instance-cached-p location)
228 (remove-cached-instance location))
229 (unreference-foreign class location))))
12d0437e 230
94f15c3c 231
232;;;; Metaclass used for subclasses of proxy
233
234(eval-when (:compile-toplevel :load-toplevel :execute)
9adccb27 235 (defclass proxy-class (virtual-slots-class)
236 ((size :reader proxy-instance-size)))
94f15c3c 237
238 (defclass direct-alien-slot-definition (direct-virtual-slot-definition)
12d0437e 239 ((allocation :initform :alien)
240 (offset :reader slot-definition-offset :initarg :offset)))
94f15c3c 241
242 (defclass effective-alien-slot-definition (effective-virtual-slot-definition)
4d83a8a6 243 ((offset :reader slot-definition-offset :initarg :offset)))
12d0437e 244
94f15c3c 245
246 (defmethod most-specific-proxy-superclass ((class proxy-class))
247 (find-if
248 #'(lambda (class)
249 (subtypep (class-name class) 'proxy))
4d83a8a6 250 (cdr (compute-class-precedence-list class))))
251
12d0437e 252 (defmethod direct-proxy-superclass ((class proxy-class))
253 (find-if
254 #'(lambda (class)
255 (subtypep (class-name class) 'proxy))
4d83a8a6 256 (class-direct-superclasses class)))
257
94f15c3c 258 (defmethod shared-initialize ((class proxy-class) names
9adccb27 259 &rest initargs &key size)
94f15c3c 260 (declare (ignore initargs))
261 (call-next-method)
12d0437e 262 (cond
4d83a8a6 263 (size (setf (slot-value class 'size) (first size)))
9adccb27 264 ((slot-boundp class 'size) (slot-makunbound class 'size))))
4d83a8a6 265
266 (defmethod direct-slot-definition-class ((class proxy-class) &rest initargs)
94f15c3c 267 (case (getf initargs :allocation)
268 ((nil :alien) (find-class 'direct-alien-slot-definition))
94f15c3c 269 (t (call-next-method))))
4d83a8a6 270
271 (defmethod effective-slot-definition-class ((class proxy-class) &rest initargs)
94f15c3c 272 (case (getf initargs :allocation)
273 (:alien (find-class 'effective-alien-slot-definition))
94f15c3c 274 (t (call-next-method))))
275
4d83a8a6 276
277 (defmethod compute-effective-slot-definition-initargs ((class proxy-class) direct-slotds)
278 (if (eq (most-specific-slot-value direct-slotds 'allocation) :alien)
279 (nconc
280 (list :offset (most-specific-slot-value direct-slotds 'offset))
281 (call-next-method))
282 (call-next-method)))
283
284
285 (defmethod initialize-internal-slot-functions ((slotd effective-alien-slot-definition))
286 (with-slots (offset) slotd
9adccb27 287 (let ((type (slot-definition-type slotd)))
4d83a8a6 288 (unless (slot-boundp slotd 'reader-function)
9adccb27 289 (let ((reader (reader-function type)))
290 (setf
291 (slot-value slotd 'reader-function)
292 #'(lambda (object)
293 (funcall reader (proxy-location object) offset)))))
4d83a8a6 294
295 (unless (slot-boundp slotd 'writer-function)
9adccb27 296 (let ((writer (writer-function type))
297 (destroy (destroy-function type)))
298 (setf
299 (slot-value slotd 'writer-function)
300 #'(lambda (value object)
301 (let ((location (proxy-location object)))
302 (funcall destroy location offset) ; destroy old value
303 (funcall writer value location offset))))))
4d83a8a6 304
305 (unless (slot-boundp slotd 'boundp-function)
306 (setf
307 (slot-value slotd 'boundp-function)
308 #'(lambda (object)
309 (declare (ignore object))
310 t)))))
311 (call-next-method))
312
313
4d83a8a6 314 ;; TODO: call some C code to detect this a compile time
315 (defconstant +struct-alignmen+ 4)
94f15c3c 316
317 (defmethod compute-slots ((class proxy-class))
4d83a8a6 318 (loop
9adccb27 319 with offset = (proxy-instance-size (most-specific-proxy-superclass class))
4d83a8a6 320 with size = offset
321 for slotd in (class-direct-slots class)
322 when (eq (slot-definition-allocation slotd) :alien)
323 do (if (not (slot-boundp slotd 'offset))
324 (setf (slot-value slotd 'offset) offset)
325 (setq offset (slot-value slotd 'offset)))
326
327 (incf offset (size-of (slot-definition-type slotd)))
328 (incf offset (mod offset +struct-alignmen+))
329 (setq size (max size offset))
330
331 finally (unless (slot-boundp class 'size)
332 (setf (slot-value class 'size) size)))
94f15c3c 333 (call-next-method))
3e15002d 334
4d83a8a6 335
336 (defmethod validate-superclass ((class proxy-class) (super standard-class))
337 (subtypep (class-name super) 'proxy))
338
9adccb27 339 (defmethod proxy-instance-size (class)
12d0437e 340 (declare (ignore class))
341 0)
4d83a8a6 342)
343
9adccb27 344(defmethod alien-type ((class proxy-class) &rest args)
345 (declare (ignore class args))
346 (alien-type 'pointer))
347
348(defmethod size-of ((class proxy-class) &rest args)
349 (declare (ignore class args))
350 (size-of 'pointer))
351
352(defmethod from-alien-form (location (class proxy-class) &rest args)
353 (declare (ignore args))
354 `(ensure-proxy-instance ',(class-name class) ,location))
355
356(defmethod from-alien-function ((class proxy-class) &rest args)
357 (declare (ignore args))
358 #'(lambda (location)
359 (ensure-proxy-instance class location)))
94f15c3c 360
9adccb27 361(defmethod to-alien-form (instance (class proxy-class) &rest args)
362 (declare (ignore class args))
363 `(proxy-location ,instance))
94f15c3c 364
9adccb27 365(defmethod to-alien-function ((class proxy-class) &rest args)
366 (declare (ignore class args))
367 #'proxy-location)
368
369(defmethod writer-function ((class proxy-class) &rest args)
370 (declare (ignore args))
371 #'(lambda (instance location &optional (offset 0))
372 (assert (null-pointer-p (sap-ref-sap location offset)))
373 (setf
374 (sap-ref-sap location offset)
375 (reference-foreign class (proxy-location instance)))))
94f15c3c 376
9adccb27 377(defmethod reader-function ((class proxy-class) &rest args)
378 (declare (ignore args))
379 #'(lambda (location &optional (offset 0))
380 (ensure-proxy-instance class (sap-ref-sap location offset))))
94f15c3c 381
9adccb27 382(defmethod destroy-function ((class proxy-class) &rest args)
383 (declare (ignore args))
384 #'(lambda (location &optional (offset 0))
385 (unreference-foreign class (sap-ref-sap location offset))))
386
387
388(defgeneric ensure-proxy-instance (class location)
389 (:documentation "Returns a proxy object representing the foreign object at the give location."))
390
391(defmethod ensure-proxy-instance :around (class location)
392 (unless (null-pointer-p location)
393 (or
394 (find-cached-instance location)
395 (call-next-method))))
396
397(defmethod ensure-proxy-instance ((class symbol) location)
398 (ensure-proxy-instance (find-class class) location))
399
400(defmethod ensure-proxy-instance ((class proxy-class) location)
401 (make-instance class :location location))
94f15c3c 402
12d0437e 403
404;;;; Superclasses for wrapping of C structures
94f15c3c 405
9adccb27 406(defclass struct (proxy)
407 ()
408 (:metaclass proxy-class))
94f15c3c 409
9adccb27 410(defmethod initialize-instance ((struct struct) &rest initargs)
94f15c3c 411 (declare (ignore initargs))
412 (setf
9adccb27 413 (slot-value struct 'location)
414 (allocate-memory (proxy-instance-size (class-of struct))))
94f15c3c 415 (call-next-method))
416
417
9adccb27 418;;;; Metaclasses used for subclasses of struct
419
420(defclass struct-class (proxy-class)
421 ())
94f15c3c 422
9adccb27 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)
12d0437e 427 (deallocate-memory location))
94f15c3c 428
9adccb27 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
94f15c3c 436
9adccb27 437(defclass static-struct-class (struct-class)
438 ())
94f15c3c 439
9adccb27 440(defmethod reference-foreign ((class static-struct-class) location)
441 (declare (ignore class))
12d0437e 442 location)
94f15c3c 443
9adccb27 444(defmethod unreference-foreign ((class static-struct-class) location)
445 (declare (ignore class location))
12d0437e 446 nil)