Commit | Line | Data |
---|---|---|
6dd12ef2 | 1 | ;;; eieio.el --- Enhanced Implementation of Emacs Interpreted Objects |
f6b1b0a8 | 2 | ;;; or maybe Eric's Implementation of Emacs Interpreted Objects |
6dd12ef2 | 3 | |
acaf905b | 4 | ;; Copyright (C) 1995-1996, 1998-2012 Free Software Foundation, Inc. |
6dd12ef2 | 5 | |
9ffe3f52 | 6 | ;; Author: Eric M. Ludlam <zappo@gnu.org> |
fd795bf4 | 7 | ;; Version: 1.3 |
6dd12ef2 CY |
8 | ;; Keywords: OO, lisp |
9 | ||
10 | ;; This file is part of GNU Emacs. | |
11 | ||
12 | ;; GNU Emacs 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 3 of the License, or | |
15 | ;; (at your option) any later version. | |
16 | ||
17 | ;; GNU Emacs 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | |
24 | ||
25 | ;;; Commentary: | |
26 | ;; | |
27 | ;; EIEIO is a series of Lisp routines which implements a subset of | |
28 | ;; CLOS, the Common Lisp Object System. In addition, EIEIO also adds | |
29 | ;; a few new features which help it integrate more strongly with the | |
30 | ;; Emacs running environment. | |
31 | ;; | |
32 | ;; See eieio.texi for complete documentation on using this package. | |
a2930e43 EL |
33 | ;; |
34 | ;; Note: the implementation of the c3 algorithm is based on: | |
35 | ;; Kim Barrett et al.: A Monotonic Superclass Linearization for Dylan | |
36 | ;; Retrieved from: | |
37 | ;; http://192.220.96.201/dylan/linearization-oopsla96.html | |
6dd12ef2 CY |
38 | |
39 | ;; There is funny stuff going on with typep and deftype. This | |
40 | ;; is the only way I seem to be able to make this stuff load properly. | |
41 | ||
42 | ;; @TODO - fix :initform to be a form, not a quoted value | |
43 | ;; @TODO - Prefix non-clos functions with `eieio-'. | |
44 | ||
45 | ;;; Code: | |
46 | ||
12999ea8 | 47 | (eval-when-compile (require 'cl)) ;FIXME: Use cl-lib! |
6dd12ef2 | 48 | |
a2930e43 | 49 | (defvar eieio-version "1.3" |
6dd12ef2 CY |
50 | "Current version of EIEIO.") |
51 | ||
52 | (defun eieio-version () | |
53 | "Display the current version of EIEIO." | |
54 | (interactive) | |
55 | (message eieio-version)) | |
56 | ||
57 | (eval-and-compile | |
a8f316ca | 58 | ;; About the above. EIEIO must process its own code when it compiles |
c80e3b4a | 59 | ;; itself, thus, by eval-and-compiling ourselves, we solve the problem. |
6dd12ef2 CY |
60 | |
61 | ;; Compatibility | |
62 | (if (fboundp 'compiled-function-arglist) | |
63 | ||
64 | ;; XEmacs can only access a compiled functions arglist like this: | |
65 | (defalias 'eieio-compiled-function-arglist 'compiled-function-arglist) | |
66 | ||
67 | ;; Emacs doesn't have this function, but since FUNC is a vector, we can just | |
68 | ;; grab the appropriate element. | |
69 | (defun eieio-compiled-function-arglist (func) | |
70 | "Return the argument list for the compiled function FUNC." | |
71 | (aref func 0)) | |
72 | ||
73 | ) | |
74 | ||
75 | \f | |
76 | ;;; | |
77 | ;; Variable declarations. | |
78 | ;; | |
79 | ||
80 | (defvar eieio-hook nil | |
fb7ada5f | 81 | "This hook is executed, then cleared each time `defclass' is called.") |
6dd12ef2 CY |
82 | |
83 | (defvar eieio-error-unsupported-class-tags nil | |
a2930e43 | 84 | "Non-nil to throw an error if an encountered tag is unsupported. |
6dd12ef2 CY |
85 | This may prevent classes from CLOS applications from being used with EIEIO |
86 | since EIEIO does not support all CLOS tags.") | |
87 | ||
88 | (defvar eieio-skip-typecheck nil | |
fb7ada5f | 89 | "If non-nil, skip all slot typechecking. |
6dd12ef2 CY |
90 | Set this to t permanently if a program is functioning well to get a |
91 | small speed increase. This variable is also used internally to handle | |
92 | default setting for optimization purposes.") | |
93 | ||
94 | (defvar eieio-optimize-primary-methods-flag t | |
95 | "Non-nil means to optimize the method dispatch on primary methods.") | |
96 | ||
6dd12ef2 CY |
97 | (defvar eieio-initializing-object nil |
98 | "Set to non-nil while initializing an object.") | |
99 | ||
100 | (defconst eieio-unbound | |
101 | (if (and (boundp 'eieio-unbound) (symbolp eieio-unbound)) | |
102 | eieio-unbound | |
103 | (make-symbol "unbound")) | |
104 | "Uninterned symbol representing an unbound slot in an object.") | |
105 | ||
106 | ;; This is a bootstrap for eieio-default-superclass so it has a value | |
107 | ;; while it is being built itself. | |
108 | (defvar eieio-default-superclass nil) | |
109 | ||
7200d79c | 110 | ;; FIXME: The constants below should have an `eieio-' prefix added!! |
6dd12ef2 CY |
111 | (defconst class-symbol 1 "Class's symbol (self-referencing.).") |
112 | (defconst class-parent 2 "Class parent slot.") | |
113 | (defconst class-children 3 "Class children class slot.") | |
114 | (defconst class-symbol-obarray 4 "Obarray permitting fast access to variable position indexes.") | |
115 | ;; @todo | |
116 | ;; the word "public" here is leftovers from the very first version. | |
117 | ;; Get rid of it! | |
118 | (defconst class-public-a 5 "Class attribute index.") | |
119 | (defconst class-public-d 6 "Class attribute defaults index.") | |
120 | (defconst class-public-doc 7 "Class documentation strings for attributes.") | |
121 | (defconst class-public-type 8 "Class type for a slot.") | |
122 | (defconst class-public-custom 9 "Class custom type for a slot.") | |
123 | (defconst class-public-custom-label 10 "Class custom group for a slot.") | |
124 | (defconst class-public-custom-group 11 "Class custom group for a slot.") | |
125 | (defconst class-public-printer 12 "Printer for a slot.") | |
126 | (defconst class-protection 13 "Class protection for a slot.") | |
127 | (defconst class-initarg-tuples 14 "Class initarg tuples list.") | |
128 | (defconst class-class-allocation-a 15 "Class allocated attributes.") | |
129 | (defconst class-class-allocation-doc 16 "Class allocated documentation.") | |
130 | (defconst class-class-allocation-type 17 "Class allocated value type.") | |
131 | (defconst class-class-allocation-custom 18 "Class allocated custom descriptor.") | |
132 | (defconst class-class-allocation-custom-label 19 "Class allocated custom descriptor.") | |
133 | (defconst class-class-allocation-custom-group 20 "Class allocated custom group.") | |
134 | (defconst class-class-allocation-printer 21 "Class allocated printer for a slot.") | |
135 | (defconst class-class-allocation-protection 22 "Class allocated protection list.") | |
136 | (defconst class-class-allocation-values 23 "Class allocated value vector.") | |
137 | (defconst class-default-object-cache 24 | |
138 | "Cache index of what a newly created object would look like. | |
139 | This will speed up instantiation time as only a `copy-sequence' will | |
140 | be needed, instead of looping over all the values and setting them | |
141 | from the default.") | |
142 | (defconst class-options 25 | |
143 | "Storage location of tagged class options. | |
144 | Stored outright without modifications or stripping.") | |
145 | ||
146 | (defconst class-num-slots 26 | |
147 | "Number of slots in the class definition object.") | |
148 | ||
149 | (defconst object-class 1 "Index in an object vector where the class is stored.") | |
150 | (defconst object-name 2 "Index in an object where the name is stored.") | |
151 | ||
152 | (defconst method-static 0 "Index into :static tag on a method.") | |
153 | (defconst method-before 1 "Index into :before tag on a method.") | |
154 | (defconst method-primary 2 "Index into :primary tag on a method.") | |
155 | (defconst method-after 3 "Index into :after tag on a method.") | |
156 | (defconst method-num-lists 4 "Number of indexes into methods vector in which groups of functions are kept.") | |
157 | (defconst method-generic-before 4 "Index into generic :before tag on a method.") | |
158 | (defconst method-generic-primary 5 "Index into generic :primary tag on a method.") | |
159 | (defconst method-generic-after 6 "Index into generic :after tag on a method.") | |
160 | (defconst method-num-slots 7 "Number of indexes into a method's vector.") | |
161 | ||
a2930e43 EL |
162 | (defsubst eieio-specialized-key-to-generic-key (key) |
163 | "Convert a specialized KEY into a generic method key." | |
164 | (cond ((eq key method-static) 0) ;; don't convert | |
165 | ((< key method-num-lists) (+ key 3)) ;; The conversion | |
166 | (t key) ;; already generic.. maybe. | |
167 | )) | |
168 | ||
6dd12ef2 CY |
169 | \f |
170 | ;;; Important macros used in eieio. | |
171 | ;; | |
172 | (defmacro class-v (class) | |
173 | "Internal: Return the class vector from the CLASS symbol." | |
174 | ;; No check: If eieio gets this far, it's probably been checked already. | |
175 | `(get ,class 'eieio-class-definition)) | |
176 | ||
177 | (defmacro class-p (class) | |
178 | "Return t if CLASS is a valid class vector. | |
179 | CLASS is a symbol." | |
180 | ;; this new method is faster since it doesn't waste time checking lots of | |
181 | ;; things. | |
182 | `(condition-case nil | |
183 | (eq (aref (class-v ,class) 0) 'defclass) | |
184 | (error nil))) | |
185 | ||
186 | (defmacro eieio-object-p (obj) | |
187 | "Return non-nil if OBJ is an EIEIO object." | |
188 | `(condition-case nil | |
189 | (let ((tobj ,obj)) | |
190 | (and (eq (aref tobj 0) 'object) | |
191 | (class-p (aref tobj object-class)))) | |
192 | (error nil))) | |
193 | (defalias 'object-p 'eieio-object-p) | |
194 | ||
195 | (defmacro class-constructor (class) | |
196 | "Return the symbol representing the constructor of CLASS." | |
197 | `(aref (class-v ,class) class-symbol)) | |
198 | ||
199 | (defmacro generic-p (method) | |
200 | "Return t if symbol METHOD is a generic function. | |
a8f316ca JB |
201 | Only methods have the symbol `eieio-method-obarray' as a property |
202 | \(which contains a list of all bindings to that method type.)" | |
6dd12ef2 CY |
203 | `(and (fboundp ,method) (get ,method 'eieio-method-obarray))) |
204 | ||
205 | (defun generic-primary-only-p (method) | |
206 | "Return t if symbol METHOD is a generic function with only primary methods. | |
207 | Only methods have the symbol `eieio-method-obarray' as a property (which | |
208 | contains a list of all bindings to that method type.) | |
209 | Methods with only primary implementations are executed in an optimized way." | |
210 | (and (generic-p method) | |
211 | (let ((M (get method 'eieio-method-tree))) | |
212 | (and (< 0 (length (aref M method-primary))) | |
213 | (not (aref M method-static)) | |
214 | (not (aref M method-before)) | |
215 | (not (aref M method-after)) | |
216 | (not (aref M method-generic-before)) | |
217 | (not (aref M method-generic-primary)) | |
218 | (not (aref M method-generic-after)))) | |
219 | )) | |
220 | ||
221 | (defun generic-primary-only-one-p (method) | |
222 | "Return t if symbol METHOD is a generic function with only primary methods. | |
223 | Only methods have the symbol `eieio-method-obarray' as a property (which | |
224 | contains a list of all bindings to that method type.) | |
225 | Methods with only primary implementations are executed in an optimized way." | |
226 | (and (generic-p method) | |
227 | (let ((M (get method 'eieio-method-tree))) | |
228 | (and (= 1 (length (aref M method-primary))) | |
229 | (not (aref M method-static)) | |
230 | (not (aref M method-before)) | |
231 | (not (aref M method-after)) | |
232 | (not (aref M method-generic-before)) | |
233 | (not (aref M method-generic-primary)) | |
234 | (not (aref M method-generic-after)))) | |
235 | )) | |
236 | ||
237 | (defmacro class-option-assoc (list option) | |
a2930e43 | 238 | "Return from LIST the found OPTION, or nil if it doesn't exist." |
6dd12ef2 CY |
239 | `(car-safe (cdr (memq ,option ,list)))) |
240 | ||
241 | (defmacro class-option (class option) | |
242 | "Return the value stored for CLASS' OPTION. | |
243 | Return nil if that option doesn't exist." | |
244 | `(class-option-assoc (aref (class-v ,class) class-options) ',option)) | |
245 | ||
246 | (defmacro class-abstract-p (class) | |
247 | "Return non-nil if CLASS is abstract. | |
248 | Abstract classes cannot be instantiated." | |
249 | `(class-option ,class :abstract)) | |
250 | ||
251 | (defmacro class-method-invocation-order (class) | |
252 | "Return the invocation order of CLASS. | |
253 | Abstract classes cannot be instantiated." | |
254 | `(or (class-option ,class :method-invocation-order) | |
255 | :breadth-first)) | |
256 | ||
257 | \f | |
258 | ;;; Defining a new class | |
259 | ;; | |
260 | (defmacro defclass (name superclass slots &rest options-and-doc) | |
261 | "Define NAME as a new class derived from SUPERCLASS with SLOTS. | |
262 | OPTIONS-AND-DOC is used as the class' options and base documentation. | |
263 | SUPERCLASS is a list of superclasses to inherit from, with SLOTS | |
264 | being the slots residing in that class definition. NOTE: Currently | |
265 | only one slot may exist in SUPERCLASS as multiple inheritance is not | |
266 | yet supported. Supported tags are: | |
267 | ||
a8f316ca JB |
268 | :initform - Initializing form. |
269 | :initarg - Tag used during initialization. | |
270 | :accessor - Tag used to create a function to access this slot. | |
271 | :allocation - Specify where the value is stored. | |
272 | Defaults to `:instance', but could also be `:class'. | |
273 | :writer - A function symbol which will `write' an object's slot. | |
274 | :reader - A function symbol which will `read' an object. | |
275 | :type - The type of data allowed in this slot (see `typep'). | |
6dd12ef2 CY |
276 | :documentation |
277 | - A string documenting use of this slot. | |
278 | ||
279 | The following are extensions on CLOS: | |
280 | :protection - Specify protection for this slot. | |
a8f316ca | 281 | Defaults to `:public'. Also use `:protected', or `:private'. |
6dd12ef2 CY |
282 | :custom - When customizing an object, the custom :type. Public only. |
283 | :label - A text string label used for a slot when customizing. | |
284 | :group - Name of a customization group this slot belongs in. | |
285 | :printer - A function to call to print the value of a slot. | |
286 | See `eieio-override-prin1' as an example. | |
287 | ||
288 | A class can also have optional options. These options happen in place | |
a8f316ca | 289 | of documentation (including a :documentation tag), in addition to |
6dd12ef2 CY |
290 | documentation, or not at all. Supported options are: |
291 | ||
292 | :documentation - The doc-string used for this class. | |
293 | ||
294 | Options added to EIEIO: | |
295 | ||
a8f316ca | 296 | :allow-nil-initform - Non-nil to skip typechecking of null initforms. |
6dd12ef2 CY |
297 | :custom-groups - List of custom group names. Organizes slots into |
298 | reasonable groups for customizations. | |
299 | :abstract - Non-nil to prevent instances of this class. | |
300 | If a string, use as an error string if someone does | |
301 | try to make an instance. | |
302 | :method-invocation-order | |
9ffe3f52 | 303 | - Control the method invocation order if there is |
6dd12ef2 CY |
304 | multiple inheritance. Valid values are: |
305 | :breadth-first - The default. | |
306 | :depth-first | |
307 | ||
308 | Options in CLOS not supported in EIEIO: | |
309 | ||
310 | :metaclass - Class to use in place of `standard-class' | |
311 | :default-initargs - Initargs to use when initializing new objects of | |
312 | this class. | |
313 | ||
a8f316ca JB |
314 | Due to the way class options are set up, you can add any tags you wish, |
315 | and reference them using the function `class-option'." | |
6dd12ef2 CY |
316 | ;; We must `eval-and-compile' this so that when we byte compile |
317 | ;; an eieio program, there is no need to load it ahead of time. | |
318 | ;; It also provides lots of nice debugging errors at compile time. | |
319 | `(eval-and-compile | |
320 | (eieio-defclass ',name ',superclass ',slots ',options-and-doc))) | |
321 | ||
322 | (defvar eieio-defclass-autoload-map (make-vector 7 nil) | |
323 | "Symbol map of superclasses we find in autoloads.") | |
324 | ||
325 | ;; We autoload this because it's used in `make-autoload'. | |
326 | ;;;###autoload | |
327 | (defun eieio-defclass-autoload (cname superclasses filename doc) | |
328 | "Create autoload symbols for the EIEIO class CNAME. | |
9ffe3f52 | 329 | SUPERCLASSES are the superclasses that CNAME inherits from. |
6dd12ef2 CY |
330 | DOC is the docstring for CNAME. |
331 | This function creates a mock-class for CNAME and adds it into | |
332 | SUPERCLASSES as children. | |
333 | It creates an autoload function for CNAME's constructor." | |
334 | ;; Assume we've already debugged inputs. | |
335 | ||
336 | (let* ((oldc (when (class-p cname) (class-v cname))) | |
337 | (newc (make-vector class-num-slots nil)) | |
338 | ) | |
339 | (if oldc | |
340 | nil ;; Do nothing if we already have this class. | |
341 | ||
342 | ;; Create the class in NEWC, but don't fill anything else in. | |
343 | (aset newc 0 'defclass) | |
344 | (aset newc class-symbol cname) | |
345 | ||
346 | (let ((clear-parent nil)) | |
347 | ;; No parents? | |
348 | (when (not superclasses) | |
349 | (setq superclasses '(eieio-default-superclass) | |
350 | clear-parent t) | |
351 | ) | |
352 | ||
353 | ;; Hook our new class into the existing structures so we can | |
354 | ;; autoload it later. | |
355 | (dolist (SC superclasses) | |
356 | ||
357 | ||
358 | ;; TODO - If we create an autoload that is in the map, that | |
359 | ;; map needs to be cleared! | |
360 | ||
361 | ||
362 | ;; Does our parent exist? | |
363 | (if (not (class-p SC)) | |
364 | ||
365 | ;; Create a symbol for this parent, and then store this | |
366 | ;; parent on that symbol. | |
367 | (let ((sym (intern (symbol-name SC) eieio-defclass-autoload-map))) | |
368 | (if (not (boundp sym)) | |
369 | (set sym (list cname)) | |
370 | (add-to-list sym cname)) | |
371 | ) | |
372 | ||
373 | ;; We have a parent, save the child in there. | |
374 | (when (not (member cname (aref (class-v SC) class-children))) | |
375 | (aset (class-v SC) class-children | |
376 | (cons cname (aref (class-v SC) class-children))))) | |
377 | ||
378 | ;; save parent in child | |
379 | (aset newc class-parent (cons SC (aref newc class-parent))) | |
380 | ) | |
381 | ||
8350f087 | 382 | ;; turn this into a usable self-pointing symbol |
6dd12ef2 CY |
383 | (set cname cname) |
384 | ||
385 | ;; Store the new class vector definition into the symbol. We need to | |
386 | ;; do this first so that we can call defmethod for the accessor. | |
387 | ;; The vector will be updated by the following while loop and will not | |
388 | ;; need to be stored a second time. | |
389 | (put cname 'eieio-class-definition newc) | |
390 | ||
391 | ;; Clear the parent | |
392 | (if clear-parent (aset newc class-parent nil)) | |
393 | ||
394 | ;; Create an autoload on top of our constructor function. | |
395 | (autoload cname filename doc nil nil) | |
396 | (autoload (intern (concat (symbol-name cname) "-p")) filename "" nil nil) | |
397 | (autoload (intern (concat (symbol-name cname) "-child-p")) filename "" nil nil) | |
62a81506 | 398 | (autoload (intern (concat (symbol-name cname) "-list-p")) filename "" nil nil) |
6dd12ef2 CY |
399 | |
400 | )))) | |
401 | ||
402 | (defsubst eieio-class-un-autoload (cname) | |
a8f316ca | 403 | "If class CNAME is in an autoload state, load its file." |
6dd12ef2 CY |
404 | (when (eq (car-safe (symbol-function cname)) 'autoload) |
405 | (load-library (car (cdr (symbol-function cname)))))) | |
406 | ||
407 | (defun eieio-defclass (cname superclasses slots options-and-doc) | |
d1dc2cc2 | 408 | ;; FIXME: Most of this should be moved to the `defclass' macro. |
a8f316ca JB |
409 | "Define CNAME as a new subclass of SUPERCLASSES. |
410 | SLOTS are the slots residing in that class definition, and options or | |
411 | documentation OPTIONS-AND-DOC is the toplevel documentation for this class. | |
412 | See `defclass' for more information." | |
6dd12ef2 CY |
413 | ;; Run our eieio-hook each time, and clear it when we are done. |
414 | ;; This way people can add hooks safely if they want to modify eieio | |
415 | ;; or add definitions when eieio is loaded or something like that. | |
416 | (run-hooks 'eieio-hook) | |
417 | (setq eieio-hook nil) | |
418 | ||
12999ea8 SM |
419 | (if (not (listp superclasses)) |
420 | (signal 'wrong-type-argument '(listp superclasses))) | |
6dd12ef2 | 421 | |
12999ea8 | 422 | (let* ((pname superclasses) |
6dd12ef2 CY |
423 | (newc (make-vector class-num-slots nil)) |
424 | (oldc (when (class-p cname) (class-v cname))) | |
425 | (groups nil) ;; list of groups id'd from slots | |
426 | (options nil) | |
427 | (clearparent nil)) | |
428 | ||
429 | (aset newc 0 'defclass) | |
430 | (aset newc class-symbol cname) | |
431 | ||
a8f316ca | 432 | ;; If this class already existed, and we are updating its structure, |
6dd12ef2 CY |
433 | ;; make sure we keep the old child list. This can cause bugs, but |
434 | ;; if no new slots are created, it also saves time, and prevents | |
435 | ;; method table breakage, particularly when the users is only | |
436 | ;; byte compiling an EIEIO file. | |
437 | (if oldc | |
438 | (aset newc class-children (aref oldc class-children)) | |
439 | ;; If the old class did not exist, but did exist in the autoload map, then adopt those children. | |
440 | ;; This is like the above, but deals with autoloads nicely. | |
441 | (let ((sym (intern-soft (symbol-name cname) eieio-defclass-autoload-map))) | |
442 | (when sym | |
443 | (condition-case nil | |
444 | (aset newc class-children (symbol-value sym)) | |
445 | (error nil)) | |
446 | (unintern (symbol-name cname) eieio-defclass-autoload-map) | |
447 | )) | |
448 | ) | |
449 | ||
450 | (cond ((and (stringp (car options-and-doc)) | |
451 | (/= 1 (% (length options-and-doc) 2))) | |
452 | (error "Too many arguments to `defclass'")) | |
453 | ((and (symbolp (car options-and-doc)) | |
454 | (/= 0 (% (length options-and-doc) 2))) | |
455 | (error "Too many arguments to `defclass'")) | |
456 | ) | |
457 | ||
458 | (setq options | |
459 | (if (stringp (car options-and-doc)) | |
460 | (cons :documentation options-and-doc) | |
461 | options-and-doc)) | |
462 | ||
463 | (if pname | |
464 | (progn | |
465 | (while pname | |
466 | (if (and (car pname) (symbolp (car pname))) | |
467 | (if (not (class-p (car pname))) | |
468 | ;; bad class | |
469 | (error "Given parent class %s is not a class" (car pname)) | |
470 | ;; good parent class... | |
471 | ;; save new child in parent | |
472 | (when (not (member cname (aref (class-v (car pname)) class-children))) | |
473 | (aset (class-v (car pname)) class-children | |
474 | (cons cname (aref (class-v (car pname)) class-children)))) | |
475 | ;; Get custom groups, and store them into our local copy. | |
476 | (mapc (lambda (g) (add-to-list 'groups g)) | |
477 | (class-option (car pname) :custom-groups)) | |
478 | ;; save parent in child | |
479 | (aset newc class-parent (cons (car pname) (aref newc class-parent)))) | |
480 | (error "Invalid parent class %s" pname)) | |
481 | (setq pname (cdr pname))) | |
482 | ;; Reverse the list of our parents so that they are prioritized in | |
483 | ;; the same order as specified in the code. | |
484 | (aset newc class-parent (nreverse (aref newc class-parent))) ) | |
485 | ;; If there is nothing to loop over, then inherit from the | |
486 | ;; default superclass. | |
487 | (unless (eq cname 'eieio-default-superclass) | |
488 | ;; adopt the default parent here, but clear it later... | |
489 | (setq clearparent t) | |
490 | ;; save new child in parent | |
491 | (if (not (member cname (aref (class-v 'eieio-default-superclass) class-children))) | |
492 | (aset (class-v 'eieio-default-superclass) class-children | |
493 | (cons cname (aref (class-v 'eieio-default-superclass) class-children)))) | |
494 | ;; save parent in child | |
495 | (aset newc class-parent (list eieio-default-superclass)))) | |
496 | ||
8350f087 | 497 | ;; turn this into a usable self-pointing symbol |
6dd12ef2 CY |
498 | (set cname cname) |
499 | ||
500 | ;; These two tests must be created right away so we can have self- | |
501 | ;; referencing classes. ei, a class whose slot can contain only | |
502 | ;; pointers to itself. | |
503 | ||
504 | ;; Create the test function | |
505 | (let ((csym (intern (concat (symbol-name cname) "-p")))) | |
506 | (fset csym | |
507 | (list 'lambda (list 'obj) | |
508 | (format "Test OBJ to see if it an object of type %s" cname) | |
509 | (list 'and '(eieio-object-p obj) | |
510 | (list 'same-class-p 'obj cname))))) | |
511 | ||
512 | ;; Make sure the method invocation order is a valid value. | |
513 | (let ((io (class-option-assoc options :method-invocation-order))) | |
a2930e43 | 514 | (when (and io (not (member io '(:depth-first :breadth-first :c3)))) |
6dd12ef2 CY |
515 | (error "Method invocation order %s is not allowed" io) |
516 | )) | |
517 | ||
518 | ;; Create a handy child test too | |
519 | (let ((csym (intern (concat (symbol-name cname) "-child-p")))) | |
520 | (fset csym | |
521 | `(lambda (obj) | |
522 | ,(format | |
523 | "Test OBJ to see if it an object is a child of type %s" | |
524 | cname) | |
525 | (and (eieio-object-p obj) | |
526 | (object-of-class-p obj ,cname)))) | |
527 | ||
62a81506 CY |
528 | ;; Create a handy list of the class test too |
529 | (let ((csym (intern (concat (symbol-name cname) "-list-p")))) | |
530 | (fset csym | |
531 | `(lambda (obj) | |
532 | ,(format | |
533 | "Test OBJ to see if it a list of objects which are a child of type %s" | |
534 | cname) | |
535 | (when (listp obj) | |
536 | (let ((ans t)) ;; nil is valid | |
537 | ;; Loop over all the elements of the input list, test | |
538 | ;; each to make sure it is a child of the desired object class. | |
539 | (while (and obj ans) | |
540 | (setq ans (and (eieio-object-p (car obj)) | |
541 | (object-of-class-p (car obj) ,cname))) | |
542 | (setq obj (cdr obj))) | |
543 | ans))))) | |
544 | ||
6dd12ef2 CY |
545 | ;; When using typep, (typep OBJ 'myclass) returns t for objects which |
546 | ;; are subclasses of myclass. For our predicates, however, it is | |
547 | ;; important for EIEIO to be backwards compatible, where | |
548 | ;; myobject-p, and myobject-child-p are different. | |
549 | ;; "cl" uses this technique to specify symbols with specific typep | |
550 | ;; test, so we can let typep have the CLOS documented behavior | |
551 | ;; while keeping our above predicate clean. | |
6dd12ef2 | 552 | |
67868d26 CY |
553 | ;; It would be cleaner to use `defsetf' here, but that requires cl |
554 | ;; at runtime. | |
555 | (put cname 'cl-deftype-handler | |
556 | (list 'lambda () `(list 'satisfies (quote ,csym))))) | |
6dd12ef2 | 557 | |
12999ea8 SM |
558 | ;; Before adding new slots, let's add all the methods and classes |
559 | ;; in from the parent class. | |
6dd12ef2 CY |
560 | (eieio-copy-parents-into-subclass newc superclasses) |
561 | ||
562 | ;; Store the new class vector definition into the symbol. We need to | |
563 | ;; do this first so that we can call defmethod for the accessor. | |
564 | ;; The vector will be updated by the following while loop and will not | |
565 | ;; need to be stored a second time. | |
566 | (put cname 'eieio-class-definition newc) | |
567 | ||
568 | ;; Query each slot in the declaration list and mangle into the | |
569 | ;; class structure I have defined. | |
570 | (while slots | |
571 | (let* ((slot1 (car slots)) | |
572 | (name (car slot1)) | |
573 | (slot (cdr slot1)) | |
574 | (acces (plist-get slot ':accessor)) | |
575 | (init (or (plist-get slot ':initform) | |
576 | (if (member ':initform slot) nil | |
577 | eieio-unbound))) | |
578 | (initarg (plist-get slot ':initarg)) | |
579 | (docstr (plist-get slot ':documentation)) | |
580 | (prot (plist-get slot ':protection)) | |
581 | (reader (plist-get slot ':reader)) | |
582 | (writer (plist-get slot ':writer)) | |
583 | (alloc (plist-get slot ':allocation)) | |
584 | (type (plist-get slot ':type)) | |
585 | (custom (plist-get slot ':custom)) | |
586 | (label (plist-get slot ':label)) | |
587 | (customg (plist-get slot ':group)) | |
588 | (printer (plist-get slot ':printer)) | |
589 | ||
590 | (skip-nil (class-option-assoc options :allow-nil-initform)) | |
591 | ) | |
592 | ||
593 | (if eieio-error-unsupported-class-tags | |
594 | (let ((tmp slot)) | |
595 | (while tmp | |
596 | (if (not (member (car tmp) '(:accessor | |
597 | :initform | |
598 | :initarg | |
599 | :documentation | |
600 | :protection | |
601 | :reader | |
602 | :writer | |
603 | :allocation | |
604 | :type | |
605 | :custom | |
606 | :label | |
607 | :group | |
608 | :printer | |
609 | :allow-nil-initform | |
610 | :custom-groups))) | |
611 | (signal 'invalid-slot-type (list (car tmp)))) | |
612 | (setq tmp (cdr (cdr tmp)))))) | |
613 | ||
614 | ;; Clean up the meaning of protection. | |
615 | (cond ((or (eq prot 'public) (eq prot :public)) (setq prot nil)) | |
616 | ((or (eq prot 'protected) (eq prot :protected)) (setq prot 'protected)) | |
617 | ((or (eq prot 'private) (eq prot :private)) (setq prot 'private)) | |
618 | ((eq prot nil) nil) | |
619 | (t (signal 'invalid-slot-type (list ':protection prot)))) | |
620 | ||
621 | ;; Make sure the :allocation parameter has a valid value. | |
622 | (if (not (or (not alloc) (eq alloc :class) (eq alloc :instance))) | |
623 | (signal 'invalid-slot-type (list ':allocation alloc))) | |
624 | ||
625 | ;; The default type specifier is supposed to be t, meaning anything. | |
626 | (if (not type) (setq type t)) | |
627 | ||
628 | ;; Label is nil, or a string | |
629 | (if (not (or (null label) (stringp label))) | |
630 | (signal 'invalid-slot-type (list ':label label))) | |
631 | ||
632 | ;; Is there an initarg, but allocation of class? | |
633 | (if (and initarg (eq alloc :class)) | |
634 | (message "Class allocated slots do not need :initarg")) | |
635 | ||
636 | ;; intern the symbol so we can use it blankly | |
637 | (if initarg (set initarg initarg)) | |
638 | ||
639 | ;; The customgroup should be a list of symbols | |
640 | (cond ((null customg) | |
641 | (setq customg '(default))) | |
642 | ((not (listp customg)) | |
643 | (setq customg (list customg)))) | |
644 | ;; The customgroup better be a symbol, or list of symbols. | |
645 | (mapc (lambda (cg) | |
646 | (if (not (symbolp cg)) | |
647 | (signal 'invalid-slot-type (list ':group cg)))) | |
648 | customg) | |
649 | ||
650 | ;; First up, add this slot into our new class. | |
651 | (eieio-add-new-slot newc name init docstr type custom label customg printer | |
652 | prot initarg alloc 'defaultoverride skip-nil) | |
653 | ||
654 | ;; We need to id the group, and store them in a group list attribute. | |
655 | (mapc (lambda (cg) (add-to-list 'groups cg)) customg) | |
656 | ||
12999ea8 | 657 | ;; Anyone can have an accessor function. This creates a function |
6dd12ef2 | 658 | ;; of the specified name, and also performs a `defsetf' if applicable |
12999ea8 | 659 | ;; so that users can `setf' the space returned by this function. |
6dd12ef2 CY |
660 | (if acces |
661 | (progn | |
9869b3ae SM |
662 | (eieio--defmethod |
663 | acces (if (eq alloc :class) :static :primary) cname | |
664 | `(lambda (this) | |
665 | ,(format | |
6dd12ef2 CY |
666 | "Retrieves the slot `%s' from an object of class `%s'" |
667 | name cname) | |
9869b3ae SM |
668 | (if (slot-boundp this ',name) |
669 | (eieio-oref this ',name) | |
6dd12ef2 | 670 | ;; Else - Some error? nil? |
67868d26 CY |
671 | nil))) |
672 | ||
12999ea8 SM |
673 | (if (fboundp 'gv-define-setter) |
674 | ;; FIXME: We should move more of eieio-defclass into the | |
675 | ;; defclass macro so we don't have to use `eval' and require | |
676 | ;; `gv' at run-time. | |
677 | (eval `(gv-define-setter ,acces (eieio--store eieio--object) | |
678 | (list 'eieio-oset eieio--object '',name | |
679 | eieio--store))) | |
680 | ;; Provide a setf method. It would be cleaner to use | |
681 | ;; defsetf, but that would require CL at runtime. | |
682 | (put acces 'setf-method | |
683 | `(lambda (widget) | |
684 | (let* ((--widget-sym-- (make-symbol "--widget--")) | |
685 | (--store-sym-- (make-symbol "--store--"))) | |
686 | (list | |
687 | (list --widget-sym--) | |
688 | (list widget) | |
689 | (list --store-sym--) | |
690 | (list 'eieio-oset --widget-sym-- '',name | |
691 | --store-sym--) | |
692 | (list 'getfoo --widget-sym--)))))))) | |
67868d26 | 693 | |
6dd12ef2 CY |
694 | ;; If a writer is defined, then create a generic method of that |
695 | ;; name whose purpose is to set the value of the slot. | |
696 | (if writer | |
9869b3ae SM |
697 | (eieio--defmethod |
698 | writer nil cname | |
699 | `(lambda (this value) | |
700 | ,(format "Set the slot `%s' of an object of class `%s'" | |
6dd12ef2 | 701 | name cname) |
9869b3ae | 702 | (setf (slot-value this ',name) value)))) |
6dd12ef2 CY |
703 | ;; If a reader is defined, then create a generic method |
704 | ;; of that name whose purpose is to access this slot value. | |
705 | (if reader | |
9869b3ae SM |
706 | (eieio--defmethod |
707 | reader nil cname | |
708 | `(lambda (this) | |
709 | ,(format "Access the slot `%s' from object of class `%s'" | |
6dd12ef2 | 710 | name cname) |
9869b3ae | 711 | (slot-value this ',name)))) |
6dd12ef2 CY |
712 | ) |
713 | (setq slots (cdr slots))) | |
714 | ||
12999ea8 SM |
715 | ;; Now that everything has been loaded up, all our lists are backwards! |
716 | ;; Fix that up now. | |
6dd12ef2 CY |
717 | (aset newc class-public-a (nreverse (aref newc class-public-a))) |
718 | (aset newc class-public-d (nreverse (aref newc class-public-d))) | |
719 | (aset newc class-public-doc (nreverse (aref newc class-public-doc))) | |
720 | (aset newc class-public-type | |
721 | (apply 'vector (nreverse (aref newc class-public-type)))) | |
722 | (aset newc class-public-custom (nreverse (aref newc class-public-custom))) | |
723 | (aset newc class-public-custom-label (nreverse (aref newc class-public-custom-label))) | |
724 | (aset newc class-public-custom-group (nreverse (aref newc class-public-custom-group))) | |
725 | (aset newc class-public-printer (nreverse (aref newc class-public-printer))) | |
726 | (aset newc class-protection (nreverse (aref newc class-protection))) | |
727 | (aset newc class-initarg-tuples (nreverse (aref newc class-initarg-tuples))) | |
728 | ||
729 | ;; The storage for class-class-allocation-type needs to be turned into | |
730 | ;; a vector now. | |
731 | (aset newc class-class-allocation-type | |
732 | (apply 'vector (aref newc class-class-allocation-type))) | |
733 | ||
734 | ;; Also, take class allocated values, and vectorize them for speed. | |
735 | (aset newc class-class-allocation-values | |
736 | (apply 'vector (aref newc class-class-allocation-values))) | |
737 | ||
738 | ;; Attach slot symbols into an obarray, and store the index of | |
739 | ;; this slot as the variable slot in this new symbol. We need to | |
740 | ;; know about primes, because obarrays are best set in vectors of | |
741 | ;; prime number length, and we also need to make our vector small | |
742 | ;; to save space, and also optimal for the number of items we have. | |
743 | (let* ((cnt 0) | |
744 | (pubsyms (aref newc class-public-a)) | |
745 | (prots (aref newc class-protection)) | |
746 | (l (length pubsyms)) | |
747 | (vl (let ((primes '( 3 5 7 11 13 17 19 23 29 31 37 41 43 47 | |
748 | 53 59 61 67 71 73 79 83 89 97 101 ))) | |
749 | (while (and primes (< (car primes) l)) | |
750 | (setq primes (cdr primes))) | |
751 | (car primes))) | |
752 | (oa (make-vector vl 0)) | |
753 | (newsym)) | |
754 | (while pubsyms | |
755 | (setq newsym (intern (symbol-name (car pubsyms)) oa)) | |
756 | (set newsym cnt) | |
757 | (setq cnt (1+ cnt)) | |
758 | (if (car prots) (put newsym 'protection (car prots))) | |
759 | (setq pubsyms (cdr pubsyms) | |
760 | prots (cdr prots))) | |
761 | (aset newc class-symbol-obarray oa) | |
762 | ) | |
763 | ||
764 | ;; Create the constructor function | |
765 | (if (class-option-assoc options :abstract) | |
766 | ;; Abstract classes cannot be instantiated. Say so. | |
767 | (let ((abs (class-option-assoc options :abstract))) | |
768 | (if (not (stringp abs)) | |
769 | (setq abs (format "Class %s is abstract" cname))) | |
770 | (fset cname | |
771 | `(lambda (&rest stuff) | |
772 | ,(format "You cannot create a new object of type %s" cname) | |
773 | (error ,abs)))) | |
774 | ||
775 | ;; Non-abstract classes need a constructor. | |
776 | (fset cname | |
777 | `(lambda (newname &rest slots) | |
778 | ,(format "Create a new object with name NAME of class type %s" cname) | |
779 | (apply 'constructor ,cname newname slots))) | |
780 | ) | |
781 | ||
782 | ;; Set up a specialized doc string. | |
783 | ;; Use stored value since it is calculated in a non-trivial way | |
784 | (put cname 'variable-documentation | |
785 | (class-option-assoc options :documentation)) | |
786 | ||
62a81506 CY |
787 | ;; Save the file location where this class is defined. |
788 | (let ((fname (if load-in-progress | |
789 | load-file-name | |
790 | buffer-file-name)) | |
791 | loc) | |
792 | (when fname | |
793 | (when (string-match "\\.elc$" fname) | |
794 | (setq fname (substring fname 0 (1- (length fname))))) | |
795 | (put cname 'class-location fname))) | |
796 | ||
6dd12ef2 CY |
797 | ;; We have a list of custom groups. Store them into the options. |
798 | (let ((g (class-option-assoc options :custom-groups))) | |
799 | (mapc (lambda (cg) (add-to-list 'g cg)) groups) | |
800 | (if (memq :custom-groups options) | |
801 | (setcar (cdr (memq :custom-groups options)) g) | |
802 | (setq options (cons :custom-groups (cons g options))))) | |
803 | ||
804 | ;; Set up the options we have collected. | |
805 | (aset newc class-options options) | |
806 | ||
807 | ;; if this is a superclass, clear out parent (which was set to the | |
808 | ;; default superclass eieio-default-superclass) | |
809 | (if clearparent (aset newc class-parent nil)) | |
810 | ||
811 | ;; Create the cached default object. | |
812 | (let ((cache (make-vector (+ (length (aref newc class-public-a)) | |
813 | 3) nil))) | |
814 | (aset cache 0 'object) | |
815 | (aset cache object-class cname) | |
816 | (aset cache object-name 'default-cache-object) | |
817 | (let ((eieio-skip-typecheck t)) | |
818 | ;; All type-checking has been done to our satisfaction | |
819 | ;; before this call. Don't waste our time in this call.. | |
820 | (eieio-set-defaults cache t)) | |
821 | (aset newc class-default-object-cache cache)) | |
822 | ||
823 | ;; Return our new class object | |
824 | ;; newc | |
825 | cname | |
826 | )) | |
827 | ||
828 | (defun eieio-perform-slot-validation-for-default (slot spec value skipnil) | |
829 | "For SLOT, signal if SPEC does not match VALUE. | |
a8f316ca | 830 | If SKIPNIL is non-nil, then if VALUE is nil return t instead." |
a2930e43 EL |
831 | (if (and (not (eieio-eval-default-p value)) |
832 | (not eieio-skip-typecheck) | |
833 | (not (and skipnil (null value))) | |
834 | (not (eieio-perform-slot-validation spec value))) | |
835 | (signal 'invalid-slot-type (list slot spec value)))) | |
6dd12ef2 CY |
836 | |
837 | (defun eieio-add-new-slot (newc a d doc type cust label custg print prot init alloc | |
838 | &optional defaultoverride skipnil) | |
839 | "Add into NEWC attribute A. | |
840 | If A already exists in NEWC, then do nothing. If it doesn't exist, | |
a8f316ca | 841 | then also add in D (default), DOC, TYPE, CUST, LABEL, CUSTG, PRINT, PROT, and INIT arg. |
6dd12ef2 CY |
842 | Argument ALLOC specifies if the slot is allocated per instance, or per class. |
843 | If optional DEFAULTOVERRIDE is non-nil, then if A exists in NEWC, | |
a8f316ca | 844 | we must override its value for a default. |
6dd12ef2 CY |
845 | Optional argument SKIPNIL indicates if type checking should be skipped |
846 | if default value is nil." | |
847 | ;; Make sure we duplicate those items that are sequences. | |
848 | (condition-case nil | |
849 | (if (sequencep d) (setq d (copy-sequence d))) | |
c7015153 | 850 | ;; This copy can fail on a cons cell with a non-cons in the cdr. Let's skip it if it doesn't work. |
6dd12ef2 CY |
851 | (error nil)) |
852 | (if (sequencep type) (setq type (copy-sequence type))) | |
853 | (if (sequencep cust) (setq cust (copy-sequence cust))) | |
854 | (if (sequencep custg) (setq custg (copy-sequence custg))) | |
855 | ||
856 | ;; To prevent override information w/out specification of storage, | |
857 | ;; we need to do this little hack. | |
858 | (if (member a (aref newc class-class-allocation-a)) (setq alloc ':class)) | |
859 | ||
860 | (if (or (not alloc) (and (symbolp alloc) (eq alloc ':instance))) | |
861 | ;; In this case, we modify the INSTANCE version of a given slot. | |
862 | ||
863 | (progn | |
864 | ||
865 | ;; Only add this element if it is so-far unique | |
866 | (if (not (member a (aref newc class-public-a))) | |
867 | (progn | |
868 | (eieio-perform-slot-validation-for-default a type d skipnil) | |
869 | (aset newc class-public-a (cons a (aref newc class-public-a))) | |
870 | (aset newc class-public-d (cons d (aref newc class-public-d))) | |
871 | (aset newc class-public-doc (cons doc (aref newc class-public-doc))) | |
872 | (aset newc class-public-type (cons type (aref newc class-public-type))) | |
873 | (aset newc class-public-custom (cons cust (aref newc class-public-custom))) | |
874 | (aset newc class-public-custom-label (cons label (aref newc class-public-custom-label))) | |
875 | (aset newc class-public-custom-group (cons custg (aref newc class-public-custom-group))) | |
876 | (aset newc class-public-printer (cons print (aref newc class-public-printer))) | |
877 | (aset newc class-protection (cons prot (aref newc class-protection))) | |
878 | (aset newc class-initarg-tuples (cons (cons init a) (aref newc class-initarg-tuples))) | |
879 | ) | |
880 | ;; When defaultoverride is true, we are usually adding new local | |
881 | ;; attributes which must override the default value of any slot | |
882 | ;; passed in by one of the parent classes. | |
883 | (when defaultoverride | |
884 | ;; There is a match, and we must override the old value. | |
885 | (let* ((ca (aref newc class-public-a)) | |
886 | (np (member a ca)) | |
887 | (num (- (length ca) (length np))) | |
888 | (dp (if np (nthcdr num (aref newc class-public-d)) | |
889 | nil)) | |
890 | (tp (if np (nth num (aref newc class-public-type)))) | |
891 | ) | |
892 | (if (not np) | |
a8f316ca | 893 | (error "EIEIO internal error overriding default value for %s" |
6dd12ef2 CY |
894 | a) |
895 | ;; If type is passed in, is it the same? | |
896 | (if (not (eq type t)) | |
897 | (if (not (equal type tp)) | |
898 | (error | |
899 | "Child slot type `%s' does not match inherited type `%s' for `%s'" | |
900 | type tp a))) | |
901 | ;; If we have a repeat, only update the initarg... | |
902 | (unless (eq d eieio-unbound) | |
903 | (eieio-perform-slot-validation-for-default a tp d skipnil) | |
904 | (setcar dp d)) | |
905 | ;; If we have a new initarg, check for it. | |
906 | (when init | |
907 | (let* ((inits (aref newc class-initarg-tuples)) | |
908 | (inita (rassq a inits))) | |
909 | ;; Replace the CAR of the associate INITA. | |
910 | ;;(message "Initarg: %S replace %s" inita init) | |
911 | (setcar inita init) | |
912 | )) | |
913 | ||
914 | ;; PLN Tue Jun 26 11:57:06 2007 : The protection is | |
915 | ;; checked and SHOULD match the superclass | |
916 | ;; protection. Otherwise an error is thrown. However | |
917 | ;; I wonder if a more flexible schedule might be | |
918 | ;; implemented. | |
919 | ;; | |
920 | ;; EML - We used to have (if prot... here, | |
921 | ;; but a prot of 'nil means public. | |
922 | ;; | |
923 | (let ((super-prot (nth num (aref newc class-protection))) | |
924 | ) | |
925 | (if (not (eq prot super-prot)) | |
926 | (error "Child slot protection `%s' does not match inherited protection `%s' for `%s'" | |
927 | prot super-prot a))) | |
928 | ;; End original PLN | |
929 | ||
930 | ;; PLN Tue Jun 26 11:57:06 2007 : | |
67868d26 CY |
931 | ;; Do a non redundant combination of ancient custom |
932 | ;; groups and new ones. | |
6dd12ef2 | 933 | (when custg |
67868d26 CY |
934 | (let* ((groups |
935 | (nthcdr num (aref newc class-public-custom-group))) | |
936 | (list1 (car groups)) | |
937 | (list2 (if (listp custg) custg (list custg)))) | |
938 | (if (< (length list1) (length list2)) | |
939 | (setq list1 (prog1 list2 (setq list2 list1)))) | |
940 | (dolist (elt list2) | |
941 | (unless (memq elt list1) | |
942 | (push elt list1))) | |
943 | (setcar groups list1))) | |
6dd12ef2 CY |
944 | ;; End PLN |
945 | ||
946 | ;; PLN Mon Jun 25 22:44:34 2007 : If a new cust is | |
947 | ;; set, simply replaces the old one. | |
948 | (when cust | |
949 | ;; (message "Custom type redefined to %s" cust) | |
950 | (setcar (nthcdr num (aref newc class-public-custom)) cust)) | |
951 | ||
952 | ;; If a new label is specified, it simply replaces | |
953 | ;; the old one. | |
954 | (when label | |
955 | ;; (message "Custom label redefined to %s" label) | |
956 | (setcar (nthcdr num (aref newc class-public-custom-label)) label)) | |
957 | ;; End PLN | |
958 | ||
959 | ;; PLN Sat Jun 30 17:24:42 2007 : when a new | |
960 | ;; doc is specified, simply replaces the old one. | |
961 | (when doc | |
962 | ;;(message "Documentation redefined to %s" doc) | |
963 | (setcar (nthcdr num (aref newc class-public-doc)) | |
964 | doc)) | |
965 | ;; End PLN | |
966 | ||
967 | ;; If a new printer is specified, it simply replaces | |
968 | ;; the old one. | |
969 | (when print | |
970 | ;; (message "printer redefined to %s" print) | |
971 | (setcar (nthcdr num (aref newc class-public-printer)) print)) | |
972 | ||
973 | ))) | |
974 | )) | |
975 | ||
976 | ;; CLASS ALLOCATED SLOTS | |
977 | (let ((value (eieio-default-eval-maybe d))) | |
978 | (if (not (member a (aref newc class-class-allocation-a))) | |
979 | (progn | |
980 | (eieio-perform-slot-validation-for-default a type value skipnil) | |
981 | ;; Here we have found a :class version of a slot. This | |
91af3942 | 982 | ;; requires a very different approach. |
6dd12ef2 CY |
983 | (aset newc class-class-allocation-a (cons a (aref newc class-class-allocation-a))) |
984 | (aset newc class-class-allocation-doc (cons doc (aref newc class-class-allocation-doc))) | |
985 | (aset newc class-class-allocation-type (cons type (aref newc class-class-allocation-type))) | |
986 | (aset newc class-class-allocation-custom (cons cust (aref newc class-class-allocation-custom))) | |
987 | (aset newc class-class-allocation-custom-label (cons label (aref newc class-class-allocation-custom-label))) | |
988 | (aset newc class-class-allocation-custom-group (cons custg (aref newc class-class-allocation-custom-group))) | |
989 | (aset newc class-class-allocation-protection (cons prot (aref newc class-class-allocation-protection))) | |
990 | ;; Default value is stored in the 'values section, since new objects | |
991 | ;; can't initialize from this element. | |
992 | (aset newc class-class-allocation-values (cons value (aref newc class-class-allocation-values)))) | |
993 | (when defaultoverride | |
994 | ;; There is a match, and we must override the old value. | |
995 | (let* ((ca (aref newc class-class-allocation-a)) | |
996 | (np (member a ca)) | |
997 | (num (- (length ca) (length np))) | |
998 | (dp (if np | |
999 | (nthcdr num | |
1000 | (aref newc class-class-allocation-values)) | |
1001 | nil)) | |
1002 | (tp (if np (nth num (aref newc class-class-allocation-type)) | |
1003 | nil))) | |
1004 | (if (not np) | |
a8f316ca | 1005 | (error "EIEIO internal error overriding default value for %s" |
6dd12ef2 CY |
1006 | a) |
1007 | ;; If type is passed in, is it the same? | |
1008 | (if (not (eq type t)) | |
1009 | (if (not (equal type tp)) | |
1010 | (error | |
1011 | "Child slot type `%s' does not match inherited type `%s' for `%s'" | |
1012 | type tp a))) | |
1013 | ;; EML - Note: the only reason to override a class bound slot | |
1014 | ;; is to change the default, so allow unbound in. | |
1015 | ||
ee7683eb | 1016 | ;; If we have a repeat, only update the value... |
6dd12ef2 CY |
1017 | (eieio-perform-slot-validation-for-default a tp value skipnil) |
1018 | (setcar dp value)) | |
1019 | ||
1020 | ;; PLN Tue Jun 26 11:57:06 2007 : The protection is | |
1021 | ;; checked and SHOULD match the superclass | |
1022 | ;; protection. Otherwise an error is thrown. However | |
1023 | ;; I wonder if a more flexible schedule might be | |
1024 | ;; implemented. | |
1025 | (let ((super-prot | |
1026 | (car (nthcdr num (aref newc class-class-allocation-protection))))) | |
1027 | (if (not (eq prot super-prot)) | |
1028 | (error "Child slot protection `%s' does not match inherited protection `%s' for `%s'" | |
1029 | prot super-prot a))) | |
67868d26 CY |
1030 | ;; Do a non redundant combination of ancient custom groups |
1031 | ;; and new ones. | |
6dd12ef2 | 1032 | (when custg |
67868d26 CY |
1033 | (let* ((groups |
1034 | (nthcdr num (aref newc class-class-allocation-custom-group))) | |
1035 | (list1 (car groups)) | |
1036 | (list2 (if (listp custg) custg (list custg)))) | |
1037 | (if (< (length list1) (length list2)) | |
1038 | (setq list1 (prog1 list2 (setq list2 list1)))) | |
1039 | (dolist (elt list2) | |
1040 | (unless (memq elt list1) | |
1041 | (push elt list1))) | |
1042 | (setcar groups list1))) | |
6dd12ef2 CY |
1043 | |
1044 | ;; PLN Sat Jun 30 17:24:42 2007 : when a new | |
1045 | ;; doc is specified, simply replaces the old one. | |
1046 | (when doc | |
1047 | ;;(message "Documentation redefined to %s" doc) | |
1048 | (setcar (nthcdr num (aref newc class-class-allocation-doc)) | |
1049 | doc)) | |
1050 | ;; End PLN | |
1051 | ||
1052 | ;; If a new printer is specified, it simply replaces | |
1053 | ;; the old one. | |
1054 | (when print | |
1055 | ;; (message "printer redefined to %s" print) | |
1056 | (setcar (nthcdr num (aref newc class-class-allocation-printer)) print)) | |
1057 | ||
1058 | )) | |
1059 | )) | |
1060 | )) | |
1061 | ||
1062 | (defun eieio-copy-parents-into-subclass (newc parents) | |
1063 | "Copy into NEWC the slots of PARENTS. | |
9ffe3f52 | 1064 | Follow the rules of not overwriting early parents when applying to |
6dd12ef2 CY |
1065 | the new child class." |
1066 | (let ((ps (aref newc class-parent)) | |
1067 | (sn (class-option-assoc (aref newc class-options) | |
1068 | ':allow-nil-initform))) | |
1069 | (while ps | |
1070 | ;; First, duplicate all the slots of the parent. | |
1071 | (let ((pcv (class-v (car ps)))) | |
1072 | (let ((pa (aref pcv class-public-a)) | |
1073 | (pd (aref pcv class-public-d)) | |
1074 | (pdoc (aref pcv class-public-doc)) | |
1075 | (ptype (aref pcv class-public-type)) | |
1076 | (pcust (aref pcv class-public-custom)) | |
1077 | (plabel (aref pcv class-public-custom-label)) | |
1078 | (pcustg (aref pcv class-public-custom-group)) | |
1079 | (printer (aref pcv class-public-printer)) | |
1080 | (pprot (aref pcv class-protection)) | |
1081 | (pinit (aref pcv class-initarg-tuples)) | |
1082 | (i 0)) | |
1083 | (while pa | |
1084 | (eieio-add-new-slot newc | |
1085 | (car pa) (car pd) (car pdoc) (aref ptype i) | |
1086 | (car pcust) (car plabel) (car pcustg) | |
1087 | (car printer) | |
1088 | (car pprot) (car-safe (car pinit)) nil nil sn) | |
1089 | ;; Increment each value. | |
1090 | (setq pa (cdr pa) | |
1091 | pd (cdr pd) | |
1092 | pdoc (cdr pdoc) | |
1093 | i (1+ i) | |
1094 | pcust (cdr pcust) | |
1095 | plabel (cdr plabel) | |
1096 | pcustg (cdr pcustg) | |
1097 | printer (cdr printer) | |
1098 | pprot (cdr pprot) | |
1099 | pinit (cdr pinit)) | |
1100 | )) ;; while/let | |
1101 | ;; Now duplicate all the class alloc slots. | |
1102 | (let ((pa (aref pcv class-class-allocation-a)) | |
1103 | (pdoc (aref pcv class-class-allocation-doc)) | |
1104 | (ptype (aref pcv class-class-allocation-type)) | |
1105 | (pcust (aref pcv class-class-allocation-custom)) | |
1106 | (plabel (aref pcv class-class-allocation-custom-label)) | |
1107 | (pcustg (aref pcv class-class-allocation-custom-group)) | |
1108 | (printer (aref pcv class-class-allocation-printer)) | |
1109 | (pprot (aref pcv class-class-allocation-protection)) | |
1110 | (pval (aref pcv class-class-allocation-values)) | |
1111 | (i 0)) | |
1112 | (while pa | |
1113 | (eieio-add-new-slot newc | |
1114 | (car pa) (aref pval i) (car pdoc) (aref ptype i) | |
1115 | (car pcust) (car plabel) (car pcustg) | |
1116 | (car printer) | |
1117 | (car pprot) nil ':class sn) | |
1118 | ;; Increment each value. | |
1119 | (setq pa (cdr pa) | |
1120 | pdoc (cdr pdoc) | |
1121 | pcust (cdr pcust) | |
1122 | plabel (cdr plabel) | |
1123 | pcustg (cdr pcustg) | |
1124 | printer (cdr printer) | |
1125 | pprot (cdr pprot) | |
1126 | i (1+ i)) | |
1127 | ))) ;; while/let | |
1128 | ;; Loop over each parent class | |
1129 | (setq ps (cdr ps))) | |
1130 | )) | |
1131 | ||
1132 | ;;; CLOS style implementation of object creators. | |
1133 | ;; | |
1134 | (defun make-instance (class &rest initargs) | |
1135 | "Make a new instance of CLASS based on INITARGS. | |
1136 | CLASS is a class symbol. For example: | |
1137 | ||
1138 | (make-instance 'foo) | |
1139 | ||
1140 | INITARGS is a property list with keywords based on the :initarg | |
1141 | for each slot. For example: | |
1142 | ||
1143 | (make-instance 'foo :slot1 value1 :slotN valueN) | |
1144 | ||
9ffe3f52 | 1145 | Compatibility note: |
6dd12ef2 CY |
1146 | |
1147 | If the first element of INITARGS is a string, it is used as the | |
1148 | name of the class. | |
1149 | ||
1150 | In EIEIO, the class' constructor requires a name for use when printing. | |
1151 | `make-instance' in CLOS doesn't use names the way Emacs does, so the | |
1152 | class is used as the name slot instead when INITARGS doesn't start with | |
1153 | a string." | |
1154 | (if (and (car initargs) (stringp (car initargs))) | |
1155 | (apply (class-constructor class) initargs) | |
1156 | (apply (class-constructor class) | |
1157 | (cond ((symbolp class) (symbol-name class)) | |
1158 | (t (format "%S" class))) | |
1159 | initargs))) | |
1160 | ||
1161 | \f | |
1162 | ;;; CLOS methods and generics | |
1163 | ;; | |
d1dc2cc2 SM |
1164 | |
1165 | (put 'eieio--defalias 'byte-hunk-handler | |
1166 | #'byte-compile-file-form-defalias) ;;(get 'defalias 'byte-hunk-handler) | |
1167 | (defun eieio--defalias (name body) | |
1168 | "Like `defalias', but with less side-effects. | |
1169 | More specifically, it has no side-effects at all when the new function | |
1170 | definition is the same (`eq') as the old one." | |
1171 | (unless (and (fboundp name) | |
1172 | (eq (symbol-function name) body)) | |
1173 | (defalias name body))) | |
1174 | ||
6dd12ef2 | 1175 | (defmacro defgeneric (method args &optional doc-string) |
a8f316ca | 1176 | "Create a generic function METHOD. |
6dd12ef2 | 1177 | DOC-STRING is the base documentation for this class. A generic |
a8f316ca JB |
1178 | function has no body, as its purpose is to decide which method body |
1179 | is appropriate to use. Uses `defmethod' to create methods, and calls | |
1180 | `defgeneric' for you. With this implementation the ARGS are | |
6dd12ef2 CY |
1181 | currently ignored. You can use `defgeneric' to apply specialized |
1182 | top level documentation to a method." | |
d1dc2cc2 SM |
1183 | `(eieio--defalias ',method |
1184 | (eieio--defgeneric-init-form ',method ,doc-string))) | |
1185 | ||
1186 | (defun eieio--defgeneric-init-form (method doc-string) | |
1187 | "Form to use for the initial definition of a generic." | |
1188 | (cond | |
1189 | ((or (not (fboundp method)) | |
1190 | (eq 'autoload (car-safe (symbol-function method)))) | |
1191 | ;; Make sure the method tables are installed. | |
1192 | (eieiomt-install method) | |
1193 | ;; Construct the actual body of this function. | |
1194 | (eieio-defgeneric-form method doc-string)) | |
1195 | ((generic-p method) (symbol-function method)) ;Leave it as-is. | |
1196 | (t (error "You cannot create a generic/method over an existing symbol: %s" | |
1197 | method)))) | |
6dd12ef2 CY |
1198 | |
1199 | (defun eieio-defgeneric-form (method doc-string) | |
1200 | "The lambda form that would be used as the function defined on METHOD. | |
1201 | All methods should call the same EIEIO function for dispatch. | |
1202 | DOC-STRING is the documentation attached to METHOD." | |
1203 | `(lambda (&rest local-args) | |
1204 | ,doc-string | |
1205 | (eieio-generic-call (quote ,method) local-args))) | |
1206 | ||
1207 | (defsubst eieio-defgeneric-reset-generic-form (method) | |
1208 | "Setup METHOD to call the generic form." | |
1209 | (let ((doc-string (documentation method))) | |
1210 | (fset method (eieio-defgeneric-form method doc-string)))) | |
1211 | ||
1212 | (defun eieio-defgeneric-form-primary-only (method doc-string) | |
1213 | "The lambda form that would be used as the function defined on METHOD. | |
1214 | All methods should call the same EIEIO function for dispatch. | |
1215 | DOC-STRING is the documentation attached to METHOD." | |
1216 | `(lambda (&rest local-args) | |
1217 | ,doc-string | |
1218 | (eieio-generic-call-primary-only (quote ,method) local-args))) | |
1219 | ||
1220 | (defsubst eieio-defgeneric-reset-generic-form-primary-only (method) | |
1221 | "Setup METHOD to call the generic form." | |
1222 | (let ((doc-string (documentation method))) | |
1223 | (fset method (eieio-defgeneric-form-primary-only method doc-string)))) | |
1224 | ||
1225 | (defun eieio-defgeneric-form-primary-only-one (method doc-string | |
1226 | class | |
1227 | impl | |
1228 | ) | |
1229 | "The lambda form that would be used as the function defined on METHOD. | |
1230 | All methods should call the same EIEIO function for dispatch. | |
1231 | DOC-STRING is the documentation attached to METHOD. | |
1232 | CLASS is the class symbol needed for private method access. | |
1233 | IMPL is the symbol holding the method implementation." | |
1234 | ;; NOTE: I tried out byte compiling this little fcn. Turns out it | |
1235 | ;; is faster to execute this for not byte-compiled. ie, install this, | |
1236 | ;; then measure calls going through here. I wonder why. | |
1237 | (require 'bytecomp) | |
3e21b6a7 SM |
1238 | (let ((byte-compile-warnings nil)) |
1239 | (byte-compile | |
6dd12ef2 CY |
1240 | `(lambda (&rest local-args) |
1241 | ,doc-string | |
1242 | ;; This is a cool cheat. Usually we need to look up in the | |
1243 | ;; method table to find out if there is a method or not. We can | |
1244 | ;; instead make that determination at load time when there is | |
1245 | ;; only one method. If the first arg is not a child of the class | |
1246 | ;; of that one implementation, then clearly, there is no method def. | |
1247 | (if (not (eieio-object-p (car local-args))) | |
1248 | ;; Not an object. Just signal. | |
3e21b6a7 | 1249 | (signal 'no-method-definition |
b1ef1257 | 1250 | (list ',method local-args)) |
6dd12ef2 CY |
1251 | |
1252 | ;; We do have an object. Make sure it is the right type. | |
1253 | (if ,(if (eq class eieio-default-superclass) | |
b1ef1257 | 1254 | nil ; default superclass means just an obj. Already asked. |
6dd12ef2 | 1255 | `(not (child-of-class-p (aref (car local-args) object-class) |
b1ef1257 | 1256 | ',class))) |
6dd12ef2 CY |
1257 | |
1258 | ;; If not the right kind of object, call no applicable | |
1259 | (apply 'no-applicable-method (car local-args) | |
b1ef1257 | 1260 | ',method local-args) |
6dd12ef2 CY |
1261 | |
1262 | ;; It is ok, do the call. | |
1263 | ;; Fill in inter-call variables then evaluate the method. | |
b1ef1257 | 1264 | (let ((scoped-class ',class) |
6dd12ef2 CY |
1265 | (eieio-generic-call-next-method-list nil) |
1266 | (eieio-generic-call-key method-primary) | |
b1ef1257 | 1267 | (eieio-generic-call-methodname ',method) |
6dd12ef2 CY |
1268 | (eieio-generic-call-arglst local-args) |
1269 | ) | |
62a81506 CY |
1270 | ,(if (< emacs-major-version 24) |
1271 | `(apply ,(list 'quote impl) local-args) | |
1272 | `(apply #',impl local-args)) | |
1273 | ;(,impl local-args) | |
3e21b6a7 | 1274 | ))))))) |
6dd12ef2 CY |
1275 | |
1276 | (defsubst eieio-defgeneric-reset-generic-form-primary-only-one (method) | |
1277 | "Setup METHOD to call the generic form." | |
1278 | (let* ((doc-string (documentation method)) | |
1279 | (M (get method 'eieio-method-tree)) | |
1280 | (entry (car (aref M method-primary))) | |
1281 | ) | |
1282 | (fset method (eieio-defgeneric-form-primary-only-one | |
1283 | method doc-string | |
1284 | (car entry) | |
1285 | (cdr entry) | |
1286 | )))) | |
1287 | ||
6dd12ef2 | 1288 | (defun eieio-unbind-method-implementations (method) |
a8f316ca JB |
1289 | "Make the generic method METHOD have no implementations. |
1290 | It will leave the original generic function in place, | |
1291 | but remove reference to all implementations of METHOD." | |
6dd12ef2 CY |
1292 | (put method 'eieio-method-tree nil) |
1293 | (put method 'eieio-method-obarray nil)) | |
1294 | ||
1295 | (defmacro defmethod (method &rest args) | |
1296 | "Create a new METHOD through `defgeneric' with ARGS. | |
1297 | ||
a8f316ca | 1298 | The optional second argument KEY is a specifier that |
6dd12ef2 | 1299 | modifies how the method is called, including: |
a8f316ca JB |
1300 | :before - Method will be called before the :primary |
1301 | :primary - The default if not specified | |
1302 | :after - Method will be called after the :primary | |
1303 | :static - First arg could be an object or class | |
6dd12ef2 CY |
1304 | The next argument is the ARGLIST. The ARGLIST specifies the arguments |
1305 | to the method as with `defun'. The first argument can have a type | |
1306 | specifier, such as: | |
1307 | ((VARNAME CLASS) ARG2 ...) | |
1308 | where VARNAME is the name of the local variable for the method being | |
1309 | created. The CLASS is a class symbol for a class made with `defclass'. | |
1310 | A DOCSTRING comes after the ARGLIST, and is optional. | |
1311 | All the rest of the args are the BODY of the method. A method will | |
1312 | return the value of the last form in the BODY. | |
1313 | ||
1314 | Summary: | |
1315 | ||
1316 | (defmethod mymethod [:before | :primary | :after | :static] | |
1317 | ((typearg class-name) arg2 &optional opt &rest rest) | |
1318 | \"doc-string\" | |
1319 | body)" | |
9869b3ae | 1320 | (let* ((key (if (keywordp (car args)) (pop args))) |
876c194c | 1321 | (params (car args)) |
876c194c | 1322 | (arg1 (car params)) |
c4662635 | 1323 | (fargs (if (consp arg1) |
d1dc2cc2 SM |
1324 | (cons (car arg1) (cdr params)) |
1325 | params)) | |
1326 | (class (if (consp arg1) (nth 1 arg1))) | |
c4662635 | 1327 | (code `(lambda ,fargs ,@(cdr args)))) |
d1dc2cc2 SM |
1328 | `(progn |
1329 | ;; Make sure there is a generic and the byte-compiler sees it. | |
1330 | (defgeneric ,method ,args | |
1331 | ,(or (documentation code) | |
1332 | (format "Generically created method `%s'." method))) | |
31d55be9 | 1333 | (eieio--defmethod ',method ',key ',class #',code)))) |
9869b3ae SM |
1334 | |
1335 | (defun eieio--defmethod (method kind argclass code) | |
6dd12ef2 | 1336 | "Work part of the `defmethod' macro defining METHOD with ARGS." |
9869b3ae | 1337 | (let ((key |
93b6b5e1 | 1338 | ;; find optional keys |
9869b3ae SM |
1339 | (cond ((or (eq ':BEFORE kind) |
1340 | (eq ':before kind)) | |
93b6b5e1 | 1341 | method-before) |
9869b3ae SM |
1342 | ((or (eq ':AFTER kind) |
1343 | (eq ':after kind)) | |
93b6b5e1 | 1344 | method-after) |
9869b3ae SM |
1345 | ((or (eq ':PRIMARY kind) |
1346 | (eq ':primary kind)) | |
93b6b5e1 | 1347 | method-primary) |
9869b3ae SM |
1348 | ((or (eq ':STATIC kind) |
1349 | (eq ':static kind)) | |
93b6b5e1 SM |
1350 | method-static) |
1351 | ;; Primary key | |
9869b3ae | 1352 | (t method-primary)))) |
d1dc2cc2 SM |
1353 | ;; Make sure there is a generic (when called from defclass). |
1354 | (eieio--defalias | |
1355 | method (eieio--defgeneric-init-form | |
1356 | method (or (documentation code) | |
1357 | (format "Generically created method `%s'." method)))) | |
6dd12ef2 CY |
1358 | ;; create symbol for property to bind to. If the first arg is of |
1359 | ;; the form (varname vartype) and `vartype' is a class, then | |
1360 | ;; that class will be the type symbol. If not, then it will fall | |
1361 | ;; under the type `primary' which is a non-specific calling of the | |
1362 | ;; function. | |
9869b3ae | 1363 | (if argclass |
93b6b5e1 SM |
1364 | (if (not (class-p argclass)) |
1365 | (error "Unknown class type %s in method parameters" | |
9869b3ae | 1366 | argclass)) |
6dd12ef2 CY |
1367 | (if (= key -1) |
1368 | (signal 'wrong-type-argument (list :static 'non-class-arg))) | |
1369 | ;; generics are higher | |
a2930e43 | 1370 | (setq key (eieio-specialized-key-to-generic-key key))) |
6dd12ef2 | 1371 | ;; Put this lambda into the symbol so we can find it |
876c194c | 1372 | (eieiomt-add method code key argclass) |
6dd12ef2 CY |
1373 | ) |
1374 | ||
1375 | (when eieio-optimize-primary-methods-flag | |
1376 | ;; Optimizing step: | |
1377 | ;; | |
1378 | ;; If this method, after this setup, only has primary methods, then | |
1379 | ;; we can setup the generic that way. | |
1380 | (if (generic-primary-only-p method) | |
1381 | ;; If there is only one primary method, then we can go one more | |
1382 | ;; optimization step. | |
1383 | (if (generic-primary-only-one-p method) | |
1384 | (eieio-defgeneric-reset-generic-form-primary-only-one method) | |
1385 | (eieio-defgeneric-reset-generic-form-primary-only method)) | |
1386 | (eieio-defgeneric-reset-generic-form method))) | |
1387 | ||
1388 | method) | |
1389 | ||
1390 | ;;; Slot type validation | |
67868d26 CY |
1391 | |
1392 | ;; This is a hideous hack for replacing `typep' from cl-macs, to avoid | |
1393 | ;; requiring the CL library at run-time. It can be eliminated if/when | |
1394 | ;; `typep' is merged into Emacs core. | |
1395 | (defun eieio--typep (val type) | |
1396 | (if (symbolp type) | |
1397 | (cond ((get type 'cl-deftype-handler) | |
1398 | (eieio--typep val (funcall (get type 'cl-deftype-handler)))) | |
1399 | ((eq type t) t) | |
1400 | ((eq type 'null) (null val)) | |
1401 | ((eq type 'atom) (atom val)) | |
1402 | ((eq type 'float) (and (numberp val) (not (integerp val)))) | |
1403 | ((eq type 'real) (numberp val)) | |
1404 | ((eq type 'fixnum) (integerp val)) | |
1405 | ((memq type '(character string-char)) (characterp val)) | |
1406 | (t | |
1407 | (let* ((name (symbol-name type)) | |
1408 | (namep (intern (concat name "p")))) | |
1409 | (if (fboundp namep) | |
1410 | (funcall `(lambda () (,namep val))) | |
1411 | (funcall `(lambda () | |
1412 | (,(intern (concat name "-p")) val))))))) | |
1413 | (cond ((get (car type) 'cl-deftype-handler) | |
1414 | (eieio--typep val (apply (get (car type) 'cl-deftype-handler) | |
1415 | (cdr type)))) | |
1416 | ((memq (car type) '(integer float real number)) | |
1417 | (and (eieio--typep val (car type)) | |
1418 | (or (memq (cadr type) '(* nil)) | |
1419 | (if (consp (cadr type)) | |
1420 | (> val (car (cadr type))) | |
1421 | (>= val (cadr type)))) | |
1422 | (or (memq (caddr type) '(* nil)) | |
1423 | (if (consp (car (cddr type))) | |
1424 | (< val (caar (cddr type))) | |
1425 | (<= val (car (cddr type))))))) | |
1426 | ((memq (car type) '(and or not)) | |
1427 | (eval (cons (car type) | |
1428 | (mapcar (lambda (x) | |
1429 | `(eieio--typep (quote ,val) (quote ,x))) | |
1430 | (cdr type))))) | |
1431 | ((memq (car type) '(member member*)) | |
1432 | (memql val (cdr type))) | |
1433 | ((eq (car type) 'satisfies) | |
1434 | (funcall `(lambda () (,(cadr type) val)))) | |
1435 | (t (error "Bad type spec: %s" type))))) | |
1436 | ||
6dd12ef2 CY |
1437 | (defun eieio-perform-slot-validation (spec value) |
1438 | "Return non-nil if SPEC does not match VALUE." | |
6dd12ef2 CY |
1439 | (or (eq spec t) ; t always passes |
1440 | (eq value eieio-unbound) ; unbound always passes | |
67868d26 | 1441 | (eieio--typep value spec))) |
6dd12ef2 CY |
1442 | |
1443 | (defun eieio-validate-slot-value (class slot-idx value slot) | |
a8f316ca | 1444 | "Make sure that for CLASS referencing SLOT-IDX, VALUE is valid. |
6dd12ef2 CY |
1445 | Checks the :type specifier. |
1446 | SLOT is the slot that is being checked, and is only used when throwing | |
a8f316ca | 1447 | an error." |
6dd12ef2 CY |
1448 | (if eieio-skip-typecheck |
1449 | nil | |
1450 | ;; Trim off object IDX junk added in for the object index. | |
1451 | (setq slot-idx (- slot-idx 3)) | |
1452 | (let ((st (aref (aref (class-v class) class-public-type) slot-idx))) | |
1453 | (if (not (eieio-perform-slot-validation st value)) | |
1454 | (signal 'invalid-slot-type (list class slot st value)))))) | |
1455 | ||
1456 | (defun eieio-validate-class-slot-value (class slot-idx value slot) | |
a8f316ca | 1457 | "Make sure that for CLASS referencing SLOT-IDX, VALUE is valid. |
6dd12ef2 CY |
1458 | Checks the :type specifier. |
1459 | SLOT is the slot that is being checked, and is only used when throwing | |
a8f316ca | 1460 | an error." |
6dd12ef2 CY |
1461 | (if eieio-skip-typecheck |
1462 | nil | |
1463 | (let ((st (aref (aref (class-v class) class-class-allocation-type) | |
1464 | slot-idx))) | |
1465 | (if (not (eieio-perform-slot-validation st value)) | |
1466 | (signal 'invalid-slot-type (list class slot st value)))))) | |
1467 | ||
1468 | (defun eieio-barf-if-slot-unbound (value instance slotname fn) | |
1469 | "Throw a signal if VALUE is a representation of an UNBOUND slot. | |
1470 | INSTANCE is the object being referenced. SLOTNAME is the offending | |
1471 | slot. If the slot is ok, return VALUE. | |
1472 | Argument FN is the function calling this verifier." | |
1473 | (if (and (eq value eieio-unbound) (not eieio-skip-typecheck)) | |
1474 | (slot-unbound instance (object-class instance) slotname fn) | |
1475 | value)) | |
1476 | ||
6dd12ef2 CY |
1477 | ;;; Get/Set slots in an object. |
1478 | ;; | |
1479 | (defmacro oref (obj slot) | |
1480 | "Retrieve the value stored in OBJ in the slot named by SLOT. | |
1481 | Slot is the name of the slot when created by `defclass' or the label | |
1482 | created by the :initarg tag." | |
1483 | `(eieio-oref ,obj (quote ,slot))) | |
1484 | ||
1485 | (defun eieio-oref (obj slot) | |
1486 | "Return the value in OBJ at SLOT in the object vector." | |
1487 | (if (not (or (eieio-object-p obj) (class-p obj))) | |
1488 | (signal 'wrong-type-argument (list '(or eieio-object-p class-p) obj))) | |
1489 | (if (not (symbolp slot)) | |
1490 | (signal 'wrong-type-argument (list 'symbolp slot))) | |
1491 | (if (class-p obj) (eieio-class-un-autoload obj)) | |
1492 | (let* ((class (if (class-p obj) obj (aref obj object-class))) | |
1493 | (c (eieio-slot-name-index class obj slot))) | |
1494 | (if (not c) | |
1495 | ;; It might be missing because it is a :class allocated slot. | |
c7015153 | 1496 | ;; Let's check that info out. |
6dd12ef2 CY |
1497 | (if (setq c (eieio-class-slot-name-index class slot)) |
1498 | ;; Oref that slot. | |
1499 | (aref (aref (class-v class) class-class-allocation-values) c) | |
1500 | ;; The slot-missing method is a cool way of allowing an object author | |
1501 | ;; to intercept missing slot definitions. Since it is also the LAST | |
a8f316ca | 1502 | ;; thing called in this fn, its return value would be retrieved. |
6dd12ef2 CY |
1503 | (slot-missing obj slot 'oref) |
1504 | ;;(signal 'invalid-slot-name (list (object-name obj) slot)) | |
1505 | ) | |
1506 | (if (not (eieio-object-p obj)) | |
1507 | (signal 'wrong-type-argument (list 'eieio-object-p obj))) | |
1508 | (eieio-barf-if-slot-unbound (aref obj c) obj slot 'oref)))) | |
1509 | ||
1510 | (defalias 'slot-value 'eieio-oref) | |
1511 | (defalias 'set-slot-value 'eieio-oset) | |
1512 | ||
1513 | (defmacro oref-default (obj slot) | |
a8f316ca | 1514 | "Get the default value of OBJ (maybe a class) for SLOT. |
6dd12ef2 CY |
1515 | The default value is the value installed in a class with the :initform |
1516 | tag. SLOT can be the slot name, or the tag specified by the :initarg | |
1517 | tag in the `defclass' call." | |
1518 | `(eieio-oref-default ,obj (quote ,slot))) | |
1519 | ||
1520 | (defun eieio-oref-default (obj slot) | |
a8f316ca JB |
1521 | "Do the work for the macro `oref-default' with similar parameters. |
1522 | Fills in OBJ's SLOT with its default value." | |
6dd12ef2 CY |
1523 | (if (not (or (eieio-object-p obj) (class-p obj))) (signal 'wrong-type-argument (list 'eieio-object-p obj))) |
1524 | (if (not (symbolp slot)) (signal 'wrong-type-argument (list 'symbolp slot))) | |
1525 | (let* ((cl (if (eieio-object-p obj) (aref obj object-class) obj)) | |
1526 | (c (eieio-slot-name-index cl obj slot))) | |
1527 | (if (not c) | |
1528 | ;; It might be missing because it is a :class allocated slot. | |
c7015153 | 1529 | ;; Let's check that info out. |
6dd12ef2 CY |
1530 | (if (setq c |
1531 | (eieio-class-slot-name-index cl slot)) | |
1532 | ;; Oref that slot. | |
1533 | (aref (aref (class-v cl) class-class-allocation-values) | |
1534 | c) | |
1535 | (slot-missing obj slot 'oref-default) | |
1536 | ;;(signal 'invalid-slot-name (list (class-name cl) slot)) | |
1537 | ) | |
1538 | (eieio-barf-if-slot-unbound | |
1539 | (let ((val (nth (- c 3) (aref (class-v cl) class-public-d)))) | |
1540 | (eieio-default-eval-maybe val)) | |
1541 | obj cl 'oref-default)))) | |
1542 | ||
a2930e43 EL |
1543 | (defsubst eieio-eval-default-p (val) |
1544 | "Whether the default value VAL should be evaluated for use." | |
1545 | (and (consp val) (symbolp (car val)) (fboundp (car val)))) | |
1546 | ||
6dd12ef2 CY |
1547 | (defun eieio-default-eval-maybe (val) |
1548 | "Check VAL, and return what `oref-default' would provide." | |
a2930e43 EL |
1549 | (cond |
1550 | ;; Is it a function call? If so, evaluate it. | |
1551 | ((eieio-eval-default-p val) | |
1552 | (eval val)) | |
1553 | ;;;; check for quoted things, and unquote them | |
1554 | ;;((and (consp val) (eq (car val) 'quote)) | |
1555 | ;; (car (cdr val))) | |
1556 | ;; return it verbatim | |
1557 | (t val))) | |
6dd12ef2 | 1558 | |
6dd12ef2 CY |
1559 | ;;; Handy CLOS macros |
1560 | ;; | |
1561 | (defmacro with-slots (spec-list object &rest body) | |
1562 | "Bind SPEC-LIST lexically to slot values in OBJECT, and execute BODY. | |
1563 | This establishes a lexical environment for referring to the slots in | |
1564 | the instance named by the given slot-names as though they were | |
1565 | variables. Within such a context the value of the slot can be | |
1566 | specified by using its slot name, as if it were a lexically bound | |
1567 | variable. Both setf and setq can be used to set the value of the | |
1568 | slot. | |
1569 | ||
1570 | SPEC-LIST is of a form similar to `let'. For example: | |
1571 | ||
1572 | ((VAR1 SLOT1) | |
1573 | SLOT2 | |
1574 | SLOTN | |
1575 | (VARN+1 SLOTN+1)) | |
1576 | ||
1577 | Where each VAR is the local variable given to the associated | |
a8f316ca | 1578 | SLOT. A slot specified without a variable name is given a |
6dd12ef2 | 1579 | variable name of the same name as the slot." |
f291fe60 | 1580 | (declare (indent 2)) |
6dd12ef2 CY |
1581 | ;; Transform the spec-list into a symbol-macrolet spec-list. |
1582 | (let ((mappings (mapcar (lambda (entry) | |
1583 | (let ((var (if (listp entry) (car entry) entry)) | |
1584 | (slot (if (listp entry) (cadr entry) entry))) | |
1585 | (list var `(slot-value ,object ',slot)))) | |
1586 | spec-list))) | |
1587 | (append (list 'symbol-macrolet mappings) | |
1588 | body))) | |
6dd12ef2 CY |
1589 | \f |
1590 | ;;; Simple generators, and query functions. None of these would do | |
1591 | ;; well embedded into an object. | |
1592 | ;; | |
1593 | (defmacro object-class-fast (obj) "Return the class struct defining OBJ with no check." | |
1594 | `(aref ,obj object-class)) | |
1595 | ||
1596 | (defun class-name (class) "Return a Lisp like symbol name for CLASS." | |
1597 | (if (not (class-p class)) (signal 'wrong-type-argument (list 'class-p class))) | |
1598 | ;; I think this is supposed to return a symbol, but to me CLASS is a symbol, | |
1599 | ;; and I wanted a string. Arg! | |
1600 | (format "#<class %s>" (symbol-name class))) | |
1601 | ||
1602 | (defun object-name (obj &optional extra) | |
1603 | "Return a Lisp like symbol string for object OBJ. | |
1604 | If EXTRA, include that in the string returned to represent the symbol." | |
1605 | (if (not (eieio-object-p obj)) (signal 'wrong-type-argument (list 'eieio-object-p obj))) | |
1606 | (format "#<%s %s%s>" (symbol-name (object-class-fast obj)) | |
1607 | (aref obj object-name) (or extra ""))) | |
1608 | ||
1609 | (defun object-name-string (obj) "Return a string which is OBJ's name." | |
1610 | (if (not (eieio-object-p obj)) (signal 'wrong-type-argument (list 'eieio-object-p obj))) | |
1611 | (aref obj object-name)) | |
1612 | ||
1613 | (defun object-set-name-string (obj name) "Set the string which is OBJ's NAME." | |
1614 | (if (not (eieio-object-p obj)) (signal 'wrong-type-argument (list 'eieio-object-p obj))) | |
1615 | (if (not (stringp name)) (signal 'wrong-type-argument (list 'stringp name))) | |
1616 | (aset obj object-name name)) | |
1617 | ||
1618 | (defun object-class (obj) "Return the class struct defining OBJ." | |
1619 | (if (not (eieio-object-p obj)) (signal 'wrong-type-argument (list 'eieio-object-p obj))) | |
1620 | (object-class-fast obj)) | |
1621 | (defalias 'class-of 'object-class) | |
1622 | ||
1623 | (defun object-class-name (obj) "Return a Lisp like symbol name for OBJ's class." | |
1624 | (if (not (eieio-object-p obj)) (signal 'wrong-type-argument (list 'eieio-object-p obj))) | |
1625 | (class-name (object-class-fast obj))) | |
1626 | ||
1627 | (defmacro class-parents-fast (class) "Return parent classes to CLASS with no check." | |
1628 | `(aref (class-v ,class) class-parent)) | |
1629 | ||
1630 | (defun class-parents (class) | |
1631 | "Return parent classes to CLASS. (overload of variable). | |
1632 | ||
1633 | The CLOS function `class-direct-superclasses' is aliased to this function." | |
1634 | (if (not (class-p class)) (signal 'wrong-type-argument (list 'class-p class))) | |
1635 | (class-parents-fast class)) | |
1636 | ||
1637 | (defmacro class-children-fast (class) "Return child classes to CLASS with no check." | |
1638 | `(aref (class-v ,class) class-children)) | |
1639 | ||
1640 | (defun class-children (class) | |
9ffe3f52 | 1641 | "Return child classes to CLASS. |
6dd12ef2 CY |
1642 | |
1643 | The CLOS function `class-direct-subclasses' is aliased to this function." | |
1644 | (if (not (class-p class)) (signal 'wrong-type-argument (list 'class-p class))) | |
1645 | (class-children-fast class)) | |
1646 | ||
a2930e43 EL |
1647 | (defun eieio-c3-candidate (class remaining-inputs) |
1648 | "Returns CLASS if it can go in the result now, otherwise nil" | |
1649 | ;; Ensure CLASS is not in any position but the first in any of the | |
1650 | ;; element lists of REMAINING-INPUTS. | |
1651 | (and (not (let ((found nil)) | |
1652 | (while (and remaining-inputs (not found)) | |
1653 | (setq found (member class (cdr (car remaining-inputs))) | |
1654 | remaining-inputs (cdr remaining-inputs))) | |
1655 | found)) | |
1656 | class)) | |
1657 | ||
1658 | (defun eieio-c3-merge-lists (reversed-partial-result remaining-inputs) | |
1659 | "Merge REVERSED-PARTIAL-RESULT REMAINING-INPUTS in a consistent order, if possible. | |
1660 | If a consistent order does not exist, signal an error." | |
1661 | (if (let ((tail remaining-inputs) | |
1662 | (found nil)) | |
1663 | (while (and tail (not found)) | |
1664 | (setq found (car tail) tail (cdr tail))) | |
1665 | (not found)) | |
1666 | ;; If all remaining inputs are empty lists, we are done. | |
1667 | (nreverse reversed-partial-result) | |
1668 | ;; Otherwise, we try to find the next element of the result. This | |
1669 | ;; is achieved by considering the first element of each | |
1670 | ;; (non-empty) input list and accepting a candidate if it is | |
1671 | ;; consistent with the rests of the input lists. | |
1672 | (let* ((found nil) | |
1673 | (tail remaining-inputs) | |
1674 | (next (progn | |
1675 | (while (and tail (not found)) | |
1676 | (setq found (and (car tail) | |
1677 | (eieio-c3-candidate (caar tail) | |
1678 | remaining-inputs)) | |
1679 | tail (cdr tail))) | |
1680 | found))) | |
1681 | (if next | |
1682 | ;; The graph is consistent so far, add NEXT to result and | |
1683 | ;; merge input lists, dropping NEXT from their heads where | |
1684 | ;; applicable. | |
1685 | (eieio-c3-merge-lists | |
1686 | (cons next reversed-partial-result) | |
1687 | (mapcar (lambda (l) (if (eq (first l) next) (rest l) l)) | |
1688 | remaining-inputs)) | |
1689 | ;; The graph is inconsistent, give up | |
1690 | (signal 'inconsistent-class-hierarchy (list remaining-inputs)))))) | |
1691 | ||
1692 | (defun eieio-class-precedence-dfs (class) | |
1693 | "Return all parents of CLASS in depth-first order." | |
1694 | (let* ((parents (class-parents-fast class)) | |
1695 | (classes (copy-sequence | |
1696 | (apply #'append | |
1697 | (list class) | |
1698 | (or | |
1699 | (mapcar | |
1700 | (lambda (parent) | |
1701 | (cons parent | |
1702 | (eieio-class-precedence-dfs parent))) | |
1703 | parents) | |
1704 | '((eieio-default-superclass)))))) | |
1705 | (tail classes)) | |
1706 | ;; Remove duplicates. | |
1707 | (while tail | |
1708 | (setcdr tail (delq (car tail) (cdr tail))) | |
1709 | (setq tail (cdr tail))) | |
1710 | classes)) | |
1711 | ||
1712 | (defun eieio-class-precedence-bfs (class) | |
1713 | "Return all parents of CLASS in breadth-first order." | |
1714 | (let ((result) | |
1715 | (queue (or (class-parents-fast class) | |
1716 | '(eieio-default-superclass)))) | |
1717 | (while queue | |
1718 | (let ((head (pop queue))) | |
1719 | (unless (member head result) | |
1720 | (push head result) | |
1721 | (unless (eq head 'eieio-default-superclass) | |
1722 | (setq queue (append queue (or (class-parents-fast head) | |
1723 | '(eieio-default-superclass)))))))) | |
1724 | (cons class (nreverse result))) | |
1725 | ) | |
1726 | ||
1727 | (defun eieio-class-precedence-c3 (class) | |
1728 | "Return all parents of CLASS in c3 order." | |
1729 | (let ((parents (class-parents-fast class))) | |
1730 | (eieio-c3-merge-lists | |
1731 | (list class) | |
1732 | (append | |
1733 | (or | |
1734 | (mapcar | |
1735 | (lambda (x) | |
1736 | (eieio-class-precedence-c3 x)) | |
1737 | parents) | |
1738 | '((eieio-default-superclass))) | |
1739 | (list parents)))) | |
1740 | ) | |
1741 | ||
1742 | (defun class-precedence-list (class) | |
1743 | "Return (transitively closed) list of parents of CLASS. | |
1744 | The order, in which the parents are returned depends on the | |
1745 | method invocation orders of the involved classes." | |
1746 | (if (or (null class) (eq class 'eieio-default-superclass)) | |
1747 | nil | |
1748 | (case (class-method-invocation-order class) | |
1749 | (:depth-first | |
1750 | (eieio-class-precedence-dfs class)) | |
1751 | (:breadth-first | |
1752 | (eieio-class-precedence-bfs class)) | |
1753 | (:c3 | |
1754 | (eieio-class-precedence-c3 class)))) | |
1755 | ) | |
1756 | ||
6dd12ef2 CY |
1757 | ;; Official CLOS functions. |
1758 | (defalias 'class-direct-superclasses 'class-parents) | |
1759 | (defalias 'class-direct-subclasses 'class-children) | |
1760 | ||
1761 | (defmacro class-parent-fast (class) "Return first parent class to CLASS with no check." | |
1762 | `(car (class-parents-fast ,class))) | |
1763 | ||
1764 | (defmacro class-parent (class) "Return first parent class to CLASS. (overload of variable)." | |
1765 | `(car (class-parents ,class))) | |
1766 | ||
1767 | (defmacro same-class-fast-p (obj class) "Return t if OBJ is of class-type CLASS with no error checking." | |
1768 | `(eq (aref ,obj object-class) ,class)) | |
1769 | ||
1770 | (defun same-class-p (obj class) "Return t if OBJ is of class-type CLASS." | |
1771 | (if (not (class-p class)) (signal 'wrong-type-argument (list 'class-p class))) | |
1772 | (if (not (eieio-object-p obj)) (signal 'wrong-type-argument (list 'eieio-object-p obj))) | |
1773 | (same-class-fast-p obj class)) | |
1774 | ||
1775 | (defun object-of-class-p (obj class) | |
1776 | "Return non-nil if OBJ is an instance of CLASS or CLASS' subclasses." | |
1777 | (if (not (eieio-object-p obj)) (signal 'wrong-type-argument (list 'eieio-object-p obj))) | |
1778 | ;; class will be checked one layer down | |
1779 | (child-of-class-p (aref obj object-class) class)) | |
1780 | ;; Backwards compatibility | |
1781 | (defalias 'obj-of-class-p 'object-of-class-p) | |
1782 | ||
1783 | (defun child-of-class-p (child class) | |
a8f316ca | 1784 | "Return non-nil if CHILD class is a subclass of CLASS." |
6dd12ef2 CY |
1785 | (if (not (class-p class)) (signal 'wrong-type-argument (list 'class-p class))) |
1786 | (if (not (class-p child)) (signal 'wrong-type-argument (list 'class-p child))) | |
1787 | (let ((p nil)) | |
1788 | (while (and child (not (eq child class))) | |
1789 | (setq p (append p (aref (class-v child) class-parent)) | |
1790 | child (car p) | |
1791 | p (cdr p))) | |
1792 | (if child t))) | |
1793 | ||
a2930e43 EL |
1794 | (defun object-slots (obj) |
1795 | "Return list of slots available in OBJ." | |
6dd12ef2 CY |
1796 | (if (not (eieio-object-p obj)) (signal 'wrong-type-argument (list 'eieio-object-p obj))) |
1797 | (aref (class-v (object-class-fast obj)) class-public-a)) | |
1798 | ||
1799 | (defun class-slot-initarg (class slot) "Fetch from CLASS, SLOT's :initarg." | |
1800 | (if (not (class-p class)) (signal 'wrong-type-argument (list 'class-p class))) | |
1801 | (let ((ia (aref (class-v class) class-initarg-tuples)) | |
1802 | (f nil)) | |
1803 | (while (and ia (not f)) | |
1804 | (if (eq (cdr (car ia)) slot) | |
1805 | (setq f (car (car ia)))) | |
1806 | (setq ia (cdr ia))) | |
1807 | f)) | |
1808 | ||
9760c73c SM |
1809 | ;;; Object Set macros |
1810 | ;; | |
1811 | (defmacro oset (obj slot value) | |
1812 | "Set the value in OBJ for slot SLOT to VALUE. | |
1813 | SLOT is the slot name as specified in `defclass' or the tag created | |
1814 | with in the :initarg slot. VALUE can be any Lisp object." | |
1815 | `(eieio-oset ,obj (quote ,slot) ,value)) | |
1816 | ||
1817 | (defun eieio-oset (obj slot value) | |
1818 | "Do the work for the macro `oset'. | |
1819 | Fills in OBJ's SLOT with VALUE." | |
1820 | (if (not (eieio-object-p obj)) (signal 'wrong-type-argument (list 'eieio-object-p obj))) | |
1821 | (if (not (symbolp slot)) (signal 'wrong-type-argument (list 'symbolp slot))) | |
1822 | (let ((c (eieio-slot-name-index (object-class-fast obj) obj slot))) | |
1823 | (if (not c) | |
1824 | ;; It might be missing because it is a :class allocated slot. | |
1825 | ;; Let's check that info out. | |
1826 | (if (setq c | |
1827 | (eieio-class-slot-name-index (aref obj object-class) slot)) | |
1828 | ;; Oset that slot. | |
1829 | (progn | |
1830 | (eieio-validate-class-slot-value (object-class-fast obj) c value slot) | |
1831 | (aset (aref (class-v (aref obj object-class)) | |
1832 | class-class-allocation-values) | |
1833 | c value)) | |
1834 | ;; See oref for comment on `slot-missing' | |
1835 | (slot-missing obj slot 'oset value) | |
1836 | ;;(signal 'invalid-slot-name (list (object-name obj) slot)) | |
1837 | ) | |
1838 | (eieio-validate-slot-value (object-class-fast obj) c value slot) | |
1839 | (aset obj c value)))) | |
1840 | ||
1841 | (defmacro oset-default (class slot value) | |
1842 | "Set the default slot in CLASS for SLOT to VALUE. | |
1843 | The default value is usually set with the :initform tag during class | |
1844 | creation. This allows users to change the default behavior of classes | |
1845 | after they are created." | |
1846 | `(eieio-oset-default ,class (quote ,slot) ,value)) | |
1847 | ||
1848 | (defun eieio-oset-default (class slot value) | |
1849 | "Do the work for the macro `oset-default'. | |
1850 | Fills in the default value in CLASS' in SLOT with VALUE." | |
1851 | (if (not (class-p class)) (signal 'wrong-type-argument (list 'class-p class))) | |
1852 | (if (not (symbolp slot)) (signal 'wrong-type-argument (list 'symbolp slot))) | |
1853 | (let* ((scoped-class class) | |
1854 | (c (eieio-slot-name-index class nil slot))) | |
1855 | (if (not c) | |
1856 | ;; It might be missing because it is a :class allocated slot. | |
1857 | ;; Let's check that info out. | |
1858 | (if (setq c (eieio-class-slot-name-index class slot)) | |
1859 | (progn | |
1860 | ;; Oref that slot. | |
1861 | (eieio-validate-class-slot-value class c value slot) | |
1862 | (aset (aref (class-v class) class-class-allocation-values) c | |
1863 | value)) | |
1864 | (signal 'invalid-slot-name (list (class-name class) slot))) | |
1865 | (eieio-validate-slot-value class c value slot) | |
1866 | ;; Set this into the storage for defaults. | |
1867 | (setcar (nthcdr (- c 3) (aref (class-v class) class-public-d)) | |
1868 | value) | |
1869 | ;; Take the value, and put it into our cache object. | |
1870 | (eieio-oset (aref (class-v class) class-default-object-cache) | |
1871 | slot value) | |
1872 | ))) | |
1873 | ||
6dd12ef2 CY |
1874 | ;;; CLOS queries into classes and slots |
1875 | ;; | |
1876 | (defun slot-boundp (object slot) | |
a8f316ca | 1877 | "Return non-nil if OBJECT's SLOT is bound. |
6dd12ef2 CY |
1878 | Setting a slot's value makes it bound. Calling `slot-makeunbound' will |
1879 | make a slot unbound. | |
1880 | OBJECT can be an instance or a class." | |
1881 | ;; Skip typechecking while retrieving this value. | |
1882 | (let ((eieio-skip-typecheck t)) | |
1883 | ;; Return nil if the magic symbol is in there. | |
9869b3ae SM |
1884 | (not (eq (cond |
1885 | ((eieio-object-p object) (eieio-oref object slot)) | |
1886 | ((class-p object) (eieio-oref-default object slot)) | |
1887 | (t (signal 'wrong-type-argument (list 'eieio-object-p object)))) | |
1888 | eieio-unbound)))) | |
6dd12ef2 CY |
1889 | |
1890 | (defun slot-makeunbound (object slot) | |
1891 | "In OBJECT, make SLOT unbound." | |
1892 | (eieio-oset object slot eieio-unbound)) | |
1893 | ||
1894 | (defun slot-exists-p (object-or-class slot) | |
a8f316ca | 1895 | "Return non-nil if OBJECT-OR-CLASS has SLOT." |
6dd12ef2 CY |
1896 | (let ((cv (class-v (cond ((eieio-object-p object-or-class) |
1897 | (object-class object-or-class)) | |
1898 | ((class-p object-or-class) | |
1899 | object-or-class)) | |
1900 | ))) | |
1901 | (or (memq slot (aref cv class-public-a)) | |
1902 | (memq slot (aref cv class-class-allocation-a))) | |
1903 | )) | |
1904 | ||
1905 | (defun find-class (symbol &optional errorp) | |
1906 | "Return the class that SYMBOL represents. | |
1907 | If there is no class, nil is returned if ERRORP is nil. | |
1908 | If ERRORP is non-nil, `wrong-argument-type' is signaled." | |
1909 | (if (not (class-p symbol)) | |
1910 | (if errorp (signal 'wrong-type-argument (list 'class-p symbol)) | |
1911 | nil) | |
1912 | (class-v symbol))) | |
1913 | ||
1914 | ;;; Slightly more complex utility functions for objects | |
1915 | ;; | |
1916 | (defun object-assoc (key slot list) | |
1917 | "Return an object if KEY is `equal' to SLOT's value of an object in LIST. | |
a8f316ca | 1918 | LIST is a list of objects whose slots are searched. |
6dd12ef2 CY |
1919 | Objects in LIST do not need to have a slot named SLOT, nor does |
1920 | SLOT need to be bound. If these errors occur, those objects will | |
1921 | be ignored." | |
1922 | (if (not (listp list)) (signal 'wrong-type-argument (list 'listp list))) | |
1923 | (while (and list (not (condition-case nil | |
1924 | ;; This prevents errors for missing slots. | |
1925 | (equal key (eieio-oref (car list) slot)) | |
1926 | (error nil)))) | |
1927 | (setq list (cdr list))) | |
1928 | (car list)) | |
1929 | ||
1930 | (defun object-assoc-list (slot list) | |
1931 | "Return an association list with the contents of SLOT as the key element. | |
1932 | LIST must be a list of objects with SLOT in it. | |
1933 | This is useful when you need to do completing read on an object group." | |
1934 | (if (not (listp list)) (signal 'wrong-type-argument (list 'listp list))) | |
1935 | (let ((assoclist nil)) | |
1936 | (while list | |
1937 | (setq assoclist (cons (cons (eieio-oref (car list) slot) | |
1938 | (car list)) | |
1939 | assoclist)) | |
1940 | (setq list (cdr list))) | |
1941 | (nreverse assoclist))) | |
1942 | ||
1943 | (defun object-assoc-list-safe (slot list) | |
1944 | "Return an association list with the contents of SLOT as the key element. | |
1945 | LIST must be a list of objects, but those objects do not need to have | |
1946 | SLOT in it. If it does not, then that element is left out of the association | |
1947 | list." | |
1948 | (if (not (listp list)) (signal 'wrong-type-argument (list 'listp list))) | |
1949 | (let ((assoclist nil)) | |
1950 | (while list | |
1951 | (if (slot-exists-p (car list) slot) | |
1952 | (setq assoclist (cons (cons (eieio-oref (car list) slot) | |
1953 | (car list)) | |
1954 | assoclist))) | |
1955 | (setq list (cdr list))) | |
1956 | (nreverse assoclist))) | |
1957 | ||
1958 | (defun object-add-to-list (object slot item &optional append) | |
1959 | "In OBJECT's SLOT, add ITEM to the list of elements. | |
1960 | Optional argument APPEND indicates we need to append to the list. | |
1961 | If ITEM already exists in the list in SLOT, then it is not added. | |
1962 | Comparison is done with `equal' through the `member' function call. | |
1963 | If SLOT is unbound, bind it to the list containing ITEM." | |
1964 | (let (ov) | |
1965 | ;; Find the originating list. | |
1966 | (if (not (slot-boundp object slot)) | |
1967 | (setq ov (list item)) | |
1968 | (setq ov (eieio-oref object slot)) | |
1969 | ;; turn it into a list. | |
1970 | (unless (listp ov) | |
1971 | (setq ov (list ov))) | |
1972 | ;; Do the combination | |
1973 | (if (not (member item ov)) | |
1974 | (setq ov | |
1975 | (if append | |
1976 | (append ov (list item)) | |
1977 | (cons item ov))))) | |
1978 | ;; Set back into the slot. | |
1979 | (eieio-oset object slot ov))) | |
1980 | ||
1981 | (defun object-remove-from-list (object slot item) | |
1982 | "In OBJECT's SLOT, remove occurrences of ITEM. | |
a8f316ca | 1983 | Deletion is done with `delete', which deletes by side effect, |
6dd12ef2 CY |
1984 | and comparisons are done with `equal'. |
1985 | If SLOT is unbound, do nothing." | |
1986 | (if (not (slot-boundp object slot)) | |
1987 | nil | |
1988 | (eieio-oset object slot (delete item (eieio-oref object slot))))) | |
1989 | \f | |
1990 | ;;; EIEIO internal search functions | |
1991 | ;; | |
1992 | (defun eieio-slot-originating-class-p (start-class slot) | |
a8f316ca | 1993 | "Return non-nil if START-CLASS is the first class to define SLOT. |
6dd12ef2 CY |
1994 | This is for testing if `scoped-class' is the class that defines SLOT |
1995 | so that we can protect private slots." | |
1996 | (let ((par (class-parents start-class)) | |
1997 | (ret t)) | |
1998 | (if (not par) | |
1999 | t | |
2000 | (while (and par ret) | |
2001 | (if (intern-soft (symbol-name slot) | |
2002 | (aref (class-v (car par)) | |
2003 | class-symbol-obarray)) | |
2004 | (setq ret nil)) | |
2005 | (setq par (cdr par))) | |
2006 | ret))) | |
2007 | ||
2008 | (defun eieio-slot-name-index (class obj slot) | |
2009 | "In CLASS for OBJ find the index of the named SLOT. | |
2010 | The slot is a symbol which is installed in CLASS by the `defclass' | |
2011 | call. OBJ can be nil, but if it is an object, and the slot in question | |
a8f316ca | 2012 | is protected, access will be allowed if OBJ is a child of the currently |
6dd12ef2 CY |
2013 | `scoped-class'. |
2014 | If SLOT is the value created with :initarg instead, | |
2015 | reverse-lookup that name, and recurse with the associated slot value." | |
2016 | ;; Removed checks to outside this call | |
2017 | (let* ((fsym (intern-soft (symbol-name slot) | |
2018 | (aref (class-v class) | |
2019 | class-symbol-obarray))) | |
2020 | (fsi (if (symbolp fsym) (symbol-value fsym) nil))) | |
2021 | (if (integerp fsi) | |
2022 | (cond | |
2023 | ((not (get fsym 'protection)) | |
2024 | (+ 3 fsi)) | |
2025 | ((and (eq (get fsym 'protection) 'protected) | |
62a81506 | 2026 | (bound-and-true-p scoped-class) |
6dd12ef2 CY |
2027 | (or (child-of-class-p class scoped-class) |
2028 | (and (eieio-object-p obj) | |
2029 | (child-of-class-p class (object-class obj))))) | |
2030 | (+ 3 fsi)) | |
2031 | ((and (eq (get fsym 'protection) 'private) | |
62a81506 | 2032 | (or (and (bound-and-true-p scoped-class) |
6dd12ef2 CY |
2033 | (eieio-slot-originating-class-p scoped-class slot)) |
2034 | eieio-initializing-object)) | |
2035 | (+ 3 fsi)) | |
2036 | (t nil)) | |
2037 | (let ((fn (eieio-initarg-to-attribute class slot))) | |
2038 | (if fn (eieio-slot-name-index class obj fn) nil))))) | |
2039 | ||
2040 | (defun eieio-class-slot-name-index (class slot) | |
2041 | "In CLASS find the index of the named SLOT. | |
2042 | The slot is a symbol which is installed in CLASS by the `defclass' | |
2043 | call. If SLOT is the value created with :initarg instead, | |
2044 | reverse-lookup that name, and recurse with the associated slot value." | |
2045 | ;; This will happen less often, and with fewer slots. Do this the | |
2046 | ;; storage cheap way. | |
2047 | (let* ((a (aref (class-v class) class-class-allocation-a)) | |
2048 | (l1 (length a)) | |
2049 | (af (memq slot a)) | |
2050 | (l2 (length af))) | |
2051 | ;; Slot # is length of the total list, minus the remaining list of | |
2052 | ;; the found slot. | |
2053 | (if af (- l1 l2)))) | |
2054 | \f | |
2055 | ;;; CLOS generics internal function handling | |
2056 | ;; | |
2057 | (defvar eieio-generic-call-methodname nil | |
2058 | "When using `call-next-method', provides a context on how to do it.") | |
2059 | (defvar eieio-generic-call-arglst nil | |
2060 | "When using `call-next-method', provides a context for parameters.") | |
2061 | (defvar eieio-generic-call-key nil | |
2062 | "When using `call-next-method', provides a context for the current key. | |
2063 | Keys are a number representing :before, :primary, and :after methods.") | |
2064 | (defvar eieio-generic-call-next-method-list nil | |
2065 | "When executing a PRIMARY or STATIC method, track the 'next-method'. | |
2066 | During executions, the list is first generated, then as each next method | |
2067 | is called, the next method is popped off the stack.") | |
2068 | ||
d1069532 SM |
2069 | (define-obsolete-variable-alias 'eieio-pre-method-execution-hooks |
2070 | 'eieio-pre-method-execution-functions "24.3") | |
2071 | (defvar eieio-pre-method-execution-functions nil | |
f1a4e679 | 2072 | "Abnormal hook run just before an EIEIO method is executed. |
a8f316ca | 2073 | The hook function must accept one argument, the list of forms |
6dd12ef2 CY |
2074 | about to be executed.") |
2075 | ||
2076 | (defun eieio-generic-call (method args) | |
2077 | "Call METHOD with ARGS. | |
2078 | ARGS provides the context on which implementation to use. | |
2079 | This should only be called from a generic function." | |
2080 | ;; We must expand our arguments first as they are always | |
2081 | ;; passed in as quoted symbols | |
2082 | (let ((newargs nil) (mclass nil) (lambdas nil) (tlambdas nil) (keys nil) | |
2083 | (eieio-generic-call-methodname method) | |
2084 | (eieio-generic-call-arglst args) | |
2085 | (firstarg nil) | |
2086 | (primarymethodlist nil)) | |
2087 | ;; get a copy | |
2088 | (setq newargs args | |
2089 | firstarg (car newargs)) | |
2090 | ;; Is the class passed in autoloaded? | |
2091 | ;; Since class names are also constructors, they can be autoloaded | |
2092 | ;; via the autoload command. Check for this, and load them in. | |
2093 | ;; It's ok if it doesn't turn out to be a class. Probably want that | |
2094 | ;; function loaded anyway. | |
2095 | (if (and (symbolp firstarg) | |
2096 | (fboundp firstarg) | |
2097 | (listp (symbol-function firstarg)) | |
2098 | (eq 'autoload (car (symbol-function firstarg)))) | |
2099 | (load (nth 1 (symbol-function firstarg)))) | |
2100 | ;; Determine the class to use. | |
2101 | (cond ((eieio-object-p firstarg) | |
2102 | (setq mclass (object-class-fast firstarg))) | |
2103 | ((class-p firstarg) | |
2104 | (setq mclass firstarg)) | |
2105 | ) | |
2106 | ;; Make sure the class is a valid class | |
2107 | ;; mclass can be nil (meaning a generic for should be used. | |
2108 | ;; mclass cannot have a value that is not a class, however. | |
2109 | (when (and (not (null mclass)) (not (class-p mclass))) | |
2110 | (error "Cannot dispatch method %S on class %S" | |
2111 | method mclass) | |
2112 | ) | |
2113 | ;; Now create a list in reverse order of all the calls we have | |
2114 | ;; make in order to successfully do this right. Rules: | |
2115 | ;; 1) Only call generics if scoped-class is not defined | |
2116 | ;; This prevents multiple calls in the case of recursion | |
2117 | ;; 2) Only call static if this is a static method. | |
2118 | ;; 3) Only call specifics if the definition allows for them. | |
2119 | ;; 4) Call in order based on :before, :primary, and :after | |
2120 | (when (eieio-object-p firstarg) | |
2121 | ;; Non-static calls do all this stuff. | |
2122 | ||
2123 | ;; :after methods | |
2124 | (setq tlambdas | |
2125 | (if mclass | |
2126 | (eieiomt-method-list method method-after mclass) | |
2127 | (list (eieio-generic-form method method-after nil))) | |
2128 | ;;(or (and mclass (eieio-generic-form method method-after mclass)) | |
2129 | ;; (eieio-generic-form method method-after nil)) | |
2130 | ) | |
2131 | (setq lambdas (append tlambdas lambdas) | |
2132 | keys (append (make-list (length tlambdas) method-after) keys)) | |
2133 | ||
2134 | ;; :primary methods | |
2135 | (setq tlambdas | |
2136 | (or (and mclass (eieio-generic-form method method-primary mclass)) | |
2137 | (eieio-generic-form method method-primary nil))) | |
2138 | (when tlambdas | |
2139 | (setq lambdas (cons tlambdas lambdas) | |
2140 | keys (cons method-primary keys) | |
2141 | primarymethodlist | |
2142 | (eieiomt-method-list method method-primary mclass))) | |
2143 | ||
2144 | ;; :before methods | |
2145 | (setq tlambdas | |
2146 | (if mclass | |
2147 | (eieiomt-method-list method method-before mclass) | |
2148 | (list (eieio-generic-form method method-before nil))) | |
2149 | ;;(or (and mclass (eieio-generic-form method method-before mclass)) | |
2150 | ;; (eieio-generic-form method method-before nil)) | |
2151 | ) | |
2152 | (setq lambdas (append tlambdas lambdas) | |
2153 | keys (append (make-list (length tlambdas) method-before) keys)) | |
2154 | ) | |
2155 | ||
a2930e43 EL |
2156 | (if mclass |
2157 | ;; For the case of a class, | |
2158 | ;; if there were no methods found, then there could be :static methods. | |
2159 | (when (not lambdas) | |
2160 | (setq tlambdas | |
2161 | (eieio-generic-form method method-static mclass)) | |
2162 | (setq lambdas (cons tlambdas lambdas) | |
2163 | keys (cons method-static keys) | |
2164 | primarymethodlist ;; Re-use even with bad name here | |
2165 | (eieiomt-method-list method method-static mclass))) | |
2166 | ;; For the case of no class (ie - mclass == nil) then there may | |
2167 | ;; be a primary method. | |
6dd12ef2 | 2168 | (setq tlambdas |
a2930e43 EL |
2169 | (eieio-generic-form method method-primary nil)) |
2170 | (when tlambdas | |
2171 | (setq lambdas (cons tlambdas lambdas) | |
2172 | keys (cons method-primary keys) | |
2173 | primarymethodlist | |
2174 | (eieiomt-method-list method method-primary nil))) | |
2175 | ) | |
6dd12ef2 | 2176 | |
d1069532 | 2177 | (run-hook-with-args 'eieio-pre-method-execution-functions |
6dd12ef2 CY |
2178 | primarymethodlist) |
2179 | ||
db9e401b | 2180 | ;; Now loop through all occurrences forms which we must execute |
6dd12ef2 CY |
2181 | ;; (which are happily sorted now) and execute them all! |
2182 | (let ((rval nil) (lastval nil) (rvalever nil) (found nil)) | |
2183 | (while lambdas | |
2184 | (if (car lambdas) | |
2185 | (let* ((scoped-class (cdr (car lambdas))) | |
2186 | (eieio-generic-call-key (car keys)) | |
2187 | (has-return-val | |
2188 | (or (= eieio-generic-call-key method-primary) | |
2189 | (= eieio-generic-call-key method-static))) | |
2190 | (eieio-generic-call-next-method-list | |
2191 | ;; Use the cdr, as the first element is the fcn | |
2192 | ;; we are calling right now. | |
2193 | (when has-return-val (cdr primarymethodlist))) | |
2194 | ) | |
2195 | (setq found t) | |
2196 | ;;(setq rval (apply (car (car lambdas)) newargs)) | |
2197 | (setq lastval (apply (car (car lambdas)) newargs)) | |
2198 | (when has-return-val | |
2199 | (setq rval lastval | |
2200 | rvalever t)) | |
2201 | )) | |
2202 | (setq lambdas (cdr lambdas) | |
2203 | keys (cdr keys))) | |
2204 | (if (not found) | |
2205 | (if (eieio-object-p (car args)) | |
2206 | (setq rval (apply 'no-applicable-method (car args) method args) | |
2207 | rvalever t) | |
2208 | (signal | |
2209 | 'no-method-definition | |
2210 | (list method args)))) | |
2211 | ;; Right Here... it could be that lastval is returned when | |
2212 | ;; rvalever is nil. Is that right? | |
2213 | rval))) | |
2214 | ||
2215 | (defun eieio-generic-call-primary-only (method args) | |
2216 | "Call METHOD with ARGS for methods with only :PRIMARY implementations. | |
2217 | ARGS provides the context on which implementation to use. | |
2218 | This should only be called from a generic function. | |
2219 | ||
2220 | This method is like `eieio-generic-call', but only | |
2221 | implementations in the :PRIMARY slot are queried. After many | |
2222 | years of use, it appears that over 90% of methods in use | |
2223 | have :PRIMARY implementations only. We can therefore optimize | |
2224 | for this common case to improve performance." | |
2225 | ;; We must expand our arguments first as they are always | |
2226 | ;; passed in as quoted symbols | |
2227 | (let ((newargs nil) (mclass nil) (lambdas nil) | |
2228 | (eieio-generic-call-methodname method) | |
2229 | (eieio-generic-call-arglst args) | |
2230 | (firstarg nil) | |
2231 | (primarymethodlist nil) | |
2232 | ) | |
2233 | ;; get a copy | |
2234 | (setq newargs args | |
2235 | firstarg (car newargs)) | |
2236 | ||
2237 | ;; Determine the class to use. | |
2238 | (cond ((eieio-object-p firstarg) | |
2239 | (setq mclass (object-class-fast firstarg))) | |
2240 | ((not firstarg) | |
2241 | (error "Method %s called on nil" method)) | |
2242 | ((not (eieio-object-p firstarg)) | |
2243 | (error "Primary-only method %s called on something not an object" method)) | |
2244 | (t | |
2245 | (error "EIEIO Error: Improperly classified method %s as primary only" | |
2246 | method) | |
2247 | )) | |
2248 | ;; Make sure the class is a valid class | |
2249 | ;; mclass can be nil (meaning a generic for should be used. | |
2250 | ;; mclass cannot have a value that is not a class, however. | |
2251 | (when (null mclass) | |
2252 | (error "Cannot dispatch method %S on class %S" method mclass) | |
2253 | ) | |
2254 | ||
2255 | ;; :primary methods | |
2256 | (setq lambdas (eieio-generic-form method method-primary mclass)) | |
2257 | (setq primarymethodlist ;; Re-use even with bad name here | |
2258 | (eieiomt-method-list method method-primary mclass)) | |
2259 | ||
db9e401b | 2260 | ;; Now loop through all occurrences forms which we must execute |
6dd12ef2 CY |
2261 | ;; (which are happily sorted now) and execute them all! |
2262 | (let* ((rval nil) (lastval nil) (rvalever nil) | |
2263 | (scoped-class (cdr lambdas)) | |
2264 | (eieio-generic-call-key method-primary) | |
2265 | ;; Use the cdr, as the first element is the fcn | |
2266 | ;; we are calling right now. | |
2267 | (eieio-generic-call-next-method-list (cdr primarymethodlist)) | |
2268 | ) | |
2269 | ||
2270 | (if (or (not lambdas) (not (car lambdas))) | |
2271 | ||
2272 | ;; No methods found for this impl... | |
2273 | (if (eieio-object-p (car args)) | |
2274 | (setq rval (apply 'no-applicable-method (car args) method args) | |
2275 | rvalever t) | |
2276 | (signal | |
2277 | 'no-method-definition | |
2278 | (list method args))) | |
2279 | ||
2280 | ;; Do the regular implementation here. | |
2281 | ||
d1069532 | 2282 | (run-hook-with-args 'eieio-pre-method-execution-functions |
6dd12ef2 CY |
2283 | lambdas) |
2284 | ||
2285 | (setq lastval (apply (car lambdas) newargs)) | |
2286 | (setq rval lastval | |
2287 | rvalever t) | |
2288 | ) | |
2289 | ||
2290 | ;; Right Here... it could be that lastval is returned when | |
2291 | ;; rvalever is nil. Is that right? | |
2292 | rval))) | |
2293 | ||
2294 | (defun eieiomt-method-list (method key class) | |
2295 | "Return an alist list of methods lambdas. | |
2296 | METHOD is the method name. | |
2297 | KEY represents either :before, or :after methods. | |
2298 | CLASS is the starting class to search from in the method tree. | |
2299 | If CLASS is nil, then an empty list of methods should be returned." | |
2300 | ;; Note: eieiomt - the MT means MethodTree. See more comments below | |
2301 | ;; for the rest of the eieiomt methods. | |
a2930e43 EL |
2302 | |
2303 | ;; Collect lambda expressions stored for the class and its parent | |
2304 | ;; classes. | |
2305 | (let (lambdas) | |
2306 | (dolist (ancestor (class-precedence-list class)) | |
2307 | ;; Lookup the form to use for the PRIMARY object for the next level | |
2308 | (let ((tmpl (eieio-generic-form method key ancestor))) | |
2309 | (when (and tmpl | |
2310 | (or (not lambdas) | |
2311 | ;; This prevents duplicates coming out of the | |
2312 | ;; class method optimizer. Perhaps we should | |
2313 | ;; just not optimize before/afters? | |
2314 | (not (member tmpl lambdas)))) | |
2315 | (push tmpl lambdas)))) | |
2316 | ||
2317 | ;; Return collected lambda. For :after methods, return in current | |
2318 | ;; order (most general class last); Otherwise, reverse order. | |
6dd12ef2 CY |
2319 | (if (eq key method-after) |
2320 | lambdas | |
2321 | (nreverse lambdas)))) | |
2322 | ||
2323 | (defun next-method-p () | |
a8f316ca | 2324 | "Return non-nil if there is a next method. |
6dd12ef2 CY |
2325 | Returns a list of lambda expressions which is the `next-method' |
2326 | order." | |
2327 | eieio-generic-call-next-method-list) | |
2328 | ||
2329 | (defun call-next-method (&rest replacement-args) | |
2330 | "Call the superclass method from a subclass method. | |
2331 | The superclass method is specified in the current method list, | |
2332 | and is called the next method. | |
2333 | ||
2334 | If REPLACEMENT-ARGS is non-nil, then use them instead of | |
2335 | `eieio-generic-call-arglst'. The generic arg list are the | |
2336 | arguments passed in at the top level. | |
2337 | ||
2338 | Use `next-method-p' to find out if there is a next method to call." | |
62a81506 | 2339 | (if (not (bound-and-true-p scoped-class)) |
a8f316ca | 2340 | (error "`call-next-method' not called within a class specific method")) |
6dd12ef2 CY |
2341 | (if (and (/= eieio-generic-call-key method-primary) |
2342 | (/= eieio-generic-call-key method-static)) | |
2343 | (error "Cannot `call-next-method' except in :primary or :static methods") | |
2344 | ) | |
2345 | (let ((newargs (or replacement-args eieio-generic-call-arglst)) | |
2346 | (next (car eieio-generic-call-next-method-list)) | |
2347 | ) | |
2348 | (if (or (not next) (not (car next))) | |
2349 | (apply 'no-next-method (car newargs) (cdr newargs)) | |
2350 | (let* ((eieio-generic-call-next-method-list | |
2351 | (cdr eieio-generic-call-next-method-list)) | |
a2930e43 | 2352 | (eieio-generic-call-arglst newargs) |
6dd12ef2 CY |
2353 | (scoped-class (cdr next)) |
2354 | (fcn (car next)) | |
2355 | ) | |
2356 | (apply fcn newargs) | |
2357 | )))) | |
2358 | \f | |
2359 | ;;; | |
2360 | ;; eieio-method-tree : eieiomt- | |
2361 | ;; | |
2362 | ;; Stored as eieio-method-tree in property list of a generic method | |
2363 | ;; | |
2364 | ;; (eieio-method-tree . [BEFORE PRIMARY AFTER | |
2365 | ;; genericBEFORE genericPRIMARY genericAFTER]) | |
2366 | ;; and | |
2367 | ;; (eieio-method-obarray . [BEFORE PRIMARY AFTER | |
2368 | ;; genericBEFORE genericPRIMARY genericAFTER]) | |
2369 | ;; where the association is a vector. | |
2370 | ;; (aref 0 -- all static methods. | |
2371 | ;; (aref 1 -- all methods classified as :before | |
2372 | ;; (aref 2 -- all methods classified as :primary | |
2373 | ;; (aref 3 -- all methods classified as :after | |
2374 | ;; (aref 4 -- a generic classified as :before | |
2375 | ;; (aref 5 -- a generic classified as :primary | |
2376 | ;; (aref 6 -- a generic classified as :after | |
2377 | ;; | |
2378 | (defvar eieiomt-optimizing-obarray nil | |
2379 | "While mapping atoms, this contain the obarray being optimized.") | |
2380 | ||
2381 | (defun eieiomt-install (method-name) | |
2382 | "Install the method tree, and obarray onto METHOD-NAME. | |
2383 | Do not do the work if they already exist." | |
2384 | (let ((emtv (get method-name 'eieio-method-tree)) | |
2385 | (emto (get method-name 'eieio-method-obarray))) | |
2386 | (if (or (not emtv) (not emto)) | |
2387 | (progn | |
2388 | (setq emtv (put method-name 'eieio-method-tree | |
2389 | (make-vector method-num-slots nil)) | |
2390 | emto (put method-name 'eieio-method-obarray | |
2391 | (make-vector method-num-slots nil))) | |
2392 | (aset emto 0 (make-vector 11 0)) | |
2393 | (aset emto 1 (make-vector 11 0)) | |
2394 | (aset emto 2 (make-vector 41 0)) | |
2395 | (aset emto 3 (make-vector 11 0)) | |
2396 | )))) | |
2397 | ||
2398 | (defun eieiomt-add (method-name method key class) | |
2399 | "Add to METHOD-NAME the forms METHOD in a call position KEY for CLASS. | |
2400 | METHOD-NAME is the name created by a call to `defgeneric'. | |
2401 | METHOD are the forms for a given implementation. | |
2402 | KEY is an integer (see comment in eieio.el near this function) which | |
2403 | is associated with the :static :before :primary and :after tags. | |
2404 | It also indicates if CLASS is defined or not. | |
2405 | CLASS is the class this method is associated with." | |
2406 | (if (or (> key method-num-slots) (< key 0)) | |
a8f316ca | 2407 | (error "eieiomt-add: method key error!")) |
6dd12ef2 CY |
2408 | (let ((emtv (get method-name 'eieio-method-tree)) |
2409 | (emto (get method-name 'eieio-method-obarray))) | |
2410 | ;; Make sure the method tables are available. | |
2411 | (if (or (not emtv) (not emto)) | |
2412 | (error "Programmer error: eieiomt-add")) | |
2413 | ;; only add new cells on if it doesn't already exist! | |
2414 | (if (assq class (aref emtv key)) | |
2415 | (setcdr (assq class (aref emtv key)) method) | |
2416 | (aset emtv key (cons (cons class method) (aref emtv key)))) | |
2417 | ;; Add function definition into newly created symbol, and store | |
2418 | ;; said symbol in the correct obarray, otherwise use the | |
2419 | ;; other array to keep this stuff | |
2420 | (if (< key method-num-lists) | |
2421 | (let ((nsym (intern (symbol-name class) (aref emto key)))) | |
2422 | (fset nsym method))) | |
62a81506 CY |
2423 | ;; Save the defmethod file location in a symbol property. |
2424 | (let ((fname (if load-in-progress | |
2425 | load-file-name | |
2426 | buffer-file-name)) | |
2427 | loc) | |
2428 | (when fname | |
2429 | (when (string-match "\\.elc$" fname) | |
2430 | (setq fname (substring fname 0 (1- (length fname))))) | |
2431 | (setq loc (get method-name 'method-locations)) | |
2432 | (add-to-list 'loc | |
2433 | (list class fname)) | |
2434 | (put method-name 'method-locations loc))) | |
6dd12ef2 CY |
2435 | ;; Now optimize the entire obarray |
2436 | (if (< key method-num-lists) | |
2437 | (let ((eieiomt-optimizing-obarray (aref emto key))) | |
2438 | ;; @todo - Is this overkill? Should we just clear the symbol? | |
2439 | (mapatoms 'eieiomt-sym-optimize eieiomt-optimizing-obarray))) | |
2440 | )) | |
2441 | ||
2442 | (defun eieiomt-next (class) | |
2443 | "Return the next parent class for CLASS. | |
a8f316ca JB |
2444 | If CLASS is a superclass, return variable `eieio-default-superclass'. |
2445 | If CLASS is variable `eieio-default-superclass' then return nil. | |
2446 | This is different from function `class-parent' as class parent returns | |
2447 | nil for superclasses. This function performs no type checking!" | |
6dd12ef2 CY |
2448 | ;; No type-checking because all calls are made from functions which |
2449 | ;; are safe and do checking for us. | |
2450 | (or (class-parents-fast class) | |
2451 | (if (eq class 'eieio-default-superclass) | |
2452 | nil | |
2453 | '(eieio-default-superclass)))) | |
2454 | ||
2455 | (defun eieiomt-sym-optimize (s) | |
2456 | "Find the next class above S which has a function body for the optimizer." | |
a2930e43 EL |
2457 | ;; Set the value to nil in case there is no nearest cell. |
2458 | (set s nil) | |
2459 | ;; Find the nearest cell that has a function body. If we find one, | |
2460 | ;; we replace the nil from above. | |
2461 | (let ((external-symbol (intern-soft (symbol-name s)))) | |
2462 | (catch 'done | |
2463 | (dolist (ancestor (rest (class-precedence-list external-symbol))) | |
2464 | (let ((ov (intern-soft (symbol-name ancestor) | |
2465 | eieiomt-optimizing-obarray))) | |
2466 | (when (fboundp ov) | |
2467 | (set s ov) ;; store ov as our next symbol | |
2468 | (throw 'done ancestor))))))) | |
6dd12ef2 CY |
2469 | |
2470 | (defun eieio-generic-form (method key class) | |
2471 | "Return the lambda form belonging to METHOD using KEY based upon CLASS. | |
a8f316ca JB |
2472 | If CLASS is not a class then use `generic' instead. If class has |
2473 | no form, but has a parent class, then trace to that parent class. | |
2474 | The first time a form is requested from a symbol, an optimized path | |
db9e401b | 2475 | is memorized for faster future use." |
6dd12ef2 | 2476 | (let ((emto (aref (get method 'eieio-method-obarray) |
a2930e43 | 2477 | (if class key (eieio-specialized-key-to-generic-key key))))) |
6dd12ef2 CY |
2478 | (if (class-p class) |
2479 | ;; 1) find our symbol | |
2480 | (let ((cs (intern-soft (symbol-name class) emto))) | |
2481 | (if (not cs) | |
2482 | ;; 2) If there isn't one, then make one. | |
2483 | ;; This can be slow since it only occurs once | |
2484 | (progn | |
2485 | (setq cs (intern (symbol-name class) emto)) | |
a8f316ca | 2486 | ;; 2.1) Cache its nearest neighbor with a quick optimize |
6dd12ef2 CY |
2487 | ;; which should only occur once for this call ever |
2488 | (let ((eieiomt-optimizing-obarray emto)) | |
2489 | (eieiomt-sym-optimize cs)))) | |
2490 | ;; 3) If it's bound return this one. | |
2491 | (if (fboundp cs) | |
2492 | (cons cs (aref (class-v class) class-symbol)) | |
2493 | ;; 4) If it's not bound then this variable knows something | |
2494 | (if (symbol-value cs) | |
2495 | (progn | |
a8f316ca | 2496 | ;; 4.1) This symbol holds the next class in its value |
6dd12ef2 CY |
2497 | (setq class (symbol-value cs) |
2498 | cs (intern-soft (symbol-name class) emto)) | |
2499 | ;; 4.2) The optimizer should always have chosen a | |
2500 | ;; function-symbol | |
2501 | ;;(if (fboundp cs) | |
2502 | (cons cs (aref (class-v (intern (symbol-name class))) | |
2503 | class-symbol)) | |
2504 | ;;(error "EIEIO optimizer: erratic data loss!")) | |
2505 | ) | |
2506 | ;; There never will be a funcall... | |
2507 | nil))) | |
2508 | ;; for a generic call, what is a list, is the function body we want. | |
2509 | (let ((emtl (aref (get method 'eieio-method-tree) | |
a2930e43 | 2510 | (if class key (eieio-specialized-key-to-generic-key key))))) |
6dd12ef2 CY |
2511 | (if emtl |
2512 | ;; The car of EMTL is supposed to be a class, which in this | |
2513 | ;; case is nil, so skip it. | |
2514 | (cons (cdr (car emtl)) nil) | |
2515 | nil))))) | |
2516 | ||
2517 | ;;; | |
2518 | ;; Way to assign slots based on a list. Used for constructors, or | |
2519 | ;; even resetting an object at run-time | |
2520 | ;; | |
2521 | (defun eieio-set-defaults (obj &optional set-all) | |
2522 | "Take object OBJ, and reset all slots to their defaults. | |
2523 | If SET-ALL is non-nil, then when a default is nil, that value is | |
2524 | reset. If SET-ALL is nil, the slots are only reset if the default is | |
2525 | not nil." | |
2526 | (let ((scoped-class (aref obj object-class)) | |
2527 | (eieio-initializing-object t) | |
2528 | (pub (aref (class-v (aref obj object-class)) class-public-a))) | |
2529 | (while pub | |
2530 | (let ((df (eieio-oref-default obj (car pub)))) | |
2531 | (if (or df set-all) | |
2532 | (eieio-oset obj (car pub) df))) | |
2533 | (setq pub (cdr pub))))) | |
2534 | ||
2535 | (defun eieio-initarg-to-attribute (class initarg) | |
2536 | "For CLASS, convert INITARG to the actual attribute name. | |
2537 | If there is no translation, pass it in directly (so we can cheat if | |
a8f316ca | 2538 | need be... May remove that later...)" |
6dd12ef2 CY |
2539 | (let ((tuple (assoc initarg (aref (class-v class) class-initarg-tuples)))) |
2540 | (if tuple | |
2541 | (cdr tuple) | |
2542 | nil))) | |
2543 | ||
2544 | (defun eieio-attribute-to-initarg (class attribute) | |
2545 | "In CLASS, convert the ATTRIBUTE into the corresponding init argument tag. | |
2546 | This is usually a symbol that starts with `:'." | |
2547 | (let ((tuple (rassoc attribute (aref (class-v class) class-initarg-tuples)))) | |
2548 | (if tuple | |
2549 | (car tuple) | |
2550 | nil))) | |
2551 | ||
2552 | \f | |
2553 | ;;; Here are some special types of errors | |
2554 | ;; | |
2555 | (intern "no-method-definition") | |
2556 | (put 'no-method-definition 'error-conditions '(no-method-definition error)) | |
2557 | (put 'no-method-definition 'error-message "No method definition") | |
2558 | ||
2559 | (intern "no-next-method") | |
2560 | (put 'no-next-method 'error-conditions '(no-next-method error)) | |
2561 | (put 'no-next-method 'error-message "No next method") | |
2562 | ||
2563 | (intern "invalid-slot-name") | |
2564 | (put 'invalid-slot-name 'error-conditions '(invalid-slot-name error)) | |
2565 | (put 'invalid-slot-name 'error-message "Invalid slot name") | |
2566 | ||
2567 | (intern "invalid-slot-type") | |
2568 | (put 'invalid-slot-type 'error-conditions '(invalid-slot-type error nil)) | |
2569 | (put 'invalid-slot-type 'error-message "Invalid slot type") | |
2570 | ||
2571 | (intern "unbound-slot") | |
2572 | (put 'unbound-slot 'error-conditions '(unbound-slot error nil)) | |
2573 | (put 'unbound-slot 'error-message "Unbound slot") | |
2574 | ||
a2930e43 EL |
2575 | (intern "inconsistent-class-hierarchy") |
2576 | (put 'inconsistent-class-hierarchy 'error-conditions | |
2577 | '(inconsistent-class-hierarchy error nil)) | |
2578 | (put 'inconsistent-class-hierarchy 'error-message "Inconsistent class hierarchy") | |
2579 | ||
6dd12ef2 CY |
2580 | ;;; Here are some CLOS items that need the CL package |
2581 | ;; | |
2582 | ||
dc5d230c | 2583 | (defsetf eieio-oref eieio-oset) |
12999ea8 SM |
2584 | |
2585 | (if (eval-when-compile (fboundp 'gv-define-expander)) | |
2a1e2476 | 2586 | ;; Not needed for Emacs>=24.3 since gv.el's setf expands macros and |
12999ea8 SM |
2587 | ;; follows aliases. |
2588 | nil | |
dc5d230c | 2589 | (defsetf slot-value eieio-oset) |
6dd12ef2 CY |
2590 | |
2591 | ;; The below setf method was written by Arnd Kohrs <kohrs@acm.org> | |
2592 | (define-setf-method oref (obj slot) | |
67868d26 CY |
2593 | (with-no-warnings |
2594 | (require 'cl) | |
2595 | (let ((obj-temp (gensym)) | |
2596 | (slot-temp (gensym)) | |
2597 | (store-temp (gensym))) | |
2598 | (list (list obj-temp slot-temp) | |
2599 | (list obj `(quote ,slot)) | |
2600 | (list store-temp) | |
2601 | (list 'set-slot-value obj-temp slot-temp | |
2602 | store-temp) | |
12999ea8 | 2603 | (list 'slot-value obj-temp slot-temp)))))) |
6dd12ef2 CY |
2604 | |
2605 | \f | |
2606 | ;;; | |
2607 | ;; We want all objects created by EIEIO to have some default set of | |
91af3942 | 2608 | ;; behaviors so we can create object utilities, and allow various |
6dd12ef2 CY |
2609 | ;; types of error checking. To do this, create the default EIEIO |
2610 | ;; class, and when no parent class is specified, use this as the | |
2611 | ;; default. (But don't store it in the other classes as the default, | |
2612 | ;; allowing for transparent support.) | |
2613 | ;; | |
2614 | ||
2615 | (defclass eieio-default-superclass nil | |
2616 | nil | |
2617 | "Default parent class for classes with no specified parent class. | |
a8f316ca JB |
2618 | Its slots are automatically adopted by classes with no specified parents. |
2619 | This class is not stored in the `parent' slot of a class vector." | |
6dd12ef2 CY |
2620 | :abstract t) |
2621 | ||
2622 | (defalias 'standard-class 'eieio-default-superclass) | |
2623 | ||
2624 | (defgeneric constructor (class newname &rest slots) | |
a8f316ca | 2625 | "Default constructor for CLASS `eieio-default-superclass'.") |
6dd12ef2 CY |
2626 | |
2627 | (defmethod constructor :static | |
2628 | ((class eieio-default-superclass) newname &rest slots) | |
a8f316ca | 2629 | "Default constructor for CLASS `eieio-default-superclass'. |
6dd12ef2 CY |
2630 | NEWNAME is the name to be given to the constructed object. |
2631 | SLOTS are the initialization slots used by `shared-initialize'. | |
2632 | This static method is called when an object is constructed. | |
2633 | It allocates the vector used to represent an EIEIO object, and then | |
2634 | calls `shared-initialize' on that object." | |
2635 | (let* ((new-object (copy-sequence (aref (class-v class) | |
2636 | class-default-object-cache)))) | |
2637 | ;; Update the name for the newly created object. | |
2638 | (aset new-object object-name newname) | |
2639 | ;; Call the initialize method on the new object with the slots | |
2640 | ;; that were passed down to us. | |
2641 | (initialize-instance new-object slots) | |
2642 | ;; Return the created object. | |
2643 | new-object)) | |
2644 | ||
2645 | (defgeneric shared-initialize (obj slots) | |
2646 | "Set slots of OBJ with SLOTS which is a list of name/value pairs. | |
2647 | Called from the constructor routine.") | |
2648 | ||
2649 | (defmethod shared-initialize ((obj eieio-default-superclass) slots) | |
2650 | "Set slots of OBJ with SLOTS which is a list of name/value pairs. | |
2651 | Called from the constructor routine." | |
2652 | (let ((scoped-class (aref obj object-class))) | |
2653 | (while slots | |
2654 | (let ((rn (eieio-initarg-to-attribute (object-class-fast obj) | |
2655 | (car slots)))) | |
2656 | (if (not rn) | |
2657 | (slot-missing obj (car slots) 'oset (car (cdr slots))) | |
2658 | (eieio-oset obj rn (car (cdr slots))))) | |
2659 | (setq slots (cdr (cdr slots)))))) | |
2660 | ||
2661 | (defgeneric initialize-instance (this &optional slots) | |
a8f316ca | 2662 | "Construct the new object THIS based on SLOTS.") |
6dd12ef2 CY |
2663 | |
2664 | (defmethod initialize-instance ((this eieio-default-superclass) | |
2665 | &optional slots) | |
a8f316ca | 2666 | "Construct the new object THIS based on SLOTS. |
6dd12ef2 | 2667 | SLOTS is a tagged list where odd numbered elements are tags, and |
a8f316ca JB |
2668 | even numbered elements are the values to store in the tagged slot. |
2669 | If you overload the `initialize-instance', there you will need to | |
2670 | call `shared-initialize' yourself, or you can call `call-next-method' | |
2671 | to have this constructor called automatically. If these steps are | |
2672 | not taken, then new objects of your class will not have their values | |
6dd12ef2 CY |
2673 | dynamically set from SLOTS." |
2674 | ;; First, see if any of our defaults are `lambda', and | |
2675 | ;; re-evaluate them and apply the value to our slots. | |
2676 | (let* ((scoped-class (class-v (aref this object-class))) | |
2677 | (slot (aref scoped-class class-public-a)) | |
2678 | (defaults (aref scoped-class class-public-d))) | |
2679 | (while slot | |
a2930e43 EL |
2680 | ;; For each slot, see if we need to evaluate it. |
2681 | ;; | |
2682 | ;; Paul Landes said in an email: | |
2683 | ;; > CL evaluates it if it can, and otherwise, leaves it as | |
2684 | ;; > the quoted thing as you already have. This is by the | |
2685 | ;; > Sonya E. Keene book and other things I've look at on the | |
2686 | ;; > web. | |
2687 | (let ((dflt (eieio-default-eval-maybe (car defaults)))) | |
2688 | (when (not (eq dflt (car defaults))) | |
2689 | (eieio-oset this (car slot) dflt) )) | |
2690 | ;; Next. | |
6dd12ef2 CY |
2691 | (setq slot (cdr slot) |
2692 | defaults (cdr defaults)))) | |
2693 | ;; Shared initialize will parse our slots for us. | |
2694 | (shared-initialize this slots)) | |
2695 | ||
2696 | (defgeneric slot-missing (object slot-name operation &optional new-value) | |
2697 | "Method invoked when an attempt to access a slot in OBJECT fails.") | |
2698 | ||
2699 | (defmethod slot-missing ((object eieio-default-superclass) slot-name | |
2700 | operation &optional new-value) | |
2701 | "Method invoked when an attempt to access a slot in OBJECT fails. | |
2702 | SLOT-NAME is the name of the failed slot, OPERATION is the type of access | |
2703 | that was requested, and optional NEW-VALUE is the value that was desired | |
2704 | to be set. | |
2705 | ||
2706 | This method is called from `oref', `oset', and other functions which | |
2707 | directly reference slots in EIEIO objects." | |
2708 | (signal 'invalid-slot-name (list (object-name object) | |
2709 | slot-name))) | |
2710 | ||
2711 | (defgeneric slot-unbound (object class slot-name fn) | |
2712 | "Slot unbound is invoked during an attempt to reference an unbound slot.") | |
2713 | ||
2714 | (defmethod slot-unbound ((object eieio-default-superclass) | |
2715 | class slot-name fn) | |
2716 | "Slot unbound is invoked during an attempt to reference an unbound slot. | |
2717 | OBJECT is the instance of the object being reference. CLASS is the | |
2718 | class of OBJECT, and SLOT-NAME is the offending slot. This function | |
2719 | throws the signal `unbound-slot'. You can overload this function and | |
2720 | return the value to use in place of the unbound value. | |
2721 | Argument FN is the function signaling this error. | |
2722 | Use `slot-boundp' to determine if a slot is bound or not. | |
2723 | ||
2724 | In CLOS, the argument list is (CLASS OBJECT SLOT-NAME), but | |
2725 | EIEIO can only dispatch on the first argument, so the first two are swapped." | |
2726 | (signal 'unbound-slot (list (class-name class) (object-name object) | |
2727 | slot-name fn))) | |
2728 | ||
2729 | (defgeneric no-applicable-method (object method &rest args) | |
2730 | "Called if there are no implementations for OBJECT in METHOD.") | |
2731 | ||
2732 | (defmethod no-applicable-method ((object eieio-default-superclass) | |
2733 | method &rest args) | |
2734 | "Called if there are no implementations for OBJECT in METHOD. | |
2735 | OBJECT is the object which has no method implementation. | |
2736 | ARGS are the arguments that were passed to METHOD. | |
2737 | ||
2738 | Implement this for a class to block this signal. The return | |
2739 | value becomes the return value of the original method call." | |
2740 | (signal 'no-method-definition (list method (object-name object))) | |
2741 | ) | |
2742 | ||
2743 | (defgeneric no-next-method (object &rest args) | |
2744 | "Called from `call-next-method' when no additional methods are available.") | |
2745 | ||
2746 | (defmethod no-next-method ((object eieio-default-superclass) | |
2747 | &rest args) | |
2748 | "Called from `call-next-method' when no additional methods are available. | |
2749 | OBJECT is othe object being called on `call-next-method'. | |
a8f316ca | 2750 | ARGS are the arguments it is called by. |
6dd12ef2 | 2751 | This method signals `no-next-method' by default. Override this |
a8f316ca | 2752 | method to not throw an error, and its return value becomes the |
6dd12ef2 CY |
2753 | return value of `call-next-method'." |
2754 | (signal 'no-next-method (list (object-name object) args)) | |
bd0ffffd | 2755 | ) |
6dd12ef2 CY |
2756 | |
2757 | (defgeneric clone (obj &rest params) | |
2758 | "Make a copy of OBJ, and then supply PARAMS. | |
2759 | PARAMS is a parameter list of the same form used by `initialize-instance'. | |
2760 | ||
2761 | When overloading `clone', be sure to call `call-next-method' | |
2762 | first and modify the returned object.") | |
2763 | ||
2764 | (defmethod clone ((obj eieio-default-superclass) &rest params) | |
2765 | "Make a copy of OBJ, and then apply PARAMS." | |
2766 | (let ((nobj (copy-sequence obj)) | |
2767 | (nm (aref obj object-name)) | |
2768 | (passname (and params (stringp (car params)))) | |
2769 | (num 1)) | |
2770 | (if params (shared-initialize nobj (if passname (cdr params) params))) | |
2771 | (if (not passname) | |
2772 | (save-match-data | |
2773 | (if (string-match "-\\([0-9]+\\)" nm) | |
2774 | (setq num (1+ (string-to-number (match-string 1 nm))) | |
2775 | nm (substring nm 0 (match-beginning 0)))) | |
2776 | (aset nobj object-name (concat nm "-" (int-to-string num)))) | |
2777 | (aset nobj object-name (car params))) | |
2778 | nobj)) | |
2779 | ||
2780 | (defgeneric destructor (this &rest params) | |
2781 | "Destructor for cleaning up any dynamic links to our object.") | |
2782 | ||
2783 | (defmethod destructor ((this eieio-default-superclass) &rest params) | |
2784 | "Destructor for cleaning up any dynamic links to our object. | |
2785 | Argument THIS is the object being destroyed. PARAMS are additional | |
2786 | ignored parameters." | |
2787 | ;; No cleanup... yet. | |
2788 | ) | |
2789 | ||
2790 | (defgeneric object-print (this &rest strings) | |
2791 | "Pretty printer for object THIS. Call function `object-name' with STRINGS. | |
2792 | ||
2793 | It is sometimes useful to put a summary of the object into the | |
a8f316ca | 2794 | default #<notation> string when using EIEIO browsing tools. |
6dd12ef2 CY |
2795 | Implement this method to customize the summary.") |
2796 | ||
2797 | (defmethod object-print ((this eieio-default-superclass) &rest strings) | |
2798 | "Pretty printer for object THIS. Call function `object-name' with STRINGS. | |
2799 | The default method for printing object THIS is to use the | |
2800 | function `object-name'. | |
2801 | ||
2802 | It is sometimes useful to put a summary of the object into the | |
a8f316ca | 2803 | default #<notation> string when using EIEIO browsing tools. |
6dd12ef2 CY |
2804 | |
2805 | Implement this function and specify STRINGS in a call to | |
2806 | `call-next-method' to provide additional summary information. | |
2807 | When passing in extra strings from child classes, always remember | |
2808 | to prepend a space." | |
2809 | (object-name this (apply 'concat strings))) | |
2810 | ||
2811 | (defvar eieio-print-depth 0 | |
2812 | "When printing, keep track of the current indentation depth.") | |
2813 | ||
2814 | (defgeneric object-write (this &optional comment) | |
2815 | "Write out object THIS to the current stream. | |
a8f316ca | 2816 | Optional COMMENT will add comments to the beginning of the output.") |
6dd12ef2 CY |
2817 | |
2818 | (defmethod object-write ((this eieio-default-superclass) &optional comment) | |
2819 | "Write object THIS out to the current stream. | |
2820 | This writes out the vector version of this object. Complex and recursive | |
2821 | object are discouraged from being written. | |
2822 | If optional COMMENT is non-nil, include comments when outputting | |
2823 | this object." | |
2824 | (when comment | |
2825 | (princ ";; Object ") | |
2826 | (princ (object-name-string this)) | |
2827 | (princ "\n") | |
2828 | (princ comment) | |
2829 | (princ "\n")) | |
2830 | (let* ((cl (object-class this)) | |
2831 | (cv (class-v cl))) | |
2832 | ;; Now output readable lisp to recreate this object | |
2833 | ;; It should look like this: | |
2834 | ;; (<constructor> <name> <slot> <slot> ... ) | |
2835 | ;; Each slot's slot is writen using its :writer. | |
2836 | (princ (make-string (* eieio-print-depth 2) ? )) | |
2837 | (princ "(") | |
2838 | (princ (symbol-name (class-constructor (object-class this)))) | |
62a81506 CY |
2839 | (princ " ") |
2840 | (prin1 (object-name-string this)) | |
2841 | (princ "\n") | |
6dd12ef2 CY |
2842 | ;; Loop over all the public slots |
2843 | (let ((publa (aref cv class-public-a)) | |
2844 | (publd (aref cv class-public-d)) | |
2845 | (publp (aref cv class-public-printer)) | |
2846 | (eieio-print-depth (1+ eieio-print-depth))) | |
2847 | (while publa | |
2848 | (when (slot-boundp this (car publa)) | |
2849 | (let ((i (class-slot-initarg cl (car publa))) | |
2850 | (v (eieio-oref this (car publa))) | |
2851 | ) | |
2852 | (unless (or (not i) (equal v (car publd))) | |
2853 | (princ (make-string (* eieio-print-depth 2) ? )) | |
2854 | (princ (symbol-name i)) | |
2855 | (princ " ") | |
2856 | (if (car publp) | |
2857 | ;; Use our public printer | |
2858 | (funcall (car publp) v) | |
2859 | ;; Use our generic override prin1 function. | |
2860 | (eieio-override-prin1 v)) | |
2861 | (princ "\n")))) | |
2862 | (setq publa (cdr publa) publd (cdr publd) | |
2863 | publp (cdr publp))) | |
2864 | (princ (make-string (* eieio-print-depth 2) ? ))) | |
2865 | (princ ")\n"))) | |
2866 | ||
2867 | (defun eieio-override-prin1 (thing) | |
a8f316ca | 2868 | "Perform a `prin1' on THING taking advantage of object knowledge." |
6dd12ef2 CY |
2869 | (cond ((eieio-object-p thing) |
2870 | (object-write thing)) | |
2871 | ((listp thing) | |
2872 | (eieio-list-prin1 thing)) | |
2873 | ((class-p thing) | |
2874 | (princ (class-name thing))) | |
2875 | ((symbolp thing) | |
2876 | (princ (concat "'" (symbol-name thing)))) | |
2877 | (t (prin1 thing)))) | |
2878 | ||
2879 | (defun eieio-list-prin1 (list) | |
2880 | "Display LIST where list may contain objects." | |
2881 | (if (not (eieio-object-p (car list))) | |
2882 | (progn | |
2883 | (princ "'") | |
2884 | (prin1 list)) | |
2885 | (princ "(list ") | |
2886 | (if (eieio-object-p (car list)) (princ "\n ")) | |
2887 | (while list | |
2888 | (if (eieio-object-p (car list)) | |
2889 | (object-write (car list)) | |
2890 | (princ "'") | |
2891 | (prin1 (car list))) | |
2892 | (princ " ") | |
2893 | (setq list (cdr list))) | |
2894 | (princ (make-string (* eieio-print-depth 2) ? )) | |
2895 | (princ ")"))) | |
2896 | ||
2897 | \f | |
2898 | ;;; Unimplemented functions from CLOS | |
2899 | ;; | |
2900 | (defun change-class (obj class) | |
2901 | "Change the class of OBJ to type CLASS. | |
2902 | This may create or delete slots, but does not affect the return value | |
2903 | of `eq'." | |
a8f316ca | 2904 | (error "EIEIO: `change-class' is unimplemented")) |
6dd12ef2 CY |
2905 | |
2906 | ) | |
2907 | ||
93b6b5e1 SM |
2908 | ;;; Obsolete backward compatibility functions. |
2909 | ;; Needed to run byte-code compiled with the EIEIO of Emacs-23. | |
2910 | ||
2911 | (defun eieio-defmethod (method args) | |
2912 | "Obsolete work part of an old version of the `defmethod' macro." | |
2913 | (let ((key nil) (body nil) (firstarg nil) (argfix nil) (argclass nil) loopa) | |
2914 | ;; find optional keys | |
2915 | (setq key | |
2916 | (cond ((or (eq ':BEFORE (car args)) | |
2917 | (eq ':before (car args))) | |
2918 | (setq args (cdr args)) | |
2919 | method-before) | |
2920 | ((or (eq ':AFTER (car args)) | |
2921 | (eq ':after (car args))) | |
2922 | (setq args (cdr args)) | |
2923 | method-after) | |
2924 | ((or (eq ':PRIMARY (car args)) | |
2925 | (eq ':primary (car args))) | |
2926 | (setq args (cdr args)) | |
2927 | method-primary) | |
2928 | ((or (eq ':STATIC (car args)) | |
2929 | (eq ':static (car args))) | |
2930 | (setq args (cdr args)) | |
2931 | method-static) | |
2932 | ;; Primary key | |
2933 | (t method-primary))) | |
2934 | ;; get body, and fix contents of args to be the arguments of the fn. | |
2935 | (setq body (cdr args) | |
2936 | args (car args)) | |
2937 | (setq loopa args) | |
2938 | ;; Create a fixed version of the arguments | |
2939 | (while loopa | |
2940 | (setq argfix (cons (if (listp (car loopa)) (car (car loopa)) (car loopa)) | |
2941 | argfix)) | |
2942 | (setq loopa (cdr loopa))) | |
2943 | ;; make sure there is a generic | |
2944 | (eieio-defgeneric | |
2945 | method | |
2946 | (if (stringp (car body)) | |
2947 | (car body) (format "Generically created method `%s'." method))) | |
2948 | ;; create symbol for property to bind to. If the first arg is of | |
2949 | ;; the form (varname vartype) and `vartype' is a class, then | |
2950 | ;; that class will be the type symbol. If not, then it will fall | |
2951 | ;; under the type `primary' which is a non-specific calling of the | |
2952 | ;; function. | |
2953 | (setq firstarg (car args)) | |
2954 | (if (listp firstarg) | |
2955 | (progn | |
2956 | (setq argclass (nth 1 firstarg)) | |
2957 | (if (not (class-p argclass)) | |
2958 | (error "Unknown class type %s in method parameters" | |
2959 | (nth 1 firstarg)))) | |
2960 | (if (= key -1) | |
2961 | (signal 'wrong-type-argument (list :static 'non-class-arg))) | |
2962 | ;; generics are higher | |
2963 | (setq key (eieio-specialized-key-to-generic-key key))) | |
2964 | ;; Put this lambda into the symbol so we can find it | |
2965 | (if (byte-code-function-p (car-safe body)) | |
2966 | (eieiomt-add method (car-safe body) key argclass) | |
2967 | (eieiomt-add method (append (list 'lambda (reverse argfix)) body) | |
2968 | key argclass)) | |
2969 | ) | |
2970 | ||
2971 | (when eieio-optimize-primary-methods-flag | |
2972 | ;; Optimizing step: | |
2973 | ;; | |
2974 | ;; If this method, after this setup, only has primary methods, then | |
2975 | ;; we can setup the generic that way. | |
2976 | (if (generic-primary-only-p method) | |
2977 | ;; If there is only one primary method, then we can go one more | |
2978 | ;; optimization step. | |
2979 | (if (generic-primary-only-one-p method) | |
2980 | (eieio-defgeneric-reset-generic-form-primary-only-one method) | |
2981 | (eieio-defgeneric-reset-generic-form-primary-only method)) | |
2982 | (eieio-defgeneric-reset-generic-form method))) | |
2983 | ||
2984 | method) | |
2985 | (make-obsolete 'eieio-defmethod 'eieio--defmethod "24.1") | |
2986 | ||
2987 | (defun eieio-defgeneric (method doc-string) | |
2988 | "Obsolete work part of an old version of the `defgeneric' macro." | |
2989 | (if (and (fboundp method) (not (generic-p method)) | |
2990 | (or (byte-code-function-p (symbol-function method)) | |
2991 | (not (eq 'autoload (car (symbol-function method))))) | |
2992 | ) | |
2993 | (error "You cannot create a generic/method over an existing symbol: %s" | |
2994 | method)) | |
2995 | ;; Don't do this over and over. | |
2996 | (unless (fboundp 'method) | |
2997 | ;; This defun tells emacs where the first definition of this | |
2998 | ;; method is defined. | |
2999 | `(defun ,method nil) | |
3000 | ;; Make sure the method tables are installed. | |
3001 | (eieiomt-install method) | |
3002 | ;; Apply the actual body of this function. | |
3003 | (fset method (eieio-defgeneric-form method doc-string)) | |
3004 | ;; Return the method | |
3005 | 'method)) | |
3006 | (make-obsolete 'eieio-defgeneric nil "24.1") | |
3007 | ||
6dd12ef2 CY |
3008 | ;;; Interfacing with edebug |
3009 | ;; | |
3010 | (defun eieio-edebug-prin1-to-string (object &optional noescape) | |
a8f316ca JB |
3011 | "Display EIEIO OBJECT in fancy format. |
3012 | Overrides the edebug default. | |
6dd12ef2 CY |
3013 | Optional argument NOESCAPE is passed to `prin1-to-string' when appropriate." |
3014 | (cond ((class-p object) (class-name object)) | |
3015 | ((eieio-object-p object) (object-print object)) | |
3016 | ((and (listp object) (or (class-p (car object)) | |
3017 | (eieio-object-p (car object)))) | |
3018 | (concat "(" (mapconcat 'eieio-edebug-prin1-to-string object " ") ")")) | |
3019 | (t (prin1-to-string object noescape)))) | |
3020 | ||
3021 | (add-hook 'edebug-setup-hook | |
3022 | (lambda () | |
3023 | (def-edebug-spec defmethod | |
3024 | (&define ; this means we are defining something | |
3025 | [&or name ("setf" :name setf name)] | |
3026 | ;; ^^ This is the methods symbol | |
3027 | [ &optional symbolp ] ; this is key :before etc | |
3028 | list ; arguments | |
3029 | [ &optional stringp ] ; documentation string | |
3030 | def-body ; part to be debugged | |
3031 | )) | |
3032 | ;; The rest of the macros | |
3033 | (def-edebug-spec oref (form quote)) | |
3034 | (def-edebug-spec oref-default (form quote)) | |
3035 | (def-edebug-spec oset (form quote form)) | |
3036 | (def-edebug-spec oset-default (form quote form)) | |
3037 | (def-edebug-spec class-v form) | |
3038 | (def-edebug-spec class-p form) | |
3039 | (def-edebug-spec eieio-object-p form) | |
3040 | (def-edebug-spec class-constructor form) | |
3041 | (def-edebug-spec generic-p form) | |
3042 | (def-edebug-spec with-slots (list list def-body)) | |
3043 | ;; I suspect this isn't the best way to do this, but when | |
3044 | ;; cust-print was used on my system all my objects | |
3045 | ;; appeared as "#1 =" which was not useful. This allows | |
3046 | ;; edebug to print my objects in the nice way they were | |
3047 | ;; meant to with `object-print' and `class-name' | |
3048 | ;; (defalias 'edebug-prin1-to-string 'eieio-edebug-prin1-to-string) | |
3049 | ) | |
3050 | ) | |
3051 | ||
6dd12ef2 CY |
3052 | ;;; Autoloading some external symbols, and hooking into the help system |
3053 | ;; | |
3054 | ||
002b46b7 GM |
3055 | \f |
3056 | ;;; Start of automatically extracted autoloads. | |
3057 | \f | |
3058 | ;;;### (autoloads (customize-object) "eieio-custom" "eieio-custom.el" | |
b68b3337 | 3059 | ;;;;;; "928623502e8bf40454822355388542b5") |
002b46b7 GM |
3060 | ;;; Generated autoloads from eieio-custom.el |
3061 | ||
3062 | (autoload 'customize-object "eieio-custom" "\ | |
3063 | Customize OBJ in a custom buffer. | |
3064 | Optional argument GROUP is the sub-group of slots to display. | |
3065 | ||
3066 | \(fn OBJ &optional GROUP)" nil nil) | |
3067 | ||
3068 | ;;;*** | |
3069 | \f | |
3070 | ;;;### (autoloads (eieio-help-mode-augmentation-maybee eieio-describe-generic | |
3071 | ;;;;;; eieio-describe-constructor eieio-describe-class eieio-browse) | |
539aa513 | 3072 | ;;;;;; "eieio-opt" "eieio-opt.el" "d808328f9c0156ecbd412d77ba8c569e") |
002b46b7 GM |
3073 | ;;; Generated autoloads from eieio-opt.el |
3074 | ||
3075 | (autoload 'eieio-browse "eieio-opt" "\ | |
3076 | Create an object browser window to show all objects. | |
3077 | If optional ROOT-CLASS, then start with that, otherwise start with | |
3078 | variable `eieio-default-superclass'. | |
3079 | ||
3080 | \(fn &optional ROOT-CLASS)" t nil) | |
002b46b7 GM |
3081 | (defalias 'describe-class 'eieio-describe-class) |
3082 | ||
3083 | (autoload 'eieio-describe-class "eieio-opt" "\ | |
3084 | Describe a CLASS defined by a string or symbol. | |
3085 | If CLASS is actually an object, then also display current values of that object. | |
3086 | Optional HEADERFCN should be called to insert a few bits of info first. | |
3087 | ||
3088 | \(fn CLASS &optional HEADERFCN)" t nil) | |
3089 | ||
3090 | (autoload 'eieio-describe-constructor "eieio-opt" "\ | |
3091 | Describe the constructor function FCN. | |
3092 | Uses `eieio-describe-class' to describe the class being constructed. | |
3093 | ||
3094 | \(fn FCN)" t nil) | |
002b46b7 GM |
3095 | (defalias 'describe-generic 'eieio-describe-generic) |
3096 | ||
3097 | (autoload 'eieio-describe-generic "eieio-opt" "\ | |
3098 | Describe the generic function GENERIC. | |
3099 | Also extracts information about all methods specific to this generic. | |
3100 | ||
3101 | \(fn GENERIC)" t nil) | |
3102 | ||
3103 | (autoload 'eieio-help-mode-augmentation-maybee "eieio-opt" "\ | |
3104 | For buffers thrown into help mode, augment for EIEIO. | |
3105 | Arguments UNUSED are not used. | |
3106 | ||
3107 | \(fn &rest UNUSED)" nil nil) | |
3108 | ||
3109 | ;;;*** | |
3110 | \f | |
3111 | ;;; End of automatically extracted autoloads. | |
6dd12ef2 CY |
3112 | |
3113 | (provide 'eieio) | |
3114 | ||
6dd12ef2 | 3115 | ;;; eieio ends here |