FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
May 07, 2007

Graphic Objects

(Page 3 of 5)

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.

Previous Page | 1 Introduction | 2 Tiny Database Program | 3 Demons | 4 Simple Graphic Objects | 5 Fancier Graphic Objects Next Page
TOP 5 ARTICLES
No Top Articles.
DR. DOBB'S CAREER CENTER
Ready to take that job and shove it? open | close
Search jobs on Dr. Dobb's TechCareers
Function:

Keyword(s):

State:  
  • Post Your Resume
  • Employers Area
  • News & Features
  • Blogs & Forums
  • Career Resources

    Browse By:
    Location | Employer | City
  • Most Recent Posts:
    MEDIA CENTER  more
    NetSeminar
    Modernize your Development by Moving Build and Code Quality Upstream
    Moderated by Jon Erickson, Editor-in-Chief of Dr. Dobb's, this interactive panel discussion brings industry experts Anders Wallgren, CTO of Electric Cloud and Gwyn Fisher, CTO of Klocwork together for a candid discussion of the cost savings, productivity and quality benefits that can be achieved by stabilizing builds and code quality as early in the development cycle as possible.

    The reality of today's development environment - geographically distributed teams, the use of Agile development practices, increasing application complexity, etc. - is straining the viability of the traditional coding, build and release process. To stay ahead of the curve, development teams are modernizing their approach to dealing with these issues, and as a result are achieving new levels of development productivity. Register for the webcast.
    Date: Wednesday, July 15, 2009
    Time: 11 am PT/2 pm ET
    Modernize your Development by Moving Build and Code Quality Upstream
    Moderated by Jon Erickson, Editor-in-Chief of Dr. Dobb's, this interactive panel discussion brings industry experts Anders Wallgren, CTO of Electric Cloud and Gwyn Fisher, CTO of Klocwork together for a candid discussion of the cost savings, productivity and quality benefits that can be achieved by stabilizing builds and code quality as early in the development cycle as possible.

    The reality of today's development environment - geographically distributed teams, the use of Agile development practices, increasing application complexity, etc. - is straining the viability of the traditional coding, build and release process. To stay ahead of the curve, development teams are modernizing their approach to dealing with these issues, and as a result are achieving new levels of development productivity. Register for the webcast.
    Date: Wednesday, July 15, 2009
    Time: 11 am PT/2 pm ET
                                   
    INFO-LINK

    Resource Links: