2 ;;; Copyright (c) 2005--2007, by A.J. Rossini <blindglobe@gmail.com>
3 ;;; See COPYRIGHT file for any additional restrictions (BSD license).
4 ;;; Since 1991, ANSI was finally finished. Edited for ANSI Common Lisp.
6 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
9 ;;;; LISP-STAT Object System
12 ;;;; Simple CL implementation of the object system for Lisp-Stat (LSOS)
13 ;;;; as described in Tierney (1990).
15 ;;;; Copyright (c) 1991--, by Luke Tierney. Permission is granted for
16 ;;;; unrestricted use.
21 ;;;; If your CL's handling of packages is compliant with CLtL, 2nd
22 ;;;; Edition (like Macintosh CL version 2), add the feature :CLtL2
23 ;;;; before loading or compiling this code.
25 ;;;; This implementation does not make use of CLOS. It can coexist
26 ;;;; with CLOS, but there are two name conflicts: slot-value and
27 ;;;; call-next-method.
28 ;;;; FIXED-ME: SLOT-VALUE now becomes PROTO-SLOT-VALUE...
29 ;;;; These two symbols are shadowed in the LSOS
30 ;;;; package and must be shadowed in any package that uses LSOS.
31 ;;;; Evaluating the function (lsos::use-lsos) from a package after
32 ;;;; loading this code shadows these two symbols and does a
33 ;;;; use-package for LSOS.
35 ;;;; The :compile-method method uses function-lambda-expression
36 ;;;; defined in CLtL, 2nd Edition. (This method is only needed if
37 ;;;; you want to force compilation of an interpreted method. It is
38 ;;;; not used by the compiler.)
40 ;;;; The efficiency of this code could be improved by low level
41 ;;;; coding of the dispatching functions send, call-method and
42 ;;;; call-next-method to avoid creating an argument list. Other
43 ;;;; efficiency improvements are possible as well, in particular
44 ;;;; by good use of declarations. It may also be possible to build
45 ;;;; a more efficient implementation using the CLOS metaclass
48 ;;;; There are a few minimal tools for experimenting with constraints
49 ;;;; in the code; they are marked by #+:constreinthooks. Sometime
50 ;;;; soon I hope to augment or replace these hooks with a CORAL-like
51 ;;;; constraint system (as used in GARNET).
53 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
54 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
56 (in-package :lisp-stat-object-system
)
59 "Formerly set up to import lisp-stat-object-system into current package."
60 (shadowing-import (package-shadowing-symbols 'lisp-stat-object-system
))
61 (use-package 'lisp-stat-object-system
))
63 ;;; Structure Implementation of Lisp-Stat Object System
65 ;; We might consider a global rewrite if it doesn't seem to break
66 ;; anything. In particular, the real name ought to be
67 ;; proto-sys-object or similar so that we can ensure that the right
68 ;; interpretation is made for this. Call it the prototype object
69 ;; system, and possibly be done with it then.
71 (defvar *object-serial
* 0)
74 (:constructor make-object-structure
) ;; why not make-ls-object?
75 (:print-function print-object-structure
)
76 (:predicate objectp
)) ;; why not ls-object-p?
80 preclist
;; precedence list
81 (serial (incf *object-serial
*)))
83 (defun print-object-structure (object stream depth
)
84 (declare (ignore depth
))
85 (send object
:print stream
))
86 ;;; The following is probably more useful than the above...
87 (defmethod print-object ((object ls-object
) stream
)
88 (send object
:print stream
))
90 (setf (documentation 'objectp
'function
)
92 Returns T if X is an object, NIL otherwise.")
94 (defvar *object
* (make-object-structure)
95 "*object* is the global root object.")
97 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
101 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
103 ;;; AJR:FIXME:Is this going to cause issues with
104 ;;; concurrency/threading?
105 ;;; (need to appropriately handle interrupts). Could change this to
106 ;;; a stack or list that could handle modifications and interrupts
109 "special variable holding current value of SELF.
110 Assigned with current object being worked on.")
113 "Return object we are operating on, otherwise throw error."
114 (if (not (objectp *self
*))
115 (error "Error: no method focused on."))
118 (defun has-duplicates (list)
119 "predicate: takes a list, and returns true if duplicates.
120 This should be simpler, right?"
121 (do ((next list
(rest next
)))
122 ((not (consp next
)) nil
)
123 (if (member (first next
) (rest next
)) (return t
))))
125 (defun assoc-eq (item alist
)
126 "Version of assoc using eq -- should be faster than regular assoc."
127 (declare (inline car eq
))
129 (if (eq (car i
) item
) (return i
))))
131 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
133 ;;; Predicates for Consistency Checking
135 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
137 (defun check-non-nil-symbol (x)
138 (unless (and x
(symbolp x
)) (error "bad symbol - ~s" x
)))
140 (defun check-object (x)
141 "Returns self if true, throws an error otherwise."
142 (if (objectp x
) x
(error "bad object - ~s" x
)))
144 (defun kind-of-p (x y
)
147 Returns T if X and Y are objects and X inherits from Y, NIL otherwise."
149 (if (and (objectp x
) (objectp y
))
150 (if (member y
(ls-object-preclist x
)) t nil
)
153 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
155 ;;;; Precedence List Functions
157 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
159 (defun find-SC (object)
160 "find set of object and ancestors. (diff from this and find-S?)"
161 (copy-list (ls-object-preclist (check-object object
))))
163 (defun find-S (object)
164 "find set of object and ancestors. (diff from this and find-SC?)"
166 (parents (ls-object-parents object
) (cdr parents
)))
167 ((not (consp parents
))
168 (delete-duplicates (cons object result
)))
169 (setf result
(nconc (find-SC (first parents
)) result
))))
171 (defun find-RC (object)
172 "find local precedence ordering."
173 (let ((list (copy-list (ls-object-parents (check-object object
)))))
174 (do ((next list
(rest next
)))
175 ((not (consp next
)) list
)
176 (setf (first next
) (cons object
(first next
)))
177 (setf object
(rest (first next
))))))
180 "find partial precedence ordering."
184 (delete-duplicates result
))
185 (setf result
(nconc result
(find-RC (first S
))))))
187 (defun has-predecessor (x R
)
188 "check if x has a predecessor according to R."
190 (if (and (consp cell
) (eq x
(rest cell
))) (return t
))))
192 (defun find-no-predecessor-list (S R
)
193 "find list of objects in S without predecessors, by R."
196 (unless (has-predecessor x R
) (setf result
(cons x result
))))))
198 (defun child-position (x P
)
199 "find the position of child, if any, of x in P, the list found so
202 (declare (fixnum count
))
204 (if (member x
(ls-object-parents next
)) (return count
))
207 (defun next-object (no-preds P
)
208 "find the next object in the precedence list from objects with no
209 predecessor and current list."
211 ((not (consp no-preds
)) nil
)
212 ((not (consp (rest no-preds
))) (first no-preds
))
216 (declare (fixnum count
))
217 (dolist (x no-preds result
)
218 (let ((tcount (child-position x P
)))
219 (declare (fixnum tcount
))
220 (when (> tcount count
)
222 (setf count tcount
))))))))
225 "Remove object x from S."
229 "Remove all pairs containing x from R. x is assumed to have no
230 predecessors, so only the first position is checked."
231 (delete x R
:key
#'first
))
233 (defun precedence-list (object)
234 "Calculate the object's precedence list."
235 (do* ((S (find-S object
))
241 (setf no-preds
(find-no-predecessor-list S R
))
242 (setf next
(next-object no-preds P
))
243 (if (null next
) (error "inconsistent precedence order"))
244 (setf P
(nconc P
(list next
)))
245 (setf S
(trim-S next S
))
246 (setf R
(trim-R next R
))))
248 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
250 ;;;; Object Construction Functions
252 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
254 (defun calculate-preclist (object)
255 "Return the precedence list for the object."
256 (let ((parents (ls-object-parents (check-object object
))))
257 (if (not (consp parents
)) (error "bad parent list - ~s" parents
))
258 (if (consp (rest parents
))
259 (precedence-list object
)
260 (let ((parent (check-object (first parents
))))
261 (cons object
(ls-object-preclist parent
))))))
263 (defun check-parents (parents)
264 "Ensure valid parents: They must be null, object, or consp without duplicates."
266 ((or (null parents
) (objectp parents
)) parents
)
268 (dolist (x parents
) (check-object x
))
269 (if (has-duplicates parents
)
270 (error "parents may not contain duplicates")))
271 (t (error "bad parents - ~s" parents
))))
273 (defun make-basic-object (parents object
)
274 "Creates a basic object for the prototype system by ensuring that it
275 can be placed into the storage heirarchy.
276 If object is not initialized, instantiate the structure.
277 Place into parental structure.
278 If parents is null, use root *object*,
279 if parents is a single object, use it (encapsulate as list)
280 otherwise, use parents"
282 (check-parents parents
)
284 (if (not (objectp object
)) (setf object
(make-object-structure)))
286 (setf (ls-object-preclist object
) (ls-object-preclist *object
*))
287 (setf (ls-object-parents object
)
288 (cond ((null parents
) (list *object
*))
289 ((objectp parents
) (list parents
))
291 (setf (ls-object-preclist object
) (calculate-preclist object
))
295 (defun make-object (&rest parents
)
296 "Args: (&rest parents)
297 Returns a new object with parents PARENTS. If PARENTS is NIL, (list *OBJECT*) is used."
298 (make-basic-object parents NIL
))
300 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
302 ;;;; Constraint Hook Functions
304 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
306 (pushnew :constrainthooks
*features
*)
310 (defvar *message-hook
* nil
)
311 (defvar *set-slot-hook
* nil
)
313 (defun check-constraint-hooks (object sym slot
)
314 (let ((hook (if slot
*set-slot-hook
* *message-hook
*)))
317 (let ((*set-slot-hook
* nil
))
318 (funcall hook object sym
))
319 (let ((*message-hook
* nil
))
320 (funcall hook object sym
)))))))
322 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
324 ;;; Slot Access Functions
326 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
328 (defun make-slot-entry (x y
) (cons x y
))
329 (defun slot-entry-p (x) (consp x
))
330 (defun slot-entry-key (x) (first x
))
331 (defun slot-entry-value (x) (rest x
))
332 (defun set-slot-entry-value (x v
) (setf (rest x
) v
))
333 (defsetf slot-entry-value set-slot-entry-value
)
335 (defun find-own-slot (x slot
)
336 (if (objectp x
) (assoc-eq slot
(ls-object-slots x
))))
338 (defun find-slot (x slot
)
340 (let ((preclist (ls-object-preclist x
)))
341 (dolist (object preclist
)
342 (let ((slot-entry (find-own-slot object slot
)))
343 (if slot-entry
(return slot-entry
)))))))
345 (defun add-slot (x slot value
)
347 (check-non-nil-symbol slot
)
348 (let ((slot-entry (find-own-slot x slot
)))
350 (setf (slot-entry-value slot-entry
) value
)
351 (setf (ls-object-slots x
)
352 (cons (make-slot-entry slot value
) (ls-object-slots x
)))))
355 (defun delete-slot (x slot
)
357 (setf (ls-object-slots x
)
358 (delete slot
(ls-object-slots x
) :key
#'slot-entry-key
)))
360 (defun get-proto-slot-value (x slot
&optional no-err
)
362 (let ((slot-entry (find-slot x slot
)))
363 (if (slot-entry-p slot-entry
)
364 (slot-entry-value slot-entry
)
365 (unless no-err
(error "no slot named ~s in this object" slot
)))))
367 (defun set-proto-slot-value (x slot value
)
369 (let ((slot-entry (find-own-slot x slot
)))
371 ((slot-entry-p slot-entry
)
372 (set-slot-entry-value slot-entry value
)
373 #+:constrainthooks
(check-constraint-hooks x slot t
))
375 (if (find-slot x slot
) ;; either way we error...?
376 (error "object does not own slot ~s" slot
)
377 (error "no slot named ~s in this object" slot
))))))
379 ;;; FIXME: THIS WAS EVIL -- renamed SLOT-VALUE to PROTO-SLOT-VALUE, so
380 ;;; that we can do CLOS simultaneously.
381 (defun proto-slot-value (slot)
384 Must be used in a method. Returns the value of current objects slot
386 EVIL -- it conflicts with CLOS object slots."
387 (get-proto-slot-value (get-self) slot
))
389 (defun proto-slot-value-setf (slot value
)
390 (set-proto-slot-value (get-self) slot value
))
392 (defsetf proto-slot-value proto-slot-value-setf
)
394 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
396 ;;;; Method Access Functions;
398 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
400 (defun make-method-entry (x y
) (cons x y
))
401 (defun method-entry-p (x) (consp x
))
402 (defun method-entry-key (x) (first x
))
403 (defun method-entry-method (x) (rest x
))
404 (defun set-method-entry-method (x v
) (setf (rest x
) v
))
405 (defsetf method-entry-method set-method-entry-method
)
407 (defun find-own-method (x selector
)
408 (if (objectp x
) (assoc-eq selector
(ls-object-methods x
)))) ;; prev was assoc not assoc-eq
410 (defun find-lsos-method (x selector
)
412 (let ((preclist (ls-object-preclist x
)))
413 (dolist (object preclist
)
414 (let ((method-entry (find-own-method object selector
)))
415 (if method-entry
(return method-entry
)))))))
417 (defun add-lsos-method (x selector value
)
418 "x = object; selector = name of method; value = form computing the method."
420 (check-non-nil-symbol selector
)
421 (let ((method-entry (find-own-method x selector
)))
423 (setf (method-entry-method method-entry
) value
)
424 (setf (ls-object-methods x
)
425 (cons (make-method-entry selector value
) (ls-object-methods x
)))))
428 (defun delete-method (x selector
)
430 (setf (ls-object-methods x
)
431 (delete selector
(ls-object-methods x
) :key
#'method-entry-key
)))
433 (defun get-message-method (x selector
&optional no-err
)
435 (let ((method-entry (find-lsos-method x selector
)))
436 (if (method-entry-p method-entry
)
437 (method-entry-method method-entry
)
438 (unless no-err
(error "no method for selector ~s" selector
)))))
440 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
442 ;;; Message Sending Functions
444 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
446 (defvar *current-preclist
* nil
)
447 (defvar *current-selector
* nil
)
449 (defun sendmsg (object selector preclist args
)
450 (let ((method-entry nil
)
453 ;; look for the message in the precedence list
455 (setf method-entry
(find-own-method (first preclist
) selector
))
456 (if (or method-entry
(not (consp preclist
))) (return))
457 (setf preclist
(rest preclist
)))
459 ((null method-entry
) (error "no method for selector ~s" selector
))
460 ((not (method-entry-p method-entry
)) (error "bad method entry"))
461 (t (setf method
(method-entry-method method-entry
))))
464 (let ((*current-preclist
* preclist
)
465 (*current-selector
* selector
)
467 (multiple-value-prog1
468 (apply method object args
)
469 #+:constrainthooks
(check-constraint-hooks object selector nil
)))))
471 ;;;; built-in send function
472 (defun send (object selector
&rest args
)
473 "Args: (object selector &rest args)
474 Applies first method for SELECTOR found in OBJECT's precedence list to
476 (sendmsg object selector
(ls-object-preclist object
) args
))
478 ;;;FIXME: Need to include a "setter" for "send".
482 ;;;; call-next-method - call inherited version of current method
483 (defun call-next-method (&rest args
)
485 Funcalls next method for current selector and precedence list. Can only be
487 (sendmsg *self
* *current-selector
* (rest *current-preclist
*) args
))
489 (defun call-method (object selector
&rest args
)
490 "Args (object selector &rest args)
491 Funcalls method for SELECTOR found in OBJECT to SELF. Can only be used in
493 Call method belonging to another object on current object."
494 (sendmsg *self
* selector
(ls-object-preclist object
) args
))
496 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
498 ;;; Object Documentation
500 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
502 (defun find-documentation (x sym add
)
504 (let ((doc (find-own-slot x
'documentation
)))
505 (if (and (null doc
) add
) (add-slot x
'documentation nil
))
506 (if (slot-entry-p doc
) (assoc sym
(slot-entry-value doc
))))))
508 (defun add-documentation (x sym value
)
510 (check-non-nil-symbol sym
)
511 (let ((doc-entry (find-documentation x sym t
)))
513 ((not (null doc-entry
))
514 (setf (rest doc-entry
) value
))
516 (set-proto-slot-value x
518 (cons (cons sym value
)
519 (get-proto-slot-value x
'documentation
))))))
522 (defun get-documentation (x sym
)
524 (dolist (object (ls-object-preclist x
))
525 (let ((doc-entry (find-documentation object sym nil
))) ;; FIXME: verify
526 (if doc-entry
(return (rest doc-entry
))))))
532 (defmacro defmeth
(object name arglist first
&rest body
)
533 "Syntax: (defmeth object method-name lambda-list [doc] {form}*)
534 OBJECT must evaluate to an existing object. Installs a method for NAME in
535 the value of OBJECT and installs DOC in OBJECTS's documentation.
536 RETURNS: method-name."
537 ;; (declare (ignorable self)) ;; compiler hint that it isn't always used.
538 (if (and body
(stringp first
))
539 `(progn ;; first=docstring + body
540 (add-lsos-method ,object
,name
541 #'(lambda (self ,@arglist
) (block ,name
,@body
)))
542 (add-documentation ,object
,name
,first
)
544 `(progn ;; first=code + body
545 (add-lsos-method ,object
,name
546 #'(lambda (self ,@arglist
) (block ,name
,first
,@body
)))
550 ;;; Prototype Construction
553 (defun find-instance-slots (x slots
)
554 (let ((result (nreverse (delete-duplicates (copy-list slots
)))))
555 (dolist (parent (ls-object-parents x
) (nreverse result
))
556 (dolist (slot (get-proto-slot-value parent
'instance-slots
))
557 (pushnew slot result
)))))
559 (defun get-initial-proto-slot-value (object slot
)
560 (let ((entry (find-slot object slot
)))
561 (if (slot-entry-p entry
) (slot-entry-value entry
))))
563 (defun make-prototype (object name ivars cvars doc set
)
564 (setf ivars
(find-instance-slots object ivars
))
565 (add-slot object
'instance-slots ivars
)
566 (add-slot object
'proto-name name
)
568 (add-slot object slot
(get-initial-proto-slot-value object slot
)))
570 (add-slot object slot nil
))
572 (if (and doc
(stringp doc
))
573 (add-documentation object
'proto doc
))
574 (if set
(setf (symbol-value name
) object
)))
577 (defmacro defproto
(name &optional ivars cvars parents doc
)
578 "Syntax (defproto name &optional ivars cvars (parent *object*) doc)
579 Makes a new object prototype with instantiated/set variables IVARS,
580 'class' variables CVARS (class or 'cleared'??)) and parents
581 PARENT. PARENT can be a single object or a list of objects. IVARS and
582 CVARS must be lists."
583 (let ((obsym (gensym))
587 (let* ((,namesym
',name
)
589 (,obsym
(make-basic-object (if (listp ,parsym
)
591 (list ,parsym
)) ;; should this be ,@parsym ?
593 (make-prototype ,obsym
,namesym
,ivars
,cvars
,doc t
)
597 ;; Infrastructure for new defproto from Common-Lisp Cookbook! Thanks!
599 ;(defmacro odd-define (name buildargs)
600 ; `(progn (defun ,(build-symbol make-a- (:< name))
602 ; (vector ,(length buildargs) ',name ,@buildargs))
603 ; (defun ,(build-symbol test-whether- (:< name)) (x)
604 ; (and (vectorp x) (eq (aref x 1) ',name))
605 ; (defun ,(build-symbol (:< name) -copy) (x)
607 ; (defun ,(build-symbol (:< name) -deactivate) (x)
610 ;(defmacro for (listspec exp)
611 ; (cond ((and (= (length listspec) 3)
612 ; (symbolp (car listspec))
613 ; (eq (cadr listspec) ':in))
614 ; `(mapcar (lambda (,(car listspec))
616 ; ,(caddr listspec)))
617 ; (t (error "Ill-formed: ~s" `(for ,listspec ,exp)))))
619 ;(defmacro symstuff (l)
620 ; `(concatenate 'string
625 ; `',(format nil "~a" x))
627 ; `(format nil "~a" ,(cadr x)))
629 ; `(format nil "~a" (incf ,(cadr x))))
631 ; `(format nil "~a" ,x))))))
633 ;(defmacro build-symbol (&rest l)
634 ; (let ((p (find-if (lambda (x)
636 ; (eq (car x) ':package)))
639 ; (setq l (remove p l))))
640 ; (let ((pkg (cond ((eq (cadr p) 'nil)
642 ; (t `(find-package ',(cadr p))))))
645 ; `(values (intern ,(symstuff l) ,pkg)))
647 ; `(make-symbol ,(symstuff l)))))
649 ; `(values (intern ,(symstuff l))))))))
651 (defmacro defproto2
(name &optional ivars cvars parents doc
)
652 "Syntax (defproto name &optional ivars cvars (parent *object*) doc)
653 Makes a new object prototype with instance variables IVARS, 'class'
654 variables CVARS and parents PARENT. PARENT can be a single object or
655 a list of objects. IVARS and CVARS must be lists."
657 (error "can not rebind a prototype object yet")
658 (let ((namesym (gensym))
662 (let* ((,namesym
,name
)
664 (,obsym
(make-basic-object
667 (list ,@parsym
)) ;; should this be ,@parsym ?
669 (make-prototype ,obsym
,name
,ivars
,cvars
,doc t
)
673 ;; , => turn on evaluation again (not macro substitution)
674 ;; ` => template comes (use , to undo template and restore eval
675 ;; ' => regular quote (not special in this context), 'ted => (quote ted)
678 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
680 ;;; Initialize the Root Object
682 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
684 (setf (ls-object-preclist *object
*) (list *object
*))
685 (add-slot *object
* 'instance-slots nil
)
686 (add-slot *object
* 'proto-name
'*object
*)
687 (add-slot *object
* 'documentation nil
)
689 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
693 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
695 (defmeth *object
* :nop
(&rest args
)
698 Do a NOP. Used to quiet compiler warnings."
701 (defmeth *object
* :isnew
(&rest args
)
702 "Method args: (&rest args)
703 Checks ARGS for keyword arguments matching slots and uses them to
706 (dolist (slot-entry (ls-object-slots self
))
707 (let* ((slot (slot-entry-key slot-entry
))
708 (key (intern (symbol-name slot
) (find-package 'keyword
)))
709 (val (proto-slot-value slot
))
710 (new-val (getf args key val
)))
711 (unless (eq val new-val
) (setf (proto-slot-value slot
) new-val
)))))
714 (defmeth *object
* :has-slot
(slot &key own
)
715 "Method args: (slot &optional own)
716 Returns T if slot SLOT exists, NIL if not. If OWN is not NIL
717 only checks the object; otherwise check the entire precedence list."
718 (let ((entry (if own
(find-own-slot self slot
) (find-slot self slot
))))
721 (defmeth *object
* :add-slot
(slot &optional value
)
722 "Method args: (slot &optional value)
723 Installs slot SLOT in object, if it does not already exist, and
724 sets its value to VLAUE."
725 (add-slot self slot value
)
728 (defmeth *object
* :delete-slot
(slot)
730 Deletes slot SLOT from object if it exists."
731 (delete-slot self slot
)
734 (defmeth *object
* :own-slots
()
736 Returns list of names of slots owned by object."
737 (mapcar #'slot-entry-key
(ls-object-slots self
)))
739 (defmeth *object
* :has-method
(selector &key own
)
740 "Method args: (selector &optional own)
741 Returns T if method for SELECTOR exists, NIL if not. If OWN is not NIL
742 only checks the object; otherwise check the entire precedence list."
744 (find-own-method self selector
)
745 (find-lsos-method self selector
))))
748 (defmeth *object
* :add-method
(selector method
)
749 "Method args: (selector method)
750 Installs METHOD for SELECTOR in object."
751 (add-lsos-method self selector method
)
754 (defmeth *object
* :delete-method
(selector)
755 "Method args: (selector)
756 Deletes method for SELECTOR in object if it exists."
757 (delete-method self selector
)
760 (defmeth *object
* :get-method
(selector)
761 "Method args: (selector)
762 Returns method for SELECTOR symbol from object's precedence list."
763 (get-message-method self selector
))
765 (defmeth *object
* :own-methods
()
767 Returns copy of selectors for methods owned by object."
768 (mapcar #'method-entry-key
(ls-object-methods self
)))
770 (defmeth *object
* :parents
()
772 Returns copy of parents list."
773 (copy-list (ls-object-parents self
)))
775 (defmeth *object
* :precedence-list
()
777 Returns copy of the precedence list."
778 (copy-list (ls-object-preclist self
)))
780 (defmeth *object
* :show
(&optional
(stream t
))
782 Prints object's internal data."
783 (format stream
"Slots = ~s~%" (ls-object-slots self
))
784 (format stream
"Methods = ~s~%" (ls-object-methods self
))
785 (format stream
"Parents = ~s~%" (ls-object-parents self
))
786 (format stream
"Precedence List = ~s~%" (ls-object-preclist self
))
789 (defmeth *object
* :reparent
(&rest parents
)
790 "Method args: (&rest parents)
791 Changes precedence list to correspond to PARENTS. Does not change descendants."
792 (make-basic-object parents self
))
794 (defmeth *object
* :make-prototype
(name &optional ivars
)
795 (make-prototype self name ivars nil nil nil
)
798 (defmeth *object
* :internal-doc
(sym &optional new
)
799 "Method args (topic &optional value)
800 Retrieves or installs documentation for topic."
801 (if new
(add-documentation self sym new
))
802 (get-documentation self sym
))
804 (defmeth *object
* :new
(&rest args
)
805 "Method args: (&rest args)
806 Creates new object using self as prototype."
807 (let* ((object (make-object self
)))
808 (if (proto-slot-value 'instance-slots
)
809 (dolist (s (proto-slot-value 'instance-slots
))
810 (send object
:add-slot s
(proto-slot-value s
))))
811 (apply #'send object
:isnew args
)
814 (defmeth *object
* :retype
(proto &rest args
)
815 "Method args: (proto &rest args)
816 Changes object to inherit directly from prototype PROTO. PROTO
817 must be a prototype and SELF must not be one."
818 (if (send self
:has-slot
'instance-slots
:own t
)
819 (error "can't retype a prototype"))
820 (if (not (send proto
:has-slot
'instance-slots
:own t
))
821 (error "not a prototype - ~a" proto
))
822 (send self
:reparent proto
)
823 (dolist (s (send proto
:proto-slot-value
'instance-slots
))
824 (send self
:add-slot s
(proto-slot-value s
)))
825 (apply #'send self
:isnew args
)
828 (defmeth *object
* :print
(&optional
(stream *standard-output
*))
829 "Method args: (&optional (stream *standard-output*))
830 Default object printing method."
832 ((send self
:has-slot
'proto-name
)
834 "#<Object: ~D, prototype = ~A>"
835 (ls-object-serial self
)
836 (proto-slot-value 'proto-name
)))
837 (t (format stream
"#<Object: ~D>" (ls-object-serial self
)))))
839 (defmeth *object
* :proto-slot-value
(sym &optional
(val nil set
))
840 "Method args: (sym &optional val)
842 Sets and retrieves value of slot named SYM. Signals an error if slot
845 (if set
(setf (proto-slot-value sym
) val
))
846 (proto-slot-value sym
))
848 (defmeth *object
* :slot-names
()
850 Returns list of slots available to the object."
852 (mapcar #'(lambda (x) (send x
:own-slots
))
853 (send self
:precedence-list
))))
855 (defmeth *object
* :method-selectors
()
857 Returns list of method selectors available to object."
859 (mapcar #'(lambda (x) (send x
:own-methods
))
860 (send self
:precedence-list
))))
863 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
865 ;;;; Object Help Methods
867 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
869 (defmeth *object
* :doc-topics
()
871 Returns all topics with documentation for this object."
877 (if (send x
:has-slot
'documentation
:own t
)
878 (send x
:proto-slot-value
(quote documentation
))))
879 (send self
:precedence-list
))))))
881 (defmeth *object
* :documentation
(topic &optional
(val nil set
))
882 "Method args: (topic &optional val)
883 Retrieves or sets object documentation for topic."
884 (if set
(send self
:internal-doc topic val
))
885 (let ((val (dolist (i (send self
:precedence-list
))
886 (let ((val (send i
:internal-doc topic
)))
887 (if val
(return val
))))))
890 (defmeth *object
* :delete-documentation
(topic)
891 "Method args: (topic)
892 Deletes object documentation for TOPIC."
893 (setf (proto-slot-value 'documentation
)
894 ;;(remove :title nil :test #'(lambda (x y) (eql x (first y)))) ;; original
895 (remove topic
(send self
:documentation
) :test
#'(lambda (x y
) (eql x
(first y
))))) ;; AJR:PROBLEM?
898 (defmeth *object
* :help
(&optional topic
)
899 "Method args: (&optional topic)
900 Prints help message for TOPIC, or genreal help if TOPIC is NIL."
902 (let ((doc (send self
:documentation topic
)))
904 (doc (princ topic
) (terpri) (princ doc
) (terpri))
905 (t (format t
"Sorry, no help available on ~a~%" topic
))))
906 (let ((topics (stable-sort (copy-seq (send self
:doc-topics
))
908 (string-lessp (string x
) (string y
)))))
909 (proto-doc (send self
:documentation
'proto
)))
910 (if (send self
:has-slot
'proto-name
)
911 (format t
"~s~%" (proto-slot-value 'proto-name
)))
912 (when proto-doc
(princ proto-doc
) (terpri))
913 (format t
"Help is available on the following:~%~%")
914 (dolist (i topics
) (format t
"~s " i
))
918 (defmeth *object
* :compile-method
(name)
920 Compiles method NAME unless it is already compiled. The object must
922 (unless (send self
:has-method name
)
923 (error "No ~s method in this object" name
))
924 (unless (send self
:has-method name
:own t
)
925 (error "Object does not own ~s method" name
))
926 (let ((fun (send self
:get-method name
)))
927 (unless (compiled-function-p fun
)
928 (multiple-value-bind (form env
) (function-lambda-expression fun
)
931 "method may have been defined in non-null environment"))
932 (send self
:add-method name
(compile nil form
))))))