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 | |
73572c12 |
18 | ;; $Id: proxy.lisp,v 1.19 2005-02-03 23:09:04 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) |
eeda1c2d |
31 | (unbound :reader slot-definition-unbound :initarg :unbound) |
4d83a8a6 |
32 | (boundp :reader slot-definition-boundp :initarg :boundp))) |
94f15c3c |
33 | |
4d83a8a6 |
34 | (defclass effective-virtual-slot-definition (standard-effective-slot-definition) |
35 | ((setter :reader slot-definition-setter :initarg :setter) |
36 | (getter :reader slot-definition-getter :initarg :getter) |
eeda1c2d |
37 | (unbound :reader slot-definition-unbound :initarg :unbound) |
73572c12 |
38 | (boundp :reader slot-definition-boundp :initarg :boundp)))) |
eeda1c2d |
39 | |
40 | (defvar *unbound-marker* (gensym "UNBOUND-MARKER-")) |
4d83a8a6 |
41 | |
eeda1c2d |
42 | (defun most-specific-slot-value (instances slot &optional |
43 | (default *unbound-marker*)) |
4d83a8a6 |
44 | (let ((object (find-if |
45 | #'(lambda (ob) |
46 | (and (slot-exists-p ob slot) (slot-boundp ob slot))) |
47 | instances))) |
48 | (if object |
49 | (slot-value object slot) |
73572c12 |
50 | default)));) |
4d83a8a6 |
51 | |
94f15c3c |
52 | |
53 | |
9adccb27 |
54 | (defmethod direct-slot-definition-class ((class virtual-slots-class) &rest initargs) |
94f15c3c |
55 | (if (eq (getf initargs :allocation) :virtual) |
56 | (find-class 'direct-virtual-slot-definition) |
57 | (call-next-method))) |
58 | |
9adccb27 |
59 | (defmethod effective-slot-definition-class ((class virtual-slots-class) &rest initargs) |
94f15c3c |
60 | (if (eq (getf initargs :allocation) :virtual) |
61 | (find-class 'effective-virtual-slot-definition) |
62 | (call-next-method))) |
63 | |
4d83a8a6 |
64 | |
65 | (defmethod initialize-internal-slot-functions ((slotd effective-virtual-slot-definition)) |
eeda1c2d |
66 | (if (not (slot-boundp slotd 'getter)) |
67 | (setf |
4d83a8a6 |
68 | (slot-value slotd 'reader-function) |
eeda1c2d |
69 | #'(lambda (object) |
70 | (declare (ignore object)) |
71 | (error "Can't read slot: ~A" (slot-definition-name slotd))) |
72 | (slot-value slotd 'boundp-function) |
73 | #'(lambda (object) (declare (ignore object)) nil)) |
74 | |
75 | (let ((getter-function |
76 | (let ((getter (slot-value slotd 'getter))) |
77 | (etypecase getter |
78 | (function getter) |
79 | (symbol |
80 | #'(lambda (object) |
81 | (funcall getter object))) |
82 | (string |
83 | (let ((reader nil)) |
84 | (setf (slot-value slotd 'reader-function) |
85 | #'(lambda (object) |
86 | (unless reader |
12b7df04 |
87 | (setq reader |
88 | (mkbinding getter |
89 | (slot-definition-type slotd) 'pointer))) |
eeda1c2d |
90 | (funcall reader (proxy-location object)))))))))) |
91 | |
4d83a8a6 |
92 | (setf |
eeda1c2d |
93 | (slot-value slotd 'boundp-function) |
94 | (cond |
eeda1c2d |
95 | ((slot-boundp slotd 'unbound) |
96 | (let ((unbound-value (slot-value slotd 'unbound))) |
12b7df04 |
97 | #'(lambda (object) |
98 | (not (eq (funcall getter-function object) unbound-value))))) |
99 | ((slot-boundp slotd 'boundp) |
100 | (let ((boundp (slot-value slotd 'boundp))) |
eeda1c2d |
101 | (etypecase boundp |
102 | (function boundp) |
103 | (symbol #'(lambda (object) |
104 | (funcall boundp object))) |
105 | (string (let ((reader ())) |
106 | #'(lambda (object) |
107 | (unless reader |
108 | (setq reader |
109 | (mkbinding boundp |
110 | (slot-definition-type slotd) 'pointer))) |
12b7df04 |
111 | (funcall reader (proxy-location object)))))))) |
112 | ((multiple-value-bind (unbound-p unbound-value) |
113 | (unbound-value (slot-definition-type slotd)) |
114 | (when unbound-p |
115 | #'(lambda (object) |
116 | (not (eq (funcall getter-function object) unbound-value)))))) |
117 | (#'(lambda (object) (declare (ignore object)) t)))) |
eeda1c2d |
118 | |
119 | (setf |
120 | (slot-value slotd 'reader-function) |
121 | (cond |
122 | ((slot-boundp slotd 'unbound) |
123 | (let ((unbound (slot-value slotd 'unbound)) |
124 | (slot-name (slot-definition-name slotd))) |
12b7df04 |
125 | #'(lambda (object) |
126 | (let ((value (funcall getter-function object))) |
127 | (if (eq value unbound) |
128 | (slot-unbound (class-of object) object slot-name) |
129 | value))))) |
eeda1c2d |
130 | ((slot-boundp slotd 'boundp) |
131 | (let ((boundp-function (slot-value slotd 'boundp-function))) |
12b7df04 |
132 | #'(lambda (object) |
133 | (and |
134 | (funcall boundp-function object) |
135 | (funcall getter-function object))))) |
136 | ((multiple-value-bind (unbound-p unbound-value) |
137 | (unbound-value (slot-definition-type slotd)) |
138 | (let ((slot-name (slot-definition-name slotd))) |
139 | (when unbound-p |
140 | #'(lambda (object) |
141 | (let ((value (funcall getter-function object))) |
142 | (if (eq value unbound-value) |
143 | (slot-unbound (class-of object) object slot-name) |
144 | value))))))) |
eeda1c2d |
145 | (getter-function))))) |
146 | |
147 | (setf |
148 | (slot-value slotd 'writer-function) |
149 | (if (not (slot-boundp slotd 'setter)) |
150 | #'(lambda (object) |
151 | (declare (ignore object)) |
152 | (error "Can't set slot: ~A" (slot-definition-name slotd))) |
153 | (with-slots (setter) slotd |
4d83a8a6 |
154 | (etypecase setter |
155 | (function setter) |
eeda1c2d |
156 | ((or symbol cons) |
157 | #'(lambda (value object) |
158 | (funcall (fdefinition setter) value object))) |
7d1ddc9e |
159 | (string |
eeda1c2d |
160 | (let ((writer ())) |
161 | (setf |
162 | (slot-value slotd 'writer-function) |
163 | #'(lambda (value object) |
164 | (unless writer |
165 | (setq writer |
166 | (mkbinding setter 'nil 'pointer |
167 | (slot-definition-type slotd)))) |
168 | (funcall writer (proxy-location object) value))))))))) |
169 | |
4d83a8a6 |
170 | (initialize-internal-slot-gfs (slot-definition-name slotd))) |
171 | |
172 | |
173 | |
eeda1c2d |
174 | (defmethod compute-slot-accessor-info ((slotd effective-virtual-slot-definition) type gf) |
4d83a8a6 |
175 | nil) |
176 | |
9adccb27 |
177 | (defmethod compute-effective-slot-definition-initargs ((class virtual-slots-class) direct-slotds) |
eeda1c2d |
178 | (if (typep (first direct-slotds) 'direct-virtual-slot-definition) |
179 | (let ((initargs ())) |
180 | (let ((getter (most-specific-slot-value direct-slotds 'getter))) |
181 | (unless (eq getter *unbound-marker*) |
182 | (setf (getf initargs :getter) getter))) |
183 | (let ((setter (most-specific-slot-value direct-slotds 'setter))) |
184 | (unless (eq setter *unbound-marker*) |
185 | (setf (getf initargs :setter) setter))) |
186 | (let ((unbound (most-specific-slot-value direct-slotds 'unbound))) |
187 | (unless (eq unbound *unbound-marker*) |
188 | (setf (getf initargs :unbound) unbound))) |
189 | (let ((boundp (most-specific-slot-value direct-slotds 'boundp))) |
190 | (unless (eq boundp *unbound-marker*) |
191 | (setf (getf initargs :boundp) boundp))) |
192 | (nconc initargs (call-next-method))) |
4d83a8a6 |
193 | (call-next-method))) |
194 | |
94f15c3c |
195 | |
94f15c3c |
196 | (defmethod slot-value-using-class |
9adccb27 |
197 | ((class virtual-slots-class) (object standard-object) |
94f15c3c |
198 | (slotd effective-virtual-slot-definition)) |
4d83a8a6 |
199 | (if (funcall (slot-value slotd 'boundp-function) object) |
200 | (funcall (slot-value slotd 'reader-function) object) |
201 | (slot-unbound class object (slot-definition-name slotd)))) |
94f15c3c |
202 | |
94f15c3c |
203 | (defmethod slot-boundp-using-class |
9adccb27 |
204 | ((class virtual-slots-class) (object standard-object) |
94f15c3c |
205 | (slotd effective-virtual-slot-definition)) |
4d83a8a6 |
206 | (funcall (slot-value slotd 'boundp-function) object)) |
207 | |
208 | (defmethod (setf slot-value-using-class) |
9adccb27 |
209 | (value (class virtual-slots-class) (object standard-object) |
94f15c3c |
210 | (slotd effective-virtual-slot-definition)) |
4d83a8a6 |
211 | (funcall (slot-value slotd 'writer-function) value object)) |
212 | |
213 | |
94f15c3c |
214 | (defmethod validate-superclass |
9adccb27 |
215 | ((class virtual-slots-class) (super standard-class)) |
94f15c3c |
216 | t) |
217 | |
218 | |
219 | ;;;; Proxy cache |
220 | |
221 | (internal *instance-cache*) |
222 | (defvar *instance-cache* (make-hash-table :test #'eql)) |
223 | |
224 | (defun cache-instance (instance) |
225 | (setf |
73572c12 |
226 | (gethash (sap-int (proxy-location instance)) *instance-cache*) |
227 | (make-weak-pointer instance))) |
94f15c3c |
228 | |
229 | (defun find-cached-instance (location) |
73572c12 |
230 | (let ((ref (gethash (sap-int location) *instance-cache*))) |
94f15c3c |
231 | (when ref |
73572c12 |
232 | (weak-pointer-value ref)))) |
94f15c3c |
233 | |
0f134a29 |
234 | (defun instance-cached-p (location) |
73572c12 |
235 | (gethash (sap-int location) *instance-cache*)) |
0f134a29 |
236 | |
94f15c3c |
237 | (defun remove-cached-instance (location) |
73572c12 |
238 | (remhash (sap-int location) *instance-cache*)) |
94f15c3c |
239 | |
9adccb27 |
240 | ;; For debuging |
241 | (defun cached-instances () |
242 | (let ((instances ())) |
243 | (maphash #'(lambda (location ref) |
244 | (declare (ignore location)) |
73572c12 |
245 | (push (weak-pointer-value ref) instances)) |
9adccb27 |
246 | *instance-cache*) |
247 | instances)) |
248 | |
94f15c3c |
249 | |
250 | |
251 | ;;;; Proxy for alien instances |
252 | |
9adccb27 |
253 | (defclass proxy () |
254 | ((location :reader proxy-location :type system-area-pointer))) |
94f15c3c |
255 | |
9adccb27 |
256 | (defgeneric initialize-proxy (object &rest initargs)) |
257 | (defgeneric instance-finalizer (object)) |
258 | (defgeneric reference-foreign (class location)) |
259 | (defgeneric unreference-foreign (class location)) |
260 | |
3b167652 |
261 | (defmethod reference-foreign ((name symbol) location) |
262 | (reference-foreign (find-class name) location)) |
263 | |
264 | (defmethod unreference-foreign ((name symbol) location) |
265 | (unreference-foreign (find-class name) location)) |
266 | |
9adccb27 |
267 | (defmethod unreference-foreign :around ((class class) location) |
268 | (unless (null-pointer-p location) |
269 | ;; (format t "Unreferencing ~A at ~A" (class-name class) location) |
270 | ;; (finish-output *standard-output*) |
271 | (call-next-method) |
272 | ;; (write-line " done") |
273 | ;; (finish-output *standard-output*) |
274 | )) |
94f15c3c |
275 | |
0f134a29 |
276 | (defmethod print-object ((instance proxy) stream) |
277 | (print-unreadable-object (instance stream :type t :identity nil) |
eeda1c2d |
278 | (when (slot-boundp instance 'location) |
279 | (format stream "at 0x~X" (sap-int (proxy-location instance)))))) |
94f15c3c |
280 | |
9adccb27 |
281 | (defmethod initialize-instance :around ((instance proxy) &key location) |
282 | (if location |
283 | (setf (slot-value instance 'location) location) |
284 | (call-next-method)) |
12d0437e |
285 | (cache-instance instance) |
73572c12 |
286 | (finalize instance (instance-finalizer instance)) |
9adccb27 |
287 | instance) |
94f15c3c |
288 | |
289 | (defmethod instance-finalizer ((instance proxy)) |
9adccb27 |
290 | (let ((location (proxy-location instance)) |
291 | (class (class-of instance))) |
292 | ;; (unless (find-method #'unreference-foreign nil (list (class-of class) t) nil) |
293 | ;; (error "No matching method for UNREFERENCE-INSTANCE when called with class ~A" class)) |
294 | #'(lambda () |
1dbf4216 |
295 | (remove-cached-instance location) |
9adccb27 |
296 | (unreference-foreign class location)))) |
12d0437e |
297 | |
94f15c3c |
298 | |
299 | ;;;; Metaclass used for subclasses of proxy |
300 | |
73572c12 |
301 | (defgeneric most-specific-proxy-superclass (class)) |
302 | (defgeneric direct-proxy-superclass (class)) |
303 | |
304 | |
94f15c3c |
305 | (eval-when (:compile-toplevel :load-toplevel :execute) |
9adccb27 |
306 | (defclass proxy-class (virtual-slots-class) |
307 | ((size :reader proxy-instance-size))) |
94f15c3c |
308 | |
309 | (defclass direct-alien-slot-definition (direct-virtual-slot-definition) |
12d0437e |
310 | ((allocation :initform :alien) |
311 | (offset :reader slot-definition-offset :initarg :offset))) |
94f15c3c |
312 | |
313 | (defclass effective-alien-slot-definition (effective-virtual-slot-definition) |
4d83a8a6 |
314 | ((offset :reader slot-definition-offset :initarg :offset))) |
12d0437e |
315 | |
94f15c3c |
316 | (defmethod most-specific-proxy-superclass ((class proxy-class)) |
317 | (find-if |
318 | #'(lambda (class) |
319 | (subtypep (class-name class) 'proxy)) |
4d83a8a6 |
320 | (cdr (compute-class-precedence-list class)))) |
73572c12 |
321 | |
12d0437e |
322 | (defmethod direct-proxy-superclass ((class proxy-class)) |
323 | (find-if |
324 | #'(lambda (class) |
325 | (subtypep (class-name class) 'proxy)) |
4d83a8a6 |
326 | (class-direct-superclasses class))) |
327 | |
eeda1c2d |
328 | (defmethod shared-initialize ((class proxy-class) names &key size) |
94f15c3c |
329 | (call-next-method) |
12d0437e |
330 | (cond |
4d83a8a6 |
331 | (size (setf (slot-value class 'size) (first size))) |
9adccb27 |
332 | ((slot-boundp class 'size) (slot-makunbound class 'size)))) |
4d83a8a6 |
333 | |
334 | (defmethod direct-slot-definition-class ((class proxy-class) &rest initargs) |
94f15c3c |
335 | (case (getf initargs :allocation) |
336 | ((nil :alien) (find-class 'direct-alien-slot-definition)) |
94f15c3c |
337 | (t (call-next-method)))) |
4d83a8a6 |
338 | |
339 | (defmethod effective-slot-definition-class ((class proxy-class) &rest initargs) |
94f15c3c |
340 | (case (getf initargs :allocation) |
341 | (:alien (find-class 'effective-alien-slot-definition)) |
94f15c3c |
342 | (t (call-next-method)))) |
343 | |
4d83a8a6 |
344 | |
345 | (defmethod compute-effective-slot-definition-initargs ((class proxy-class) direct-slotds) |
346 | (if (eq (most-specific-slot-value direct-slotds 'allocation) :alien) |
347 | (nconc |
348 | (list :offset (most-specific-slot-value direct-slotds 'offset)) |
349 | (call-next-method)) |
350 | (call-next-method))) |
351 | |
352 | |
353 | (defmethod initialize-internal-slot-functions ((slotd effective-alien-slot-definition)) |
354 | (with-slots (offset) slotd |
9adccb27 |
355 | (let ((type (slot-definition-type slotd))) |
eeda1c2d |
356 | (unless (slot-boundp slotd 'getter) |
9adccb27 |
357 | (let ((reader (reader-function type))) |
358 | (setf |
eeda1c2d |
359 | (slot-value slotd 'getter) |
9adccb27 |
360 | #'(lambda (object) |
361 | (funcall reader (proxy-location object) offset))))) |
4d83a8a6 |
362 | |
eeda1c2d |
363 | (unless (slot-boundp slotd 'setter) |
9adccb27 |
364 | (let ((writer (writer-function type)) |
365 | (destroy (destroy-function type))) |
366 | (setf |
eeda1c2d |
367 | (slot-value slotd 'setter) |
9adccb27 |
368 | #'(lambda (value object) |
369 | (let ((location (proxy-location object))) |
370 | (funcall destroy location offset) ; destroy old value |
eeda1c2d |
371 | (funcall writer value location offset)))))))) |
372 | |
4d83a8a6 |
373 | (call-next-method)) |
374 | |
375 | |
4d83a8a6 |
376 | ;; TODO: call some C code to detect this a compile time |
377 | (defconstant +struct-alignmen+ 4) |
94f15c3c |
378 | |
379 | (defmethod compute-slots ((class proxy-class)) |
4d83a8a6 |
380 | (loop |
3b167652 |
381 | with offset = (let ((size-of-super-classes |
382 | (proxy-instance-size |
383 | (most-specific-proxy-superclass class)))) |
384 | (+ size-of-super-classes |
385 | (mod size-of-super-classes +struct-alignmen+))) |
4d83a8a6 |
386 | with size = offset |
387 | for slotd in (class-direct-slots class) |
388 | when (eq (slot-definition-allocation slotd) :alien) |
389 | do (if (not (slot-boundp slotd 'offset)) |
390 | (setf (slot-value slotd 'offset) offset) |
391 | (setq offset (slot-value slotd 'offset))) |
392 | |
393 | (incf offset (size-of (slot-definition-type slotd))) |
394 | (incf offset (mod offset +struct-alignmen+)) |
395 | (setq size (max size offset)) |
396 | |
397 | finally (unless (slot-boundp class 'size) |
398 | (setf (slot-value class 'size) size))) |
94f15c3c |
399 | (call-next-method)) |
3e15002d |
400 | |
4d83a8a6 |
401 | |
402 | (defmethod validate-superclass ((class proxy-class) (super standard-class)) |
403 | (subtypep (class-name super) 'proxy)) |
404 | |
9adccb27 |
405 | (defmethod proxy-instance-size (class) |
12d0437e |
406 | (declare (ignore class)) |
407 | 0) |
47a11c16 |
408 | |
409 | (defmethod proxy-instance-size ((class-name symbol)) |
410 | (proxy-instance-size (find-class class-name))) |
4d83a8a6 |
411 | ) |
412 | |
9adccb27 |
413 | (defmethod alien-type ((class proxy-class) &rest args) |
414 | (declare (ignore class args)) |
415 | (alien-type 'pointer)) |
416 | |
417 | (defmethod size-of ((class proxy-class) &rest args) |
418 | (declare (ignore class args)) |
419 | (size-of 'pointer)) |
420 | |
421 | (defmethod from-alien-form (location (class proxy-class) &rest args) |
422 | (declare (ignore args)) |
423 | `(ensure-proxy-instance ',(class-name class) ,location)) |
424 | |
425 | (defmethod from-alien-function ((class proxy-class) &rest args) |
426 | (declare (ignore args)) |
427 | #'(lambda (location) |
428 | (ensure-proxy-instance class location))) |
94f15c3c |
429 | |
9adccb27 |
430 | (defmethod to-alien-form (instance (class proxy-class) &rest args) |
431 | (declare (ignore class args)) |
432 | `(proxy-location ,instance)) |
94f15c3c |
433 | |
9adccb27 |
434 | (defmethod to-alien-function ((class proxy-class) &rest args) |
435 | (declare (ignore class args)) |
436 | #'proxy-location) |
437 | |
9ca5565a |
438 | (defmethod copy-from-alien-form (location (class proxy-class) &rest args) |
439 | (declare (ignore args)) |
440 | (let ((class-name (class-name class))) |
441 | `(ensure-proxy-instance ',class-name |
442 | (reference-foreign ',class-name ,location)))) |
443 | |
444 | (defmethod copy-from-alien-function ((class proxy-class) &rest args) |
445 | (declare (ignore args)) |
446 | #'(lambda (location) |
447 | (ensure-proxy-instance class (reference-foreign class location)))) |
448 | |
449 | (defmethod copy-to-alien-form (instance (class proxy-class) &rest args) |
450 | (declare (ignore args)) |
451 | `(reference-foreign ',(class-name class) (proxy-location ,instance))) |
452 | |
453 | (defmethod copy-to-alien-function ((class proxy-class) &rest args) |
73572c12 |
454 | (declare (ignore args)) |
9ca5565a |
455 | #'(lambda (instance) |
456 | (reference-foreign class (proxy-location instance)))) |
457 | |
9adccb27 |
458 | (defmethod writer-function ((class proxy-class) &rest args) |
459 | (declare (ignore args)) |
460 | #'(lambda (instance location &optional (offset 0)) |
461 | (assert (null-pointer-p (sap-ref-sap location offset))) |
462 | (setf |
463 | (sap-ref-sap location offset) |
464 | (reference-foreign class (proxy-location instance))))) |
94f15c3c |
465 | |
9adccb27 |
466 | (defmethod reader-function ((class proxy-class) &rest args) |
467 | (declare (ignore args)) |
468 | #'(lambda (location &optional (offset 0)) |
9ca5565a |
469 | (let ((instance (sap-ref-sap location offset))) |
470 | (unless (null-pointer-p instance) |
471 | (ensure-proxy-instance class (reference-foreign class instance)))))) |
94f15c3c |
472 | |
9adccb27 |
473 | (defmethod destroy-function ((class proxy-class) &rest args) |
474 | (declare (ignore args)) |
475 | #'(lambda (location &optional (offset 0)) |
476 | (unreference-foreign class (sap-ref-sap location offset)))) |
477 | |
12b7df04 |
478 | (defmethod unbound-value ((class proxy-class) &rest args) |
47a11c16 |
479 | (declare (ignore args)) |
12b7df04 |
480 | (values t nil)) |
9adccb27 |
481 | |
482 | (defgeneric ensure-proxy-instance (class location) |
483 | (:documentation "Returns a proxy object representing the foreign object at the give location.")) |
484 | |
485 | (defmethod ensure-proxy-instance :around (class location) |
486 | (unless (null-pointer-p location) |
487 | (or |
488 | (find-cached-instance location) |
489 | (call-next-method)))) |
490 | |
491 | (defmethod ensure-proxy-instance ((class symbol) location) |
492 | (ensure-proxy-instance (find-class class) location)) |
493 | |
494 | (defmethod ensure-proxy-instance ((class proxy-class) location) |
495 | (make-instance class :location location)) |
94f15c3c |
496 | |
12d0437e |
497 | |
498 | ;;;; Superclasses for wrapping of C structures |
94f15c3c |
499 | |
9adccb27 |
500 | (defclass struct (proxy) |
501 | () |
502 | (:metaclass proxy-class)) |
94f15c3c |
503 | |
9adccb27 |
504 | (defmethod initialize-instance ((struct struct) &rest initargs) |
94f15c3c |
505 | (declare (ignore initargs)) |
ec1a4146 |
506 | (unless (slot-boundp struct 'location) |
507 | (let ((size (proxy-instance-size (class-of struct)))) |
508 | (if (zerop size) |
509 | (error "~A has zero size" (class-of struct)) |
47a11c16 |
510 | (setf (slot-value struct 'location) (allocate-memory size))))) |
94f15c3c |
511 | (call-next-method)) |
512 | |
513 | |
9adccb27 |
514 | ;;;; Metaclasses used for subclasses of struct |
515 | |
516 | (defclass struct-class (proxy-class) |
517 | ()) |
94f15c3c |
518 | |
9adccb27 |
519 | (defmethod reference-foreign ((class struct-class) location) |
520 | (copy-memory location (proxy-instance-size class))) |
521 | |
522 | (defmethod unreference-foreign ((class struct-class) location) |
12d0437e |
523 | (deallocate-memory location)) |
94f15c3c |
524 | |
525 | |
9adccb27 |
526 | (defclass static-struct-class (struct-class) |
527 | ()) |
94f15c3c |
528 | |
9adccb27 |
529 | (defmethod reference-foreign ((class static-struct-class) location) |
530 | (declare (ignore class)) |
12d0437e |
531 | location) |
94f15c3c |
532 | |
9adccb27 |
533 | (defmethod unreference-foreign ((class static-struct-class) location) |
534 | (declare (ignore class location)) |
12d0437e |
535 | nil) |