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 | |
0466f75e |
18 | ;; $Id: proxy.lisp,v 1.9 2004/10/28 19:29: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) |
0466f75e |
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))))))))) |
935a783c |
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) |
0466f75e |
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)))))))) |
935a783c |
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 | |
b44caf77 |
129 | |
b44caf77 |
130 | (defmethod slot-value-using-class |
ba25fa44 |
131 | ((class virtual-slot-class) (object standard-object) |
b44caf77 |
132 | (slotd effective-virtual-slot-definition)) |
935a783c |
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)))) |
b44caf77 |
136 | |
b44caf77 |
137 | (defmethod slot-boundp-using-class |
ba25fa44 |
138 | ((class virtual-slot-class) (object standard-object) |
b44caf77 |
139 | (slotd effective-virtual-slot-definition)) |
935a783c |
140 | (funcall (slot-value slotd 'boundp-function) object)) |
141 | |
142 | (defmethod (setf slot-value-using-class) |
ba25fa44 |
143 | (value (class virtual-slot-class) (object standard-object) |
b44caf77 |
144 | (slotd effective-virtual-slot-definition)) |
935a783c |
145 | (funcall (slot-value slotd 'writer-function) value object)) |
146 | |
147 | |
b44caf77 |
148 | (defmethod validate-superclass |
935a783c |
149 | ((class virtual-slot-class) (super standard-class)) |
b44caf77 |
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 () |
ba25fa44 |
177 | ((location :reader proxy-location :type system-area-pointer))) |
b44caf77 |
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 | |
b44caf77 |
189 | (defmethod initialize-proxy ((instance proxy) |
ba25fa44 |
190 | &rest initargs &key location weak-ref) |
b44caf77 |
191 | (declare (ignore initargs)) |
ba25fa44 |
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))) |
b44caf77 |
201 | |
202 | (defmethod instance-finalizer ((instance proxy)) |
935a783c |
203 | (let ((class (class-of instance)) |
ba25fa44 |
204 | (type (type-of instance)) |
205 | (location (proxy-location instance))) |
935a783c |
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))))) |
b44caf77 |
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 | |
ba25fa44 |
227 | (deftype-method translate-to-alien |
228 | proxy (type-spec instance &optional weak-ref) |
229 | (if weak-ref |
230 | `(proxy-location ,instance) |
935a783c |
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)))))) |
b44caf77 |
235 | |
ba25fa44 |
236 | (deftype-method unreference-alien proxy (type-spec location) |
935a783c |
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)))) |
ba25fa44 |
241 | |
b44caf77 |
242 | |
243 | ;;;; Metaclass used for subclasses of proxy |
244 | |
245 | (eval-when (:compile-toplevel :load-toplevel :execute) |
ba25fa44 |
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))) |
b44caf77 |
250 | |
251 | (defclass direct-alien-slot-definition (direct-virtual-slot-definition) |
ba25fa44 |
252 | ((allocation :initform :alien) |
253 | (offset :reader slot-definition-offset :initarg :offset))) |
b44caf77 |
254 | |
255 | (defclass effective-alien-slot-definition (effective-virtual-slot-definition) |
935a783c |
256 | ((offset :reader slot-definition-offset :initarg :offset))) |
ba25fa44 |
257 | |
b44caf77 |
258 | |
259 | (defmethod most-specific-proxy-superclass ((class proxy-class)) |
260 | (find-if |
261 | #'(lambda (class) |
262 | (subtypep (class-name class) 'proxy)) |
935a783c |
263 | (cdr (compute-class-precedence-list class)))) |
264 | |
ba25fa44 |
265 | (defmethod direct-proxy-superclass ((class proxy-class)) |
266 | (find-if |
267 | #'(lambda (class) |
268 | (subtypep (class-name class) 'proxy)) |
935a783c |
269 | (class-direct-superclasses class))) |
270 | |
b44caf77 |
271 | (defmethod shared-initialize ((class proxy-class) names |
ba25fa44 |
272 | &rest initargs &key size copy free) |
b44caf77 |
273 | (declare (ignore initargs)) |
274 | (call-next-method) |
ba25fa44 |
275 | (cond |
935a783c |
276 | (size (setf (slot-value class 'size) (first size))) |
277 | ((slot-boundp class 'size) (slot-makunbound class 'size))) |
ba25fa44 |
278 | (cond |
935a783c |
279 | (copy (setf (slot-value class 'copy) (first copy))) |
280 | ((slot-boundp class 'copy) (slot-makunbound class 'copy))) |
ba25fa44 |
281 | (cond |
935a783c |
282 | (free (setf (slot-value class 'free) (first free))) |
283 | ((slot-boundp class 'free) (slot-makunbound class 'free)))) |
284 | |
935a783c |
285 | (defmethod shared-initialize :after ((class proxy-class) names &rest initargs) |
8ae7ddc2 |
286 | (let ((super (most-specific-proxy-superclass class))) |
287 | (unless (or (not super) (eq super (find-class 'proxy))) |
935a783c |
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) |
b44caf77 |
294 | (case (getf initargs :allocation) |
295 | ((nil :alien) (find-class 'direct-alien-slot-definition)) |
b44caf77 |
296 | (t (call-next-method)))) |
935a783c |
297 | |
298 | (defmethod effective-slot-definition-class ((class proxy-class) &rest initargs) |
b44caf77 |
299 | (case (getf initargs :allocation) |
300 | (:alien (find-class 'effective-alien-slot-definition)) |
b44caf77 |
301 | (t (call-next-method)))) |
302 | |
935a783c |
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 | |
935a783c |
341 | ;; TODO: call some C code to detect this a compile time |
342 | (defconstant +struct-alignmen+ 4) |
b44caf77 |
343 | |
344 | (defmethod compute-slots ((class proxy-class)) |
935a783c |
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))) |
b44caf77 |
361 | (call-next-method)) |
8ae7ddc2 |
362 | |
935a783c |
363 | |
364 | (defmethod validate-superclass ((class proxy-class) (super standard-class)) |
365 | (subtypep (class-name super) 'proxy)) |
366 | |
ba25fa44 |
367 | (defmethod proxy-class-size (class) |
368 | (declare (ignore class)) |
369 | 0) |
935a783c |
370 | ) |
371 | |
372 | (defgeneric make-proxy-instance (class location weak-ref |
373 | &rest initargs &key));) |
b44caf77 |
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 | |
ba25fa44 |
393 | |
394 | ;;;; Superclasses for wrapping of C structures |
b44caf77 |
395 | |
396 | (eval-when (:compile-toplevel :load-toplevel :execute) |
ba25fa44 |
397 | (defclass struct (proxy) |
b44caf77 |
398 | () |
399 | (:metaclass proxy-class) |
ba25fa44 |
400 | (:copy %copy-struct) |
401 | (:free %free-struct))) |
b44caf77 |
402 | |
935a783c |
403 | (defmethod initialize-instance ((structure struct) &rest initargs) |
b44caf77 |
404 | (declare (ignore initargs)) |
405 | (setf |
406 | (slot-value structure 'location) |
ba25fa44 |
407 | (allocate-memory (proxy-class-size (class-of structure)))) |
b44caf77 |
408 | (call-next-method)) |
409 | |
410 | |
ba25fa44 |
411 | (defun %copy-struct (type location) |
412 | (copy-memory location (proxy-class-size (find-class type)))) |
b44caf77 |
413 | |
ba25fa44 |
414 | (defun %free-struct (type location) |
415 | (declare (ignore type)) |
416 | (deallocate-memory location)) |
b44caf77 |
417 | |
418 | |
8ae7ddc2 |
419 | ;(eval-when (:compile-toplevel :load-toplevel :execute) |
ba25fa44 |
420 | (defclass static (struct) |
421 | () |
8ae7ddc2 |
422 | (:metaclass proxy-class) |
423 | (:copy %copy-static) |
424 | (:free %free-static));) |
b44caf77 |
425 | |
ba25fa44 |
426 | (defun %copy-static (type location) |
427 | (declare (ignore type)) |
428 | location) |
b44caf77 |
429 | |
ba25fa44 |
430 | (defun %free-static (type location) |
431 | (declare (ignore type location)) |
432 | nil) |