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