Added statusbar example
[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
0f134a29 18;; $Id: proxy.lisp,v 1.10 2004-11-03 16:18:16 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
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
174
175
176;;;; Proxy for alien instances
177
178(eval-when (:compile-toplevel :load-toplevel :execute)
179 (defclass proxy ()
12d0437e 180 ((location :reader proxy-location :type system-area-pointer)))
94f15c3c 181
182 (defgeneric initialize-proxy (object &rest initargs))
183 (defgeneric instance-finalizer (object)))
184
0f134a29 185(defmethod print-object ((instance proxy) stream)
186 (print-unreadable-object (instance stream :type t :identity nil)
187 (format stream "at 0x~X" (sap-int (proxy-location instance)))))
188
94f15c3c 189
190(defmethod initialize-instance :after ((instance proxy)
191 &rest initargs &key)
192 (declare (ignore initargs))
193 (cache-instance instance)
194 (ext:finalize instance (instance-finalizer instance)))
195
94f15c3c 196(defmethod initialize-proxy ((instance proxy)
12d0437e 197 &rest initargs &key location weak-ref)
94f15c3c 198 (declare (ignore initargs))
12d0437e 199 (setf
200 (slot-value instance 'location)
201 (if weak-ref
202 (funcall
203 (proxy-class-copy (class-of instance))
204 (type-of instance) location)
205 location))
206 (cache-instance instance)
207 (ext:finalize instance (instance-finalizer instance)))
94f15c3c 208
209(defmethod instance-finalizer ((instance proxy))
4d83a8a6 210 (let ((class (class-of instance))
12d0437e 211 (type (type-of instance))
212 (location (proxy-location instance)))
4d83a8a6 213 (declare (type symbol type) (type system-area-pointer location))
214 (let ((free (proxy-class-free class)))
215 #'(lambda ()
0f134a29 216 (when (instance-cached-p location)
217 (remove-cached-instance location)
218 (funcall free type location))))))
94f15c3c 219
220
221(deftype-method translate-type-spec proxy (type-spec)
222 (declare (ignore type-spec))
223 (translate-type-spec 'pointer))
224
225(deftype-method size-of proxy (type-spec)
226 (declare (ignore type-spec))
227 (size-of 'pointer))
228
229(deftype-method translate-from-alien
230 proxy (type-spec location &optional weak-ref)
231 `(let ((location ,location))
232 (unless (null-pointer-p location)
233 (ensure-proxy-instance ',type-spec location ,weak-ref))))
234
12d0437e 235(deftype-method translate-to-alien
236 proxy (type-spec instance &optional weak-ref)
237 (if weak-ref
238 `(proxy-location ,instance)
4d83a8a6 239 (let ((copy (proxy-class-copy (find-class type-spec))))
240 (if (symbolp copy)
241 `(,copy ',type-spec (proxy-location ,instance))
242 `(funcall ',copy ',type-spec (proxy-location ,instance))))))
94f15c3c 243
12d0437e 244(deftype-method unreference-alien proxy (type-spec location)
4d83a8a6 245 (let ((free (proxy-class-free (find-class type-spec))))
246 (if (symbolp free)
247 `(,free ',type-spec ,location)
248 `(funcall ',free ',type-spec ,location))))
12d0437e 249
94f15c3c 250
251;;;; Metaclass used for subclasses of proxy
252
253(eval-when (:compile-toplevel :load-toplevel :execute)
12d0437e 254 (defclass proxy-class (virtual-slot-class)
255 ((size :reader proxy-class-size)
256 (copy :reader proxy-class-copy)
257 (free :reader proxy-class-free)))
94f15c3c 258
259 (defclass direct-alien-slot-definition (direct-virtual-slot-definition)
12d0437e 260 ((allocation :initform :alien)
261 (offset :reader slot-definition-offset :initarg :offset)))
94f15c3c 262
263 (defclass effective-alien-slot-definition (effective-virtual-slot-definition)
4d83a8a6 264 ((offset :reader slot-definition-offset :initarg :offset)))
12d0437e 265
94f15c3c 266
267 (defmethod most-specific-proxy-superclass ((class proxy-class))
268 (find-if
269 #'(lambda (class)
270 (subtypep (class-name class) 'proxy))
4d83a8a6 271 (cdr (compute-class-precedence-list class))))
272
12d0437e 273 (defmethod direct-proxy-superclass ((class proxy-class))
274 (find-if
275 #'(lambda (class)
276 (subtypep (class-name class) 'proxy))
4d83a8a6 277 (class-direct-superclasses class)))
278
94f15c3c 279 (defmethod shared-initialize ((class proxy-class) names
12d0437e 280 &rest initargs &key size copy free)
94f15c3c 281 (declare (ignore initargs))
282 (call-next-method)
12d0437e 283 (cond
4d83a8a6 284 (size (setf (slot-value class 'size) (first size)))
285 ((slot-boundp class 'size) (slot-makunbound class 'size)))
12d0437e 286 (cond
4d83a8a6 287 (copy (setf (slot-value class 'copy) (first copy)))
288 ((slot-boundp class 'copy) (slot-makunbound class 'copy)))
12d0437e 289 (cond
4d83a8a6 290 (free (setf (slot-value class 'free) (first free)))
291 ((slot-boundp class 'free) (slot-makunbound class 'free))))
292
4d83a8a6 293 (defmethod shared-initialize :after ((class proxy-class) names &rest initargs)
3e15002d 294 (let ((super (most-specific-proxy-superclass class)))
295 (unless (or (not super) (eq super (find-class 'proxy)))
4d83a8a6 296 (unless (or (slot-boundp class 'copy) (not (slot-boundp super 'copy)))
297 (setf (slot-value class 'copy) (proxy-class-copy super)))
298 (unless (or (slot-boundp class 'free) (not (slot-boundp super 'free)))
299 (setf (slot-value class 'free) (proxy-class-free super))))))
300
301 (defmethod direct-slot-definition-class ((class proxy-class) &rest initargs)
94f15c3c 302 (case (getf initargs :allocation)
303 ((nil :alien) (find-class 'direct-alien-slot-definition))
94f15c3c 304 (t (call-next-method))))
4d83a8a6 305
306 (defmethod effective-slot-definition-class ((class proxy-class) &rest initargs)
94f15c3c 307 (case (getf initargs :allocation)
308 (:alien (find-class 'effective-alien-slot-definition))
94f15c3c 309 (t (call-next-method))))
310
4d83a8a6 311
312 (defmethod compute-effective-slot-definition-initargs ((class proxy-class) direct-slotds)
313 (if (eq (most-specific-slot-value direct-slotds 'allocation) :alien)
314 (nconc
315 (list :offset (most-specific-slot-value direct-slotds 'offset))
316 (call-next-method))
317 (call-next-method)))
318
319
320 (defmethod initialize-internal-slot-functions ((slotd effective-alien-slot-definition))
321 (with-slots (offset) slotd
322 (let* ((type (slot-definition-type slotd))
323 (reader (intern-reader-function type))
324 (writer (intern-writer-function type))
325 (destroy (intern-destroy-function type)))
326 (unless (slot-boundp slotd 'reader-function)
327 (setf
328 (slot-value slotd 'reader-function)
329 #'(lambda (object)
330 (funcall reader (proxy-location object) offset))))
331
332 (unless (slot-boundp slotd 'writer-function)
333 (setf
334 (slot-value slotd 'writer-function)
335 #'(lambda (value object)
336 (let ((location (proxy-location object)))
337 (funcall destroy location offset)
338 (funcall writer value location offset)))))
339
340 (unless (slot-boundp slotd 'boundp-function)
341 (setf
342 (slot-value slotd 'boundp-function)
343 #'(lambda (object)
344 (declare (ignore object))
345 t)))))
346 (call-next-method))
347
348
4d83a8a6 349 ;; TODO: call some C code to detect this a compile time
350 (defconstant +struct-alignmen+ 4)
94f15c3c 351
352 (defmethod compute-slots ((class proxy-class))
4d83a8a6 353 ;; This stuff should really go somewhere else
354 (loop
355 with offset = (proxy-class-size (most-specific-proxy-superclass class))
356 with size = offset
357 for slotd in (class-direct-slots class)
358 when (eq (slot-definition-allocation slotd) :alien)
359 do (if (not (slot-boundp slotd 'offset))
360 (setf (slot-value slotd 'offset) offset)
361 (setq offset (slot-value slotd 'offset)))
362
363 (incf offset (size-of (slot-definition-type slotd)))
364 (incf offset (mod offset +struct-alignmen+))
365 (setq size (max size offset))
366
367 finally (unless (slot-boundp class 'size)
368 (setf (slot-value class 'size) size)))
94f15c3c 369 (call-next-method))
3e15002d 370
4d83a8a6 371
372 (defmethod validate-superclass ((class proxy-class) (super standard-class))
373 (subtypep (class-name super) 'proxy))
374
12d0437e 375 (defmethod proxy-class-size (class)
376 (declare (ignore class))
377 0)
4d83a8a6 378)
379
380(defgeneric make-proxy-instance (class location weak-ref
381 &rest initargs &key));)
94f15c3c 382
383(defmethod make-proxy-instance ((class symbol) location weak-ref
384 &rest initargs &key)
385 (apply #'make-proxy-instance (find-class class) location weak-ref initargs))
386
387(defmethod make-proxy-instance ((class proxy-class) location weak-ref
388 &rest initargs &key)
389 (let ((instance (allocate-instance class)))
390 (apply
391 #'initialize-proxy
392 instance :location location :weak-ref weak-ref initargs)
393 instance))
394
395(defun ensure-proxy-instance (class location weak-ref &rest initargs)
396 (or
397 (find-cached-instance location)
398 (apply #'make-proxy-instance class location weak-ref initargs)))
399
400
12d0437e 401
402;;;; Superclasses for wrapping of C structures
94f15c3c 403
404(eval-when (:compile-toplevel :load-toplevel :execute)
12d0437e 405 (defclass struct (proxy)
94f15c3c 406 ()
407 (:metaclass proxy-class)
12d0437e 408 (:copy %copy-struct)
409 (:free %free-struct)))
94f15c3c 410
4d83a8a6 411(defmethod initialize-instance ((structure struct) &rest initargs)
94f15c3c 412 (declare (ignore initargs))
413 (setf
414 (slot-value structure 'location)
12d0437e 415 (allocate-memory (proxy-class-size (class-of structure))))
94f15c3c 416 (call-next-method))
417
418
12d0437e 419(defun %copy-struct (type location)
420 (copy-memory location (proxy-class-size (find-class type))))
94f15c3c 421
12d0437e 422(defun %free-struct (type location)
423 (declare (ignore type))
424 (deallocate-memory location))
94f15c3c 425
426
3e15002d 427;(eval-when (:compile-toplevel :load-toplevel :execute)
12d0437e 428 (defclass static (struct)
429 ()
3e15002d 430 (:metaclass proxy-class)
431 (:copy %copy-static)
432 (:free %free-static));)
94f15c3c 433
12d0437e 434(defun %copy-static (type location)
435 (declare (ignore type))
436 location)
94f15c3c 437
12d0437e 438(defun %free-static (type location)
439 (declare (ignore type location))
440 nil)