[cffi-devel] swig - cffi module

Hi, I try to add cffi support in swig. cffi module is a fork of Allegro CL module. Everything is working as described in http://cvs.sourceforge.net/viewcvs.py/swig/SWIG/Doc/Manual/Allegrocl.html?rev=1.3&view=auto except automatic array handling and wide char support. A few questions before submitting the module to swig: c++ classes wraped by a (defclass c-proxy () ((foreign-pointer :initarg :foreign-pointer :reader foreign-pointer))) (cffi:defctype :c-proxy :pointer) (cffi:define-type-translator :c-proxy :to-c (value) `(foreign-pointer ,value)) for a c++ class with name cname there is a (cffi:defctype ff_cname :c-proxy) i want to check if a cffi-type is an alias of :c-proxy and i use (defun c-proxy-alias-p (cffi-type) (let ((tp (cffi::find-type cffi-type))) (when (subtypep (type-of tp) 'cffi::foreign-type-alias) (eql (cffi::name (cffi::actual-type tp)) :c-proxy)))) is there a way to do that without accessing cffi internals? any ideas for array handling?, as i can see there is no :array type in cffi. for wide char support Allegro CL module use (excl:with-native-string (out in :external-format #+little-endian :fat-le #-little-endian :fat) ...) and (excl:native-to-string ptr :external-format #+little-endian :fat-le #-little-endian :fat) Thanks, Vasilis Margioulas _________________________________________________________________ Express yourself instantly with MSN Messenger! Download today it's FREE! http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

On 2005-dec-17, at 12:22, Vasilis M. wrote:
i want to check if a cffi-type is an alias of :c-proxy and i use
(defun c-proxy-alias-p (cffi-type) (let ((tp (cffi::find-type cffi-type))) (when (subtypep (type-of tp) 'cffi::foreign-type-alias) (eql (cffi::name (cffi::actual-type tp)) :c-proxy))))
is there a way to do that without accessing cffi internals?
Probably not, I suppose a foreign-subtype-p would be useful. Any suggestions for a better name?
any ideas for array handling?, as i can see there is no :array type in cffi.
What is it that you want to do with arrays that CFFI doesn't support? -- Luís Oliveira http://student.dei.uc.pt/~lmoliv/ Equipa Portuguesa do Translation Project http://www.iro.umontreal.ca/translation/registry.cgi?team=pt

On Sunday, December 18, 2005 6:04 AM Luís Oliveira wrote:
What is it that you want to do with arrays that CFFI doesn't support? I want a handy way to access multidimensional c arrays. After some hacking i find it, here is the code:
(defun indexes-to-row-major-index (dimensions &rest subscripts) (apply #'+ (maplist #'(lambda (x y) (* (car x) (apply #'* (cdr y)))) subscripts dimensions))) (defun row-major-index-to-indexes (index dimensions) (loop with idx = index with rank = (length dimensions) with indexes = (make-list rank) for dim-index from (- rank 1) downto 0 do (setf (values idx (nth dim-index indexes)) (floor idx (nth dim-index dimensions))) finally (return indexes))) (defun lisp-array-to-foreign (array ptr cffi-type dimensions) "Copy elements from a Lisp array to ptr." (loop with foreign-type-size = (foreign-type-size cffi-type) with size = (reduce #'* dimensions) for i from 0 below size for offset = (* i foreign-type-size) for element = (apply #'aref array (row-major-index-to-indexes i dimensions)) do (setf (%mem-ref ptr cffi-type offset) element))) (defun foreign-array-to-lisp (ptr cffi-type dimensions) "Copy elements from ptr into a Lisp array. If ptr is a null pointer, returns nil." (unless (null-pointer-p ptr) (let ((array (make-array dimensions))) (loop with foreign-type-size = (foreign-type-size cffi-type) with size = (reduce #'* dimensions) for i from 0 below size for offset = (* i foreign-type-size) for element = (mem-ref ptr cffi-type offset) do (setf (apply #'aref array (row-major-index-to-indexes i dimensions)) element)) array))) (defun foreign-array-alloc (array cffi-type dimensions) "Allocate a foreign array containing the elements of lisp array. The foreign array must be freed with foreign-array-free." (check-type array array) (let ((ptr (foreign-alloc cffi-type :count (reduce #'* dimensions)))) (lisp-array-to-foreign array ptr cffi-type dimensions) ptr)) (defun foreign-array-free (ptr) "Free a foreign array allocated by foreign-array-alloc." (foreign-free ptr)) (defmacro with-foreign-array ((var lisp-array cffi-type dimensions) &body body) "Bind var to a foreign array containing lisp-array elements in body." `(with-foreign-pointer (,var (* (reduce #'* ,dimensions) (foreign-type-size ,cffi-type))) (lisp-array-to-foreign ,lisp-array ,var ,cffi-type ,dimensions) ,@body)) (defun foreign-aref (ptr cffi-type dimensions &rest indexes) (let ((offset (* (foreign-type-size cffi-type) (apply #'indexes-to-row-major-index dimensions indexes)))) (mem-ref ptr cffi-type offset))) (defun (setf foreign-aref) (value ptr cffi-type dimensions &rest indexes) (let ((offset (* (foreign-type-size cffi-type) (apply #'indexes-to-row-major-index dimensions indexes)))) (setf (%mem-ref ptr cffi-type offset) value)))
participants (3)
-
Luís Oliveira
-
Vasilis M
-
Vasilis M.