Help with the ULTIMATE derererencing macro for CFFI

Discussion of Common Lisp
Post Reply
joeish80829
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

Help with the ULTIMATE derererencing macro for CFFI

Post by joeish80829 » Fri Nov 01, 2013 12:47 am

I'm trying to write an all purpose dereferencing macro...ive done pretty good so far I have

Code: Select all

(defmacro -> (var1 var2 var3 &optional var4 var5)

         ;; if var4 and var5 arent supplied give value of struct member
         (if (not (and var4 var5)) `(cffi:with-foreign-slots ((,var3)
                                  ,var2 (:struct ,var1)) (list ,var3))

         ;; if var4 and var5 are supplied give value of nested struct member
         (if (and var4 var5)

             `(cffi:with-foreign-slots ((,var3)
                        ,var2 (:struct ,var1))
           
            (cffi:with-foreign-slots ((,var5)
                          ,var3 (:struct ,var4)) ,var5) ))))


the roi struct member of ipl-image is defined as this

Code: Select all

    (roi (:pointer (:struct ipl-roi)))

;this gets the roi slot value of ipl-image correctly

Code: Select all

CL-OPENCV> (-> ipl-image a roi )
(#.(SB-SYS:INT-SAP #X7FFFDC000E80))
;this gets the nested struct member "width" of ipl-roi which is just a an :int correctly

Code: Select all

CL-OPENCV> (-> ipl-image a roi ipl-roi width)
100
both work correctly but i could use help getting rid of some of the typing to access these members

in this

Code: Select all

(-> ipl-image a roi ipl-roi width) 
i have to type "ipl-image"(the name of the struct) "a"( the name of the variable i'm dereferencing) "roi" (the name of the ipl-image struct member) "ipl-roi" (the foreign-type of roi) and "width" (the ipl-roi struct member) . if there was a way i can get the foreign type of the roi struct member like i can call foreign-slot-names to get all the slot names of a struct i could not have to use the ipl-roi here

Code: Select all

 (-> ipl-image a roi ipl-roi width)
but i looked in the manual and nothing....I couldnt think of any other way to shorten this

Code: Select all

 (-> ipl-image a roi ipl-roi width)
and make it a little closer to c so if someone could give me advice, or better yet their awesome dereferencing macro=), i named this post so everyone....including me=) would have access to this great information

Post Reply