chiark / gitweb /
Code moved from callback.c
[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
7d1ddc9e 18;; $Id: proxy.lisp,v 1.9 2004-10-28 19:29:00 espen Exp $
94f15c3c 19
20(in-package "GLIB")
21
4d83a8a6 22(import
23'(pcl::initialize-internal-slot-functions
24 pcl::compute-effective-slot-definition-initargs
25 pcl::compute-slot-accessor-info
26 pcl::reader-function pcl::writer-function pcl::boundp-function))
94f15c3c 27
28;;;; Superclass for all metaclasses implementing some sort of virtual slots
29
30(eval-when (:compile-toplevel :load-toplevel :execute)
4d83a8a6 31 (defclass virtual-slot-class (standard-class)
32 ())
94f15c3c 33
34 (defclass direct-virtual-slot-definition (standard-direct-slot-definition)
12d0437e 35 ((setter :reader slot-definition-setter :initarg :setter)
4d83a8a6 36 (getter :reader slot-definition-getter :initarg :getter)
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)
42 (boundp :reader slot-definition-boundp :initarg :boundp)))
43
44 (defun most-specific-slot-value (instances slot &optional default)
45 (let ((object (find-if
46 #'(lambda (ob)
47 (and (slot-exists-p ob slot) (slot-boundp ob slot)))
48 instances)))
49 (if object
50 (slot-value object slot)
51 default)))
52)
53
94f15c3c 54
55
4d83a8a6 56(defmethod direct-slot-definition-class ((class virtual-slot-class) &rest initargs)
94f15c3c 57 (if (eq (getf initargs :allocation) :virtual)
58 (find-class 'direct-virtual-slot-definition)
59 (call-next-method)))
60
4d83a8a6 61(defmethod effective-slot-definition-class ((class virtual-slot-class) &rest initargs)
94f15c3c 62 (if (eq (getf initargs :allocation) :virtual)
63 (find-class 'effective-virtual-slot-definition)
64 (call-next-method)))
65
4d83a8a6 66
67(defmethod initialize-internal-slot-functions ((slotd effective-virtual-slot-definition))
68 (with-slots (getter setter boundp) slotd
69 (unless (slot-boundp slotd 'reader-function)
70 (setf
71 (slot-value slotd 'reader-function)
72 (etypecase getter
73 (function getter)
74 (null #'(lambda (object)
75 (declare (ignore object))
76 (error "Can't read slot: ~A" (slot-definition-name slotd))))
77 (symbol #'(lambda (object)
7d1ddc9e 78 (funcall getter object)))
79 (string (let ((reader (mkbinding-late getter
80 (slot-definition-type slotd) 'pointer)))
81 (setf (slot-value slotd 'reader-function)
82 #'(lambda (object)
83 (funcall reader (proxy-location object)))))))))
4d83a8a6 84
85 (unless (slot-boundp slotd 'writer-function)
86 (setf
87 (slot-value slotd 'writer-function)
88 (etypecase setter
89 (function setter)
90 (null #'(lambda (object)
91 (declare (ignore object))
92 (error "Can't set slot: ~A" (slot-definition-name slotd))))
93 ((or symbol cons) #'(lambda (value object)
7d1ddc9e 94 (funcall (fdefinition setter) value object)))
95 (string
96 (let ((writer (mkbinding-late setter 'nil 'pointer
97 (slot-definition-type slotd))))
98 (setf (slot-value slotd 'writer-function)
99 #'(lambda (value object)
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
120(defmethod compute-effective-slot-definition-initargs ((class virtual-slot-class) direct-slotds)
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
12d0437e 131 ((class virtual-slot-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
12d0437e 138 ((class virtual-slot-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)
12d0437e 143 (value (class virtual-slot-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
4d83a8a6 149 ((class virtual-slot-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
168(defun remove-cached-instance (location)
169 (remhash (system:sap-int location) *instance-cache*))
170
171
172
173;;;; Proxy for alien instances
174
175(eval-when (:compile-toplevel :load-toplevel :execute)
176 (defclass proxy ()
12d0437e 177 ((location :reader proxy-location :type system-area-pointer)))
94f15c3c 178
179 (defgeneric initialize-proxy (object &rest initargs))
180 (defgeneric instance-finalizer (object)))
181
182
183(defmethod initialize-instance :after ((instance proxy)
184 &rest initargs &key)
185 (declare (ignore initargs))
186 (cache-instance instance)
187 (ext:finalize instance (instance-finalizer instance)))
188
94f15c3c 189(defmethod initialize-proxy ((instance proxy)
12d0437e 190 &rest initargs &key location weak-ref)
94f15c3c 191 (declare (ignore initargs))
12d0437e 192 (setf
193 (slot-value instance 'location)
194 (if weak-ref
195 (funcall
196 (proxy-class-copy (class-of instance))
197 (type-of instance) location)
198 location))
199 (cache-instance instance)
200 (ext:finalize instance (instance-finalizer instance)))
94f15c3c 201
202(defmethod instance-finalizer ((instance proxy))
4d83a8a6 203 (let ((class (class-of instance))
12d0437e 204 (type (type-of instance))
205 (location (proxy-location instance)))
4d83a8a6 206 (declare (type symbol type) (type system-area-pointer location))
207 (let ((free (proxy-class-free class)))
208 #'(lambda ()
209 (funcall free type location)
210 (remove-cached-instance location)))))
94f15c3c 211
212
213(deftype-method translate-type-spec proxy (type-spec)
214 (declare (ignore type-spec))
215 (translate-type-spec 'pointer))
216
217(deftype-method size-of proxy (type-spec)
218 (declare (ignore type-spec))
219 (size-of 'pointer))
220
221(deftype-method translate-from-alien
222 proxy (type-spec location &optional weak-ref)
223 `(let ((location ,location))
224 (unless (null-pointer-p location)
225 (ensure-proxy-instance ',type-spec location ,weak-ref))))
226
12d0437e 227(deftype-method translate-to-alien
228 proxy (type-spec instance &optional weak-ref)
229 (if weak-ref
230 `(proxy-location ,instance)
4d83a8a6 231 (let ((copy (proxy-class-copy (find-class type-spec))))
232 (if (symbolp copy)
233 `(,copy ',type-spec (proxy-location ,instance))
234 `(funcall ',copy ',type-spec (proxy-location ,instance))))))
94f15c3c 235
12d0437e 236(deftype-method unreference-alien proxy (type-spec location)
4d83a8a6 237 (let ((free (proxy-class-free (find-class type-spec))))
238 (if (symbolp free)
239 `(,free ',type-spec ,location)
240 `(funcall ',free ',type-spec ,location))))
12d0437e 241
94f15c3c 242
243;;;; Metaclass used for subclasses of proxy
244
245(eval-when (:compile-toplevel :load-toplevel :execute)
12d0437e 246 (defclass proxy-class (virtual-slot-class)
247 ((size :reader proxy-class-size)
248 (copy :reader proxy-class-copy)
249 (free :reader proxy-class-free)))
94f15c3c 250
251 (defclass direct-alien-slot-definition (direct-virtual-slot-definition)
12d0437e 252 ((allocation :initform :alien)
253 (offset :reader slot-definition-offset :initarg :offset)))
94f15c3c 254
255 (defclass effective-alien-slot-definition (effective-virtual-slot-definition)
4d83a8a6 256 ((offset :reader slot-definition-offset :initarg :offset)))
12d0437e 257
94f15c3c 258
259 (defmethod most-specific-proxy-superclass ((class proxy-class))
260 (find-if
261 #'(lambda (class)
262 (subtypep (class-name class) 'proxy))
4d83a8a6 263 (cdr (compute-class-precedence-list class))))
264
12d0437e 265 (defmethod direct-proxy-superclass ((class proxy-class))
266 (find-if
267 #'(lambda (class)
268 (subtypep (class-name class) 'proxy))
4d83a8a6 269 (class-direct-superclasses class)))
270
94f15c3c 271 (defmethod shared-initialize ((class proxy-class) names
12d0437e 272 &rest initargs &key size copy free)
94f15c3c 273 (declare (ignore initargs))
274 (call-next-method)
12d0437e 275 (cond
4d83a8a6 276 (size (setf (slot-value class 'size) (first size)))
277 ((slot-boundp class 'size) (slot-makunbound class 'size)))
12d0437e 278 (cond
4d83a8a6 279 (copy (setf (slot-value class 'copy) (first copy)))
280 ((slot-boundp class 'copy) (slot-makunbound class 'copy)))
12d0437e 281 (cond
4d83a8a6 282 (free (setf (slot-value class 'free) (first free)))
283 ((slot-boundp class 'free) (slot-makunbound class 'free))))
284
4d83a8a6 285 (defmethod shared-initialize :after ((class proxy-class) names &rest initargs)
3e15002d 286 (let ((super (most-specific-proxy-superclass class)))
287 (unless (or (not super) (eq super (find-class 'proxy)))
4d83a8a6 288 (unless (or (slot-boundp class 'copy) (not (slot-boundp super 'copy)))
289 (setf (slot-value class 'copy) (proxy-class-copy super)))
290 (unless (or (slot-boundp class 'free) (not (slot-boundp super 'free)))
291 (setf (slot-value class 'free) (proxy-class-free super))))))
292
293 (defmethod direct-slot-definition-class ((class proxy-class) &rest initargs)
94f15c3c 294 (case (getf initargs :allocation)
295 ((nil :alien) (find-class 'direct-alien-slot-definition))
94f15c3c 296 (t (call-next-method))))
4d83a8a6 297
298 (defmethod effective-slot-definition-class ((class proxy-class) &rest initargs)
94f15c3c 299 (case (getf initargs :allocation)
300 (:alien (find-class 'effective-alien-slot-definition))
94f15c3c 301 (t (call-next-method))))
302
4d83a8a6 303
304 (defmethod compute-effective-slot-definition-initargs ((class proxy-class) direct-slotds)
305 (if (eq (most-specific-slot-value direct-slotds 'allocation) :alien)
306 (nconc
307 (list :offset (most-specific-slot-value direct-slotds 'offset))
308 (call-next-method))
309 (call-next-method)))
310
311
312 (defmethod initialize-internal-slot-functions ((slotd effective-alien-slot-definition))
313 (with-slots (offset) slotd
314 (let* ((type (slot-definition-type slotd))
315 (reader (intern-reader-function type))
316 (writer (intern-writer-function type))
317 (destroy (intern-destroy-function type)))
318 (unless (slot-boundp slotd 'reader-function)
319 (setf
320 (slot-value slotd 'reader-function)
321 #'(lambda (object)
322 (funcall reader (proxy-location object) offset))))
323
324 (unless (slot-boundp slotd 'writer-function)
325 (setf
326 (slot-value slotd 'writer-function)
327 #'(lambda (value object)
328 (let ((location (proxy-location object)))
329 (funcall destroy location offset)
330 (funcall writer value location offset)))))
331
332 (unless (slot-boundp slotd 'boundp-function)
333 (setf
334 (slot-value slotd 'boundp-function)
335 #'(lambda (object)
336 (declare (ignore object))
337 t)))))
338 (call-next-method))
339
340
4d83a8a6 341 ;; TODO: call some C code to detect this a compile time
342 (defconstant +struct-alignmen+ 4)
94f15c3c 343
344 (defmethod compute-slots ((class proxy-class))
4d83a8a6 345 ;; This stuff should really go somewhere else
346 (loop
347 with offset = (proxy-class-size (most-specific-proxy-superclass class))
348 with size = offset
349 for slotd in (class-direct-slots class)
350 when (eq (slot-definition-allocation slotd) :alien)
351 do (if (not (slot-boundp slotd 'offset))
352 (setf (slot-value slotd 'offset) offset)
353 (setq offset (slot-value slotd 'offset)))
354
355 (incf offset (size-of (slot-definition-type slotd)))
356 (incf offset (mod offset +struct-alignmen+))
357 (setq size (max size offset))
358
359 finally (unless (slot-boundp class 'size)
360 (setf (slot-value class 'size) size)))
94f15c3c 361 (call-next-method))
3e15002d 362
4d83a8a6 363
364 (defmethod validate-superclass ((class proxy-class) (super standard-class))
365 (subtypep (class-name super) 'proxy))
366
12d0437e 367 (defmethod proxy-class-size (class)
368 (declare (ignore class))
369 0)
4d83a8a6 370)
371
372(defgeneric make-proxy-instance (class location weak-ref
373 &rest initargs &key));)
94f15c3c 374
375(defmethod make-proxy-instance ((class symbol) location weak-ref
376 &rest initargs &key)
377 (apply #'make-proxy-instance (find-class class) location weak-ref initargs))
378
379(defmethod make-proxy-instance ((class proxy-class) location weak-ref
380 &rest initargs &key)
381 (let ((instance (allocate-instance class)))
382 (apply
383 #'initialize-proxy
384 instance :location location :weak-ref weak-ref initargs)
385 instance))
386
387(defun ensure-proxy-instance (class location weak-ref &rest initargs)
388 (or
389 (find-cached-instance location)
390 (apply #'make-proxy-instance class location weak-ref initargs)))
391
392
12d0437e 393
394;;;; Superclasses for wrapping of C structures
94f15c3c 395
396(eval-when (:compile-toplevel :load-toplevel :execute)
12d0437e 397 (defclass struct (proxy)
94f15c3c 398 ()
399 (:metaclass proxy-class)
12d0437e 400 (:copy %copy-struct)
401 (:free %free-struct)))
94f15c3c 402
4d83a8a6 403(defmethod initialize-instance ((structure struct) &rest initargs)
94f15c3c 404 (declare (ignore initargs))
405 (setf
406 (slot-value structure 'location)
12d0437e 407 (allocate-memory (proxy-class-size (class-of structure))))
94f15c3c 408 (call-next-method))
409
410
12d0437e 411(defun %copy-struct (type location)
412 (copy-memory location (proxy-class-size (find-class type))))
94f15c3c 413
12d0437e 414(defun %free-struct (type location)
415 (declare (ignore type))
416 (deallocate-memory location))
94f15c3c 417
418
3e15002d 419;(eval-when (:compile-toplevel :load-toplevel :execute)
12d0437e 420 (defclass static (struct)
421 ()
3e15002d 422 (:metaclass proxy-class)
423 (:copy %copy-static)
424 (:free %free-static));)
94f15c3c 425
12d0437e 426(defun %copy-static (type location)
427 (declare (ignore type))
428 location)
94f15c3c 429
12d0437e 430(defun %free-static (type location)
431 (declare (ignore type location))
432 nil)