1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
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
10 ;;;; $Id: attrib-class.lisp,v 1.7 2003/04/29 01:35:58 kevin Exp $
12 ;;;; This file, part of KMRCL, is Copyright (c) 2002 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-dsd (kmr-mop:standard-direct-slot-definition)
25 ((attributes :initarg :attributes :initform nil
26 :accessor dsd-attributes)))
28 (defclass attributes-esd (kmr-mop:standard-effective-slot-definition)
29 ((attributes :initarg :attributes :initform nil
30 :accessor esd-attributes)))
33 (defclass attributes-class (kmr-mop:standard-class)
35 (:documentation "metaclass that implements attributes on slots. Based
36 on example from AMOP"))
40 (defmethod kmr-mop:validate-superclass ((class attributes-class)
41 (superclass kmr-mop:standard-class))
44 (defmethod kmr-mop:direct-slot-definition-class ((cl attributes-class)
45 #+(or sbcl cmu scl lispworks)
49 ;; (format t "attributes:~s iargs:~s~%" attributes iargs)
50 (kmr-mop:find-class 'attributes-dsd))
52 (defmethod kmr-mop:compute-effective-slot-definition :around
53 ((cl attributes-class) #+kmr-named-cesd name dsds)
54 #+kmr-named-cesd (declare (ignore name))
56 #'make-instance 'attributes-esd
57 :attributes (remove-duplicates (mapappend #'dsd-attributes dsds))
58 (kmr-mop:compute-effective-slot-definition-initargs cl dsds))
62 (defmethod kmr-mop:compute-effective-slot-definition :around
63 ((cl attributes-class) #+kmr-named-cesd name dsds)
64 #+kmr-named-cesd (declare (ignore name))
65 (let ((normal-slot (call-next-method)))
66 (setf (esd-attributes normal-slot)
68 (mapappend #'esd-attributes dsds)))
72 (defmethod kmr-mop:compute-slots ((class attributes-class))
73 (let* ((normal-slots (call-next-method))
77 (let ((attr-list (mapcar #'(lambda (attr) (cons attr nil))
78 (esd-attributes slot))))
80 (cons (kmr-mop:slot-definition-name slot) attr-list))))
82 (setq alist (delete nil alist))
83 (cons (make-instance 'kmr-mop:standard-effective-slot-definition
86 :initfunction #'(lambda () alist))
89 (defun slot-attribute (instance slot-name attribute)
90 (cdr (slot-attribute-bucket instance slot-name attribute)))
92 (defun (setf slot-attribute) (new-value instance slot-name attribute)
93 (setf (cdr (slot-attribute-bucket instance slot-name attribute))
96 (defun slot-attribute-bucket (instance slot-name attribute)
97 (let* ((all-buckets (slot-value instance 'all-attributes))
98 (slot-bucket (assoc slot-name all-buckets)))
100 (error "The slot named ~S of ~S has no attributes."
102 (let ((attr-bucket (assoc attribute (cdr slot-bucket))))
104 (error "The slot named ~S of ~S has no attributes named ~S."
105 slot-name instance attribute))
108 (eval-when (:compile-toplevel :load-toplevel :execute)
109 (export '(attributes-class slot-attributes)))