1 ;;; srfi-39.scm --- Parameter objects
3 ;; Copyright (C) 2004, 2005, 2006, 2008, 2011 Free Software Foundation, Inc.
5 ;; This library is free software; you can redistribute it and/or
6 ;; modify it under the terms of the GNU Lesser General Public
7 ;; License as published by the Free Software Foundation; either
8 ;; version 3 of the License, or (at your option) any later version.
10 ;; This library is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;; Lesser General Public License for more details.
15 ;; You should have received a copy of the GNU Lesser General Public
16 ;; License along with this library; if not, write to the Free Software
17 ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 ;;; Author: Jose Antonio Ortega Ruiz <jao@gnu.org>
24 ;; This is an implementation of SRFI-39 (Parameter objects).
26 ;; The implementation is based on Guile's fluid objects, and is, therefore,
27 ;; thread-safe (parameters are thread-local).
29 ;; In addition to the forms defined in SRFI-39 (`make-parameter',
30 ;; `parameterize'), a new procedure `with-parameters*' is provided.
31 ;; This procedures is analogous to `with-fluids*' but taking as first
32 ;; argument a list of parameter objects instead of a list of fluids.
37 (define-module (srfi srfi-39)
38 #:use-module (srfi srfi-16)
40 #:export (make-parameter)
41 #:export-syntax (parameterize)
43 ;; helper procedure not in srfi-39.
44 #:export (with-parameters*)
45 #:replace (current-input-port current-output-port current-error-port))
47 ;; Make 'srfi-39 available as a feature identifiere to `cond-expand'.
49 (cond-expand-provide (current-module) '(srfi-39))
51 (define make-parameter
53 ((val) (make-parameter/helper val (lambda (x) x)))
54 ((val conv) (make-parameter/helper val conv))))
56 (define get-fluid-tag (lambda () 'get-fluid)) ;; arbitrary unique (as per eq?) value
57 (define get-conv-tag (lambda () 'get-conv)) ;; arbitrary unique (as per eq?) value
59 (define (make-parameter/helper val conv)
60 (let ((value (make-fluid))
63 (fluid-set! value (conv val))
66 ((null? new-value) (fluid-ref value))
67 ((eq? (car new-value) get-fluid-tag) value)
68 ((eq? (car new-value) get-conv-tag) conv)
69 ((null? (cdr new-value)) (fluid-set! value (conv (car new-value))))
70 (else (error "make-parameter expects 0 or 1 arguments" new-value)))))))
72 (define-syntax-rule (parameterize ((?param ?value) ...) ?body ...)
73 (with-parameters* (list ?param ...)
75 (lambda () ?body ...)))
77 (define (current-input-port . new-value)
79 ((@ (guile) current-input-port))
80 (apply set-current-input-port new-value)))
82 (define (current-output-port . new-value)
84 ((@ (guile) current-output-port))
85 (apply set-current-output-port new-value)))
87 (define (current-error-port . new-value)
89 ((@ (guile) current-error-port))
90 (apply set-current-error-port new-value)))
93 (list current-input-port current-output-port current-error-port))
95 ;; There are no fluids behind current-input-port etc, so those parameter
96 ;; objects are picked out of the list and handled separately with a
97 ;; dynamic-wind to swap their values to and from a location (the "value"
98 ;; variable in the swapper procedure "let").
100 ;; current-input-port etc are already per-dynamic-root, so this arrangement
101 ;; works the same as a fluid. Perhaps they could become fluids for ease of
102 ;; implementation here.
104 ;; Notice the use of a param local variable for the swapper procedure. It
105 ;; ensures any application changes to the PARAMS list won't affect the
108 (define (with-parameters* params values thunk)
109 (let more ((params params)
111 (fluids '()) ;; fluids from each of PARAMS
112 (convs '()) ;; VALUES with conversion proc applied
113 (swapper noop)) ;; wind/unwind procedure for ports handling
115 (if (eq? noop swapper)
116 (with-fluids* fluids convs thunk)
120 (with-fluids* fluids convs thunk))
122 (if (memq (car params) port-list)
123 (more (cdr params) (cdr values)
125 (let ((param (car params))
127 (prev-swapper swapper))
129 (set! value (param value))
131 (more (cdr params) (cdr values)
132 (cons ((car params) get-fluid-tag) fluids)
133 (cons (((car params) get-conv-tag) (car values)) convs)