fix conflicts
[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 ;; Disable attrib class until understand changes in sbcl/cmucl
20 ;; using COMPUTE-SLOT-ACCESSOR-INFO and defining method
21 ;; for slot access of ALL-ATTRIBUTES. Does this work on Allegro/LW?
22
23 ;;;; Defines a metaclass that allows the use of attributes (or subslots)
24 ;;;; on slots. Based on example in AMOP, but modified to use ACL's MOP.
25
26 (in-package #:kmrcl)
27
28 (defclass attributes-class (kmr-mop:standard-class)
29   ()
30   (:documentation "metaclass that implements attributes on slots. Based
31 on example from AMOP"))
32
33 (defclass attributes-dsd (kmr-mop:standard-direct-slot-definition)
34   ((attributes :initarg :attributes :initform nil
35                :accessor dsd-attributes)))
36
37 (defclass attributes-esd (kmr-mop:standard-effective-slot-definition)
38   ((attributes :initarg :attributes :initform nil
39                :accessor esd-attributes)))
40
41 ;; encapsulating macro for Lispworks
42 (kmr-mop:process-slot-option attributes-class :attributes)
43
44 #+(or cmu scl sbcl openmcl)
45 (defmethod kmr-mop:validate-superclass ((class attributes-class)
46                                         (superclass kmr-mop:standard-class))
47   t)
48
49 (defmethod kmr-mop:direct-slot-definition-class ((cl attributes-class) #+kmr-normal-dsdc &rest initargs)
50   (declare (ignore initargs))
51   (kmr-mop:find-class 'attributes-dsd))
52
53 (defmethod kmr-mop:effective-slot-definition-class ((cl attributes-class) #+kmr-normal-dsdc &rest initargs)
54   (declare (ignore initargs))
55   (kmr-mop:find-class 'attributes-esd))
56
57 (defmethod kmr-mop:compute-effective-slot-definition
58     ((cl attributes-class) #+kmr-normal-cesd name dsds)
59   #+kmr-normal-cesd (declare (ignore name))
60   (let ((esd (call-next-method)))
61     (setf (esd-attributes esd) (remove-duplicates (mapappend #'dsd-attributes dsds)))
62     esd))
63
64 ;; This does not work in Lispworks prior to version 4.3
65
66 (defmethod kmr-mop:compute-slots ((class attributes-class))
67   (let* ((normal-slots (call-next-method))
68          (alist (mapcar
69                  #'(lambda (slot)
70                      (cons (kmr-mop:slot-definition-name slot)
71                            (mapcar #'(lambda (attr) (list attr))
72                                    (esd-attributes slot))))
73                  normal-slots)))
74
75     (cons (make-instance
76            'attributes-esd
77            :name 'all-attributes
78            :initform `',alist
79            :initfunction #'(lambda () alist)
80            :allocation :instance
81            :documentation "Attribute bucket"
82            :type t
83            )
84           normal-slots)))
85
86 (defun slot-attribute (instance slot-name attribute)
87   (cdr (slot-attribute-bucket instance slot-name attribute)))
88
89 (defun (setf slot-attribute) (new-value instance slot-name attribute)
90   (setf (cdr (slot-attribute-bucket instance slot-name attribute))
91     new-value))
92
93 (defun slot-attribute-bucket (instance slot-name attribute)
94   (let* ((all-buckets (slot-value instance 'all-attributes))
95          (slot-bucket (assoc slot-name all-buckets)))
96     (unless slot-bucket
97       (error "The slot named ~S of ~S has no attributes."
98              slot-name instance))
99     (let ((attr-bucket (assoc attribute (cdr slot-bucket))))
100       (unless attr-bucket
101         (error "The slot named ~S of ~S has no attributes named ~S."
102                slot-name instance attribute))
103       attr-bucket)))
104
105
106