* Wrapping a C function that takes a variable number of arguments
@ 2007-09-06 23:24 Joel Reymont
2007-09-07 2:34 ` [Caml-list] " skaller
0 siblings, 1 reply; 3+ messages in thread
From: Joel Reymont @ 2007-09-06 23:24 UTC (permalink / raw)
To: Caml List
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?
Thanks, Joel
[1] http://www.cocoadev.com/index.pl?NSLog
--
http://wagerlabs.com
^ permalink raw reply [flat|nested] 3+ messages in thread
* 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
* Re: [Caml-list] Wrapping a C function that takes a variable number of arguments
2007-09-07 2:34 ` [Caml-list] " skaller
@ 2007-09-07 7:08 ` Daniel Bünzli
0 siblings, 0 replies; 3+ messages in thread
From: Daniel Bünzli @ 2007-09-07 7:08 UTC (permalink / raw)
To: Caml List
Le 7 sept. 07 à 04:34, skaller a écrit :
> Variants.
Alternatively, on the caml side of things, you can use functional
unparsing [1].
Best,
Daniel
[1] Olivier Danvy. Functional unparsing. J. of functional programing,
1998.
http://dx.doi.org/10.1017/S0956796898003104
Also available here :
http://www.brics.dk/RS/98/12/
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2007-09-07 7:08 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-09-06 23:24 Wrapping a C function that takes a variable number of arguments Joel Reymont
2007-09-07 2:34 ` [Caml-list] " skaller
2007-09-07 7:08 ` Daniel Bünzli
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox