I need to define a function which will be called several times by a 3rd-party function. This external library has defined the prototype of the callback as:
typedef void (*fn) (const int *m, const int *n, const double *x, double *fvec, int *iflag );
First two args are size, 3rd is an input array, 4th is an output array, and last one is reserved for the parent function.
Now, my concern is that my function needs other data to perform the computation, and there is no way in this prototype to get user-data. Simply speaking, let's assume that the 3rd-party library exports such a function:
int makeSomeComputation(fn user_function, const int *m, const int *n, double *x, double *fvec, int *info, ...);
(There may be more arguments, let's pass over them). So in my code, I am supposed to do something like that:
void myFunc(const int *m, const int *n, const double *x, double *fvec, int *iflag) {
//do something here
}
//and in another function
{ //...
makeSomeComputation(myFunc, &m, &n, &x, &fvec, &info, ...);
}
As you see, there is no dedicated way to pass user-data (or user-callback).
I am quite reluctant to use static variables here because some day I may want to use my code in a multithreaded / distributed environment.
Is there another option?
Note: the 3rd-party is actually open-source, so I think I will change the library to add an extra parameter, but I don't like that neither.