CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Hybrid View

  1. #1
    Join Date
    Jan 2004
    Location
    California
    Posts
    6

    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";

    void foo1()
    {
    printf "Running foo1!\n"; /* that's all this guy does */

    }
    void foo2()
    {
    int i = 0; /* not much going on here either */
    }
    void foo3()
    {
    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!
    Psy

  2. #2
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    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.

  3. #3
    Join Date
    Feb 2002
    Posts
    5,757
    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.

    Kuphryn

  4. #4
    Join Date
    Jan 2004
    Location
    California
    Posts
    6
    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.
    Psy

  5. #5
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    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.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449
    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).
    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.
    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)
    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
    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.
    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.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; January 8th, 2004 at 12:15 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured