Re: Wrapping C functions with variable prameters
- Posted by jimcbrown (admin) Jun 25, 2012
- 1369 views
I need some help from the C gurus around here. I'm a bit stumped on how to wrap a few functions I've come across.
I'm trying to wrap newt and it it seems to use a variable number of parameters on a few of its functions, like so:
/* last item is NEWT_ARG_LAST for all of these */ int newtCheckboxTreeAddItem(newtComponent co, const char * text, const void * data, int flags, int index, ...);
And here's what I started with...
x_newtCheckboxTreeAddItem = define_c_func( libnewt, "newtCheckboxTreeAddItem", {C_POINTER,C_POINTER,C_POINTER,C_INT,C_INT}, C_INT )
Is there some reason you can not just call newtCheckBoxTreeAddArray() directly instead?
sprintf() has a vsprintf(), printf() has a vprintf(), execl() has execvp() ... many if not most vararg functions are implemented this way, with a varargs wrapper which then puts the varargs into an array of some sort and passes that into a function that does the real implementation.
(vprintf() and friends are a little odd, passing a raw va_list parameter directly, but that's because of the heterogenous types that can be passed in.)
I was thinking that I could just fill out the parameters with an inordinate series of C_INT,C_INT until the cows come home,
Yup, you could.
but I'm not sure what the maximum number of parameters is.
There isn't one, not per se. (There are limits on the total amount of memory and the stack size, of course.)
I also thought that I could re-declare the function on each call for that particular number of parameters (probably with some smart caching involved). Any ideas?
-Greg
Calling define_c_func() multiple times for the same function and the same set of parameters is wasteful. You'll end up with multiple c_func routine ids that refer to the same function (with the same set of parameters).
However, having a map that corresponds a single c_func routine id to that function + parameter set would probably be exactly what you want (adding only new c_func routine ids as necessary).
Assuming that you can't just use the array versions of the functions directly, of course...