Tutorial :Dynamic source code in C++ [closed]



Question:

How to process dynamic source code in C++? Is it possible to use something like eval("foo")?

I have some functions that need to be called depending on user's choice:

     void function1 ();       void function2 ();       ...       void functionN ();         int main (int argv, char * argv [])       {           char * myString = new char [100];           ...           myString = "1" //user input                       cout << eval("function"+myString)();       }  

How is it usually done?

UPD: Based on slacy's and clinisbut's answers I think I need to make a function registry. I suppose it should be made as an array of pointers to functions. Here's the question, how do I declare an array of pointers to functions?


Solution:1

C++ is a compiled language, and thus, there is no equivalent to "eval()".

For the specific example you mention, you could make a function registry that maps inputs (strings) to outputs (function pointers) and then call the resultant function.

There are several C++ interpreter libraries that are available, although performance will be very poor, they may accomplish what you want. Google search for "C++ interpreter". I've seen results for "Ch", "CINT" and "clipp"


Solution:2

The real answer to your question is this:

extern "C" {  void function1 ();  void function2 ();  void function3 ();  }       int main (int argv, char * argv [])   {       char * myString = new char [100];       ...       myString = "function1"; //user input              void (*f)() = dlsym(RTLD_NEXT, myString);       f();   }  

You can obtain the function defined in your binary (by its name, if it was declared with extern "C"), and call it.

On windows, it becomes uglier, but still possible - read on GetProcAddress


Solution:3

You could embed a C/C++ interpreter into your program if you really need this. However, you could also embed a more script-like language instead.


Solution:4

To answer your question of how this is usually done, the answer is: it isn't.

Having to use something like eval() is usually a design problem. If you really do need it, embed a scripting language such as Lua or Python.


Solution:5

If you put function1 .. functionN in a DLL, you could simply pull them out and invoke them by name using dlopen/dlsym which would probably get you 90% of the way you want to go.


Solution:6

How about function pointers?


Solution:7

The C++ language itself is not able to do that directly. But you may use plateform specific functions to call any function in a given DLL, according to user input (or whatever). For example, on Windows, look for GetProcAddress() and LoadLibrary()


Solution:8

This is a good case for embedding an interpreted language like LUA. If you create LUA bindings to your C++ API, you'll be able to dynamically execute your code via LUA code from your application.


Solution:9

There are two ways of doing this :

  1. Dynamic Loading

    • Windows: GetModuleFileName() with hModule = NULL this gives the path of the executable. Use LoadLibrary(Executable_Name) to load itself in the memory.
    • Linux : Use dloopen() with NULL and it loads itself in memory.

    In both Windows and Linux you can define a void* (function*)() pointer. This always works with all signatures without parameters. This does not work on class methods.

  2. Using Namespace System

    This namespace would be easy you can use the Inspect functionality and InvokeMember methods to dynamically call class methods and access variable from inside the class.


Solution:10

You can't do that in C++. It is a mandatorily-compiled language.

A lot of information that would be necessary to implement eval() is lost at run-time anyway.


Solution:11

I would build an array, or an STL list, of function pointers and the strings that you find that trigger the call to them. If you really need to call an arbitrary function you should investigate RTTI http://en.wikipedia.org/wiki/Run-time_type_information


Solution:12

how do I declare an array of pointers to functions?

The C programmer's response to this question:

use cdecl.

cdecl> declare functable as pointer to function (int,pointer to int) returning double

double (*functable)(int , int *)

cdecl> declare functable as array 17 of pointer to function (int) returning double

double (*functable[17])(int )

Note that functions automatically convert into pointers to functions, in the same sense that arrays decay into pointers. You don't need an & to take the address of a function to put it in a pointer.

If you're in C++, it might help to use a std::map<YourFunctionPointerType> instead of an array, since you want to map strings anyway...

The OO approach:

Use polymorphism instead. It doesn't immediately solve your problems, but there's a good chance that if you're doing this it makes sense to have a class for each function.

Note that a vtable is, fundamentally, a table of pointers to functions.


Solution:13

What you want is reflection. It's not supported by default in C++, but many people have written comprehensive reflection systems for all sorts of purposes. One that comes to mind off the top of my head is binding C++ functions to a custom scripting language.

Others have mentioned using a switch statement (or more accurately and if/else tree). This is about as simple as a reflection system is likely to come... and it is probably the best solution for your project. Reflection in C++ is a fairly advanced technique and often requires a lot of additional code... sometimes even custom preprocessors.

Also note that for security purposes, eval() is almost never something you want to be using in any language.


Solution:14

The best way to embed and eval code within a C++ application is probably to use a scripting language like python. Use Boost.Python, "a C++ library which enables seamless interoperability between C++ and the Python programming language."


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »