Commit | Line | Data |
---|---|---|
dea4d055 MW |
1 | ;;; -*-lisp-*- |
2 | ;;; | |
3 | ;;; Protocol for property sets | |
4 | ;;; | |
5 | ;;; (c) 2009 Straylight/Edgeware | |
6 | ;;; | |
7 | ||
8 | ;;;----- Licensing notice --------------------------------------------------- | |
9 | ;;; | |
e0808c47 | 10 | ;;; This file is part of the Sensible Object Design, an object system for C. |
dea4d055 MW |
11 | ;;; |
12 | ;;; SOD is free software; you can redistribute it and/or modify | |
13 | ;;; it under the terms of the GNU General Public License as published by | |
14 | ;;; the Free Software Foundation; either version 2 of the License, or | |
15 | ;;; (at your option) any later version. | |
16 | ;;; | |
17 | ;;; SOD is distributed in the hope that it will be useful, | |
18 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;;; GNU General Public License for more details. | |
21 | ;;; | |
22 | ;;; You should have received a copy of the GNU General Public License | |
23 | ;;; along with SOD; if not, write to the Free Software Foundation, | |
24 | ;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
25 | ||
26 | (cl:in-package #:sod) | |
27 | ||
28 | ;;;-------------------------------------------------------------------------- | |
29 | ;;; Property representation. | |
30 | ||
31 | (export 'property-key) | |
32 | (defun property-key (name) | |
33 | "Convert NAME into a keyword. | |
34 | ||
35 | If NAME isn't a symbol already, then flip its case (using | |
36 | `frob-identifier'), and intern into the `keyword' package." | |
37 | (etypecase name | |
38 | (symbol name) | |
39 | (string (intern (frob-identifier name) :keyword)))) | |
40 | ||
048d0b2d | 41 | (export '(property propertyp p-name p-value p-type p-key p-seenp)) |
dea4d055 MW |
42 | (defstruct (property |
43 | (:predicate propertyp) | |
44 | (:conc-name p-) | |
048d0b2d MW |
45 | (:constructor %make-property |
46 | (name value | |
47 | &key type location seenp | |
4b8e5c03 | 48 | &aux (key (property-key name)) (%type type)))) |
dea4d055 MW |
49 | "A simple structure for holding a property in a property set. |
50 | ||
51 | The main useful feature is the ability to tick off properties which have | |
52 | been used, so that we can complain about unrecognized properties. | |
53 | ||
54 | An explicit type tag is necessary because we need to be able to talk | |
55 | distinctly about identifiers, strings and symbols, and we've only got two | |
56 | obvious Lisp types to play with. Sad, but true." | |
57 | ||
6ee19709 MW |
58 | (name nil :type (or string symbol) :read-only t) |
59 | (value nil :type t :read-only t) | |
60 | (%type nil :type symbol :read-only t) | |
61 | (location (file-location nil) :type file-location :read-only t) | |
62 | (key nil :type symbol :read-only t) | |
dea4d055 | 63 | (seenp nil :type boolean)) |
96baf945 | 64 | (define-access-wrapper p-type p-%type :read-only t) |
dea4d055 | 65 | |
048d0b2d MW |
66 | (export 'decode-property) |
67 | (defgeneric decode-property (raw) | |
68 | (:documentation "Decode a RAW value into a TYPE, VALUE pair.") | |
69 | (:method ((raw symbol)) (values :symbol raw)) | |
70 | (:method ((raw integer)) (values :int raw)) | |
71 | (:method ((raw string)) (values :string raw)) | |
72 | (:method ((raw character)) (values :char raw)) | |
73 | (:method ((raw property)) (values (p-type raw) (p-value raw))) | |
ea578bb4 | 74 | (:method ((raw cons)) (values (car raw) (cdr raw))) |
6ee19709 MW |
75 | (:method ((raw function)) (values :func raw)) |
76 | (:method ((raw c-type)) (values :type raw))) | |
048d0b2d MW |
77 | |
78 | (export 'make-property) | |
79 | (defun make-property (name raw-value &key type location seenp) | |
80 | (multiple-value-bind (type value) | |
81 | (if type | |
82 | (values type raw-value) | |
83 | (decode-property raw-value)) | |
84 | (%make-property name value | |
85 | :type type | |
86 | :location (file-location location) | |
87 | :seenp seenp))) | |
88 | ||
dea4d055 MW |
89 | (export 'coerce-property-value) |
90 | (defgeneric coerce-property-value (value type wanted) | |
91 | (:documentation | |
92 | "Convert VALUE, a property of type TYPE, to be of type WANTED. | |
93 | ||
94 | It's sensible to add additional methods to this function, but there are | |
95 | all the ones we need.") | |
96 | ||
97 | ;; If TYPE matches WANTED, we'll assume that VALUE already has the right | |
98 | ;; form. Otherwise, if nothing else matched, then I guess we'll have to | |
99 | ;; say it didn't work. | |
100 | (:method (value type wanted) | |
101 | (if (eql type wanted) value | |
102 | (error "Incorrect type: expected ~A but found ~A" wanted type))) | |
103 | ||
104 | ;; If the caller asks for type T then give him the raw thing. | |
105 | (:method (value type (wanted (eql t))) | |
1d8cc67a | 106 | (declare (ignore type)) |
dea4d055 MW |
107 | value)) |
108 | ||
109 | ;;;-------------------------------------------------------------------------- | |
110 | ;;; Property set representation. | |
111 | ||
112 | (export '(pset psetp)) | |
113 | (defstruct (pset (:predicate psetp) | |
114 | (:constructor %make-pset) | |
115 | (:conc-name %pset-)) | |
116 | "A property set. | |
117 | ||
118 | Wrapped up in a structure so that we can define a print function." | |
119 | (hash (make-hash-table) :type hash-table)) | |
120 | ||
121 | (export '(make-pset pset-get pset-store pset-map)) | |
122 | (declaim (inline make-pset pset-get pset-store pset-map)) | |
123 | ||
124 | (defun make-pset () | |
125 | "Constructor for property sets." | |
126 | (%make-pset)) | |
127 | ||
128 | (defun pset-get (pset key) | |
129 | "Look KEY up in PSET and return what we find. | |
130 | ||
268f7777 | 131 | If there's no property by that name, return nil." |
dea4d055 MW |
132 | (values (gethash key (%pset-hash pset)))) |
133 | ||
134 | (defun pset-store (pset prop) | |
135 | "Store property PROP in PSET. | |
136 | ||
137 | Overwrite or replace any previous property with the same name. Mutates | |
138 | the property set." | |
139 | (setf (gethash (p-key prop) (%pset-hash pset)) prop)) | |
140 | ||
141 | (defun pset-map (func pset) | |
142 | "Call FUNC for each property in PSET." | |
143 | (maphash (lambda (key value) (declare (ignore key)) (funcall func value)) | |
144 | (%pset-hash pset))) | |
145 | ||
146 | (export 'with-pset-iterator) | |
147 | (defmacro with-pset-iterator ((name pset) &body body) | |
148 | "Evaluate BODY with NAME bound to a macro returning properties from PSET. | |
149 | ||
150 | Evaluating (NAME) returns a property object or nil if all properties have | |
151 | been read." | |
152 | (with-gensyms (next win key value) | |
153 | `(with-hash-table-iterator (,next (%pset-hash ,pset)) | |
154 | (macrolet ((,name () | |
048d0b2d MW |
155 | `(multiple-value-bind (,',win ,',key ,',value) (,',next) |
156 | (declare (ignore ,',key)) | |
157 | (and ,',win ,',value)))) | |
dea4d055 MW |
158 | ,@body)))) |
159 | ||
160 | ;;;-------------------------------------------------------------------------- | |
161 | ;;; `Cooked' property set operations. | |
162 | ||
163 | (export 'store-property) | |
164 | (defun store-property | |
048d0b2d | 165 | (pset name value &key type location) |
dea4d055 MW |
166 | "Store a property in PSET." |
167 | (pset-store pset | |
168 | (make-property name value :type type :location location))) | |
169 | ||
170 | (export 'get-property) | |
171 | (defun get-property (pset name type &optional default) | |
172 | "Fetch a property from a property set. | |
173 | ||
174 | If a property NAME is not found in PSET, or if a property is found, but | |
175 | its type doesn't match TYPE, then return DEFAULT and nil; otherwise return | |
176 | the value and its file location. In the latter case, mark the property as | |
177 | having been used. | |
178 | ||
3109662a MW |
179 | The value returned depends on the TYPE argument provided. If you pass |
180 | `nil' then you get back the entire `property' object. If you pass `t', | |
181 | then you get whatever was left in the property set, uninterpreted. | |
182 | Otherwise the value is coerced to the right kind of thing (where possible) | |
183 | and returned. | |
dea4d055 | 184 | |
ea578bb4 MW |
185 | The file location at which the property was defined is returned as a |
186 | second value. | |
187 | ||
188 | If PSET is nil, then return DEFAULT and nil." | |
dea4d055 MW |
189 | |
190 | (let ((prop (and pset (pset-get pset (property-key name))))) | |
191 | (with-default-error-location ((and prop (p-location prop))) | |
192 | (cond ((not prop) | |
193 | (values default nil)) | |
194 | ((not type) | |
195 | (setf (p-seenp prop) t) | |
196 | (values prop (p-location prop))) | |
197 | (t | |
198 | (setf (p-seenp prop) t) | |
199 | (values (coerce-property-value (p-value prop) | |
200 | (p-type prop) | |
201 | type) | |
202 | (p-location prop))))))) | |
203 | ||
204 | (export 'add-property) | |
048d0b2d | 205 | (defun add-property (pset name value &key type location) |
dea4d055 MW |
206 | "Add a property to PSET. |
207 | ||
208 | If a property with the same NAME already exists, report an error." | |
209 | ||
210 | (with-default-error-location (location) | |
211 | (let ((existing (get-property pset name nil))) | |
212 | (when existing | |
213 | (error "Property ~S already defined~@[ at ~A~]" | |
214 | name (p-location existing))) | |
215 | (store-property pset name value :type type :location location)))) | |
216 | ||
217 | (export 'make-property-set) | |
218 | (defun make-property-set (&rest plist) | |
219 | "Make a new property set, with given properties. | |
220 | ||
221 | This isn't the way to make properties when parsing, but it works well for | |
222 | programmatic generation. The arguments should form a property list | |
223 | (alternating keywords and values is good). | |
224 | ||
225 | An attempt is made to guess property types from the Lisp types of the | |
226 | values. This isn't always successful but it's not too bad. The | |
3109662a | 227 | alternative is manufacturing a `property-value' object by hand and |
048d0b2d | 228 | stuffing it into the set." |
dea4d055 MW |
229 | |
230 | (property-set plist)) | |
231 | ||
232 | (export 'property-set) | |
233 | (defgeneric property-set (thing) | |
234 | (:documentation | |
235 | "Convert THING into a property set.") | |
236 | (:method ((pset pset)) pset) | |
237 | (:method ((list list)) | |
238 | "Convert a list into a property set. This works for alists and plists." | |
239 | (multiple-value-bind (next name value) | |
240 | (if (and list (consp (car list))) | |
241 | (values #'cdr #'caar #'cdar) | |
242 | (values #'cddr #'car #'cadr)) | |
243 | (do ((pset (make-pset)) | |
244 | (list list (funcall next list))) | |
245 | ((endp list) pset) | |
246 | (add-property pset (funcall name list) (funcall value list)))))) | |
247 | ||
b2d75547 | 248 | (export 'check-unused-properties) |
dea4d055 MW |
249 | (defun check-unused-properties (pset) |
250 | "Issue errors about unused properties in PSET." | |
251 | (when pset | |
252 | (pset-map (lambda (prop) | |
253 | (unless (p-seenp prop) | |
254 | (cerror*-with-location (p-location prop) | |
255 | "Unknown property `~A'" | |
256 | (p-name prop)) | |
257 | (setf (p-seenp prop) t))) | |
258 | pset))) | |
259 | ||
260 | ;;;-------------------------------------------------------------------------- | |
261 | ;;; Utility macros. | |
262 | ||
2a8a5260 | 263 | (export 'default-slot-from-property) |
dea4d055 | 264 | (defmacro default-slot-from-property |
ecdf14f0 | 265 | ((instance slot &optional (slot-names t)) |
dea4d055 MW |
266 | (pset property type |
267 | &optional (pvar (gensym "PROP-")) | |
268 | &rest convert-forms) | |
269 | &body default-forms) | |
270 | "Initialize a slot from a property. | |
271 | ||
272 | We initialize SLOT in INSTANCE. In full: if PSET contains a property | |
273 | called NAME, then convert it to TYPE, bind the value to PVAR and evaluate | |
274 | CONVERT-FORMS -- these default to just using the property value. If | |
be01e762 MW |
275 | there's no property, and DEFAULT-FORMS contains at least one non- |
276 | declaration form, and the slot is named in SLOT-NAMES and currently | |
dea4d055 MW |
277 | unbound, then evaluate DEFAULT-FORMS and use their value to compute the |
278 | slot value." | |
279 | ||
280 | (once-only (instance slot slot-names pset property type) | |
b8c698ee MW |
281 | (multiple-value-bind (docs decls body) |
282 | (parse-body default-forms :docp nil) | |
283 | (declare (ignore docs)) | |
284 | (with-gensyms (floc) | |
285 | `(multiple-value-bind (,pvar ,floc) | |
286 | (get-property ,pset ,property ,type) | |
287 | ,@decls | |
288 | (if ,floc | |
289 | (setf (slot-value ,instance ,slot) | |
290 | (with-default-error-location (,floc) | |
291 | ,@(or convert-forms `(,pvar)))) | |
be01e762 MW |
292 | ,@(and body |
293 | `((default-slot (,instance ,slot ,slot-names) | |
294 | ,@body))))))))) | |
dea4d055 MW |
295 | |
296 | ;;;----- That's all, folks -------------------------------------------------- |