1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: kmrcl-*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
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
12 ;;;; This file, part of KMRCL, is Copyright (c) 2002-2003 by Kevin M. Rosenberg
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 ;;;; *************************************************************************
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.
24 (defclass attributes-class (kmr-mop:standard-class)
26 (:documentation "metaclass that implements attributes on slots. Based
27 on example from AMOP"))
29 (defclass attributes-dsd (kmr-mop:standard-direct-slot-definition)
30 ((attributes :initarg :attributes :initform nil
31 :accessor dsd-attributes)))
33 (defclass attributes-esd (kmr-mop:standard-effective-slot-definition)
34 ((attributes :initarg :attributes :initform nil
35 :accessor esd-attributes)))
37 ;; encapsulating macro for Lispworks
38 (kmr-mop:process-slot-option attributes-class :attributes)
40 #+(or cmu scl sbcl openmcl)
41 (defmethod kmr-mop:validate-superclass ((class attributes-class)
42 (superclass kmr-mop:standard-class))
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))
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))
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)))
60 ;; This does not work in Lispworks prior to version 4.3
62 (defmethod kmr-mop:compute-slots ((class attributes-class))
63 (let* ((normal-slots (call-next-method))
66 (cons (kmr-mop:slot-definition-name slot)
67 (mapcar #'(lambda (attr) (list attr))
68 (esd-attributes slot))))
75 :initfunction #'(lambda () alist)
77 :documentation "Attribute bucket"
82 (defun slot-attribute (instance slot-name attribute)
83 (cdr (slot-attribute-bucket instance slot-name attribute)))
85 (defun (setf slot-attribute) (new-value instance slot-name attribute)
86 (setf (cdr (slot-attribute-bucket instance slot-name attribute))
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)))
93 (error "The slot named ~S of ~S has no attributes."
95 (let ((attr-bucket (assoc attribute (cdr slot-bucket))))
97 (error "The slot named ~S of ~S has no attributes named ~S."
98 slot-name instance attribute))