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.5 2003/04/28 21:12:27 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 (standard-direct-slot-definition)
25 ((attributes :initarg :attributes :initform nil
26 :accessor attributes)))
28 (defclass attributes-esd (standard-effective-slot-definition)
29 ((attributes :initarg :attributes :initform nil
30 :accessor slot-definition-attributes)))
33 (defclass attributes-class (standard-class)
38 (defmethod validate-superclass ((class attributes-class)
39 (superclass standard-class))
42 (defmethod direct-slot-definition-class ((cl attributes-class)
43 &rest iargs &key attributes)
44 (declare (ignorable attributes))
45 ;; (format t "attributes:~s iargs:~s~%" attributes iargs)
46 (find-class 'attributes-dsd))
48 (eval-when (:compile-toplevel :load-toplevel :execute)
49 (when (>= (length (generic-function-lambda-list
50 (ensure-generic-function
51 'compute-effective-slot-definition)))
53 (push :ho-named-cesd-fun cl:*features*)))
55 (defmethod compute-effective-slot-definition :around
56 ((cl attributes-class) #+ho-named-cesd-fun name dsds)
57 #+ho-named-cesd-fun (declare (ignore name))
59 #'make-instance 'attributes-esd
60 :attributes (remove-duplicates (mapappend #'attributes dsds))
61 (compute-effective-slot-definition-initargs cl dsds))
66 (defmethod compute-effective-slot-definition :around
67 ((cl attributes-class) #+ho-named-cesd-fun name dsds)
68 #+ho-named-cesd-fun (declare (ignore name))
69 (let ((normal-slot (call-next-method)))
70 (setf (slot-definition-attributes normal-slot)
72 (mapappend #'slot-definition-attributes dsds)))
76 (defmethod compute-slots ((class attributes-class))
77 (let* ((normal-slots (call-next-method))
81 (let ((attr-list (mapcar #'(lambda (attr) (cons attr nil))
82 (slot-definition-attributes slot))))
84 (cons (mop::slot-definition-name slot) attr-list))))
86 (setq alist (delete nil alist))
87 (cons (mop::make-instance 'mop::standard-effective-slot-definition
90 :initfunction #'(lambda () alist))
93 (defun slot-attribute (instance slot-name attribute)
94 (cdr (slot-attribute-bucket instance slot-name attribute)))
96 (defun (setf slot-attribute) (new-value instance slot-name attribute)
97 (setf (cdr (slot-attribute-bucket instance slot-name attribute))
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)))
104 (error "The slot named ~S of ~S has no attributes."
106 (let ((attr-bucket (assoc attribute (cdr slot-bucket))))
108 (error "The slot named ~S of ~S has no attributes named ~S."
109 slot-name instance attribute))