Re: [Code] Passing function pointers

si12632@ci.uminho.pt
Date: 10/25/96


>    I'm messing with the event queue code found on the ftp site.  It jogged my 
> memory into the rather wild way of using a pointer to call a function, but I
> noticed that the EVENT() was somewhat limited to only sending set values
> (the ptr to ch, the pointer to vict, and a pointer to some info)
> 
>    What I would like to do is to allow the add_queue take the variables time,
> *function, and a varying number of parameters, so *function could be things

  To declare a function with variable parameters:

      <type> <function name>(<param1>,<param2>, ...)

  That means a function wich has 2 fixed parameters, and any additional number.

  To use the parameters declare an internal variable of type va_list.

  Init the list with 
     va_start(<list variable>, <number of last fix parameter>);

  va_arg(<list variable>,<type>) skips this parameter as if it were of type 
<type>. The variable to access the parameters is pvar.

  va_end(<list variable) cleans up the list. 

  To know more, in UNIX type 'man stdarg'. At least that's the page in my
machine.

> ranging from pulse violence to, (assume I'm sick) a system function call
> like select(), which has a LOT of parameters.

  You ARE sick! :)))

>    I *KNOW* I'm asking for possible problems with this, but I'd like to know

  Hmmm... Well... Theoreticaly, you can do it, just pass a pointer to the 
function declared with that prototype. (Just redefine EVENT()). Then, inside
add_event, you would have to make a copy of every parameter in the va_list.
Then, in run_events, you would have to REBUILD the va_list to pass it into the
event.

  Oh! And another thing. How do you know how many parameters are passed into
the add_event function? It depends on the event. Wich means that every new
event you create will possibly cause a change to the 'events.c'. When I
wrote the code, I tried to make it just as independant as possible. This way,
both the events and the code that causes them can be made without altering
my code.

  In other words, it will be a pain to implement, plus it won't be worth it
IMHO. Of course I could be wrong.

  In case your still thinking about going ahead with it, just one warning:
The compiler won't have the foggiest idea about what parameters you're passing
to the function. As far as it is concerned everything goes. 
      BE EXTRA CAREFULL WHEN CODING!!!


  Hope this helps. By the way. If you ever get around to doing it (wich is
completely unnecessary, in my opinion), could you send a copy my way, you
made me curious. 

    Luis Carvalho
+-----------------------------------------------------------+
| Ensure that you have read the CircleMUD Mailing List FAQ: |
|   http://cspo.queensu.ca/~fletcher/Circle/list_faq.html   |
+-----------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/18/00 PST