r4687: Automatic commit for debian_version_1_32-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: attrib-class.lisp,v 1.12 2003/04/29 07:52:38 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-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)
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 :around
54     ((cl attributes-class) #+kmr-normal-cesd name dsds)
55   #+(and kmr-normal-cesd (not lispworks)) (declare (ignore name))
56   (apply
57    #'make-instance 'attributes-esd 
58    :attributes (remove-duplicates (mapappend #'dsd-attributes dsds))
59    (kmr-mop::compute-effective-slot-definition-initargs cl #+lispworks name dsds))
60   )
61
62 #+ignore
63 (defmethod kmr-mop:compute-effective-slot-definition :around
64     ((cl attributes-class) #+kmr-normal-cesd name dsds)
65   #+kmr-normal-cesd (declare (ignore name))
66   (let ((esd (call-next-method)))
67     (setq esd (change-class esd 'attributes-esd))
68     (print esd)
69     (print (remove-duplicates (mapappend #'dsd-attributes dsds)))
70     (setf (esd-attributes esd) (remove-duplicates (mapappend #'dsd-attributes dsds)))
71     esd))
72
73
74 (defmethod kmr-mop:compute-slots ((class attributes-class))
75   (let* ((normal-slots (call-next-method))
76          (alist
77           (mapcar
78            #'(lambda (slot)
79                (let ((attr-list (mapcar #'(lambda (attr) (cons attr nil))
80                                         (esd-attributes slot))))
81                  (when attr-list
82                    (cons (kmr-mop:slot-definition-name slot) attr-list))))
83            normal-slots)))
84     (format t "normal-slots: ~A~%" normal-slots)
85     (format t "alist: ~A~%" alist)
86     (let ((attrib-slot  (make-instance 'attributes-esd
87                          :name 'all-attributes
88                          :initform `',alist
89                          :initfunction #'(lambda () alist))))
90       (format t "attrib-slot: ~A~%" attrib-slot)
91       (cons attrib-slot normal-slots))))
92   
93 (defun slot-attribute (instance slot-name attribute)
94   (cdr (slot-attribute-bucket instance slot-name attribute)))
95
96 (defun (setf slot-attribute) (new-value instance slot-name attribute)
97   (setf (cdr (slot-attribute-bucket instance slot-name attribute))
98     new-value))
99
100 (defun slot-attribute-bucket (instance slot-name attribute)
101   (let* ((all-buckets (slot-value instance 'all-attributes))
102          (slot-bucket (assoc slot-name all-buckets)))
103     (unless slot-bucket
104       (error "The slot named ~S of ~S has no attributes."
105              slot-name instance))
106     (let ((attr-bucket (assoc attribute (cdr slot-bucket))))
107       (unless attr-bucket
108         (error "The slot named ~S of ~S has no attributes named ~S."
109                slot-name instance attribute))
110       attr-bucket)))
111
112 (eval-when (:compile-toplevel :load-toplevel :execute)
113   (export '(attributes-class slot-attributes)))
114