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

Contents of /array.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 13 - (hide annotations)
Sat Dec 22 19:24:45 2012 UTC (15 months, 3 weeks ago) by rklochkov
File size: 2521 byte(s)
Fixed array. Removed redefinition of cffi:mem-ref
1 rklochkov 2 ;;;
2     ;;; array.lisp --- array
3     ;;;
4     ;;; Copyright (C) 2012, Roman Klochkov <monk@slavsoft.surgut.ru>
5     ;;;
6    
7     (in-package #:cffi-objects)
8    
9     (defvar *array-length* (foreign-alloc :uint))
10    
11     ;; TODO: add with-pointer-to-vector-data optimization
12     (define-foreign-type cffi-array (freeable)
13     ((element-type :initarg :type :accessor element-type))
14     (:actual-type :pointer))
15    
16 rklochkov 8 (define-parse-method carray (type &rest rest)
17     (apply #'make-instance 'cffi-array :type type rest))
18 rklochkov 2
19     (defmethod translate-to-foreign (value (cffi-array cffi-array))
20     (if (pointerp value)
21     value
22     (let* ((length (length value))
23     (type (element-type cffi-array))
24     (res (foreign-alloc type :count length)))
25 rklochkov 13 (if (struct-p type)
26     (dotimes (i length (values res t))
27     (clos->struct (second type) (elt value i) (mem-aptr res type i)))
28     (dotimes (i length (values res t))
29     (setf (mem-aref res type i) (elt value i)))))))
30 rklochkov 2
31     (defmethod translate-from-foreign (ptr (cffi-array cffi-array))
32     (let ((array-length (mem-ref *array-length* :uint)))
33     (let* ((res (make-array array-length))
34 rklochkov 13 (type (element-type cffi-array)))
35     (if (struct-p type)
36     (dotimes (i array-length res)
37     (setf (aref res i) (convert-from-foreign (mem-aptr ptr type i)
38     type)))
39     (dotimes (i array-length res)
40     (setf (aref res i) (mem-aref ptr type i)))))))
41 rklochkov 2
42     (define-foreign-type cffi-null-array (freeable)
43     ((element-type :initarg :type :accessor element-type))
44     (:actual-type :pointer))
45    
46 rklochkov 8 (define-parse-method null-array (type &rest rest)
47     (apply #'make-instance 'cffi-null-array :type type rest))
48 rklochkov 2
49     (defmethod translate-to-foreign (value (cffi-null-array cffi-null-array))
50     (if (pointerp value)
51     value
52     (let* ((length (length value))
53     (type (element-type cffi-null-array))
54     (res (foreign-alloc type :count (+ 1 length))))
55     (dotimes (i length (values res t))
56     (setf (mem-aref res type i) (elt value i)))
57     (setf (mem-aref res :pointer length) (null-pointer))
58     res)))
59    
60     (defmethod translate-from-foreign (ptr (cffi-null-array cffi-null-array))
61     (let* ((res nil)
62     (el-type (element-type cffi-null-array)))
63     (do ((i 0 (+ i 1))) ((null-pointer-p (mem-aref ptr :pointer i)))
64     (push (mem-aref ptr el-type i) res))
65     (coerce (nreverse res) 'array)))
66    
67 rklochkov 13 (defctype string-array (null-array :string) "Zero-terminated string array")
68    

  ViewVC Help
Powered by ViewVC 1.1.5