Remove bogus export, thanks to Gordon Sims for reporting.
[hunchentoot.git] / compat.lisp
blobe4eda7dcca0ecec752ad88cf6101d36be86caff6
1 ;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: HUNCHENTOOT; Base: 10 -*-
2 ;;; $Header: /usr/local/cvsrep/hunchentoot/util.lisp,v 1.35 2008/04/08 14:39:18 edi Exp $
4 ;;; Copyright (c) 2004-2010, Dr. Edmund Weitz. All rights reserved.
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
10 ;;; * Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
13 ;;; * Redistributions in binary form must reproduce the above
14 ;;; copyright notice, this list of conditions and the following
15 ;;; disclaimer in the documentation and/or other materials
16 ;;; provided with the distribution.
18 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
19 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 (in-package :hunchentoot)
32 (defmacro when-let ((var form) &body body)
33 "Evaluates FORM and binds VAR to the result, then executes BODY
34 if VAR has a true value."
35 `(let ((,var ,form))
36 (when ,var ,@body)))
38 (defmacro with-unique-names ((&rest bindings) &body body)
39 "Syntax: WITH-UNIQUE-NAMES ( { var | (var x) }* ) declaration* form*
41 Executes a series of forms with each VAR bound to a fresh,
42 uninterned symbol. The uninterned symbol is as if returned by a call
43 to GENSYM with the string denoted by X - or, if X is not supplied, the
44 string denoted by VAR - as argument.
46 The variable bindings created are lexical unless special declarations
47 are specified. The scopes of the name bindings and declarations do not
48 include the Xs.
50 The forms are evaluated in order, and the values of all but the last
51 are discarded \(that is, the body is an implicit PROGN)."
52 ;; reference implementation posted to comp.lang.lisp as
53 ;; <cy3bshuf30f.fsf@ljosa.com> by Vebjorn Ljosa - see also
54 ;; <http://www.cliki.net/Common%20Lisp%20Utilities>
55 `(let ,(mapcar #'(lambda (binding)
56 (check-type binding (or cons symbol))
57 (if (consp binding)
58 (destructuring-bind (var x) binding
59 (check-type var symbol)
60 `(,var (gensym ,(etypecase x
61 (symbol (symbol-name x))
62 (character (string x))
63 (string x)))))
64 `(,binding (gensym ,(symbol-name binding)))))
65 bindings)
66 ,@body))
68 (defmacro with-rebinding (bindings &body body)
69 "Syntax: WITH-REBINDING ( { var | (var prefix) }* ) form*
71 Evaluates a series of forms in the lexical environment that is
72 formed by adding the binding of each VAR to a fresh, uninterned
73 symbol, and the binding of that fresh, uninterned symbol to VAR's
74 original value, i.e., its value in the current lexical environment.
76 The uninterned symbol is created as if by a call to GENSYM with the
77 string denoted by PREFIX - or, if PREFIX is not supplied, the string
78 denoted by VAR - as argument.
80 The forms are evaluated in order, and the values of all but the last
81 are discarded \(that is, the body is an implicit PROGN)."
82 ;; reference implementation posted to comp.lang.lisp as
83 ;; <cy3wv0fya0p.fsf@ljosa.com> by Vebjorn Ljosa - see also
84 ;; <http://www.cliki.net/Common%20Lisp%20Utilities>
85 (loop for binding in bindings
86 for var = (if (consp binding) (car binding) binding)
87 for name = (gensym)
88 collect `(,name ,var) into renames
89 collect ``(,,var ,,name) into temps
90 finally (return `(let ,renames
91 (with-unique-names ,bindings
92 `(let (,,@temps)
93 ,,@body))))))
95 (defun get-peer-address-and-port (socket)
96 "Returns the peer address and port of the socket SOCKET as two
97 values. The address is returned as a string in dotted IP address
98 notation."
99 (values (usocket:vector-quad-to-dotted-quad (usocket:get-peer-address socket))
100 (usocket:get-peer-port socket)))
102 (defun make-socket-stream (socket acceptor)
103 "Returns a stream for the socket SOCKET. The ACCEPTOR argument is
104 ignored."
105 (declare (ignore acceptor))
106 (usocket:socket-stream socket))
108 (defun make-lock (name)
109 "Simple wrapper to allow LispWorks and Bordeaux Threads to coexist."
110 (bt:make-lock name))
112 (defmacro with-lock-held ((lock) &body body)
113 "Simple wrapper to allow LispWorks and Bordeaux Threads to coexist."
114 `(bt:with-lock-held (,lock) ,@body))
116 (defun make-condition-variable (&key name)
117 (declare (ignore name))
118 (bt:make-condition-variable))
120 (defun condition-variable-signal (condition-variable)
121 (bt:condition-notify condition-variable))
123 (defun condition-variable-wait (condition-variable lock)
124 (bt:condition-wait condition-variable lock))