January 6th, 2004, 07:50 PM
How doi convert/cast a c-style string's value to execute a function of the same name?
I'd prefer this question answered in C, instead of C++, if possible, but I couldn't find a proper forum. Please Help!!!
Let's say I have the following simple code:
char* szExecuteThisFunction = "foo3";
printf "Running foo1!\n"; /* that's all this guy does */
int i = 0; /* not much going on here either */
double i, j;
i = 2.0;
j = i + 10.0; /* adding 2 numbers, that's it!*/
The value for szExecuteThisFunction gets read in by a user, and it is basically the "name" of the function that I wish to execute. So if the value for szExecuteThisFunction = "foo1", I want it to execute the foo1() function. If it's "foo2", I want it to execute the foo2() function, etc.
My question is this: How do I make the "string" execute a "function", whose name matches the string's value? Is it even possible? I think you can do something similar to this in Perl using the evaluate() function, but I don't know how it's done in C or even C++.
Please understand that I am working on something that will have hundreds of functions to choose from, not just the three simple examples I gave. So a massive "case" statement is not a solution I'm looking for
Thanks in advance for any help!
January 6th, 2004, 08:22 PM
First you have to create a structure that contains both string and the assoicated function pointer. You may also like to use some sort of map implementation that can optimize your search. If you are looking for simple inplementation and performance isn't really an issue, you can use a linked list to chain up all the instances of the structure. So during your search, you just have to iterate through the list and compare the string. Then invoke the function pointer when the string matches.
January 6th, 2004, 11:11 PM
There are several solutions.
Do all functions have the same parameters and return types? If yes, then one solution is a map. Another solution is an array.
January 7th, 2004, 01:11 AM
First of all, thanks for the responses. I think I got the answer that I was looking for, but I can't use it. Let me clarify, I can use it but I don't want to, because there are too many functions that have been defined and I'm too lazy to make a map out of all of them (upwards of 200 functions). It seems to me as if the solution of "map" or "array" concludes that I must know what the functions will be (which I don't). All I'm doing is trying to enhance someone else's hand-me-down code which already contained the proper header file(s) that define the prototypes to several hundred functions, and I don't want to make a map out of all of them.
Anyways, thanks for the responses.
January 7th, 2004, 02:16 AM
How's about writing a script that opens the header file and generates code that inserts the string and function pointer into the map or array? At least, it will be more manageable.
January 7th, 2004, 04:47 AM
C++ has no built-in facilities such as Perl or Java to take a string and call a function from that string. So if you are looking for a solution using some exotic C++ syntax, you won't find it.
Originally posted by Psyberia
Let me clarify, I can use it but I don't want to, because there are too many functions that have been defined and I'm too lazy to make a map out of all of them (upwards of 200 functions).
You have the header file with all the functions that can be called, so you do know what all the functions are. If you are stating that you don't know which function will be called, then that is the purpose of the map. The map would match the string with the function pointer that points to the appropriate function
It seems to me as if the solution of "map" or "array" concludes that I must know what the functions will be (which I don't)
It will take less than half an hour to get the map initialized if you have a decent editor. Once it's done, it's done and you don't worry about it again. The only other alternative is to make a DLL and export all the functions. Then you can call the functions "by name" using the GetProcAddress(). But this requires (again) work on your part to create a DLL and export all of the function. This is IMO much more time consuming than just creating a map of strings to function pointers.
All I'm doing is trying to enhance someone else's hand-me-down code which already contained the proper header file(s) that define the prototypes to several hundred functions, and I don't want to make a map out of all of them.
Last edited by Paul McKenzie; January 8th, 2004 at 12:15 PM.
Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!