[cffi-devel] how to revise foreign type translators to work with cffi-newtypes

Jack Unrue jdunrue at gmail.com
Thu Mar 15 03:51:15 UTC 2007


Hi there. I'm looking into some compile errors that are reported by SBCL
when compiling Graphic-Forms using the cffi-newtypes branch. I have
a number of foreign types defined, and for a couple of these, I also
declare aliases for :pointer such that I am then able to implement
type translators.

Here's an example (assume there are existing CL structures called
RECTANGLE, SIZE, and POINT, and it is instances of RECTANGLE
which are to be translated into and out of the foreign struct RECT
that I'm defining below):

(cffi:defctype rect-pointer :pointer)

(cffi:defcstruct rect
  (left :long)
  (top :long)
  (right :long)
  (bottom :long))

(defmethod cffi:free-translated-object (ptr (name (eql 'rect-pointer)) param)
  (declare (ignore param))
  (cffi:foreign-free ptr))

(defmethod cffi:translate-from-foreign (ptr (name (eql 'rect-pointer)))
  (if (cffi:null-pointer-p ptr)
    (make-rectangle)
    (cffi:with-foreign-slots ((left top right bottom) ptr rect)
      (let ((pnt (make-point :x left :y top))
            (size (make-size :width (- right left) :height (- bottom top))))
        (make-rectangle :location pnt :size size)))))

(defmethod cffi:translate-to-foreign ((lisp-rect rectangle) (name (eql
'rect-pointer)))
  (let ((ptr (cffi:foreign-alloc 'rect))
        (pnt (location lisp-rect))
        (size (size lisp-rect)))
    (cffi:with-foreign-slots ((left top right bottom) ptr rect)
      (setf left   (gfs:point-x pnt)
            top    (gfs:point-y pnt)
            right  (+ (gfs:point-x pnt) (gfs:size-width size))
            bottom (+ (gfs:point-y pnt) (gfs:size-height size))))
    ptr))

Can anyone make suggestions as to what I should change to be compatible
with the cffi-newtypes branch?

Second question -- do the WITH-FOREIGN-SLOTS and WITH-FOREIGN-OBJECT
macros work the same way on cffi-newtypes as they did before? I imagine so,
but it's worth asking just to be sure.

Thanks in advance.

-- 
Jack Unrue



More information about the cffi-devel mailing list