mlt wrote:
How is it possible from main to supply MyErrorHandler wihtout arguments?
void MyErrorHandler(CGcontext context, CGerror error, void *data) {
char *progname = (char *)data;
fprintf(stderr, "%s: Error: %s\n", progname, cgGetErrorString(error));
}
void main(int argc, char *argv[])
That should be `int main', and you will be punished for this
departure from the ways of righteousness. Cardinal Fang, poke
him with the soft cushions!
{
...
cgSetErrorHandler(MyErrorHandler, (void *)argv[0]);
This is *not* a call to the MyErrorHandler function. It is
a call to the cgSetErrorHandler function, passing two arguments.
The first argument is a pointer to the MyErrorHandler function,
and the second is a pointer to the program's first command-line
argument (ordinarily the program name), converted from a `char*'
to a `void*'.
There are quite a few pieces missing from the snippet you've
posted, but I'd guess that the scenario goes something like this:
cgSetErrorHandler stores its two arguments somewhere and returns,
probably without doing much of anything else. Later on, if the
CG framework detects an error of some kind, the error-processing
code will retrieve the values that were stored earlier and will
use them to call the pointed-to function -- in this case, to call
MyErrorHandler. The MyErrorHandler function appears to take
three arguments: A CGcontext and a CGerror, which presumably give
information about the error, and a `void*' which is most likely
the other value stashed by cgErrorHandler.
This technique is often referred to as using a "callback,"
meaning that the invoker provides a function pointer to a called
function or suite of functions, which then uses the pointer to
"call back" to the function the invoker provided. The Standard C
library includes a few uses of this technique: the qsort() and
bsearch() functions are often cited as uses of callback, but the
way atexit() "registers" functions to be called during exit()
processing seems a closer match to the situation you've shown.
--
Er*********@sun.com