1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-01 04:10:18 +02:00
guile/module/oop/goops/composite-slot.scm
Andy Wingo 568174d173 Introduce <slot> objects in GOOPS
* module/oop/goops.scm (fold-class-slots): Change format to use proper
  slot specifications.
  (fold-slot-slots): Flesh out with all needed slots.
  (<class>): Update cons-layout to deal with new fold-class-slots form.
  Don't create slots; we do that later.
  (is-a?, get-keyword, *unbound, unbound?, %allocate-instance): Move
  definitions up.
  (<slot>, slot?): New definitions.
  (slot-definition-name, slot-definition-allocation)
  (slot-definition-init-keyword, slot-definition-init-form)
  (slot-definition-init-value, slot-definition-init-thunk)
  (slot-definition-options, slot-definition-getter)
  (slot-definition-setter, slot-definition-accessor)
  (slot-definition-slot-ref, slot-definition-slot-set!)
  (slot-definition-index, slot-definition-size): New definitions as
  accessors on <slot> objects.
  (class-slot-definition): Adapt to class-slots change.
  (direct-slot-definition-class, make-slot): New definitions.
  (make): Define a boot version that can allocate <slot> instances.
  (compute-direct-slot-definition)
  (compute-direct-slot-definition-initargs)
  (effective-slot-definition-class, compute-effective-slot-definition):
  New definitions.
  (build-slots-list): Adapt to slots being <slot> objects.
  (compute-get-n-set): New boot definition.
  (allocate-slots): New definition.  Replaces
  compute-getters-n-setters.
  (%compute-layout, %prep-layout): Adapt to changes.
  (make-standard-class): Make <slot> objects for direct-slots, and
  handle the allocate-slots protocol.
  (<foreign-slot>): Inherit from <slot>.
  (get-slot-value-using-name, set-slot-value-using-name!)
  (test-slot-existence): Adapt to using slot definition objects.
  (make-class): Allow slot specs or <slot> objects as the `slots'
  argument.
  (write): New method on <slot>.
  (class-slot-ref, class-slot-set!): Reimplement.
  (compute-slot-accessors, compute-getter-method)
  (compute-setter-method): Adapt to changes.
  (compute-getters-n-setters): Remove.  Yay!
  (compute-get-n-set): Adapt to use effective slot definitions instead
  of the getters-n-setters for #:class / #:each-subclass allocation.
  (%initialize-object): Adapt.
  (initialize): New method for <slot>.  Adapt method for <class>.

* module/oop/goops/active-slot.scm (compute-get-n-set):
* module/oop/goops/composite-slot.scm (compute-propagated-get-n-set):
  Use slot-definition-options to access options of slot.

* test-suite/tests/goops.test ("bad init-thunk"): Fix to be a "pass-if"
  instead of an "expect-fail".
2015-01-23 16:16:03 +01:00

83 lines
2.8 KiB
Scheme
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

;;; installed-scm-file
;;;; Copyright (C) 1999, 2000, 2001, 2006, 2015 Free Software Foundation, Inc.
;;;; Copyright (C) 1993-1998 Erick Gallesio - I3S-CNRS/ESSI <eg@unice.fr>
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;;
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;;; Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
;;;;
;;;;
;;;; This file was based upon composite-slot.stklos from the STk distribution
;;;; version 4.0.1 by Erick Gallesio <eg@unice.fr>.
;;;;
(define-module (oop goops composite-slot)
:use-module (oop goops)
:export (<composite-class>))
;;;
;;; (define-class CLASS SUPERS
;;; ...
;;; (OBJECT ...)
;;; ...
;;; (SLOT #:allocation #:propagated
;;; #:propagate-to '(PROPAGATION ...))
;;; ...
;;; #:metaclass <composite-class>)
;;;
;;; PROPAGATION ::= OBJECT | (OBJECT TARGETSLOT)
;;;
;;; The slot SLOT will be propagated to the slot TARGETSLOT in the object
;;; stored in slot OBJECT. If TARGETSLOT is omitted, assume that the target
;;; slot is named SLOT.
;;;
(define-class <composite-class> (<class>))
(define-method (compute-get-n-set (class <composite-class>) slot)
(if (eq? (slot-definition-allocation slot) #:propagated)
(compute-propagated-get-n-set slot)
(next-method)))
(define (compute-propagated-get-n-set s)
(let ((prop (get-keyword #:propagate-to
(slot-definition-options s)
#f))
(s-name (slot-definition-name s)))
(if (not prop)
(goops-error "Propagation not specified for slot ~S" s-name))
(if (not (pair? prop))
(goops-error "Bad propagation list for slot ~S" s-name))
(let ((objects (map (lambda (p) (if (pair? p) (car p) p)) prop))
(slots (map (lambda (p) (if (pair? p) (cadr p) s-name)) prop)))
(let ((first-object (car objects))
(first-slot (car slots)))
(list
;; The getter
(lambda (o)
(slot-ref (slot-ref o first-object) first-slot))
;; The setter
(if (null? (cdr objects))
(lambda (o v)
(slot-set! (slot-ref o first-object) first-slot v))
(lambda (o v)
(for-each (lambda (object slot)
(slot-set! (slot-ref o object) slot v))
objects
slots))))))))