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

Graphic Objects


Demons

The next step is to decide what form attachments should take. In the parlance of object-oriented programming, a demon is something that keeps watch over an object and does something whenever it changes. The things we are going to store in the attachments of database entries will be demons, defined thus:

(defstruct demon
   (pre-action #'nilfn)
   (post-action #'nilfn)
   (self-destruct #'nilfn))
   window
(defun nilfn (&rest args)
   (ignore args))

If a demon is the sort that runs a graphic object, its window field will contain the window in which the graphic object resides. Demons do not necessarily have to run graphic objects -- if one doesn't, its window field will simply be nil.

The three fields of a demon structure will contain functions (more precisely, closures) that, when called, perform certain actions. Note that the first three functions in Listing Two all have the same form. The first one, pre-update, is the function called by db-update just before it overwrites a database entry. The effect of calling pre-uPdate for an entry is to call the pre-action function of each of its demons. The functions post-update and delete-attachments behave analogously.

(defun pre-update (entry)
  (dolist (d (db-entry-attachments entry))
    (funcall (demon-pre-action d))))

(defun post-update (entry)
  (dolist (d (db-entry-attachments entry))
    (funcall (demon-post-action d))))

(defun delete-update (entry)
  (dolist (d (db-entry-attachments entry))
    (funcall (demon-self-destruct d))))

(defun add-demon (db key access-fn pre-fn post-fn)
  (let ((entry (gethash key db))
     (d (make-demon)))
  (setf (demon-pre-action d)
     #'(lambda ()
       (funcall pre-fn
           (funcall access-fn
               (db-entry-value entry)))))

   (setf (demon-post-action d)
    #'(lambda()
     (funcall post-fn
           (funcall access-fn
               (db-entry-value entry)))))

   (setf (demon-self-destruct d)
    #'(lambda()
      (delete d(db-entry-attachments entry))))
   (push d (db-entry-attachments entry))
   d))

Listing Two: Code supporting demons.

Listing Three shows how demons are used. We'll attach a demon to the temperature entry of our-db, (in accordance with the greenhouse effect) sets the temp-in-50-years entry to be 10 degrees' higher. There is no need for this end after only one cycle. If we wanted, we could attach further demons to temp-in-50-years, which would update sea-level-in-50-years, and so on.

> (setq d
   (let ((entry (gethas 'temperature our-db)))
   (make-demon
     :post-action
     #'(lambda ()
       (db-update
         our-db
         'temp-in-50-years
         (+ (db-entry-value entry)
            10))))))
#S(DEMON...)
>  (push d(db-entry-attachments
           (gethash 'temperature our-db)))
(#S(DEMON...))
>  (db-update our-db 'tempature 98)
98
>  (db-query out-db 'temp-in-50-years)
108

Listing Three: Example of what we can do with demons.

The remaining function in Listing Two, add-demon, does more or less what we did in Listing Three. This function looks a bit imposing, but it's long only because it's rather repetitive.

One thing that might seem confusing is the purpose of the parameter access-fn. We need it because the value field of the entry which we attach a demon might contain a structure instead of a simple atom, as temperature did. In that case we may want the demon to be attached to a subpart of the structure. The access-fn specifies where the demon should attached to within the entry's value field. If we had used add-demon to attach a demon to the temperature entry as the access-fn since we are looking at the value field itself. If we attaching a demon to an entry whose value field contained a list and wanted the demon to react specifically to the car of the list, we'd see #'car as the access-fn.

Note that add-demon fills new demon's fields with closures, not merely functions. The pre-action and post-action functions are closed over the entry to they are attached, and the self-destruct function is closed over the demon itself.


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.