New type method WEAK-READER-FUNCTION
[clg] / glib / proxy.lisp
CommitLineData
112ac1d3 1;; Common Lisp bindings for GTK+ v2.x
2;; Copyright 2000-2005 Espen S. Johnsen <espen@users.sf.net>
94f15c3c 3;;
112ac1d3 4;; Permission is hereby granted, free of charge, to any person obtaining
5;; a copy of this software and associated documentation files (the
6;; "Software"), to deal in the Software without restriction, including
7;; without limitation the rights to use, copy, modify, merge, publish,
8;; distribute, sublicense, and/or sell copies of the Software, and to
9;; permit persons to whom the Software is furnished to do so, subject to
10;; the following conditions:
94f15c3c 11;;
112ac1d3 12;; The above copyright notice and this permission notice shall be
13;; included in all copies or substantial portions of the Software.
94f15c3c 14;;
112ac1d3 15;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
94f15c3c 22
8958fa4a 23;; $Id: proxy.lisp,v 1.25 2006-02-05 15:38:57 espen Exp $
94f15c3c 24
25(in-package "GLIB")
26
94f15c3c 27;;;; Superclass for all metaclasses implementing some sort of virtual slots
28
29(eval-when (:compile-toplevel :load-toplevel :execute)
9adccb27 30 (defclass virtual-slots-class (standard-class)
4d83a8a6 31 ())
94f15c3c 32
33 (defclass direct-virtual-slot-definition (standard-direct-slot-definition)
12d0437e 34 ((setter :reader slot-definition-setter :initarg :setter)
4d83a8a6 35 (getter :reader slot-definition-getter :initarg :getter)
eeda1c2d 36 (unbound :reader slot-definition-unbound :initarg :unbound)
4d83a8a6 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)
eeda1c2d 42 (unbound :reader slot-definition-unbound :initarg :unbound)
e2ebafb1 43 (boundp :reader slot-definition-boundp :initarg :boundp)))
4d83a8a6 44
e2ebafb1 45 (defclass direct-special-slot-definition (standard-direct-slot-definition)
46 ())
94f15c3c 47
e2ebafb1 48 (defclass effective-special-slot-definition (standard-effective-slot-definition)
49 ()))
50
51(defvar *unbound-marker* (gensym "UNBOUND-MARKER-"))
52
53(defun most-specific-slot-value (instances slot &optional (default *unbound-marker*))
54 (let ((object (find-if
55 #'(lambda (ob)
56 (and (slot-exists-p ob slot) (slot-boundp ob slot)))
57 instances)))
58 (if object
59 (slot-value object slot)
60 default)))
61
62(defmethod initialize-instance ((slotd effective-special-slot-definition) &rest initargs)
63 (declare (ignore initargs))
64 (call-next-method)
65 (setf (slot-value slotd 'allocation) :instance))
66
94f15c3c 67
9adccb27 68(defmethod direct-slot-definition-class ((class virtual-slots-class) &rest initargs)
e2ebafb1 69 (case (getf initargs :allocation)
70 (:virtual (find-class 'direct-virtual-slot-definition))
71 (:special (find-class 'direct-special-slot-definition))
72 (t (call-next-method))))
94f15c3c 73
9adccb27 74(defmethod effective-slot-definition-class ((class virtual-slots-class) &rest initargs)
e2ebafb1 75 (case (getf initargs :allocation)
76 (:virtual (find-class 'effective-virtual-slot-definition))
77 (:special (find-class 'effective-special-slot-definition))
78 (t (call-next-method))))
94f15c3c 79
4d83a8a6 80
81(defmethod initialize-internal-slot-functions ((slotd effective-virtual-slot-definition))
eeda1c2d 82 (if (not (slot-boundp slotd 'getter))
83 (setf
4d83a8a6 84 (slot-value slotd 'reader-function)
eeda1c2d 85 #'(lambda (object)
86 (declare (ignore object))
87 (error "Can't read slot: ~A" (slot-definition-name slotd)))
88 (slot-value slotd 'boundp-function)
89 #'(lambda (object) (declare (ignore object)) nil))
90
91 (let ((getter-function
92 (let ((getter (slot-value slotd 'getter)))
93 (etypecase getter
94 (function getter)
95 (symbol
96 #'(lambda (object)
97 (funcall getter object)))
98 (string
99 (let ((reader nil))
100 (setf (slot-value slotd 'reader-function)
101 #'(lambda (object)
102 (unless reader
12b7df04 103 (setq reader
104 (mkbinding getter
105 (slot-definition-type slotd) 'pointer)))
09f6e237 106 (funcall reader (foreign-location object))))))))))
eeda1c2d 107
4d83a8a6 108 (setf
eeda1c2d 109 (slot-value slotd 'boundp-function)
110 (cond
eeda1c2d 111 ((slot-boundp slotd 'unbound)
112 (let ((unbound-value (slot-value slotd 'unbound)))
12b7df04 113 #'(lambda (object)
114 (not (eq (funcall getter-function object) unbound-value)))))
115 ((slot-boundp slotd 'boundp)
116 (let ((boundp (slot-value slotd 'boundp)))
eeda1c2d 117 (etypecase boundp
118 (function boundp)
119 (symbol #'(lambda (object)
120 (funcall boundp object)))
121 (string (let ((reader ()))
122 #'(lambda (object)
123 (unless reader
124 (setq reader
125 (mkbinding boundp
126 (slot-definition-type slotd) 'pointer)))
09f6e237 127 (funcall reader (foreign-location object))))))))
12b7df04 128 ((multiple-value-bind (unbound-p unbound-value)
129 (unbound-value (slot-definition-type slotd))
130 (when unbound-p
131 #'(lambda (object)
132 (not (eq (funcall getter-function object) unbound-value))))))
133 (#'(lambda (object) (declare (ignore object)) t))))
eeda1c2d 134
135 (setf
136 (slot-value slotd 'reader-function)
137 (cond
138 ((slot-boundp slotd 'unbound)
139 (let ((unbound (slot-value slotd 'unbound))
140 (slot-name (slot-definition-name slotd)))
12b7df04 141 #'(lambda (object)
142 (let ((value (funcall getter-function object)))
143 (if (eq value unbound)
144 (slot-unbound (class-of object) object slot-name)
145 value)))))
eeda1c2d 146 ((slot-boundp slotd 'boundp)
147 (let ((boundp-function (slot-value slotd 'boundp-function)))
12b7df04 148 #'(lambda (object)
149 (and
150 (funcall boundp-function object)
151 (funcall getter-function object)))))
152 ((multiple-value-bind (unbound-p unbound-value)
153 (unbound-value (slot-definition-type slotd))
154 (let ((slot-name (slot-definition-name slotd)))
155 (when unbound-p
156 #'(lambda (object)
157 (let ((value (funcall getter-function object)))
158 (if (eq value unbound-value)
159 (slot-unbound (class-of object) object slot-name)
160 value)))))))
eeda1c2d 161 (getter-function)))))
162
163 (setf
164 (slot-value slotd 'writer-function)
165 (if (not (slot-boundp slotd 'setter))
166 #'(lambda (object)
167 (declare (ignore object))
168 (error "Can't set slot: ~A" (slot-definition-name slotd)))
169 (with-slots (setter) slotd
4d83a8a6 170 (etypecase setter
171 (function setter)
eeda1c2d 172 ((or symbol cons)
173 #'(lambda (value object)
174 (funcall (fdefinition setter) value object)))
7d1ddc9e 175 (string
eeda1c2d 176 (let ((writer ()))
177 (setf
178 (slot-value slotd 'writer-function)
179 #'(lambda (value object)
180 (unless writer
181 (setq writer
182 (mkbinding setter 'nil 'pointer
183 (slot-definition-type slotd))))
09f6e237 184 (funcall writer (foreign-location object) value)))))))))
eeda1c2d 185
4d83a8a6 186 (initialize-internal-slot-gfs (slot-definition-name slotd)))
187
188
189
eeda1c2d 190(defmethod compute-slot-accessor-info ((slotd effective-virtual-slot-definition) type gf)
4d83a8a6 191 nil)
192
9adccb27 193(defmethod compute-effective-slot-definition-initargs ((class virtual-slots-class) direct-slotds)
eeda1c2d 194 (if (typep (first direct-slotds) 'direct-virtual-slot-definition)
195 (let ((initargs ()))
196 (let ((getter (most-specific-slot-value direct-slotds 'getter)))
197 (unless (eq getter *unbound-marker*)
198 (setf (getf initargs :getter) getter)))
199 (let ((setter (most-specific-slot-value direct-slotds 'setter)))
200 (unless (eq setter *unbound-marker*)
201 (setf (getf initargs :setter) setter)))
202 (let ((unbound (most-specific-slot-value direct-slotds 'unbound)))
203 (unless (eq unbound *unbound-marker*)
204 (setf (getf initargs :unbound) unbound)))
205 (let ((boundp (most-specific-slot-value direct-slotds 'boundp)))
206 (unless (eq boundp *unbound-marker*)
207 (setf (getf initargs :boundp) boundp)))
208 (nconc initargs (call-next-method)))
4d83a8a6 209 (call-next-method)))
210
94f15c3c 211
94f15c3c 212(defmethod slot-value-using-class
9adccb27 213 ((class virtual-slots-class) (object standard-object)
94f15c3c 214 (slotd effective-virtual-slot-definition))
4d83a8a6 215 (if (funcall (slot-value slotd 'boundp-function) object)
216 (funcall (slot-value slotd 'reader-function) object)
217 (slot-unbound class object (slot-definition-name slotd))))
94f15c3c 218
94f15c3c 219(defmethod slot-boundp-using-class
9adccb27 220 ((class virtual-slots-class) (object standard-object)
94f15c3c 221 (slotd effective-virtual-slot-definition))
4d83a8a6 222 (funcall (slot-value slotd 'boundp-function) object))
223
224(defmethod (setf slot-value-using-class)
9adccb27 225 (value (class virtual-slots-class) (object standard-object)
94f15c3c 226 (slotd effective-virtual-slot-definition))
4d83a8a6 227 (funcall (slot-value slotd 'writer-function) value object))
228
229
94f15c3c 230(defmethod validate-superclass
9adccb27 231 ((class virtual-slots-class) (super standard-class))
94f15c3c 232 t)
233
234
235;;;; Proxy cache
236
237(internal *instance-cache*)
238(defvar *instance-cache* (make-hash-table :test #'eql))
239
982a215a 240(defun cache-instance (instance &optional (weak-ref t))
94f15c3c 241 (setf
09f6e237 242 (gethash (sap-int (foreign-location instance)) *instance-cache*)
982a215a 243 (if weak-ref
244 (make-weak-pointer instance)
245 instance)))
94f15c3c 246
247(defun find-cached-instance (location)
73572c12 248 (let ((ref (gethash (sap-int location) *instance-cache*)))
94f15c3c 249 (when ref
982a215a 250 (if (weak-pointer-p ref)
251 (weak-pointer-value ref)
252 ref))))
94f15c3c 253
0f134a29 254(defun instance-cached-p (location)
73572c12 255 (gethash (sap-int location) *instance-cache*))
0f134a29 256
94f15c3c 257(defun remove-cached-instance (location)
73572c12 258 (remhash (sap-int location) *instance-cache*))
94f15c3c 259
9adccb27 260;; For debuging
982a215a 261(defun list-cached-instances ()
9adccb27 262 (let ((instances ()))
263 (maphash #'(lambda (location ref)
264 (declare (ignore location))
982a215a 265 (push ref instances))
9adccb27 266 *instance-cache*)
267 instances))
268
94f15c3c 269
270
271;;;; Proxy for alien instances
272
9adccb27 273(defclass proxy ()
09f6e237 274 ((location :allocation :special :reader foreign-location :type pointer))
e2ebafb1 275 (:metaclass virtual-slots-class))
94f15c3c 276
9adccb27 277(defgeneric instance-finalizer (object))
278(defgeneric reference-foreign (class location))
279(defgeneric unreference-foreign (class location))
280
3b167652 281(defmethod reference-foreign ((name symbol) location)
282 (reference-foreign (find-class name) location))
283
284(defmethod unreference-foreign ((name symbol) location)
285 (unreference-foreign (find-class name) location))
286
9adccb27 287(defmethod unreference-foreign :around ((class class) location)
288 (unless (null-pointer-p location)
09f6e237 289 (call-next-method)))
94f15c3c 290
0f134a29 291(defmethod print-object ((instance proxy) stream)
292 (print-unreadable-object (instance stream :type t :identity nil)
09f6e237 293 (if (slot-boundp instance 'location)
294 (format stream "at 0x~X" (sap-int (foreign-location instance)))
295 (write-string "at \"unbound\"" stream))))
94f15c3c 296
8958fa4a 297(defmethod initialize-instance :around ((instance proxy) &rest initargs)
298 (declare (ignore initargs))
299 (prog1
300 (call-next-method)
301 (cache-instance instance)
302 (finalize instance (instance-finalizer instance))))
94f15c3c 303
304(defmethod instance-finalizer ((instance proxy))
09f6e237 305 (let ((location (foreign-location instance))
9adccb27 306 (class (class-of instance)))
307;; (unless (find-method #'unreference-foreign nil (list (class-of class) t) nil)
308;; (error "No matching method for UNREFERENCE-INSTANCE when called with class ~A" class))
309 #'(lambda ()
1dbf4216 310 (remove-cached-instance location)
9adccb27 311 (unreference-foreign class location))))
12d0437e 312
94f15c3c 313
314;;;; Metaclass used for subclasses of proxy
315
73572c12 316(defgeneric most-specific-proxy-superclass (class))
317(defgeneric direct-proxy-superclass (class))
09f6e237 318(defgeneric compute-foreign-size (class))
73572c12 319
320
94f15c3c 321(eval-when (:compile-toplevel :load-toplevel :execute)
9adccb27 322 (defclass proxy-class (virtual-slots-class)
09f6e237 323 ((size :reader foreign-size)))
94f15c3c 324
325 (defclass direct-alien-slot-definition (direct-virtual-slot-definition)
12d0437e 326 ((allocation :initform :alien)
327 (offset :reader slot-definition-offset :initarg :offset)))
94f15c3c 328
329 (defclass effective-alien-slot-definition (effective-virtual-slot-definition)
4d83a8a6 330 ((offset :reader slot-definition-offset :initarg :offset)))
12d0437e 331
94f15c3c 332 (defmethod most-specific-proxy-superclass ((class proxy-class))
333 (find-if
334 #'(lambda (class)
335 (subtypep (class-name class) 'proxy))
4d83a8a6 336 (cdr (compute-class-precedence-list class))))
73572c12 337
12d0437e 338 (defmethod direct-proxy-superclass ((class proxy-class))
339 (find-if
340 #'(lambda (class)
341 (subtypep (class-name class) 'proxy))
4d83a8a6 342 (class-direct-superclasses class)))
343
eeda1c2d 344 (defmethod shared-initialize ((class proxy-class) names &key size)
94f15c3c 345 (call-next-method)
12d0437e 346 (cond
4d83a8a6 347 (size (setf (slot-value class 'size) (first size)))
9adccb27 348 ((slot-boundp class 'size) (slot-makunbound class 'size))))
09f6e237 349
4d83a8a6 350 (defmethod direct-slot-definition-class ((class proxy-class) &rest initargs)
94f15c3c 351 (case (getf initargs :allocation)
e2ebafb1 352 (:alien (find-class 'direct-alien-slot-definition))
94f15c3c 353 (t (call-next-method))))
4d83a8a6 354
355 (defmethod effective-slot-definition-class ((class proxy-class) &rest initargs)
94f15c3c 356 (case (getf initargs :allocation)
357 (:alien (find-class 'effective-alien-slot-definition))
94f15c3c 358 (t (call-next-method))))
359
4d83a8a6 360
361 (defmethod compute-effective-slot-definition-initargs ((class proxy-class) direct-slotds)
362 (if (eq (most-specific-slot-value direct-slotds 'allocation) :alien)
363 (nconc
364 (list :offset (most-specific-slot-value direct-slotds 'offset))
365 (call-next-method))
366 (call-next-method)))
367
368
369 (defmethod initialize-internal-slot-functions ((slotd effective-alien-slot-definition))
370 (with-slots (offset) slotd
9adccb27 371 (let ((type (slot-definition-type slotd)))
eeda1c2d 372 (unless (slot-boundp slotd 'getter)
9adccb27 373 (let ((reader (reader-function type)))
374 (setf
eeda1c2d 375 (slot-value slotd 'getter)
9adccb27 376 #'(lambda (object)
09f6e237 377 (funcall reader (foreign-location object) offset)))))
4d83a8a6 378
eeda1c2d 379 (unless (slot-boundp slotd 'setter)
9adccb27 380 (let ((writer (writer-function type))
381 (destroy (destroy-function type)))
382 (setf
eeda1c2d 383 (slot-value slotd 'setter)
9adccb27 384 #'(lambda (value object)
09f6e237 385 (let ((location (foreign-location object)))
9adccb27 386 (funcall destroy location offset) ; destroy old value
eeda1c2d 387 (funcall writer value location offset))))))))
388
4d83a8a6 389 (call-next-method))
390
09f6e237 391 (defmethod compute-foreign-size ((class proxy-class))
392 nil)
4d83a8a6 393
4d83a8a6 394 ;; TODO: call some C code to detect this a compile time
395 (defconstant +struct-alignmen+ 4)
94f15c3c 396
09f6e237 397 (defun align-offset (size)
398 (if (zerop (mod size +struct-alignmen+))
399 size
400 (+ size (- +struct-alignmen+ (mod size +struct-alignmen+)))))
401
94f15c3c 402 (defmethod compute-slots ((class proxy-class))
09f6e237 403 (let ((alien-slots
404 (remove-if-not
405 #'(lambda (slotd)
406 (eq (slot-definition-allocation slotd) :alien))
407 (class-direct-slots class))))
408 (when alien-slots
409 (loop
410 as offset = (align-offset (foreign-size
411 (most-specific-proxy-superclass class)))
412 then (align-offset
413 (+
414 (slot-definition-offset slotd)
415 (size-of (slot-definition-type slotd))))
416 for slotd in alien-slots
417 unless (slot-boundp slotd 'offset)
418 do (setf (slot-value slotd 'offset) offset))))
94f15c3c 419 (call-next-method))
3e15002d 420
09f6e237 421 (defmethod compute-slots :after ((class proxy-class))
422 (when (and (class-finalized-p class) (not (slot-boundp class 'size)))
423 (let ((size (compute-foreign-size class)))
424 (when size
425 (setf (slot-value class 'size) size)))))
4d83a8a6 426
427 (defmethod validate-superclass ((class proxy-class) (super standard-class))
428 (subtypep (class-name super) 'proxy))
429
09f6e237 430 (defmethod foreign-size ((class-name symbol))
431 (foreign-size (find-class class-name))))
47a11c16 432
09f6e237 433(defmethod foreign-size ((object proxy))
434 (foreign-size (class-of object)))
4d83a8a6 435
09f6e237 436
9adccb27 437(defmethod alien-type ((class proxy-class) &rest args)
438 (declare (ignore class args))
439 (alien-type 'pointer))
440
441(defmethod size-of ((class proxy-class) &rest args)
442 (declare (ignore class args))
443 (size-of 'pointer))
444
445(defmethod from-alien-form (location (class proxy-class) &rest args)
446 (declare (ignore args))
447 `(ensure-proxy-instance ',(class-name class) ,location))
448
449(defmethod from-alien-function ((class proxy-class) &rest args)
450 (declare (ignore args))
451 #'(lambda (location)
452 (ensure-proxy-instance class location)))
94f15c3c 453
9adccb27 454(defmethod to-alien-form (instance (class proxy-class) &rest args)
455 (declare (ignore class args))
09f6e237 456 `(foreign-location ,instance))
94f15c3c 457
9adccb27 458(defmethod to-alien-function ((class proxy-class) &rest args)
459 (declare (ignore class args))
09f6e237 460 #'foreign-location)
9adccb27 461
9ca5565a 462(defmethod copy-from-alien-form (location (class proxy-class) &rest args)
463 (declare (ignore args))
464 (let ((class-name (class-name class)))
465 `(ensure-proxy-instance ',class-name
466 (reference-foreign ',class-name ,location))))
467
468(defmethod copy-from-alien-function ((class proxy-class) &rest args)
469 (declare (ignore args))
470 #'(lambda (location)
471 (ensure-proxy-instance class (reference-foreign class location))))
472
473(defmethod copy-to-alien-form (instance (class proxy-class) &rest args)
474 (declare (ignore args))
09f6e237 475 `(reference-foreign ',(class-name class) (foreign-location ,instance)))
9ca5565a 476
477(defmethod copy-to-alien-function ((class proxy-class) &rest args)
73572c12 478 (declare (ignore args))
9ca5565a 479 #'(lambda (instance)
09f6e237 480 (reference-foreign class (foreign-location instance))))
9ca5565a 481
9adccb27 482(defmethod writer-function ((class proxy-class) &rest args)
483 (declare (ignore args))
484 #'(lambda (instance location &optional (offset 0))
485 (assert (null-pointer-p (sap-ref-sap location offset)))
486 (setf
487 (sap-ref-sap location offset)
09f6e237 488 (reference-foreign class (foreign-location instance)))))
94f15c3c 489
9adccb27 490(defmethod reader-function ((class proxy-class) &rest args)
491 (declare (ignore args))
492 #'(lambda (location &optional (offset 0))
9ca5565a 493 (let ((instance (sap-ref-sap location offset)))
494 (unless (null-pointer-p instance)
495 (ensure-proxy-instance class (reference-foreign class instance))))))
94f15c3c 496
9adccb27 497(defmethod destroy-function ((class proxy-class) &rest args)
498 (declare (ignore args))
499 #'(lambda (location &optional (offset 0))
500 (unreference-foreign class (sap-ref-sap location offset))))
501
12b7df04 502(defmethod unbound-value ((class proxy-class) &rest args)
47a11c16 503 (declare (ignore args))
12b7df04 504 (values t nil))
9adccb27 505
8958fa4a 506(defun ensure-proxy-instance (class location &rest initargs)
507 "Returns a proxy object representing the foreign object at the give
508location. If an existing object is not found in the cache
509MAKE-PROXY-INSTANCE is called to create one."
9adccb27 510 (unless (null-pointer-p location)
511 (or
512 (find-cached-instance location)
8958fa4a 513 (let ((instance (apply #'make-proxy-instance class location initargs)))
514 (cache-instance instance)
515 instance))))
516
517(defgeneric make-proxy-instance (class location &key weak)
518 (:documentation "Creates a new proxy object representing the foreign
519object at the give location. If WEAK is non NIL the foreign memory
520will not be released when the proxy is garbage collected."))
521
522(defmethod make-proxy-instance ((class symbol) location &key weak)
523 (ensure-proxy-instance (find-class class) location :weak weak))
524
525(defmethod make-proxy-instance ((class proxy-class) location &key weak)
526 (declare (ignore weak-p))
527 (let ((instance (allocate-instance class)))
528 (setf (slot-value instance 'location) location)
529 (unless weak
530 (finalize instance (instance-finalizer instance)))
531 instance))
94f15c3c 532
12d0437e 533
534;;;; Superclasses for wrapping of C structures
94f15c3c 535
9adccb27 536(defclass struct (proxy)
537 ()
09f6e237 538 (:metaclass proxy-class)
539 (:size 0))
94f15c3c 540
9adccb27 541(defmethod initialize-instance ((struct struct) &rest initargs)
94f15c3c 542 (declare (ignore initargs))
ec1a4146 543 (unless (slot-boundp struct 'location)
09f6e237 544 (let ((size (foreign-size (class-of struct))))
ec1a4146 545 (if (zerop size)
546 (error "~A has zero size" (class-of struct))
47a11c16 547 (setf (slot-value struct 'location) (allocate-memory size)))))
94f15c3c 548 (call-next-method))
549
550
9adccb27 551;;;; Metaclasses used for subclasses of struct
552
553(defclass struct-class (proxy-class)
554 ())
94f15c3c 555
e2ebafb1 556(defmethod direct-slot-definition-class ((class struct-class) &rest initargs)
557 (if (not (getf initargs :allocation))
558 (find-class 'direct-alien-slot-definition)
559 (call-next-method)))
560
9adccb27 561(defmethod reference-foreign ((class struct-class) location)
09f6e237 562 (copy-memory location (foreign-size class)))
9adccb27 563
564(defmethod unreference-foreign ((class struct-class) location)
12d0437e 565 (deallocate-memory location))
94f15c3c 566
09f6e237 567(defmethod compute-foreign-size ((class struct-class))
568 (let ((size (loop
569 for slotd in (class-slots class)
570 when (eq (slot-definition-allocation slotd) :alien)
571 maximize (+
572 (slot-definition-offset slotd)
573 (size-of (slot-definition-type slotd))))))
574 (+ size (mod size +struct-alignmen+))))
575
94f15c3c 576
9adccb27 577(defclass static-struct-class (struct-class)
578 ())
94f15c3c 579
9adccb27 580(defmethod reference-foreign ((class static-struct-class) location)
581 (declare (ignore class))
12d0437e 582 location)
94f15c3c 583
9adccb27 584(defmethod unreference-foreign ((class static-struct-class) location)
585 (declare (ignore class location))
12d0437e 586 nil)
bde0b906 587
588
589;;; Pseudo type for structs which are inlined in other objects
590
591(defmethod size-of ((type (eql 'inlined)) &rest args)
592 (declare (ignore type))
09f6e237 593 (foreign-size (first args)))
bde0b906 594
595(defmethod reader-function ((type (eql 'inlined)) &rest args)
596 (declare (ignore type))
597 (destructuring-bind (class) args
598 #'(lambda (location &optional (offset 0))
599 (ensure-proxy-instance class
600 (reference-foreign class (sap+ location offset))))))
601
602(defmethod destroy-function ((type (eql 'inlined)) &rest args)
603 (declare (ignore args))
604 #'(lambda (location &optional (offset 0))
605 (declare (ignore location offset))))
606
607(export 'inlined)