* Re: [Caml-list] Wrapping a C function that takes a variable number of arguments
2007-09-06 23:24 Wrapping a C function that takes a variable number of arguments Joel Reymont
@ 2007-09-07 2:34 ` skaller
2007-09-07 7:08 ` Daniel Bünzli
0 siblings, 1 reply; 3+ messages in thread
From: skaller @ 2007-09-07 2:34 UTC (permalink / raw)
To: Joel Reymont; +Cc: Caml List
On Fri, 2007-09-07 at 00:24 +0100, Joel Reymont wrote:
> I'm trying to figure out how to wrap Cocoa's NSLog [1].
>
> It's basically just like fprintf in that the signature looks like this
>
> void NSLog(NSString *format, ...);
>
> There must be an idiomatic way of creating a wrapper for such a
> function, probably with a bit of dark magic. Would someone please
> show me how?
Variants. In Caml you pass
NSLog(fmt, [Int 1; Double 2.0])
The wrapper can then decode the 'fmt' and check that the corresonding
variant has a suitable type.
It is not possible, however, to call the C function in a conforming way.
Instead you have to cheat. If there is a
vNSLog(fmt, va_args)
function you're cool, the cheat is easy. If you're using gcc as your
C compiler, read up on
__builtin_va_list
and how gcc implements varargs.
The best cheat is this one: scan the fmt, and break it into
separate %codes like:
"Hello %s today %d\n"
will be (say) Str.split on % to give
"Hello "
"s today "
"d\n"
Now call NSLog 3 times, adding the % back to the second and third
strings:
NSLog("Hello ");
NSLog("%s today ",vs);
NSLog("%d\n",vi);
in a loop, obviously, which sets vs if the variant is a string,
vi if it is an integer, vd if it is a double, etc etc.
Note this will NOT work if NSLog isn't functorial, that is:
NSLog (x ^ y) = NSLog (x); NSlog (y)
eg if it adds an 'end of line' or 'date and time stamp' to the
log message, then this technique won't work so well.
--
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net
^ permalink raw reply [flat|nested] 3+ messages in thread