b44caf77 |
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 | |
935a783c |
18 | ;; $Id: proxy.lisp,v 1.8 2004/10/27 14:59:00 espen Exp $ |
b44caf77 |
19 | |
20 | (in-package "GLIB") |
21 | |
935a783c |
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)) |
b44caf77 |
27 | |
28 | ;;;; Superclass for all metaclasses implementing some sort of virtual slots |
29 | |
30 | (eval-when (:compile-toplevel :load-toplevel :execute) |
935a783c |
31 | (defclass virtual-slot-class (standard-class) |
32 | ()) |
b44caf77 |
33 | |
34 | (defclass direct-virtual-slot-definition (standard-direct-slot-definition) |
ba25fa44 |
35 | ((setter :reader slot-definition-setter :initarg :setter) |
935a783c |
36 | (getter :reader slot-definition-getter :initarg :getter) |
37 | (boundp :reader slot-definition-boundp :initarg :boundp))) |
b44caf77 |
38 | |
935a783c |
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 | |
b44caf77 |
54 | |
55 | |
935a783c |
56 | (defmethod direct-slot-definition-class ((class virtual-slot-class) &rest initargs) |
b44caf77 |
57 | (if (eq (getf initargs :allocation) :virtual) |
58 | (find-class 'direct-virtual-slot-definition) |
59 | (call-next-method))) |
60 | |
935a783c |
61 | (defmethod effective-slot-definition-class ((class virtual-slot-class) &rest initargs) |
b44caf77 |
62 | (if (eq (getf initargs :allocation) :virtual) |
63 | (find-class 'effective-virtual-slot-definition) |
64 | (call-next-method))) |
65 | |
935a783c |
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) |
78 | (funcall getter object)))))) |
79 | |
80 | (unless (slot-boundp slotd 'writer-function) |
81 | (setf |
82 | (slot-value slotd 'writer-function) |
83 | (etypecase setter |
84 | (function setter) |
85 | (null #'(lambda (object) |
86 | (declare (ignore object)) |
87 | (error "Can't set slot: ~A" (slot-definition-name slotd)))) |
88 | ((or symbol cons) #'(lambda (value object) |
89 | (funcall (fdefinition setter) value object)))))) |
90 | |
91 | (unless (slot-boundp slotd 'boundp-function) |
92 | (setf |
93 | (slot-value slotd 'boundp-function) |
94 | (etypecase boundp |
95 | (function boundp) |
96 | (null #'(lambda (object) |
97 | (declare (ignore object)) |
98 | t)) |
99 | (symbol #'(lambda (object) |
100 | (funcall boundp object))))))) |
101 | (initialize-internal-slot-gfs (slot-definition-name slotd))) |
102 | |
103 | |
104 | |
105 | (defmethod compute-slot-accessor-info ((slotd effective-virtual-slot-definition) |
106 | type gf) |
107 | nil) |
108 | |
109 | (defmethod compute-effective-slot-definition-initargs ((class virtual-slot-class) direct-slotds) |
110 | (if (eq (most-specific-slot-value direct-slotds 'allocation) :virtual) |
111 | (nconc |
112 | (list :getter (most-specific-slot-value direct-slotds 'getter) |
113 | :setter (most-specific-slot-value direct-slotds 'setter) |
114 | :boundp (most-specific-slot-value direct-slotds 'boundp)) |
115 | (call-next-method)) |
116 | (call-next-method))) |
117 | |
b44caf77 |
118 | |
b44caf77 |
119 | (defmethod slot-value-using-class |
ba25fa44 |
120 | ((class virtual-slot-class) (object standard-object) |
b44caf77 |
121 | (slotd effective-virtual-slot-definition)) |
935a783c |
122 | (if (funcall (slot-value slotd 'boundp-function) object) |
123 | (funcall (slot-value slotd 'reader-function) object) |
124 | (slot-unbound class object (slot-definition-name slotd)))) |
b44caf77 |
125 | |
b44caf77 |
126 | (defmethod slot-boundp-using-class |
ba25fa44 |
127 | ((class virtual-slot-class) (object standard-object) |
b44caf77 |
128 | (slotd effective-virtual-slot-definition)) |
935a783c |
129 | (funcall (slot-value slotd 'boundp-function) object)) |
130 | |
131 | (defmethod (setf slot-value-using-class) |
ba25fa44 |
132 | (value (class virtual-slot-class) (object standard-object) |
b44caf77 |
133 | (slotd effective-virtual-slot-definition)) |
935a783c |
134 | (funcall (slot-value slotd 'writer-function) value object)) |
135 | |
136 | |
b44caf77 |
137 | (defmethod validate-superclass |
935a783c |
138 | ((class virtual-slot-class) (super standard-class)) |
b44caf77 |
139 | t) |
140 | |
141 | |
142 | ;;;; Proxy cache |
143 | |
144 | (internal *instance-cache*) |
145 | (defvar *instance-cache* (make-hash-table :test #'eql)) |
146 | |
147 | (defun cache-instance (instance) |
148 | (setf |
149 | (gethash (system:sap-int (proxy-location instance)) *instance-cache*) |
150 | (ext:make-weak-pointer instance))) |
151 | |
152 | (defun find-cached-instance (location) |
153 | (let ((ref (gethash (system:sap-int location) *instance-cache*))) |
154 | (when ref |
155 | (ext:weak-pointer-value ref)))) |
156 | |
157 | (defun remove-cached-instance (location) |
158 | (remhash (system:sap-int location) *instance-cache*)) |
159 | |
160 | |
161 | |
162 | ;;;; Proxy for alien instances |
163 | |
164 | (eval-when (:compile-toplevel :load-toplevel :execute) |
165 | (defclass proxy () |
ba25fa44 |
166 | ((location :reader proxy-location :type system-area-pointer))) |
b44caf77 |
167 | |
168 | (defgeneric initialize-proxy (object &rest initargs)) |
169 | (defgeneric instance-finalizer (object))) |
170 | |
171 | |
172 | (defmethod initialize-instance :after ((instance proxy) |
173 | &rest initargs &key) |
174 | (declare (ignore initargs)) |
175 | (cache-instance instance) |
176 | (ext:finalize instance (instance-finalizer instance))) |
177 | |
b44caf77 |
178 | (defmethod initialize-proxy ((instance proxy) |
ba25fa44 |
179 | &rest initargs &key location weak-ref) |
b44caf77 |
180 | (declare (ignore initargs)) |
ba25fa44 |
181 | (setf |
182 | (slot-value instance 'location) |
183 | (if weak-ref |
184 | (funcall |
185 | (proxy-class-copy (class-of instance)) |
186 | (type-of instance) location) |
187 | location)) |
188 | (cache-instance instance) |
189 | (ext:finalize instance (instance-finalizer instance))) |
b44caf77 |
190 | |
191 | (defmethod instance-finalizer ((instance proxy)) |
935a783c |
192 | (let ((class (class-of instance)) |
ba25fa44 |
193 | (type (type-of instance)) |
194 | (location (proxy-location instance))) |
935a783c |
195 | (declare (type symbol type) (type system-area-pointer location)) |
196 | (let ((free (proxy-class-free class))) |
197 | #'(lambda () |
198 | (funcall free type location) |
199 | (remove-cached-instance location))))) |
b44caf77 |
200 | |
201 | |
202 | (deftype-method translate-type-spec proxy (type-spec) |
203 | (declare (ignore type-spec)) |
204 | (translate-type-spec 'pointer)) |
205 | |
206 | (deftype-method size-of proxy (type-spec) |
207 | (declare (ignore type-spec)) |
208 | (size-of 'pointer)) |
209 | |
210 | (deftype-method translate-from-alien |
211 | proxy (type-spec location &optional weak-ref) |
212 | `(let ((location ,location)) |
213 | (unless (null-pointer-p location) |
214 | (ensure-proxy-instance ',type-spec location ,weak-ref)))) |
215 | |
ba25fa44 |
216 | (deftype-method translate-to-alien |
217 | proxy (type-spec instance &optional weak-ref) |
218 | (if weak-ref |
219 | `(proxy-location ,instance) |
935a783c |
220 | (let ((copy (proxy-class-copy (find-class type-spec)))) |
221 | (if (symbolp copy) |
222 | `(,copy ',type-spec (proxy-location ,instance)) |
223 | `(funcall ',copy ',type-spec (proxy-location ,instance)))))) |
b44caf77 |
224 | |
ba25fa44 |
225 | (deftype-method unreference-alien proxy (type-spec location) |
935a783c |
226 | (let ((free (proxy-class-free (find-class type-spec)))) |
227 | (if (symbolp free) |
228 | `(,free ',type-spec ,location) |
229 | `(funcall ',free ',type-spec ,location)))) |
ba25fa44 |
230 | |
935a783c |
231 | ;; (defun proxy-instance-size (proxy) |
232 | ;; (proxy-class-size (class-of proxy))) |
b44caf77 |
233 | |
234 | ;;;; Metaclass used for subclasses of proxy |
235 | |
236 | (eval-when (:compile-toplevel :load-toplevel :execute) |
ba25fa44 |
237 | (defclass proxy-class (virtual-slot-class) |
238 | ((size :reader proxy-class-size) |
239 | (copy :reader proxy-class-copy) |
240 | (free :reader proxy-class-free))) |
b44caf77 |
241 | |
242 | (defclass direct-alien-slot-definition (direct-virtual-slot-definition) |
ba25fa44 |
243 | ((allocation :initform :alien) |
244 | (offset :reader slot-definition-offset :initarg :offset))) |
b44caf77 |
245 | |
246 | (defclass effective-alien-slot-definition (effective-virtual-slot-definition) |
935a783c |
247 | ((offset :reader slot-definition-offset :initarg :offset))) |
b44caf77 |
248 | |
935a783c |
249 | (defclass effective-virtual-alien-slot-definition (effective-virtual-slot-definition) |
250 | ()) |
ba25fa44 |
251 | |
b44caf77 |
252 | |
253 | (defmethod most-specific-proxy-superclass ((class proxy-class)) |
254 | (find-if |
255 | #'(lambda (class) |
256 | (subtypep (class-name class) 'proxy)) |
935a783c |
257 | (cdr (compute-class-precedence-list class)))) |
258 | |
ba25fa44 |
259 | (defmethod direct-proxy-superclass ((class proxy-class)) |
260 | (find-if |
261 | #'(lambda (class) |
262 | (subtypep (class-name class) 'proxy)) |
935a783c |
263 | (class-direct-superclasses class))) |
264 | |
b44caf77 |
265 | (defmethod shared-initialize ((class proxy-class) names |
ba25fa44 |
266 | &rest initargs &key size copy free) |
b44caf77 |
267 | (declare (ignore initargs)) |
268 | (call-next-method) |
ba25fa44 |
269 | (cond |
935a783c |
270 | (size (setf (slot-value class 'size) (first size))) |
271 | ((slot-boundp class 'size) (slot-makunbound class 'size))) |
ba25fa44 |
272 | (cond |
935a783c |
273 | (copy (setf (slot-value class 'copy) (first copy))) |
274 | ((slot-boundp class 'copy) (slot-makunbound class 'copy))) |
ba25fa44 |
275 | (cond |
935a783c |
276 | (free (setf (slot-value class 'free) (first free))) |
277 | ((slot-boundp class 'free) (slot-makunbound class 'free)))) |
278 | |
279 | ;; (defmethod finalize-inheritance ((class proxy-class)) |
280 | ;; (call-next-method) |
281 | (defmethod shared-initialize :after ((class proxy-class) names &rest initargs) |
8ae7ddc2 |
282 | (let ((super (most-specific-proxy-superclass class))) |
283 | (unless (or (not super) (eq super (find-class 'proxy))) |
935a783c |
284 | (unless (or (slot-boundp class 'copy) (not (slot-boundp super 'copy))) |
285 | (setf (slot-value class 'copy) (proxy-class-copy super))) |
286 | (unless (or (slot-boundp class 'free) (not (slot-boundp super 'free))) |
287 | (setf (slot-value class 'free) (proxy-class-free super)))))) |
288 | |
289 | (defmethod direct-slot-definition-class ((class proxy-class) &rest initargs) |
b44caf77 |
290 | (case (getf initargs :allocation) |
291 | ((nil :alien) (find-class 'direct-alien-slot-definition)) |
b44caf77 |
292 | (t (call-next-method)))) |
935a783c |
293 | |
294 | (defmethod effective-slot-definition-class ((class proxy-class) &rest initargs) |
b44caf77 |
295 | (case (getf initargs :allocation) |
296 | (:alien (find-class 'effective-alien-slot-definition)) |
297 | (:virtual (find-class 'effective-virtual-alien-slot-definition)) |
298 | (t (call-next-method)))) |
299 | |
935a783c |
300 | |
301 | (defmethod compute-effective-slot-definition-initargs ((class proxy-class) direct-slotds) |
302 | (if (eq (most-specific-slot-value direct-slotds 'allocation) :alien) |
303 | (nconc |
304 | (list :offset (most-specific-slot-value direct-slotds 'offset)) |
305 | (call-next-method)) |
306 | (call-next-method))) |
307 | |
308 | |
309 | (defmethod initialize-internal-slot-functions ((slotd effective-alien-slot-definition)) |
310 | (with-slots (offset) slotd |
311 | (let* ((type (slot-definition-type slotd)) |
312 | (reader (intern-reader-function type)) |
313 | (writer (intern-writer-function type)) |
314 | (destroy (intern-destroy-function type))) |
315 | (unless (slot-boundp slotd 'reader-function) |
316 | (setf |
317 | (slot-value slotd 'reader-function) |
318 | #'(lambda (object) |
319 | (funcall reader (proxy-location object) offset)))) |
320 | |
321 | (unless (slot-boundp slotd 'writer-function) |
322 | (setf |
323 | (slot-value slotd 'writer-function) |
324 | #'(lambda (value object) |
325 | (let ((location (proxy-location object))) |
326 | (funcall destroy location offset) |
327 | (funcall writer value location offset))))) |
328 | |
329 | (unless (slot-boundp slotd 'boundp-function) |
330 | (setf |
331 | (slot-value slotd 'boundp-function) |
332 | #'(lambda (object) |
333 | (declare (ignore object)) |
334 | t))))) |
335 | (call-next-method)) |
336 | |
337 | |
338 | (defmethod initialize-internal-slot-functions ((slotd effective-virtual-alien-slot-definition)) |
339 | (with-slots (getter setter type) slotd |
340 | (when (and (not (slot-boundp slotd 'reader-function)) (stringp getter)) |
341 | (let ((reader (mkbinding-late getter type 'pointer))) |
342 | (setf (slot-value slotd 'reader-function) |
343 | #'(lambda (object) |
344 | (funcall reader (proxy-location object)))))) |
345 | |
346 | (when (and (not (slot-boundp slotd 'writer-function)) (stringp setter)) |
347 | (let ((writer (mkbinding-late setter 'nil 'pointer type))) |
348 | (setf (slot-value slotd 'writer-function) |
349 | #'(lambda (value object) |
350 | (funcall writer (proxy-location object) value)))))) |
351 | (call-next-method)) |
352 | |
353 | ;; TODO: call some C code to detect this a compile time |
354 | (defconstant +struct-alignmen+ 4) |
b44caf77 |
355 | |
356 | (defmethod compute-slots ((class proxy-class)) |
935a783c |
357 | ;; This stuff should really go somewhere else |
358 | (loop |
359 | with offset = (proxy-class-size (most-specific-proxy-superclass class)) |
360 | with size = offset |
361 | for slotd in (class-direct-slots class) |
362 | when (eq (slot-definition-allocation slotd) :alien) |
363 | do (if (not (slot-boundp slotd 'offset)) |
364 | (setf (slot-value slotd 'offset) offset) |
365 | (setq offset (slot-value slotd 'offset))) |
366 | |
367 | (incf offset (size-of (slot-definition-type slotd))) |
368 | (incf offset (mod offset +struct-alignmen+)) |
369 | (setq size (max size offset)) |
370 | |
371 | finally (unless (slot-boundp class 'size) |
372 | (setf (slot-value class 'size) size))) |
b44caf77 |
373 | (call-next-method)) |
8ae7ddc2 |
374 | |
935a783c |
375 | |
376 | (defmethod validate-superclass ((class proxy-class) (super standard-class)) |
377 | (subtypep (class-name super) 'proxy)) |
378 | |
ba25fa44 |
379 | (defmethod proxy-class-size (class) |
380 | (declare (ignore class)) |
381 | 0) |
935a783c |
382 | ) |
383 | |
384 | (defgeneric make-proxy-instance (class location weak-ref |
385 | &rest initargs &key));) |
b44caf77 |
386 | |
387 | (defmethod make-proxy-instance ((class symbol) location weak-ref |
388 | &rest initargs &key) |
389 | (apply #'make-proxy-instance (find-class class) location weak-ref initargs)) |
390 | |
391 | (defmethod make-proxy-instance ((class proxy-class) location weak-ref |
392 | &rest initargs &key) |
393 | (let ((instance (allocate-instance class))) |
394 | (apply |
395 | #'initialize-proxy |
396 | instance :location location :weak-ref weak-ref initargs) |
397 | instance)) |
398 | |
399 | (defun ensure-proxy-instance (class location weak-ref &rest initargs) |
400 | (or |
401 | (find-cached-instance location) |
402 | (apply #'make-proxy-instance class location weak-ref initargs))) |
403 | |
404 | |
ba25fa44 |
405 | |
406 | ;;;; Superclasses for wrapping of C structures |
b44caf77 |
407 | |
408 | (eval-when (:compile-toplevel :load-toplevel :execute) |
ba25fa44 |
409 | (defclass struct (proxy) |
b44caf77 |
410 | () |
411 | (:metaclass proxy-class) |
ba25fa44 |
412 | (:copy %copy-struct) |
413 | (:free %free-struct))) |
b44caf77 |
414 | |
935a783c |
415 | (defmethod initialize-instance ((structure struct) &rest initargs) |
b44caf77 |
416 | (declare (ignore initargs)) |
417 | (setf |
418 | (slot-value structure 'location) |
ba25fa44 |
419 | (allocate-memory (proxy-class-size (class-of structure)))) |
b44caf77 |
420 | (call-next-method)) |
421 | |
422 | |
ba25fa44 |
423 | (defun %copy-struct (type location) |
424 | (copy-memory location (proxy-class-size (find-class type)))) |
b44caf77 |
425 | |
ba25fa44 |
426 | (defun %free-struct (type location) |
427 | (declare (ignore type)) |
428 | (deallocate-memory location)) |
b44caf77 |
429 | |
430 | |
8ae7ddc2 |
431 | ;(eval-when (:compile-toplevel :load-toplevel :execute) |
ba25fa44 |
432 | (defclass static (struct) |
433 | () |
8ae7ddc2 |
434 | (:metaclass proxy-class) |
435 | (:copy %copy-static) |
436 | (:free %free-static));) |
b44caf77 |
437 | |
ba25fa44 |
438 | (defun %copy-static (type location) |
439 | (declare (ignore type)) |
440 | location) |
b44caf77 |
441 | |
ba25fa44 |
442 | (defun %free-static (type location) |
443 | (declare (ignore type location)) |
444 | nil) |