/[cffi-objects]/redefines.lisp
ViewVC logotype

Contents of /redefines.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 13 - (show annotations)
Sat Dec 22 19:24:45 2012 UTC (16 months ago) by rklochkov
File size: 2411 byte(s)
Fixed array. Removed redefinition of cffi:mem-ref
1 ;;;;<author>Roman Klochkov, monk@slavsoft.surgut.ru</author>
2 ;;;; Several ad-hoc CFFI types for real numbers, keywords and pathnames
3
4 (in-package #:cffi-objects)
5
6 ;;;[ [[* Float numbers, keywords, pathnames *]]
7
8 ;;;[ <method expand-to-foreign-dyn>
9
10 #|<doc>
11 With plain CFFI language become slightly bondage. In lisp i have number,
12 real and integer, but in CFFI only floats and ints. So, for example,
13 this code is wrong
14 \begin{alltt}
15 (defcfun sin :double (x :double))
16 (sin 0)
17 should be
18 (sin 0.0d0)
19 \end{alltt}
20
21 I think, that this is unnnecessary. So here is my hack (it is hack, because
22 it uses not exported symbols). It makes :double and :float to work, as if
23 corresponding parameters coerced to the needed type.
24 |#
25
26 (defmethod expand-to-foreign-dyn (value var body
27 (type cffi::foreign-built-in-type))
28 `(let ((,var
29 ,(case (cffi::type-keyword type)
30 (:double `(coerce ,value 'double-float))
31 (:float `(coerce ,value 'single-float))
32 (t value))
33 ))
34 ,@body))
35
36 ;;;[ <class cffi-keyword>
37
38 #|<doc>
39 Constant-like strings often used in C, particulary in GTK.
40 It is good to use lisp symbols in this case.
41 So [[cffi-keyword]] type use symbol name as a string for C parameter.
42 The name is downcased, because there are more string in downcase,
43 than in upcase (for not downcased string you still may put string as is).
44 Typical case for this type is using lisp keyword. So the name.
45 |#
46
47 (define-foreign-type cffi-keyword (freeable)
48 ()
49 (:simple-parser cffi-keyword)
50 (:actual-type :pointer))
51
52 (defmethod translate-to-foreign ((value symbol) (type cffi-keyword))
53 (foreign-string-alloc (string-downcase value)))
54
55 (defmethod translate-to-foreign ((value string) (type cffi-keyword))
56 (foreign-string-alloc value))
57
58 (defmethod free-ptr ((type (eql 'cffi-keyword)) ptr)
59 (foreign-string-free ptr))
60
61 ;;;[ <class cffi-pathname>
62
63 #|<doc>
64 The same case for pathnames. If C function expect path to file,
65 you may send it as a string or as a lisp pathname.
66 |#
67
68 (define-foreign-type cffi-pathname (freeable)
69 ()
70 (:simple-parser cffi-pathname)
71 (:actual-type :string))
72
73 (defmethod translate-to-foreign ((value pathname) (type cffi-pathname))
74 (convert-to-foreign (namestring value) :string))
75
76 (defmethod translate-to-foreign ((value string) (type cffi-pathname))
77 (convert-to-foreign value :string))
78
79
80

  ViewVC Help
Powered by ViewVC 1.1.5