r8005: Automated commit for kmrcl debian-version-1.58-1
[kmrcl.git] / attrib-class.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          attrib-class.lisp
6 ;;;; Purpose:       Defines metaclass allowing use of attributes on slots
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Apr 2000
9 ;;;;
10 ;;;; $Id$
11 ;;;;
12 ;;;; This file, part of KMRCL, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; KMRCL users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
18
19 ;;;; Defines a metaclass that allows the use of attributes (or subslots)
20 ;;;; on slots. Based on example in AMOP, but modified to use ACL's MOP.
21
22 (in-package #:kmrcl)
23
24 (defclass attributes-class (kmr-mop:standard-class)
25   ()
26   (:documentation "metaclass that implements attributes on slots. Based
27 on example from AMOP"))
28
29 (defclass attributes-dsd (kmr-mop:standard-direct-slot-definition)
30   ((attributes :initarg :attributes :initform nil
31                :accessor dsd-attributes)))
32
33 (defclass attributes-esd (kmr-mop:standard-effective-slot-definition)
34   ((attributes :initarg :attributes :initform nil 
35                :accessor esd-attributes)))
36
37 ;; encapsulating macro for Lispworks
38 (kmr-mop:process-slot-option attributes-class :attributes)
39
40 #+(or cmu scl sbcl openmcl)
41 (defmethod kmr-mop:validate-superclass ((class attributes-class)
42                                         (superclass kmr-mop:standard-class))
43   t)
44
45 (defmethod kmr-mop:direct-slot-definition-class ((cl attributes-class) #+kmr-normal-dsdc &rest initargs)
46   (declare (ignore initargs))
47   (kmr-mop:find-class 'attributes-dsd))
48
49 (defmethod kmr-mop:effective-slot-definition-class ((cl attributes-class) #+kmr-normal-dsdc &rest initargs)
50   (declare (ignore initargs))
51   (kmr-mop:find-class 'attributes-esd))
52
53 (defmethod kmr-mop:compute-effective-slot-definition
54     ((cl attributes-class) #+kmr-normal-cesd name dsds)
55   #+kmr-normal-cesd (declare (ignore name))
56   (let ((esd (call-next-method)))
57     (setf (esd-attributes esd) (remove-duplicates (mapappend #'dsd-attributes dsds)))
58     esd))
59
60 (defmethod kmr-mop:compute-slots ((class attributes-class))
61   (let* ((normal-slots (call-next-method))
62          (alist (mapcar
63                  #'(lambda (slot)
64                      (cons (kmr-mop:slot-definition-name slot)
65                            (mapcar #'(lambda (attr) (list attr))
66                                    (esd-attributes slot))))
67                  normal-slots)))
68     (cons (make-instance
69            'attributes-esd
70            :name 'all-attributes
71            :initform `',alist
72            :initfunction #'(lambda () alist)
73            :allocation :instance
74            :documentation "Attribute bucker"
75            :type t
76            #-lispworks :class #-lispworks class
77            ;; This is an attempted work-around -- lispworks doesn't work
78            ;; it appears to setup storage someplace
79            ;; #+lispworks :location #+lispworks (length normal-slots)
80            )
81           normal-slots)))
82   
83 (defun slot-attribute (instance slot-name attribute)
84   (cdr (slot-attribute-bucket instance slot-name attribute)))
85
86 (defun (setf slot-attribute) (new-value instance slot-name attribute)
87   (setf (cdr (slot-attribute-bucket instance slot-name attribute))
88     new-value))
89
90 (defun slot-attribute-bucket (instance slot-name attribute)
91   (let* ((all-buckets (slot-value instance 'all-attributes))
92          (slot-bucket (assoc slot-name all-buckets)))
93     (unless slot-bucket
94       (error "The slot named ~S of ~S has no attributes."
95              slot-name instance))
96     (let ((attr-bucket (assoc attribute (cdr slot-bucket))))
97       (unless attr-bucket
98         (error "The slot named ~S of ~S has no attributes named ~S."
99                slot-name instance attribute))
100       attr-bucket)))
101
102
103