I'm using a timer, in C, which is system-dependant (I cannot change its code). I can access it, among others, with this function :
void start_timer(int duration, void (*callback)(void*), void* arg);
So I can give to the timer a callback function and its void* argument.
The function I would like to use as callback is:
void send_message(ipaddr* source, ipaddr* destination, char* message);
I cannot directly give this function to start_timer, because it does not match the void (*)(void*) required type. As anonymous functions does not exist in C, I cannot use this solution (but it is what I would like to do):
start_timer(1000, void(*)(void* stuff){
send_message(source, destination, message);
}, NULL);
So I must give a name to this function:
void call_send_message(void* stuff) {
send_message(source, destination, message);
}
start_timer(1000, &call_send_message, NULL);
Is there a more beautiful way to call send_message function using start_timer?
Create a struct, something like this:
struct args {
ipaddr *source;
ipaddr *destination;
char *message;
};
and have your send_message function take void* argument, then you just cast it to a struct args type and from there you can access its members. If you can't edit it, then create a "wrapper" like your example:
So basically your function below
void call_send_message(void* stuff) {
send_message(source, destination, message);
}
becomes
void call_send_message(void* stuff) {
struct args *realstuff = (struct args *) stuff;
send_message(realstuff->source, realstuff->destination, realstuff->message);
}
typedef struct Data {
ipaddr *src;
ipaddr *dst;
char *msg;
} Data;
send_message function:void wrapper(void *args) {
Data *data = (Data*) args;
send_message(data->src, data->dst, data->msg);
}
start_timer(duration, &wrapper, your_data);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With