Passing Arguments to Callback Functions
When you register your callback function, you may specify a client_data pointer to any user-defined data that may be necessary for the function to complete its processing. The client_data parameter might be used to point to state information that the callback function must access and update each time it is invoked.
Assume that you want to count the number of events that your application processes. When you register your callback function, you could use the client_data pointer to point to the counter variable. When the callback is invoked, it can increment the counter.
The following example illustrates how to set up an argument for a callback function.
BrokerBoolean sample_callback(BrokerClient c, BrokerEvent e, void *counter);
long count = 0;
. . .
int main(int argc, char **argv)
{
BrokerClient c;
long n;
. . .
/* Check if can subscribe */
. . .
/* Register callback */
err = awRegisterCallbackForSubId(c, 1, sample_callback, &n);
if (err != AW_NO_ERROR) {
printf("Error on registering callback\n%s\n", awErrorToString(err));
return 0;
}
. . .
The following example illustrates how to use the client data parameter in a callback function:
BrokerBoolean sample_callback(BrokerClient c, BrokerEvent e,
void *counter)
{
/* increment counter */
(*(long*)counter)++;
/* perform rest of event processing */
. . .
}