Page 1 of 1

How do I approxmate C pointer dereferencing?

Posted: Sat Oct 05, 2013 9:11 am
by joeish80829
in my library ive already written wrappers for these opencv functions
create-camera-capture
query-frame
img-size
create-image

the struct related to create-image is IplImage and i have it defined like this






;; ;(cffi:foreign-type-size '(:struct ipl-image)) = 144
(cffi:defcstruct ipl-image
(n-size :int)
(id :int)
(n-channels :int)
(alpha-channel :int)
(depth :int)
(color-model :int) ;;Ignored by OpenCV - was :pointer, changed to :int so the struct values would match OpenCV's
(channel-seq :int) ;;Ignored by OpenCV - was :pointer, changed to :int so the struct values would match OpenCV's
(data-order :int)
(origin :int)
(align :int)
(width :int)
(height :int)
(roi (:pointer (:struct ipl-roi)))
(mask-roi :pointer)
(image-id :pointer)
(tile-info :pointer)
(image-size :int)
(image-data :string)
(width-step :int)
(border-mode :pointer)
(border-const :pointer)
(image-data-origin :string))






so when i call the above functions and then print the struct values with the below
code it looks like this






CL-OPENCV> (defparameter capture (create-camera-capture 0))
(defparameter frame (query-frame capture))
(defparameter img-size (get-size frame))
(defparameter img (create-image img-size +ipl-depth-8u+ 3))
IMG
CL-OPENCV> (cffi:with-foreign-slots ((n-size id n-channels
alpha-channel depth color-model
channel-seq data-order origin
align width height roi
mask-roi image-id tile-info
image-size image-data width-step
border-mode border-const image-data-origin)

img (:struct ipl-image))
(format t "n-size = ~a~%id = ~a~%n-channels = ~a~%alpha-channel = ~a~%depth = ~a~%color-model = ~a~%channel-seq = ~a~%data-order = ~a~%origin = ~a~%align = ~a~%width = ~a~%height = ~a~%roi = ~a~%mask-roi = ~a~%image-id = ~a~%tile-info = ~a~%image-size = ~a~%image-data = ~a~%width-step = ~a~%border-mode = ~a~%border-const = ~a~%image-data-origin = ~a~%"
n-size id n-channels
alpha-channel depth color-model
channel-seq data-order origin
align width height roi
mask-rOI image-id tile-info
image-size image-data width-step
border-mode border-const image-data-origin))
n-size = 144
id = 0
n-channels = 3
alpha-channel = 0
depth = 8
color-model = 4343634
channel-seq = 5392194
data-order = 0
origin = 0
align = 4
width = 640
height = 480
roi = #.(SB-SYS:INT-SAP #X00000000)
mask-roi = #.(SB-SYS:INT-SAP #X00000000)
image-id = #.(SB-SYS:INT-SAP #X00000000)
tile-info = #.(SB-SYS:INT-SAP #X00000000)
image-size = 921600
image-data =
width-step = 1920
border-mode = #.(SB-SYS:INT-SAP #X00000000)
border-const = #.(SB-SYS:INT-SAP #X00000000)
image-data-origin = NIL
NIL




I'd rather not have to call the with-foreign-slots function with all that code every time id like to access slot values id rather do exactly like in c and call img->depth for example to access the IplImage struct member depth....Is there a way i can have this exact functionality in lisp with the above struct or any other....if i did have to call (img depth) i guess that would be ok but id rather create some sort of dereferencing operator equivelant to do this as easy as in c....either way can someone show me how to dereference pointers in the tiniest way possible like in c

Re: How do I approxmate C pointer dereferencing?

Posted: Thu Oct 10, 2013 9:03 pm
by nuntius
It sounds like you want foreign-slot-value.

http://common-lisp.net/project/cffi/man ... _002dvalue

You could wrap this in a helper function or macro to make it shorter for the common cases.

It would take a great deal more work to get a short operator.
A reader macro could give you the syntax extension. (Though beware: such extensions don't compose.)
However, there is still the issue of getting CL to track the C structure type.
CFFI punts on this last point by having the program pass the type to functions like foreign-slot-value.

Re: How do I approxmate C pointer dereferencing?

Posted: Fri Oct 11, 2013 9:17 am
by joeish80829
im familiar with-foreign-slot-value and i did check into reader macros but i was hoping for a soloution that didnt involve the /# characters ...is there a cffi function that can read the type of struct given just a variable that is created by a function that passes a pointer to that struct like in c with img->width for example...here is what i came up with so far

CL-OPENCV> (defparameter img-size (make-size :width 640 :height 480))
(defparameter img (create-image img-size +ipl-depth-8u+ 3))

IMG
CL-OPENCV> (defmacro -> (var1 var2 var3)
`(cffi:foreign-slot-value ,var2 '(:struct ,var1) ',var3))
STYLE-WARNING: redefining CL-OPENCV::-> in DEFMACRO
->
CL-OPENCV> (-> ipl-image img width)
640


but i would like to know if there at least is any function where i can erase the ipl-image part and have that just understood as in c i thought i could make a function that would take IMG and test it against a group of foreign-slot-value functions that when one errors out it would CONTINUE then when it hits one with out an error it would print the desired output but then im left with

(-> img width)

Is there a way u know where i can transform the above into img->with or give me a link to get me started...thx for gettin back 2 me btw =).

Re: How do I approxmate C pointer dereferencing?

Posted: Fri Oct 11, 2013 10:32 am
by nuntius
In order for the "->" function to work, you would need to wrap the pointer in a structure that also contained the type.

Something like the following untested code.

Code: Select all

(defstruct wrapped-pointer
  :pointer
  :type)

(defun -> (wp field)
  "Get the specified field from the wrapped pointer."
  (cffi:foreign-slot-value (wrapped-pointer-pointer wp) (wrapped-pointer-type wp) field))
Note however that the returned value is not wrapped... There is also some difficulty if you want "->" to access C++ member functions.

As a style note, it is often best to implement macros as a simple shell for syntax, and do all the heavy processing in a normal function called by the macro.

Re: How do I approxmate C pointer dereferencing?

Posted: Fri Oct 11, 2013 3:27 pm
by joeish80829
no the (-> img width) i added did work . im just trying to get (-> img width) closer to c style img->width ....if you told me how i didnt really understand....if so can u make you make your explanation a little more basic... id like to create a -> operator if possible

Re: How do I approxmate C pointer dereferencing?

Posted: Sat Oct 12, 2013 7:36 am
by Goheeca
Why do you want to use the anti-lispish infix notation in lisp? I think using prefix notation is just perfect. It can be extended for concatenated dereferencing after that you can do (-> my-linked-list next next) instead of something in a sense my_linked_list->next->next ...

Re: How do I approxmate C pointer dereferencing?

Posted: Sun Oct 13, 2013 7:36 am
by joeish80829
I understand your point...I got into lisp because I plan to get heavy into A.I. so I chose a language that i thought wouldnt let me down in the future...so with that in mind ..is there a way that you can show me a way i can dereference in lisp better than c using the examples i posted on this ? ...even though im wrapping c I intend to make a computer vision library for lisp even better than opencv so if you could teach me a powerful way to dereference my pointers in a basic way (i didnt really understand your last post) or post a link where i could learn ways i would be appreciative....and maybe tell me why your idea is better than c i/e why(-> my-linked-list next next) is better than my_linked_list->next->next

Re: How do I approxmate C pointer dereferencing?

Posted: Sun Oct 13, 2013 9:23 am
by Goheeca
I just wanted to ask what's wrong with a prefix syntax in your eyes? If I used such a library, I would be as a lisper glad having -> as a normal operatot/function in lisp. One of the advantages is that you don't have to repeat the operator -- more clearly; look at the plus: 1+2+3+4 vs. (+ 1 2 3 4).

Re: How do I approxmate C pointer dereferencing?

Posted: Sun Oct 13, 2013 9:33 am
by joeish80829
I just thought the -> was cool but i do like prefix alot...i just wondered if it would hamper my A.I. if i could ffigure out how to code a c type dereference operator and used it and also if you could give me an example of a better way so i could make my computer vision library even better than opencv