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" jdunrue@gmail.com writes:
(cffi:defctype rect-pointer :pointer)
(define-foreign-type rect-pointer-type () () (:actual-type :pointer) (:simple-parser rect-pointer))
(defmethod cffi:free-translated-object (ptr (name (eql 'rect-pointer)) param) (declare (ignore param)) (cffi:foreign-free ptr))
Now RECT-POINTER-TYPE is actually a CLOS class. So you should use a normal specializer instead of an EQL specializer.
(defmethod free-translate-object (ptr (type rect-pointer-type) param) ...)
Can anyone make suggestions as to what I should change to be compatible with the cffi-newtypes branch?
That's basically it. Change the type definition and change EQL specializers to normal specializers. I've compiled the cffi-newtypes manual and uploaded it here:
http://common-lisp.net/~loliveira/tmp/cffi-newtypes/
The section about foreign types should be updated. (Not all of the reference nodes are though, I think.)
Now's a good time to object to this new interface and propose changes. I'm all ears. :-) I sent some ideas a few weeks ago, but I don't really have clean solutions. They can be read here:
http://article.gmane.org/gmane.lisp.cffi.devel/1033
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.
No changes there.