This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

How do i write a cffi:translate-into foreign defmethod?

1 post · 2612 views

ok I tried this translate-from-foreign method and it did work i have these defined in my structs.lisp file in my library which loads first before all my other dependencies
(cffi:defcstruct (cv-size :class cv-size-type)
  (width :int)
  (height :int))
(defmethod cffi:translate-from-foreign (p (type cv-size-type))
  (let ((plist (call-next-method)))
    (make-size :width (getf plist 'width)
               :height (getf plist 'height))))
and my opencv wrappers for CvGetSize and cvCreateImage, get-size and create-image, are defined like this
;; CvSize cvGetSize(const CvArr* arr)
 (cffi:defcfun ("cvGetSize" get-size) (:struct cv-size)
   (arr cv-arr))
 ;; IplImage* cvCreateImage(CvSize size, int depth, int channels)
 (cffi:defcfun ("cvCreateImage" %create-image) ipl-image
   (size :int64)
   (depth :int)
   (channels :int))
 (defun create-image (size depth channels)
   "Create an image with dimensions given by SIZE, DEPTH bits per
 channel, and CHANNELS number of channels."
   (let ((nsize (size->int64 size)))
     (%create-image nsize depth channels)))
here is the definition of size->int64
(DEFUN SIZE->INT64 (S) (+ (SIZE-WIDTH S) (ASH (SIZE-HEIGHT S) 32)))
it converts get-size output which is a structure here:
#S(SIZE :WIDTH 640 :HEIGHT 480)
into 64-bit integer, which CFFI can handle (at least on some platforms)

but i love the idea of the translate-foreign defmethod's

so i was wondering if you can show my how to make the translate-into-foreign version of the below from method this would really make my library awesome
(defmethod cffi:translate-from-foreign (p (type cv-size-type))
  (let ((plist (call-next-method)))
    (make-size :width (getf plist 'width)
               :height (getf plist 'height))))
I was going to try stuff and add it but for the get-size output structure, it isnt a plist so not really sure what to put there for the
(let ((plist (call-next-method)))
part, for the
 (make-size :width (getf plist 'width)
               :height (getf plist 'height))))
part, i was hoping to find another method other than the size->64 function because that was made 2 years ago when cl-opencv https://github.com/ryepup/cl-opencv first came out and i would like to make an even better wrapper than that...i've already taken cl-opencv added 100 new function 5000 lines of code samples and documentation and a new structs.lisp file so i would love if someone could help me with all the latest cffi tools so i could do something else than int64...plus the if i have a funtion to wrap where the int64 thing wouldnt work ill be ready