Advertisement

Dynamic Function Binding

Started by May 27, 2001 01:08 PM
4 comments, last by InFerN0 23 years, 8 months ago
I am trying to implement a way of abstracting the way certain functions are called for use with a console and a scripting engine. I am trying to find a way to take arguements(any number, any type) and pass them to the appropriate function. The functions are stored in a list and are looked up by name when they are called. Any ideas would be helpful. Thanks. InFerN0 Not all who wander are lost...
InFerN0Not all who wander are lost...
could you be more specific please
and maybe give an example?

Arkon
[QSoft Systems]
Advertisement
I have a class CEngineCommandList, that contains all functions that users will be able to find keys/buttons to.

The class has a linked list of
typedef struct command_s
{
command_s *next
char *name
FUNCTION command<-Function pointer
}Command

contained also in the class is ExecuteCommand(char *name)

what I need to do is have a way to pass ExecuteCommand parameters so that it can pass them to the function it calls.

InFerN0

Not all who wander are lost...
InFerN0Not all who wander are lost...
Whenever I have pointers to functions (as you call them, dynamically bound functions), I force them to have the same number and type of parameters. Trying to call functions where you do not know the number and types of parameters is nigh-on impossible.

For example, when I register a command with my console, I pass it a pointer to a function that takes two parameters, argc and argv. These act the same way as the main function in a C program. Then when the console goes to call that function, it already knows that the function expects two parameters and of what types those parameters are. In that function that is being called, I can do whatever I want. But what is important is that the function was called correctly from the console in the first place because the console knew that every registered command has a pointer to a function that takes the same parameters as every other function pointer for other registered commands.

A bit of forward planning like this will save you a whole heap of trouble later on.

Steve ''Sly'' Williams  Code Monkey  Krome Studios
Steve 'Sly' Williams  Monkey Wrangler  Krome Studios
turbo game development with Borland compilers
whee, interesting...
variables can only hold specific types of functions... I don''t mean that there''s a limit on what types of functions you can use, just that the variable has to be specific in terms of what the function''s parameters are... i.e. if you had the functions Foo(float,int) and Bar(char*), you could declare any one variable to hold either one of those, but you can''t declare one variable that could hold both functions (at different times, of course)...

to declare a function pointer, you should use som''n like this
typedef (void*)(* FUNCTION)(float,int); 

...that would declare a type FUNCTION which can hold a function pointer... so your program might look som''n like this
FUNCTION MyRendervoid Draw(){float f;int i;void* v;    v = MyRender(f,i);} 


Note that the parentheses around the function return type are only needed if the function returns a pointer (i.e. "type*").

...of course, if you can bypass the compiler''s type-checking, I suppose you could theoretically store any function in a variable, however this would have to be used with EXTREME caution and is definately not reccommended.

Probably the safest way to pass multiple (unknown) parameters is to set up your own "call stack" by allocating a chunk of memory, and using specialized insertors and extractors to get and set arguments (almost like you would do working with DirectPlay networking packets)... or it may be posible to declare your function pointer to point to functions that take variable argument lists, but that can get rather complicated.

--Tr][aD--
--Tr][aD--
Thanks. I think what I am going to try and do is make a class that can hold any variable type. My only question is how could I find how many params were past like

ExecuteCommand(char *name, PARAM *param,...)

how could I find how many params are passed?

Thanks.

InFerN0

Not all who wander are lost...
InFerN0Not all who wander are lost...

This topic is closed to new replies.

Advertisement