Paste: Alien question, expanded

Author: rswarbrick
Mode: factor
Date: Tue, 13 Jan 2009 01:45:00
Plain Text |
! In the code below, I want wand, fill and bordercolor to be passed in
! as tuple types, which each have a c-wand>> method defined on them,
! which turns them into alien types.
! MagickFloodfillPaintImage-safe expects alien types like that. It
! looks so horrible because it's generated by a parsing word from the
! (slightly) nicer thing below.
! So my question came about because, while most of ImageMagick's
! functions expect just one pointer to an object as the first argument
! and then stuff that gets converted automatically, some have more
! complicated arguments. The ::-based approach below has to be done by
! hand, which is boring and messy, so I was wondering whether there
! was a neater way.
! Incidentally, if what I was asking was a bit unclear on IRC, what I
! was hoping for is called a "foreign type translator"
! ( in Lisp's CFFI. I assumed (wrongly) that
! that sort of functionality was what was being used to make the char*
! conversion magic work.
! Anyway, essay over. I wondered if you had any suggestions for how I
! could abstract out the sort of transformations one needs.

! This one hand-written
:: floodfill-paint ( wand channel fill fuzz bordercolor x y invert -- wand )
  wand dup c-wand>> channel fill c-wand>> fuzz bordercolor c-wand>> x y invert
  MagickFloodfillPaintImage-safe ;

! This generated by a parser word.
USING: generalizations kernel magickwand.libmagickwand.syntax ;
IN: magickwand.libmagickwand
: MagickFloodfillPaintImage-safe
    ( wand channel, fill, fuzz, bordercolor, x, y, invert -- )
    8 npick [ MagickFloodfillPaintImage ] [ call 0 = ] curry
    dip swap [ throw-magick-error ] [ drop ] if ;

! This hand (and regexp!) written.
BOOL-WAND-FUN: FloodfillPaintImage ( int channel, void* fill, double fuzz, void* bordercolor, long x, long y, int invert ) ;

New Annotation