;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*- ;;;; ************************************************************************* ;;;; FILE IDENTIFICATION ;;;; ;;;; Name: attrib-class.lisp ;;;; Purpose: Defines metaclass allowing use of attributes on slots ;;;; Programmer: Kevin M. Rosenberg ;;;; Date Started: Apr 2000 ;;;; ;;;; $Id: attrib-class.lisp,v 1.4 2002/10/10 16:23:48 kevin Exp $ ;;;; ;;;; This file, part of KMRCL, is Copyright (c) 2002 by Kevin M. Rosenberg ;;;; ;;;; KMRCL users are granted the rights to distribute and use this software ;;;; as governed by the terms of the Lisp Lesser GNU Public License ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL. ;;;; ************************************************************************* ;;;; Defines a metaclass that allows the use of attributes (or subslots) ;;;; on slots. Based on example in AMOP, but modified to use ACL's MOP. (in-package :kmrcl) (defclass attributes-dsd (mop::standard-direct-slot-definition) ((attributes :initarg :attributes :initform nil :accessor attributes))) (defclass attributes-esd (mop::standard-effective-slot-definition) ((attributes :initarg :attributes :initform nil :accessor slot-definition-attributes))) (defclass attributes-class (mop::standard-class) () ) (defmethod mop::direct-slot-definition-class ((cl attributes-class) &rest iargs &key attributes) (declare (ignorable attributes)) ;; (format t "attributes:~s iargs:~s~%" attributes iargs) (find-class 'attributes-dsd)) (defmethod mop::compute-effective-slot-definition :around ((cl attributes-class) slot dsds) (declare (ignorable slot)) (apply #'make-instance 'attributes-esd :attributes (remove-duplicates (mapappend #'attributes dsds)) (excl::compute-effective-slot-definition-initargs cl dsds)) ) #+ignore (defmethod mop::compute-effective-slot-definition ((cl attributes-class) slot dsds) (declare (ignorable slot)) (let ((normal-slot (call-next-method))) (setf (slot-definition-attributes normal-slot) (remove-duplicates (mapappend #'slot-definition-attributes dsds))) normal-slot)) (defmethod mop::compute-slots ((class attributes-class)) (let* ((normal-slots (call-next-method)) (alist (mapcar #'(lambda (slot) (let ((attr-list (mapcar #'(lambda (attr) (cons attr nil)) (slot-definition-attributes slot)))) (when attr-list (cons (mop::slot-definition-name slot) attr-list)))) normal-slots))) (setq alist (delete nil alist)) (cons (mop::make-instance 'mop::standard-effective-slot-definition :name 'all-attributes :initform `',alist :initfunction #'(lambda () alist)) normal-slots))) (defun slot-attribute (instance slot-name attribute) (cdr (slot-attribute-bucket instance slot-name attribute))) (defun (setf slot-attribute) (new-value instance slot-name attribute) (setf (cdr (slot-attribute-bucket instance slot-name attribute)) new-value)) (defun slot-attribute-bucket (instance slot-name attribute) (let* ((all-buckets (slot-value instance 'all-attributes)) (slot-bucket (assoc slot-name all-buckets))) (unless slot-bucket (error "The slot named ~S of ~S has no attributes." slot-name instance)) (let ((attr-bucket (assoc attribute (cdr slot-bucket)))) (unless attr-bucket (error "The slot named ~S of ~S has no attributes named ~S." slot-name instance attribute)) attr-bucket))) #|| (in-package :kmrcl) (defclass credit-rating () ((level :attributes (date-set time-set)) (id :attributes (person-setting))) (:metaclass kmrcl:attributes-class)) (defparameter cr (make-instance 'credit-rating)) (format t "~&date-set: ~a" (slot-attribute cr 'level 'date-set)) (setf (slot-attribute cr 'level 'date-set) "12/15/1990") (format t "~&date-set: ~a" (slot-attribute cr 'level 'date-set)) (defclass monitored-credit-rating (credit-rating) ((level :attributes (last-checked interval date-set)) (cc :initarg :cc) (id :attributes (verified)) ) (:metaclass attributes-class)) (defparameter mcr (make-instance 'monitored-credit-rating)) (setf (slot-attribute mcr 'level 'date-set) "01/05/2002") (format t "~&date-set for mcr: ~a" (slot-attribute mcr 'level 'date-set)) ||#