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