[cffi-devel] cffi macro suggestion

hbabcockos1 at mac.com hbabcockos1 at mac.com
Sat Mar 25 21:21:45 UTC 2006


Hello,

I've been working on an interface for the graphics package plplot  
(http://plplot.sourceforge.net/). Its API has lots of functions that  
expect arrays, matrixes and associated variables that specify the  
size of the array/matrix. To facilitate wrapping these functions I  
wrote a macro that I thought might (after some modification/cleaning)  
be useful for others. The macro can be found at: http:// 
paste.lisp.org/display/18274.

It was written to expand a function declaration like:

(pl-defcfun ("c_plmesh" plmesh) :void
	    (x *plflt nx)
	    (y *plflt ny)
	    (z **plflt (nx ny))
	    (nx plint)
	    (ny plint)
	    (opt plint))

into: (progn statements removed)

(DEFCFUN ("c_plmesh" C-PLMESH)
            :VOID
            (X *PLFLT)
            (Y *PLFLT)
            (Z **PLFLT)
            (NX PLINT)
            (NY PLINT)
            (OPT PLINT))

(EXPORT 'C-PLMESH (PACKAGE-NAME *PACKAGE*)))

(DEFUN PLMESH (X Y Z OPT)
     (LET ((C-X (MAKE-PTR X :DOUBLE #'(LAMBDA (X) (COERCE X 'DOUBLE- 
FLOAT))))
           (C-Y (MAKE-PTR Y :DOUBLE #'(LAMBDA (X) (COERCE X 'DOUBLE- 
FLOAT))))
           (NX (ARRAY-DIMENSION Z 0))
           (NY (ARRAY-DIMENSION Z 1))
           (C-Z (MAKE-MATRIX Z)))
       (UNWIND-PROTECT
           (C-PLMESH C-X
                     C-Y
                     C-Z
                     (FUNCALL #'(LAMBDA (X) (ROUND X)) NX)
                     (FUNCALL #'(LAMBDA (X) (ROUND X)) NY)
                     (FUNCALL #'(LAMBDA (X) (ROUND X)) OPT))
         (PROGN
          (FOREIGN-FREE C-X)
          (FOREIGN-FREE C-Y)
          (FREE-MATRIX C-Z (LIST NX NY))))))

(EXPORT 'PLMESH (PACKAGE-NAME *PACKAGE*))))

Where make-ptr, make-matrix and free-matrix are functions that make  
1D arrays, make 2D arrays & free 2D arrays respectively.

best,
-Hazen




More information about the cffi-devel mailing list