home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mitsch75.zip / scheme-7_5_17-src.zip / scheme-7.5.17 / src / sos / slot.scm < prev    next >
Text File  |  1999-01-02  |  7KB  |  206 lines

  1. ;;; -*-Scheme-*-
  2. ;;;
  3. ;;; $Id: slot.scm,v 1.7 1999/01/02 06:19:10 cph Exp $
  4. ;;;
  5. ;;; Copyright (c) 1995-1999 Massachusetts Institute of Technology
  6. ;;;
  7. ;;; This program is free software; you can redistribute it and/or
  8. ;;; modify it under the terms of the GNU General Public License as
  9. ;;; published by the Free Software Foundation; either version 2 of the
  10. ;;; License, or (at your option) any later version.
  11. ;;;
  12. ;;; This program is distributed in the hope that it will be useful,
  13. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. ;;; General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with this program; if not, write to the Free Software
  19. ;;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. ;;;; Instance Slots
  22.  
  23. (declare (usual-integrations))
  24.  
  25. (define-structure (slot-descriptor (conc-name slot-descriptor/))
  26.   (name #f read-only #t)
  27.   (class #f read-only #t)
  28.   (index #f read-only #t)
  29.   (properties #f))
  30.  
  31. (define (slot-name slot)
  32.   (guarantee-slot-descriptor slot 'SLOT-NAME)
  33.   (slot-descriptor/name slot))
  34.  
  35. (define (slot-class slot)
  36.   (guarantee-slot-descriptor slot 'SLOT-CLASS)
  37.   (slot-descriptor/class slot))
  38.  
  39. (define (slot-index slot)
  40.   (guarantee-slot-descriptor slot 'SLOT-INDEX)
  41.   (slot-descriptor/index slot))
  42.  
  43. (define (slot-property slot key default)
  44.   (let ((entry (assq key (slot-descriptor/properties slot))))
  45.     (if entry
  46.     (cdr entry)
  47.     default)))
  48.  
  49. (define (slot-properties slot)
  50.   (alist-copy (slot-descriptor/properties slot)))
  51.  
  52. (define (slot-initializer slot)
  53.   (slot-property slot 'INITIALIZER #f))
  54.  
  55. (define (slot-initial-value slot)
  56.   (slot-property slot 'INITIAL-VALUE record-slot-uninitialized))
  57.  
  58. (define (slot-initial-value? slot)
  59.   (not (eq? record-slot-uninitialized (slot-initial-value slot))))
  60.  
  61. (define (guarantee-slot-descriptor slot name)
  62.   (if (not (slot-descriptor? slot))
  63.       (error:wrong-type-argument slot "slot descriptor" name)))
  64.  
  65. (add-generic-procedure-generator %record-slot-index
  66.   (lambda (generic tags)
  67.     generic
  68.     (and (class? (dispatch-tag-contents (car tags)))
  69.      (lambda (instance name)
  70.        (let ((slot (class-slot (object-class instance) name #f)))
  71.          (and slot
  72.           (slot-index slot)))))))
  73.  
  74. (add-generic-procedure-generator %record-slot-names
  75.   (lambda (generic tags)
  76.     generic
  77.     (and (class? (dispatch-tag-contents (car tags)))
  78.      (lambda (instance)
  79.        (map slot-name (class-slots (object-class instance)))))))
  80.  
  81. ;;;; Slot Accessors
  82.  
  83. (define (method-constructor make-generator)
  84.   (letrec
  85.       ((constructor
  86.     (lambda (class name)
  87.       (if (class-slot class name #f)
  88.           (make-computed-method (list class)
  89.         (let ((generator (make-generator name)))
  90.           (lambda classes
  91.             (generator #f (map class->dispatch-tag classes)))))
  92.           (constructor class (error:no-such-slot class name))))))
  93.     constructor))
  94.  
  95. (define slot-accessor-method (method-constructor %record-accessor-generator))
  96. (define slot-modifier-method (method-constructor %record-modifier-generator))
  97. (define slot-initpred-method (method-constructor %record-initpred-generator))
  98.  
  99. (define (accessor-constructor arity make-method)
  100.   (lambda (class name)
  101.     (let ((generic (make-generic-procedure arity)))
  102.       (add-method generic (make-method class name))
  103.       generic)))
  104.  
  105. (define slot-accessor (accessor-constructor 1 slot-accessor-method))
  106. (define slot-modifier (accessor-constructor 2 slot-modifier-method))
  107. (define slot-initpred (accessor-constructor 1 slot-initpred-method))
  108.  
  109. (define (install-slot-accessor-methods class)
  110.   (for-each
  111.    (lambda (name)
  112.      (let* ((slot (class-slot class name #t))
  113.         (install
  114.          (lambda (keyword maker)
  115.            (let ((accessor (slot-property slot keyword #f)))
  116.          (if accessor
  117.              (begin
  118.                (add-method accessor (maker class name))
  119.                (set-slot-descriptor/properties!
  120.             slot
  121.             (del-assq! keyword
  122.                    (slot-descriptor/properties slot)))))))))
  123.        (install 'ACCESSOR slot-accessor-method)
  124.        (install 'MODIFIER slot-modifier-method)
  125.        (install 'INITPRED slot-initpred-method)))
  126.    (class-direct-slot-names class)))
  127.  
  128. (define (slot-value object name)
  129.   (%record-ref object (compute-slot-index object name)))
  130.  
  131. (define (set-slot-value! object name value)
  132.   (%record-set! object (compute-slot-index object name) value))
  133.  
  134. (define (slot-initialized? object name)
  135.   (not (eq? record-slot-uninitialized
  136.         (%record-ref object (compute-slot-index object name)))))
  137.  
  138. (define (compute-slot-index object name)
  139.   (or (%record-slot-index object name)
  140.       (error:no-such-slot (object-class object) name)))
  141.  
  142. ;;;; Slot Arguments
  143.  
  144. (define (canonicalize-slot-argument argument caller)
  145.   (cond ((symbol? argument)
  146.      (list argument))
  147.     ((and (pair? argument)
  148.           (symbol? (car argument))
  149.           (slot-argument-plist? (cdr argument)))
  150.      argument)
  151.     (else
  152.      (error:bad-range-argument argument caller))))
  153.  
  154. (define (slot-argument-plist? object)
  155.   (let loop ((l1 object) (l2 object))
  156.     (if (pair? l1)
  157.     (and (not (eq? (cdr l1) l2))
  158.          (symbol? (car l1))
  159.          (pair? (cdr l1))
  160.          (loop (cddr l1) (cdr l2)))
  161.     (null? l1))))
  162.  
  163. (define (compute-slot-descriptor class slots index)
  164.   (let ((slot (merge-slot-arguments slots)))
  165.     (make-slot-descriptor (car slot) class index (cdr slot))))
  166.  
  167. (define (merge-slot-arguments slots)
  168.   (let ((slots
  169.      (reverse!
  170.       (map (lambda (slot)
  171.          (cons (car slot)
  172.                (plist->alist (cdr slot))))
  173.            slots))))
  174.     (let ((result (car slots)))
  175.       (for-each
  176.        (lambda (slot)
  177.      (for-each
  178.       (lambda (x)
  179.         (let ((names
  180.            (or (list-search-positive interacting-options
  181.              (lambda (names)
  182.                (memq (car x) names)))
  183.                (list (car x)))))
  184.           (let ((entry
  185.              (let loop ((names names))
  186.                (and (not (null? names))
  187.                 (or (assq (car names) (cdr result))
  188.                 (loop (cdr names)))))))
  189.         (if entry
  190.             (begin
  191.               (set-car! entry (car x))
  192.               (set-cdr! entry (cdr x)))
  193.             (set-cdr! result (cons x (cdr result)))))))
  194.       (cdr slot)))
  195.        (cdr slots))
  196.       result)))
  197.  
  198. (define interacting-options
  199.   '((INITIAL-VALUE INITIALIZER)))
  200.  
  201. (define (plist->alist plist)
  202.   (let loop ((plist plist) (alist '()))
  203.     (if (null? plist)
  204.     alist
  205.     (loop (cddr plist)
  206.           (cons (cons (car plist) (cadr plist)) alist)))))