From: skaller <skaller@users.sourceforge.net>
To: Joel Reymont <joelr1@gmail.com>
Cc: Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] Wrapping a C function that takes a variable number of arguments
Date: Fri, 07 Sep 2007 12:34:26 +1000 [thread overview]
Message-ID: <1189132466.6596.16.camel@rosella.wigram> (raw)
In-Reply-To: <6A9B2713-FD3C-4E27-89B1-002A05DA2D26@gmail.com>
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
next prev parent reply other threads:[~2007-09-07 2:34 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-09-06 23:24 Joel Reymont
2007-09-07 2:34 ` skaller [this message]
2007-09-07 7:08 ` [Caml-list] " Daniel Bünzli
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1189132466.6596.16.camel@rosella.wigram \
--to=skaller@users.sourceforge.net \
--cc=caml-list@inria.fr \
--cc=joelr1@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox