1 ;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
3 ;;; translator-test.lisp --- Testing type translators.
5 ;;; Copyright (C) 2005-2006, James Bielman <jamesjb@jamesjb.com>
7 ;;; Permission is hereby granted, free of charge, to any person
8 ;;; obtaining a copy of this software and associated documentation
9 ;;; files (the "Software"), to deal in the Software without
10 ;;; restriction, including without limitation the rights to use, copy,
11 ;;; modify, merge, publish, distribute, sublicense, and/or sell copies
12 ;;; of the Software, and to permit persons to whom the Software is
13 ;;; furnished to do so, subject to the following conditions:
15 ;;; The above copyright notice and this permission notice shall be
16 ;;; included in all copies or substantial portions of the Software.
18 ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 ;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 ;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 ;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 ;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 ;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 ;;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 ;;; DEALINGS IN THE SOFTWARE.
28 (defpackage #:cffi-translator-test
29 (:use
#:common-lisp
#:cffi
))
31 (in-package #:cffi-translator-test
)
33 ;;;# Verbose Pointer Translator
35 ;;; This is a silly type translator that doesn't actually do any
36 ;;; translating, but it prints out a debug message when the pointer is
37 ;;; converted to/from its foreign representation.
39 (define-foreign-type verbose-pointer-type
()
41 (:actual-type
:pointer
))
43 (defmethod translate-to-foreign (value (type verbose-pointer-type
))
44 (format *debug-io
* "~&;; to foreign: VERBOSE-POINTER: ~S~%" value
)
47 (defmethod translate-from-foreign (value (type verbose-pointer-type
))
48 (format *debug-io
* "~&;; from foreign: VERBOSE-POINTER: ~S~%" value
)
51 ;;;# Verbose String Translator
53 ;;; A VERBOSE-STRING extends VERBOSE-POINTER and converts Lisp strings
54 ;;; C strings. If things are working properly, both type translators
55 ;;; should be called when converting a Lisp string to/from a C string.
57 ;;; The translators should be called most-specific-first when
58 ;;; translating to C, and most-specific-last when translating from C.
60 (define-foreign-type verbose-string-type
(verbose-pointer-type)
62 (:simple-parser verbose-string
))
64 (defmethod translate-to-foreign ((s string
) (type verbose-string-type
))
65 (let ((value (foreign-string-alloc s
)))
66 (format *debug-io
* "~&;; to foreign: VERBOSE-STRING: ~S -> ~S~%" s value
)
67 (values (call-next-method value type
) t
)))
69 (defmethod translate-to-foreign (value (type verbose-string-type
))
72 (format *debug-io
* "~&;; to foreign: VERBOSE-STRING: ~S -> ~:*~S~%" value
)
73 (values (call-next-method) nil
))
74 (error "Cannot convert ~S to a foreign string: it is not a Lisp ~
75 string or pointer." value
)))
77 (defmethod translate-from-foreign (ptr (type verbose-string-type
))
78 (let ((value (foreign-string-to-lisp (call-next-method))))
79 (format *debug-io
* "~&;; from foreign: VERBOSE-STRING: ~S -> ~S~%" ptr value
)
82 (defmethod free-translated-object (ptr (type verbose-string-type
) free-p
)
84 (format *debug-io
* "~&;; freeing VERBOSE-STRING: ~S~%" ptr
)
85 (foreign-string-free ptr
)))
87 (defun test-verbose-string ()
88 (foreign-funcall "getenv" verbose-string
"SHELL" verbose-string
))