r4676: Auto commit for Debian build
[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: attrib-class.lisp,v 1.9 2003/04/29 03:50:42 kevin Exp $
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-dsd (kmr-mop:standard-direct-slot-definition)
25   ((attributes :initarg :attributes :initform nil
26                :accessor dsd-attributes)))
27
28 (defclass attributes-esd (kmr-mop:standard-effective-slot-definition)
29   ((attributes :initarg :attributes :initform nil 
30                :accessor esd-attributes)))
31
32
33 (defclass attributes-class (kmr-mop:standard-class)
34   ()
35   (:documentation "metaclass that implements attributes on slots. Based
36 on example from AMOP"))
37
38
39 #+(or cmu scl sbcl)
40 (defmethod kmr-mop:validate-superclass ((class attributes-class)
41                                         (superclass kmr-mop:standard-class))
42   t)
43
44 (defmethod kmr-mop:direct-slot-definition-class ((cl attributes-class) #+kmr-normal-dsdc &rest initargs)
45   (declare (ignore initargs))
46   (kmr-mop:find-class 'attributes-dsd))
47
48 (defmethod kmr-mop:compute-effective-slot-definition :around
49     ((cl attributes-class) #+kmr-normal-cesd name dsds)
50   #+kmr-normal-cesd (declare (ignore name))
51   (apply
52    #'make-instance 'attributes-esd 
53    :attributes (remove-duplicates (mapappend #'dsd-attributes dsds))
54    (kmr-mop:compute-effective-slot-definition-initargs cl dsds))
55   )
56
57 #+ignore
58 (defmethod kmr-mop:compute-effective-slot-definition :around
59     ((cl attributes-class) #+kmr-named-cesd name dsds)
60   #+kmr-named-cesd (declare (ignore name))
61   (let ((normal-slot (call-next-method)))
62     (setf (esd-attributes normal-slot)
63       (remove-duplicates
64        (mapappend #'esd-attributes dsds)))
65     normal-slot))
66
67
68 (defmethod kmr-mop:compute-slots ((class attributes-class))
69   (let* ((normal-slots (call-next-method))
70          (alist
71           (mapcar
72            #'(lambda (slot)
73                (let ((attr-list (mapcar #'(lambda (attr) (cons attr nil))
74                                         (esd-attributes slot))))
75                  (when attr-list
76                    (cons (kmr-mop:slot-definition-name slot) attr-list))))
77            normal-slots)))
78     (setq alist (delete nil alist))
79     (cons (make-instance 'kmr-mop:standard-effective-slot-definition
80             :name 'all-attributes
81             :initform `',alist
82             :initfunction #'(lambda () alist))
83           normal-slots)))
84   
85 (defun slot-attribute (instance slot-name attribute)
86   (cdr (slot-attribute-bucket instance slot-name attribute)))
87
88 (defun (setf slot-attribute) (new-value instance slot-name attribute)
89   (setf (cdr (slot-attribute-bucket instance slot-name attribute))
90     new-value))
91
92 (defun slot-attribute-bucket (instance slot-name attribute)
93   (let* ((all-buckets (slot-value instance 'all-attributes))
94          (slot-bucket (assoc slot-name all-buckets)))
95     (unless slot-bucket
96       (error "The slot named ~S of ~S has no attributes."
97              slot-name instance))
98     (let ((attr-bucket (assoc attribute (cdr slot-bucket))))
99       (unless attr-bucket
100         (error "The slot named ~S of ~S has no attributes named ~S."
101                slot-name instance attribute))
102       attr-bucket)))
103
104 (eval-when (:compile-toplevel :load-toplevel :execute)
105   (export '(attributes-class slot-attributes)))
106