translator-test.lisp - clic - Clic is an command line interactive client for go… | |
git clone git://bitreich.org/clic/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65… | |
Log | |
Files | |
Refs | |
Tags | |
README | |
LICENSE | |
--- | |
translator-test.lisp (3565B) | |
--- | |
1 ;;;; -*- Mode: lisp; indent-tabs-mode: nil -*- | |
2 ;;; | |
3 ;;; translator-test.lisp --- Testing type translators. | |
4 ;;; | |
5 ;;; Copyright (C) 2005-2006, James Bielman <[email protected]> | |
6 ;;; | |
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: | |
14 ;;; | |
15 ;;; The above copyright notice and this permission notice shall be | |
16 ;;; included in all copies or substantial portions of the Software. | |
17 ;;; | |
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. | |
26 ;;; | |
27 | |
28 (defpackage #:cffi-translator-test | |
29 (:use #:common-lisp #:cffi)) | |
30 | |
31 (in-package #:cffi-translator-test) | |
32 | |
33 ;;;# Verbose Pointer Translator | |
34 ;;; | |
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. | |
38 | |
39 (define-foreign-type verbose-pointer-type () | |
40 () | |
41 (:actual-type :pointer)) | |
42 | |
43 (defmethod translate-to-foreign (value (type verbose-pointer-type)) | |
44 (format *debug-io* "~&;; to foreign: VERBOSE-POINTER: ~S~%" value) | |
45 value) | |
46 | |
47 (defmethod translate-from-foreign (value (type verbose-pointer-type)) | |
48 (format *debug-io* "~&;; from foreign: VERBOSE-POINTER: ~S~%" value) | |
49 value) | |
50 | |
51 ;;;# Verbose String Translator | |
52 ;;; | |
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. | |
56 ;;; | |
57 ;;; The translators should be called most-specific-first when | |
58 ;;; translating to C, and most-specific-last when translating from C. | |
59 | |
60 (define-foreign-type verbose-string-type (verbose-pointer-type) | |
61 () | |
62 (:simple-parser verbose-string)) | |
63 | |
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 v… | |
67 (values (call-next-method value type) t))) | |
68 | |
69 (defmethod translate-to-foreign (value (type verbose-string-type)) | |
70 (if (pointerp value) | |
71 (progn | |
72 (format *debug-io* "~&;; to foreign: VERBOSE-STRING: ~S -> ~:*~S… | |
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))) | |
76 | |
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~%" p… | |
80 value)) | |
81 | |
82 (defmethod free-translated-object (ptr (type verbose-string-type) free-p) | |
83 (when free-p | |
84 (format *debug-io* "~&;; freeing VERBOSE-STRING: ~S~%" ptr) | |
85 (foreign-string-free ptr))) | |
86 | |
87 (defun test-verbose-string () | |
88 (foreign-funcall "getenv" verbose-string "SHELL" verbose-string)) |