chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] ffi questions and bug (?)


From: Carlos Pita
Subject: Re: [Chicken-users] ffi questions and bug (?)
Date: Fri, 20 May 2005 15:06:01 -0300
User-agent: KMail/1.7.2

Thank you Felix! That was very illustrative.
I'm taking notes about the C/API also
from runtime.c and chicken.h. My goal is
not only to understand it myself but at the
same produce a (barebones) document that
could be posted in a wiki or perhaps added to
the freak unidos manual. The code is
pretty self-explaining although I'm classifying
functions and macros, adding comments, etc.
One point I would like you to clarify to
me is the difference between:
  C_XXX
  C_a_i_XXX
  C_h_XXX
  C_i_XXX
functions/macros.
Well, that's all for now, hope I'll be having
something publishable soon.

Thank you in advance.
Regards,
Carlos.

> > 
> > Just to get the feeling, can you give me a
> > simple example of (manually) wrapping a
> > trivial C++ class using the ffi and tinyclos
> > (I mean "manually" without recurring to
> > easy ffi or swig)?
> 
> You can simply use the `-debug F' option to chicken/csc
> to see the code generated by the easy FFI:
> 
> % cat x.scm
> (require-extension tinyclos)
> 
> #>!
> class Foo {
>   int x_;
> 
> public:
>   Foo(int x) { x_ = x; }
>   virtual ~Foo() {}
> 
>   int bar(int y) { return x_ * y; }
> };
> <#
> 
> (define f1 (make <Foo> 99))
> (pp (bar f1 10))
> (destroy f1)
> 
> % csc -c++ x.scm -debug F
> (begin (declare (hide g2)) (define-class <Foo> (<c++-object>) ()))
> (begin
>   (define g2 (foreign-lambda void "delete " (pointer "Foo")))
>   (define-method (destroy (this <Foo>)) (g2 (slot-ref this 'this))))
> (begin
>   (declare (hide g3))
>   (define g3 (foreign-lambda (pointer "Foo") "new Foo" integer))
>   (define-method
>     (initialize (this <Foo>) initargs)
>     (slot-set!
>       this
>       'this
>       (if (and (pair? initargs) (eq? 'this (##sys#slot initargs 0)))
>         (cadr initargs)
>         (##sys#apply g3 initargs)))))
> (begin
>   (declare (hide g4))
>   (define g4
>     (foreign-lambda*
>       integer
>       (((pointer "Foo") g5) (integer g6))
>       "return(g5->bar(g6));"))
>   (define-method
>     (bar (this <Foo>) . args)
>     (##sys#apply g4 (slot-ref this 'this) args)))
> % ./x
> 990
> 
> If you need more information, just ask.
> 
> 
> cheers,
> felix
> 






reply via email to

[Prev in Thread] Current Thread [Next in Thread]