Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Parallel

Lisp: Classes in the Metaobject Protocol


Now let's look at slow-value to see how the machinery in place will accomplish this. It is important to remember that a real CLOS imple mentation will not use a mechanism as inefficient as what I'll show for slot access.

The first ingredient in slot access is a function named slot-location, which takes a class and a slot name and finds the relative position in the list of effective slot definitions for that slot. Listing 7 shows the code. slot-location simply finds the index of the slot whose name is slot-namein the list of effecitve slot definitions stored in the class. Only local slots are checked, because only local slots have storage allocated in each instance. (Check the arguments passed to allocate-slot-storage in Listing 4).

(defun slot-location (class slot-name)
  (let ((slot (find slot-name
                    (class-slots class)
                    :key #'slot-definition-name)))
    (if (null slot)
        (error "The slot ~S is missing from the class ~S."
               slot-name class)
        (let ((pos (position slot
                             (remove-if-not #'instance-slot-p
                                            (class-slots class)))))
          (if (null pos)
              (error "The slot ~S is not an instance~@
                      slot in the class ~S."
                     slot-name class)
              pos))))))
Listing 7: slot-location.

The next ingredient is slot-value (Listing 8), which just calls slot-value-using-class. The method for slot-value-using-class on standard-class finds the location of the slot in the class object, gets the slots vector from the underlying instance representation, does an svref via slot-contents to get the value, checks the value to determine whether the slot is bound, and returns the value if all is well. (self slot-value) is similar.

(defun class-of (x)
  (if (std-instance-p x)
      (std-instance-class x)
      (built-in-class-of x)))

(defun slot-contents (slots location)
  (svref slots location))

(defun slot-value (object slot-name)
  (slot-value-using-class (class-of object) object slot-name))

(defmethod slot-value-using-class
   ((class standard-class) instance slot-name)
  (let* ((location (slot-location (class-of instance) slot-name))
         (slots (std-instance-slots instance))
         (val (slot-contents slots location)))
    (if (eq secret-unbound-value val)
        (error "The slot ~S is unbound in the object ~S."
               slot-name instance)
        val)))
Listing 8: slot=value.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.