NextPrevUpTopContentsIndex

8.3.1.1 Complete example of a top-level dspec class

Define a saved-value object which has a name and a value:

(defstruct saved-value
  name
  value)

The objects are defined using def-saved-value and stored on the plist of their name:

(defmacro def-saved-value (name value)
  `(dspec:def (def-saved-value ,name)
     (when (record-definition `(def-saved-value ,',name)
                               (dspec:location))
       (setf (get ',name 'saved-value)
             (make-saved-value :name ',name
                            :value ,value))
       ',name)))

Define a function to retrieve the saved-value object:

(defun find-saved-value (name)
  (get name 'saved-value))

Define a macro to access a saved-value object:

(defmacro saved-value (name)
  `(saved-value-value (find-saved-value ',name)))

Define a dspec class for def-saved-value dspecs:

(dspec:define-dspec-class def-saved-value nil 
  "Defined saved values"
  :definedp 
  #'(lambda (name)
      ;; Find any object that def-saved-value recorded
      (not (null (find-saved-value name))))
  :undefiner
  #'(lambda (dspec)
      ;; Remove what def-saved-value recorded
      `(remprop ,(dspec:dspec-name dspec) 'saved-value))
  :object-dspec
  #'(lambda (obj)
      ;; Given a saved-value object, we can reconstruct its dspec
      (and (saved-value-p obj)
           `(def-saved-value ,(saved-value-name obj)))))

For completeness, define a form parser that generates dspecs from forms:

(dspec:define-form-parser 
    (def-saved-value
     (:parser dspec:single-form-form-parser)))

Note: this form parser for def-saved-value is not strictly necessary, because the system provides an implicit form parser which recognizes definitions beginning with "def".


LispWorks User Guide - 11 Mar 2008

NextPrevUpTopContentsIndex