r8844: laptop updates
[kmrcl.git] / attrib-class.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: kmrcl-*-
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-2003 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 ;; This does not work in Lispworks prior to version 4.3
61
62 (defmethod kmr-mop:compute-slots ((class attributes-class))
63   (let* ((normal-slots (call-next-method))
64          (alist (mapcar
65                  #'(lambda (slot)
66                      (cons (kmr-mop:slot-definition-name slot)
67                            (mapcar #'(lambda (attr) (list attr))
68                                    (esd-attributes slot))))
69                  normal-slots)))
70
71     (cons (make-instance
72            'attributes-esd
73            :name 'all-attributes
74            :initform `',alist
75            :initfunction #'(lambda () alist)
76            :allocation :instance
77            :documentation "Attribute bucket"
78            :type t
79            )
80           normal-slots)))
81   
82 (defun slot-attribute (instance slot-name attribute)
83   (cdr (slot-attribute-bucket instance slot-name attribute)))
84
85 (defun (setf slot-attribute) (new-value instance slot-name attribute)
86   (setf (cdr (slot-attribute-bucket instance slot-name attribute))
87     new-value))
88
89 (defun slot-attribute-bucket (instance slot-name attribute)
90   (let* ((all-buckets (slot-value instance 'all-attributes))
91          (slot-bucket (assoc slot-name all-buckets)))
92     (unless slot-bucket
93       (error "The slot named ~S of ~S has no attributes."
94              slot-name instance))
95     (let ((attr-bucket (assoc attribute (cdr slot-bucket))))
96       (unless attr-bucket
97         (error "The slot named ~S of ~S has no attributes named ~S."
98                slot-name instance attribute))
99       attr-bucket)))
100
101
102