1 ;; Common Lisp bindings for GTK+ v2.0
2 ;; Copyright (C) 2000 Espen S. Johnsen <esj@stud.cs.uit.no>
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.
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.
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
18 ;; $Id: proxy.lisp,v 1.2 2001/04/30 11:25:25 espen Exp $
23 ;;;; Superclass for all metaclasses implementing some sort of virtual slots
25 (eval-when (:compile-toplevel :load-toplevel :execute)
26 (defclass virtual-class (pcl::standard-class))
28 (defclass direct-virtual-slot-definition (standard-direct-slot-definition)
30 :reader slot-definition-location
33 (defclass effective-virtual-slot-definition
34 (standard-effective-slot-definition)))
37 (defmethod direct-slot-definition-class ((class virtual-class) initargs)
38 (if (eq (getf initargs :allocation) :virtual)
39 (find-class 'direct-virtual-slot-definition)
43 (defmethod effective-slot-definition-class ((class virtual-class) initargs)
44 (if (eq (getf initargs :allocation) :virtual)
45 (find-class 'effective-virtual-slot-definition)
49 (defun %direct-slot-definitions-slot-value (slotds slot &optional default)
54 (slot-exists-p slotd slot)
55 (slot-boundp slotd slot)))
58 (slot-value slotd slot)
62 (defgeneric compute-virtual-slot-location (class slotd direct-slotds))
64 (defmethod compute-virtual-slot-location
65 ((class virtual-class)
66 (slotd effective-virtual-slot-definition)
69 (%direct-slot-definitions-slot-value direct-slotds 'location)))
70 (if (and location (symbolp location))
71 (list location `(setf ,location))
75 (defmethod compute-effective-slot-definition
76 ((class virtual-class) direct-slotds)
77 (let ((slotd (call-next-method)))
78 (when (typep slotd 'effective-virtual-slot-definition)
80 (slot-value slotd 'pcl::location)
81 (compute-virtual-slot-location class slotd direct-slotds)))
85 (defmethod slot-value-using-class
86 ((class virtual-class) (object standard-object)
87 (slotd effective-virtual-slot-definition))
88 (let ((reader (first (slot-definition-location slotd))))
90 (funcall reader object)
91 (slot-unbound class object (slot-definition-name slotd)))))
94 (defmethod slot-boundp-using-class
95 ((class virtual-class) (object standard-object)
96 (slotd effective-virtual-slot-definition))
97 (and (first (slot-definition-location slotd)) t))
101 (defmethod (setf slot-value-using-class)
102 (value (class virtual-class) (object standard-object)
103 (slotd effective-virtual-slot-definition))
104 (let ((writer (second (slot-definition-location slotd))))
108 "Can't set read-only slot ~A in ~A"
109 (slot-definition-name slotd)
111 ((or (functionp writer) (symbolp writer))
112 (funcall writer value object)
115 (funcall (fdefinition writer) value object)
119 (defmethod validate-superclass
120 ((class virtual-class) (super pcl::standard-class))
126 (internal *instance-cache*)
127 (defvar *instance-cache* (make-hash-table :test #'eql))
129 (defun cache-instance (instance)
131 (gethash (system:sap-int (proxy-location instance)) *instance-cache*)
132 (ext:make-weak-pointer instance)))
134 (defun find-cached-instance (location)
135 (let ((ref (gethash (system:sap-int location) *instance-cache*)))
137 (ext:weak-pointer-value ref))))
139 (defun remove-cached-instance (location)
140 (remhash (system:sap-int location) *instance-cache*))
144 ;;;; Proxy for alien instances
146 (eval-when (:compile-toplevel :load-toplevel :execute)
149 :reader proxy-location
150 :type system-area-pointer)))
152 (defgeneric initialize-proxy (object &rest initargs))
153 (defgeneric instance-finalizer (object)))
156 (defmethod initialize-instance :after ((instance proxy)
158 (declare (ignore initargs))
159 (cache-instance instance)
160 (ext:finalize instance (instance-finalizer instance)))
163 (defmethod initialize-proxy ((instance proxy)
165 (declare (ignore initargs))
166 (cache-instance instance))
169 (defmethod instance-finalizer ((instance proxy))
170 (let ((location (proxy-location instance)))
172 (remove-cached-instance location))))
175 (deftype-method translate-type-spec proxy (type-spec)
176 (declare (ignore type-spec))
177 (translate-type-spec 'pointer))
179 (deftype-method size-of proxy (type-spec)
180 (declare (ignore type-spec))
183 (deftype-method translate-from-alien
184 proxy (type-spec location &optional weak-ref)
185 `(let ((location ,location))
186 (unless (null-pointer-p location)
187 (ensure-proxy-instance ',type-spec location ,weak-ref))))
191 ;;;; Metaclass used for subclasses of proxy
193 (eval-when (:compile-toplevel :load-toplevel :execute)
194 (defclass proxy-class (virtual-class)
195 ((size :reader proxy-class-instance-size)))
197 (defclass direct-alien-slot-definition (direct-virtual-slot-definition)
201 :reader slot-definition-offset
205 (defclass effective-alien-slot-definition (effective-virtual-slot-definition)
206 ((offset :reader slot-definition-offset)))
208 (defclass effective-virtual-alien-slot-definition
209 (effective-virtual-slot-definition))
212 (defmethod most-specific-proxy-superclass ((class proxy-class))
215 (subtypep (class-name class) 'proxy))
216 (cdr (pcl::compute-class-precedence-list class))))
219 (defmethod shared-initialize ((class proxy-class) names
220 &rest initargs &key size name)
221 (declare (ignore initargs))
224 (setf (slot-value class 'size) (first size))))
227 (defmethod shared-initialize :after ((class proxy-class) names
229 (declare (ignore initargs names))
230 (let* ((super (most-specific-proxy-superclass class))
232 (if (eq (class-name super) 'proxy)
234 (proxy-class-instance-size super))))
235 (dolist (slotd (class-slots class))
236 (when (eq (slot-definition-allocation slotd) :alien)
237 (with-slots (offset type) slotd
238 (setq actual-size (max actual-size (+ offset (size-of type)))))))
240 ((not (slot-boundp class 'size))
241 (setf (slot-value class 'size) actual-size))
242 ((> actual-size (slot-value class 'size))
243 (warn "The actual size of class ~A is lager than specified" class)))))
246 (defmethod direct-slot-definition-class ((class proxy-class) initargs)
247 (case (getf initargs :allocation)
248 ((nil :alien) (find-class 'direct-alien-slot-definition))
249 ; (:instance (error "Allocation :instance not allowed in class ~A" class))
250 (t (call-next-method))))
253 (defmethod effective-slot-definition-class ((class proxy-class) initargs)
254 (case (getf initargs :allocation)
255 (:alien (find-class 'effective-alien-slot-definition))
256 (:virtual (find-class 'effective-virtual-alien-slot-definition))
257 (t (call-next-method))))
260 (defmethod compute-virtual-slot-location
261 ((class proxy-class) (slotd effective-alien-slot-definition)
263 (with-slots (offset type) slotd
264 (setf offset (%direct-slot-definitions-slot-value direct-slotds 'offset))
265 (let ((reader (intern-reader-function type))
266 (writer (intern-writer-function type))
267 (destroy (intern-destroy-function type)))
270 (funcall reader (proxy-location object) offset))
271 #'(lambda (value object)
272 (let ((location (proxy-location object)))
273 (funcall destroy location offset)
274 (funcall writer value location offset)))))))
277 (defmethod compute-virtual-slot-location
279 (slotd effective-virtual-alien-slot-definition)
281 (let ((location (call-next-method))
282 (class-name (class-name class)))
283 (if (or (stringp location) (consp location))
284 (destructuring-bind (reader &optional writer) (mklist location)
285 (with-slots (type) slotd
288 (mkbinding reader type class-name)
291 (let ((writer (mkbinding writer 'nil class-name type)))
292 #'(lambda (value object)
293 (funcall writer object value)))
298 (defmethod compute-slots ((class proxy-class))
299 ;; Translating the user supplied relative (to previous slot) offsets
300 ;; to absolute offsets.
301 ;; This code is broken and have to be fixed.
302 (with-slots (direct-slots) class
303 (let* ((super (most-specific-proxy-superclass class))
305 (if (eq (class-name super) 'proxy)
307 (proxy-class-instance-size super))))
308 (dolist (slotd direct-slots)
309 (when (eq (slot-definition-allocation slotd) :alien)
310 (with-slots (offset type) slotd
312 offset (+ slot-offset offset)
313 slot-offset (+ offset (size-of type)))))))
315 ;; Reverse the direct slot definitions so the effective slots
316 ;; will be in correct order.
317 (setf direct-slots (reverse direct-slots))
318 ;; This nreverse caused me so much frustration that I leave it
319 ;; here just as a reminder of what not to do.
320 ; (setf direct-slots (nreverse direct-slots))
325 (defmethod validate-superclass ((class proxy-class)
326 (super pcl::standard-class))
327 (subtypep (class-name super) 'proxy))
329 (defgeneric make-proxy-instance (class location weak-ref &rest initargs &key)))
332 (defmethod make-proxy-instance ((class symbol) location weak-ref
334 (apply #'make-proxy-instance (find-class class) location weak-ref initargs))
336 (defmethod make-proxy-instance ((class proxy-class) location weak-ref
338 (let ((instance (allocate-instance class)))
341 instance :location location :weak-ref weak-ref initargs)
344 (defun ensure-proxy-instance (class location weak-ref &rest initargs)
346 (find-cached-instance location)
347 (apply #'make-proxy-instance class location weak-ref initargs)))
350 ;;;; Superclass for wrapping of C structures
352 (eval-when (:compile-toplevel :load-toplevel :execute)
353 (defclass alien-structure (proxy)
355 (:metaclass proxy-class)
359 (defmethod initialize-instance ((structure alien-structure)
361 (declare (ignore initargs))
363 (slot-value structure 'location)
364 (allocate-memory (proxy-class-instance-size (class-of structure))))
368 (defmethod initialize-proxy ((structure alien-structure)
369 &rest initargs &key location weak-ref)
370 (declare (ignore initargs))
372 (slot-value structure 'location)
374 (copy-memory location (proxy-class-instance-size (class-of structure)))
379 (defmethod instance-finalizer ((structure alien-structure))
380 (let ((location (proxy-location structure)))
381 (declare (type system-area-pointer location))
383 (deallocate-memory location)
384 (remove-cached-instance location))))
387 (deftype-method translate-to-alien
388 alien-structure (type-spec object &optional weak-ref)
390 `(proxy-location ,object)
392 (proxy-location ,object)
393 ,(proxy-class-instance-size (find-class type-spec)))))
396 (deftype-method unreference-alien alien-structure (type-spec c-struct)
397 (declare (ignore type-spec))
398 `(deallocate-memory ,c-struct))