Re: A89: Event Handlers......
[Prev][Next][Index][Thread]
Re: A89: Event Handlers......
Hi!
> Note that EVENT_HANDLER is a pointer to a function, which creates some
"odd"
> looking C calls for those who aren't experienced in using them. Refer
the
> K&RC2's sections on pointers to functions (look it up in the index) for
more
> information on them. Sample code follows (somebody please check and make
> sure I make no errors in my haste, especially on the function call in the
> handler):
>
> /* includes, defines, prototypes, etc here */
>
> EVENT_HANDLER old_hook;
>
> void _main(void)
> {
> old_hook = EV_hook;
> EV_captureEvents (Handler);
> TRY
> EV_eventLoop ();
> ONERR
> EV_captureEvents (NULL);
> ENDTRY
> /* more code here */
> }
Note that EV_captureEvents(NULL) will never be performed,
because the event handler will never throws an error.
> void Handler (EVENT *ev)
> {
> /* handle events here, and end it with */
> if (old_hook != NULL)
> (*old_hook)(ev);
> }
(*old_hook)(ev) is good, but old_hook(ev) will also works,
because pointers to functions are automatically dereferenced,
so it may be used as ordinary function calls :-)
I prefer second one: the whole tigcclib are made from
pointers to functions!!!
> HOWEVER, as I stated in my previous posts, I do not believe
> that the *-ON handlers are best implemented using the event
> handler, when int 6 is sufficient and, IMHO, superior, for
> reasons explained in the lsat message I sent to this list.
I agree.
> Yup, that's what the sizeof() operator is for. I believe it's
> valid for functions, too
No. ANSI C does not propose what is sizeof(function). In GNU C
(like TIGCC is), sizeof(function) is always 1. To determine
number of bytes occupied by function, I used the following:
void MyFunction(void)
{
// The function body...
}
void End_of_function_Marker(void);
asm("End_of_function_Marker:");
...
...
num_of_bytes=(char*)End_of_function_Marker-(char*)MyFunction;
> but the rest of your call is wrong, so let me just cover
> how memcpy is called - TI-GCC lists the prototype as:
>
> void *memcpy (void *dest, const void *src, unsigned long len);
>
> Your example needs to switch the source and destination, and
> use the address-of (unary &) operator rather than *:
>
> memcpy (pointer_to_allocated_memory, &my_hook_function, sizeof
> (my_hook_function));
1) "&" before my_hook_function is not necessary, because functions
are automatically casted to pointers when used without () after
the function name.
2) sizeof does not work with functions, as I mentioned above.
Cheers,
Zeljko Juric