[gambit-list] FFI Converting std::vector to scheme-vector (best practice)

Chris Mueller ruunsmail at gmail.com
Fri Aug 16 10:02:29 EDT 2013


This works :)

38 (define (get-fullscreen-modes)
  39   (define get-fullscreen-modes*
  40     (c-lambda () sfml:Vector<VideoMode>*
  41       "___result_voidstar = new 
std::vector<sf::VideoMode>(sf::VideoMode::getFullscreenModes());"))
  42   (define vector-video-mode-length
  43     (c-lambda (sfml:Vector<VideoMode>*) size_t
  44       "___result = ___arg1->size();"))
  45   (define vector-video-mode-ref
  46     (c-lambda (sfml:Vector<VideoMode>* size_t) sfml:VideoMode*
  47       "___result_voidstar = new sf::VideoMode(___arg1->at(___arg2));"))
  48
  49   (let* ((modes  (get-fullscreen-modes*))
  50          (size   (vector-video-mode-length modes))
  51          (result (make-vector size)))
  52     (let loop ((i 0))
  53       (cond
  54         ((< i size)
  55             (vector-set! result i (vector-video-mode-ref modes i))
  56             (loop (+ i 1)))
  57         (else
  58             result)))))


Is it also possible to accomplish the same directly from a c-lambda that 
returns a scheme-object?


Am 16.08.2013 09:54, schrieb Mikael:
> Perhaps make a c-define-type for std::vector<VideoMode>* that you 
> return the return value of .getFullscreenModes() as, and then make a 
> -length and -ref procedure for this type.
>
> Remember that for c-define-type :s that do not have a release 
> procedure specified, there is *no* deallocation (such as free() etc.) 
> invoked at their scheme-world GC.
>
>
>
> 2013/8/16 Chris Mueller <ruunsmail at gmail.com <mailto:ruunsmail at gmail.com>>
>
>     Hi,
>
>     i'm currently coding a gambit binding for sfml and could need some
>     adivces for a specific ffi problem from you.
>
>     Assume we have a class VideoMode with the following constructor:
>
>     class VideoMode {
>     public:
>        VideoMode(int width, int height, int bbp);
>        # ...
>     }
>
>
>     I have defined an interface to scheme with:
>
>     ;; Release hook is ignored for this example
>     (c-define-type VideoMode* (pointer "VideoMode"))
>
>     ;; Scheme's construction wrapper for VideoMode
>     (define video-mode
>        (c-lambda (int int int) VideoMode* "___result_voidstar = new
>     VideoMode(___arg1, ___arg2, ___arg3);"))
>
>
>     Now assume a function that retrieves all supporting video-modes in
>     C++:
>
>     std::vector<VideoMode> getFullscreenModes();
>
>
>     I would like to write now a scheme-function (with c-lambda) that
>     iterators through all elements from this std::vector
>     and constructs/returns a scheme-vector with foreign-objects of type
>     (pointer "VideoMode").
>
>     How does the c-lambda code look like for this small issue? (Or how
>     would
>     you cope this problem alternatively?)
>
>
>     Chris
>
>
>
>
>
>     _______________________________________________
>     Gambit-list mailing list
>     Gambit-list at iro.umontreal.ca <mailto:Gambit-list at iro.umontreal.ca>
>     https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list
>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.iro.umontreal.ca/pipermail/gambit-list/attachments/20130816/75fdd852/attachment.htm>


More information about the Gambit-list mailing list