Click to See Complete Forum and Search --> : CallBack Functions


sashp
August 24th, 2004, 06:53 AM
Hi can any one out there pls answer the following queries
1) what are call back functions ??
2) also give me one secnario where one can leverage call back functions

Thanx

SOCM_FP_CPP
August 24th, 2004, 07:10 AM
Hi ,

There are two players in a function call, Caller ( one who calls the function ) and Callee ( function which gets called ).

Imagine Callee is a long function and caller is interested in knowing the status of the processing ?

Callback is a mechanism meant to cater to this situation. In the example
given below main calls a function processrequest. processrequest will call back
caller with my_callback . ( u should be familiar with function pointers here )


#include <stdio.h>

//For eg :-
//callback function
//---------------------

// function prototype

// learn about function pointers to understand it

typedef void (*CALLBACK)(char *);
void processrequest( CALLBACK bck );


//////////////////////
// This function will act as a callback

void my_callback( char *status )
{

printf(status);

}

//////////////////////////////////////
//caller
//------

void main()
{

// my_call back will be called by process request
// to notify status

processrequest(my_callback);

}

/////////////////////////////////
//callee
//--------

void processrequest( CALLBACK bck )
{

int ctr = 0;

while ( ctr< 20000L )
{



if ( ctr++%100 == 0 )
{

char bfr[100];
sprintf(bfr,"%d\n",ctr);

(*bck)(bfr); // let the caller know the status

}

}


}



praseed pai
www.praseedpai.com

Bornish
August 24th, 2004, 07:48 AM
A system that supports callbacks is, in fact, maintaining a list of addresses that should be called when a certain event occurs. Therefore, callback functions are also called notifiers. These can be of many types.
I will give a few but important examples:
1. Hooks (same as system's hooks of which you've probably heard) are registered by a "client implementation" which needs to take control (hack into) over some message internally handled by a module of the "server implementation". The client that registered the hook, may or may not pass control back to the default implementation by calling the next hook in the list mantained by the server.
2. Subclassing is very similar technique, but there's no list maintained by the server module. The difference is that unsubclassing (unregistering your hook procedure/function) more than once must be done carefully. For example, subclassing twice a window (control or whatever), only the first time you'll store the original implementation's address, because second time you'll retrieve the address of the first subclassing procedure. Therefore, only first subclassing should unsubclass last, or else is not considered safe, because the client code could be unloaded from the process's memory before the subclassed object gets destroyed.
3. EnumFunctions can be used when starting a thread to enumerate a list of objects in the background, passing control to some client implementation for each enumerated object.
4. EventNotifiers are registered to be called from anywhere and at anytime some event occurs. Examples: file I/O completion or progress, mouse (or other auxiliary device) input, ... even error/warning notification.

There's a lot to talk about it, but I hope this gave you a clue about the subject.