Modified PCL's class finalization protocol to behave as specified in AMOP -- take two
[clg] / glib / gutils.lisp
1 ;; Common Lisp bindings for GTK+ v2.0
2 ;; Copyright (C) 1999-2000 Espen S. Johnsen <espejohn@online.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
18 ;; $Id: gutils.lisp,v 1.7 2001-05-20 23:10:36 espen Exp $
19
20
21 (in-package "KERNEL")
22
23 (defun type-expand-1 (form)
24 (let ((def (cond ((symbolp form)
25 (info type expander form))
26 ((and (consp form) (symbolp (car form)))
27 (info type expander (car form)))
28 (t nil))))
29 (if def
30 (values (funcall def (if (consp form) form (list form))) t)
31 (values form nil))))
32
33 (in-package "PCL")
34
35 ;;;; Make PCL's class finalization protocol behave as specified in AMOP
36
37 (defmethod finalize-inheritance ((class std-class))
38 (dolist (super (class-direct-superclasses class))
39 (unless (class-finalized-p super) (finalize-inheritance super)))
40 (update-cpl class (compute-class-precedence-list class))
41 (update-slots class (compute-slots class))
42 (update-gfs-of-class class)
43 (update-inits class (compute-default-initargs class))
44 (update-make-instance-function-table class))
45
46 (defmethod finalize-inheritance ((class forward-referenced-class))
47 (error "~A can't be finalized" class))
48
49 (defun update-class (class &optional finalizep)
50 (declare (ignore finalizep))
51 (when (and
52 (class-finalized-p class)
53 (not (class-has-a-forward-referenced-superclass-p class)))
54 (finalize-inheritance class)
55 (dolist (sub (class-direct-subclasses class))
56 (update-class sub))))
57
58 (defmethod add-method :before ((gf standard-generic-function)
59 (method standard-method))
60 (declare (ignore gf))
61 (dolist (specializer (method-specializers method))
62 (when (and
63 (typep specializer 'standard-class)
64 (not (class-finalized-p specializer))
65 (not (class-has-a-forward-referenced-superclass-p specializer)))
66 (finalize-inheritance specializer))))
67
68
69 (in-package "GLIB")
70
71 (defun type-expand-to (type form)
72 (labels ((expand (form0)
73 (if (eq (first (mklist form0)) type)
74 form0
75 (multiple-value-bind (expanded-form expanded-p)
76 (type-expand-1 form0)
77 (if expanded-p
78 (expand expanded-form)
79 (error "~A can not be expanded to ~A" form type))))))
80 (expand form)))
81
82 (defmacro with-gc-disabled (&body body)
83 (let ((gc-inhibit (make-symbol "GC-INHIBIT")))
84 `(progn
85 (let ((,gc-inhibit lisp::*gc-inhibit*))
86 (ext:gc-off)
87 (unwind-protect
88 ,@body
89 (unless ,gc-inhibit
90 (ext:gc-on)))))))
91
92 (defun mklist (obj)
93 (if (atom obj) (list obj) obj))
94
95 (defun namep (obj)
96 (and (symbolp obj) (not (member obj '(t nil)))))
97
98 (defun all-equal (&rest objects)
99 (or
100 (null objects)
101 (null (rest objects))
102 (and
103 (equal (first objects) (second objects))
104 (apply #'all-equal (rest objects)))))
105
106 (defun neq (obj1 obj2)
107 (not (eq obj1 obj2)))
108
109 (defmacro return-if (form)
110 (let ((result (make-symbol "RESULT")))
111 `(let ((,result ,form))
112 (when ,result
113 (return ,result)))))
114
115 (defun make-pointer (address)
116 (int-sap address))
117
118 (defun null-pointer-p (pointer)
119 (zerop (sap-int pointer)))
120
121
122 (defmacro when-bind ((var expr) &body body)
123 `(let ((,var ,expr))
124 (when ,var
125 ,@body)))
126
127
128 (defmacro assoc-ref (key alist &key (test #'eq))
129 `(cdr (assoc ,key ,alist :test ,test)))
130
131
132 (defmacro assoc-lref (key alist &key (test #'eq))
133 `(cadr (assoc ,key ,alist :test ,test)))
134
135
136 (defun assoc-rem (key alist &key (test #'eq))
137 (remove-if #'(lambda (element) (funcall test key (car element))) alist))
138
139
140 (defun assoc-delete (key alist &key (test #'eq))
141 (delete-if #'(lambda (element) (funcall test key (car element))) alist))
142
143
144 (defun funcallable (object)
145 (if (consp object)
146 (fdefinition object)
147 object))
148
149 (defun intersection-p (list1 list2 &key (test #'eq))
150 (dolist (obj list1 nil)
151 (when (member obj list2 :test test)
152 (return-from intersection-p t))))
153
154
155 (defun split-string (string delimiter)
156 (declare (simple-string string) (character delimiter))
157 (check-type string string)
158 (check-type delimiter character)
159 (let ((pos (position delimiter string)))
160 (if (not pos)
161 (list string)
162 (cons
163 (subseq string 0 pos)
164 (split-string (subseq string (1+ pos)) delimiter)))))
165
166 (defun split-string-if (string predicate)
167 (declare (simple-string string))
168 (check-type string string)
169 (check-type predicate (or symbol function))
170 (let ((pos (position-if predicate string :start 1)))
171 (if (not pos)
172 (list string)
173 (cons
174 (subseq string 0 pos)
175 (split-string-if (subseq string pos) predicate)))))
176
177 (defun concatenate-strings (strings &optional delimiter)
178 (if (not (rest strings))
179 (first strings)
180 (concatenate
181 'string
182 (first strings)
183 (if delimiter (string delimiter) "")
184 (concatenate-strings (rest strings) delimiter))))
185
186 (defun string-prefix-p (prefix string)
187 (and
188 (>= (length string) (length prefix))
189 (string= prefix string :end2 (length prefix))))