how do i pass structs by value with cffi-libffi?
Posted: Mon Sep 23, 2013 6:38 pm
I would appreciate any and all help/guidance on this. ok, Im under the impression that lisp's cffi cant pass structs by value, but the cffi documentation says:
"To pass or return a structure by value to a function, load the cffi-libffi system and specify the structure as (:struct structure-name). To pass or return the pointer, you can use either :pointer or (:pointer (:struct structure-name)). "
Im re-wrapping the cl-opencv function get-size, which is a wrapper for this opencv function...
CvSize cvGetSize(const CvArr* arr)
and since i don't think cffi had the ability to pass structs by value, with the cffi-libffi system, when the author of cl-opencv wrote the library, so he had to use all of the below code to wrap..cvGetSize
so now how, with the cffi-libffi system, as outlined in the cffi documentation here:
"To pass or return a structure by value to a function, load the cffi-libffi system and specify the structure as (:struct structure-name). To pass or return the pointer, you can use either :pointer or (:pointer (:struct structure-name)). "
would i pass this cvGetSize struct...CvSize by value.....pls give me detailed instructions....I'll be updating the cl-opencv package which is available here:
https://github.com/ryepup/cl-opencv
i would like to know where and how in the cl-opencv package i "load the cffi-libffi system" as the cffi documentation states....and the cffi documentation says "and specify the structure as (:struct structure-name). To pass or return the pointer, you can use either :pointer or (:pointer (:struct structure-name))." I could use detailed instructions on how to do that using the above cvGetSize wrapper..get-size....again here:
"To pass or return a structure by value to a function, load the cffi-libffi system and specify the structure as (:struct structure-name). To pass or return the pointer, you can use either :pointer or (:pointer (:struct structure-name)). "
Im re-wrapping the cl-opencv function get-size, which is a wrapper for this opencv function...
CvSize cvGetSize(const CvArr* arr)
and since i don't think cffi had the ability to pass structs by value, with the cffi-libffi system, when the author of cl-opencv wrote the library, so he had to use all of the below code to wrap..cvGetSize
Code: Select all
(defmacro make-structure-serializers (struct slot1 slot2)
"Create a serialization and deserialization function for the
structure STRUCT with integer slots SLOT1 and SLOT2. These functions
will pack and unpack the structure into an INT64."
(let ((pack-fn (intern (concatenate 'string (string struct)
(string '->int64))))
(slot1-fn (intern (concatenate 'string (string struct) "-"
(string slot1))))
(slot2-fn (intern (concatenate 'string (string struct) "-"
(string slot2))))
(unpack-fn (intern (concatenate 'string (string 'int64->)
(string struct))))
(make-fn (intern (concatenate 'string (string 'make-)
(string struct)))))
`(progn
(defun ,pack-fn (s)
(+ (,slot1-fn s) (ash (,slot2-fn s) 32)))
(defun ,unpack-fn (n)
(,make-fn ,slot1 (logand n #x00000000ffffffff)
,slot2 (ash n -32))))))
;; CvSize - Input = (defparameter a (make-size :width 640 :height 480)) Output = #S(SIZE :WIDTH 640 :HEIGHT 480) for
;; following the two.
(defstruct size (width 0) (height 0))
(make-structure-serializers :size :width :height)
;; CvSize cvGetSize(const CvArr* arr)
(cffi:defcfun ("cvGetSize" %get-size) :int64
(arr cv-array))
(defun get-size (arr)
"Get the dimensions of the OpenCV array ARR. Return a size struct with the
dimensions."
(let ((nsize (%get-size arr)))
(int64->size nsize)))
"To pass or return a structure by value to a function, load the cffi-libffi system and specify the structure as (:struct structure-name). To pass or return the pointer, you can use either :pointer or (:pointer (:struct structure-name)). "
would i pass this cvGetSize struct...CvSize by value.....pls give me detailed instructions....I'll be updating the cl-opencv package which is available here:
https://github.com/ryepup/cl-opencv
i would like to know where and how in the cl-opencv package i "load the cffi-libffi system" as the cffi documentation states....and the cffi documentation says "and specify the structure as (:struct structure-name). To pass or return the pointer, you can use either :pointer or (:pointer (:struct structure-name))." I could use detailed instructions on how to do that using the above cvGetSize wrapper..get-size....again here:
Code: Select all
;; CvSize cvGetSize(const CvArr* arr)
(cffi:defcfun ("cvGetSize" %get-size) :int64
(arr cv-array))
(defun get-size (arr)
"Get the dimensions of the OpenCV array ARR. Return a size struct with the
dimensions."
(let ((nsize (%get-size arr)))
(int64->size nsize)))