I am working an a project in C that is highly asynchronous. Unfortunately, it is quickly becoming apparent that there is a lot of boilerplate in the form of specifying structs that contain userdata for callbacks.
For example:
typedef struct SomeUserData {
int foo;
int bar;
} SomeUserData;
void someCallback(void * userData)
{
SomeUserData * myData = userData;
// ...
free(myData);
}
void someFunc(void)
{
SomeUserData * myData = calloc(1, sizeof(*myData));
myData->foo = 5;
myData->bar = 10;
doSomeAsync(someCallback, myData);
}
It's not so bad when there are just one or two callbacks, but the part of the project I'm working on now has hundreds due to the number of asynchronous steps involved.
What I'd love to have is something akin to bind in C++. Is there a way to achieve a similar result with some C macro trickery or even something at runtime using variadic arguments?